diff --git a/ScriptingAPI-Documentation.md b/ScriptingAPI-Documentation.md index 57dc4d6bb..8c98eaff9 100644 --- a/ScriptingAPI-Documentation.md +++ b/ScriptingAPI-Documentation.md @@ -26,6 +26,7 @@ The documentation completion is still pending.... 5. Armature 6. Ray 7. AABB + 8. Emitter 8. Renderable Components 1. Renderable2DComponent 2. Renderable3DComponent @@ -73,6 +74,8 @@ You can use the Renderer with the following functions, all of which are in the g - GetArmature(String name) : Armature? result - GetObjects() : string result - GetObject(String name) : Object? result +- GetEmitters() : string result +- GetEmitter(string name) : Emitter? result1,result2,... - GetMeshes() : string result - GetLights() : string result - GetMaterials() : string result @@ -258,9 +261,9 @@ It inherits functions from Node. Can be tested againt the view frustum, AABBs, rays, space partitioning trees. It inherits functions from Transform. - [constructor]Cullable() -- Intersects(Cullable cullable) -- Intersects(Ray cullable) -- Intersects(Vector cullable) +- Intersects(Cullable cullable) : boolean result +- Intersects(Ray cullable) : boolean result +- Intersects(Vector cullable) : boolean result - GetAABB() : AABB result - SetAABB(AABB aabb) @@ -270,8 +273,8 @@ It inherits functions from Cullable. - [void-constructor]Object() - SetTransparency(float value) - GetTransparency() : float? result -- SetColor(float r,g,b) -- GetColor() : float? r,g,b +- SetColor(Vector rgb) +- GetColor() : Vector? rgb - IsValid() : boolean result #### Armature @@ -305,6 +308,16 @@ Axis Aligned Bounding Box. Can be intersected with any shape, or ray. - GetMin() : Vector result - GetMax() : Vector result +#### Emitter +Emitter particlesystem. +- [void-constructor] Emitter() +- GetName() : string? name +- SetName(string name) +- GetMotionBlur() : float? amount +- SetMotionBlur(float amount) +- Burst(float amount) +- IsValid() : bool value + ### Renderable Components A RenderableComponent describes a scene wich can render itself. diff --git a/WickedEngine/pointspriteGS.hlsl b/WickedEngine/pointspriteGS.hlsl index e6383b4c8..048b52c73 100644 --- a/WickedEngine/pointspriteGS.hlsl +++ b/WickedEngine/pointspriteGS.hlsl @@ -2,7 +2,9 @@ cbuffer constatBuffer:register(c0){ float4x4 xView; float4x4 xProjection; float4 xCamPos; - float4 xAdd; + float2 xAdd; + float xMotionBlurAmount; + float padding; }; struct GS_INPUT{ @@ -10,6 +12,7 @@ struct GS_INPUT{ float4 inSizOpMir : TEXCOORD0; //float3 color : COLOR; float rot : ROTATION; + float3 vel : VELOCITY; }; struct VertextoPixel @@ -30,8 +33,9 @@ void main(point GS_INPUT p[1], inout TriangleStream triStream) sin(p[0].rot),cos(p[0].rot) ); - float3 normal = p[0].pos - xCamPos; - normal = mul(normal, xView); + p[0].vel = normalize(p[0].vel); + float3 normal = p[0].pos.xyz - xCamPos.xyz; + normal = mul(normal, (float3x3)xView); float3 r = float3( mul(float2(0,1),rot),0 ); float3 rightAxis = cross(r, normal); @@ -53,38 +57,46 @@ void main(point GS_INPUT p[1], inout TriangleStream triStream) //upVector.xy=mul(upVector.xy,rot); p1.pos.w=1; - p1.pos.xyz = p[0].pos+rightVector*(-quadLength)+upVector*(quadLength); + p1.pos.xyz = p[0].pos.xyz+rightVector.xyz*(-quadLength)+upVector.xyz*(quadLength); p1.tex = float2(0.0f, 0.0f); p1.pos = mul(p1.pos, xProjection); if(p[0].inSizOpMir.z==1) p1.tex.x=1-p1.tex.x; if(p[0].inSizOpMir.w==1) p1.tex.y=1-p1.tex.y; + // extrude along velocity (blur) + p1.pos.xyz += p[0].vel * dot(normalize(p1.pos.xyz - p[0].pos.xyz), p[0].vel) * xMotionBlurAmount; p1.pp = p1.pos; triStream.Append(p1); p1.pos.w=1; - p1.pos.xyz = p[0].pos+rightVector*(-quadLength)+upVector*(-quadLength); + p1.pos.xyz = p[0].pos.xyz+rightVector.xyz*(-quadLength)+upVector.xyz*(-quadLength); p1.tex = float2(0.0f, 1.0f); p1.pos = mul(p1.pos, xProjection); if(p[0].inSizOpMir.z==1) p1.tex.x=1-p1.tex.x; if(p[0].inSizOpMir.w==1) p1.tex.y=1-p1.tex.y; + // extrude along velocity (blur) + p1.pos.xyz += p[0].vel * dot(normalize(p1.pos.xyz - p[0].pos.xyz), p[0].vel) * xMotionBlurAmount; p1.pp = p1.pos; triStream.Append(p1); p1.pos.w=1; - p1.pos.xyz = p[0].pos+rightVector*(quadLength)+upVector*(quadLength); + p1.pos.xyz = p[0].pos.xyz +rightVector.xyz*(quadLength)+upVector.xyz*(quadLength); p1.tex = float2(1.0f, 0.0f); p1.pos = mul(p1.pos, xProjection); if(p[0].inSizOpMir.z==1) p1.tex.x=1-p1.tex.x; if(p[0].inSizOpMir.w==1) p1.tex.y=1-p1.tex.y; + // extrude along velocity (blur) + p1.pos.xyz += p[0].vel * dot(normalize(p1.pos.xyz - p[0].pos.xyz), p[0].vel) * xMotionBlurAmount; p1.pp = p1.pos; triStream.Append(p1); p1.pos.w=1; - p1.pos.xyz = p[0].pos+rightVector*(quadLength)+upVector*(-quadLength); + p1.pos.xyz = p[0].pos.xyz +rightVector.xyz*(quadLength)+upVector.xyz*(-quadLength); p1.tex = float2(1.0f, 1.0f); p1.pos = mul(p1.pos, xProjection); if(p[0].inSizOpMir.z==1) p1.tex.x=1-p1.tex.x; if(p[0].inSizOpMir.w==1) p1.tex.y=1-p1.tex.y; + // extrude along velocity (blur) + p1.pos.xyz += p[0].vel * dot(normalize(p1.pos.xyz - p[0].pos.xyz), p[0].vel) * xMotionBlurAmount; p1.pp = p1.pos; triStream.Append(p1); } \ No newline at end of file diff --git a/WickedEngine/pointspriteVS.hlsl b/WickedEngine/pointspriteVS.hlsl index 9bee08177..95279a9d3 100644 --- a/WickedEngine/pointspriteVS.hlsl +++ b/WickedEngine/pointspriteVS.hlsl @@ -3,9 +3,10 @@ struct GS_INPUT{ float4 inSizOpMir : TEXCOORD0; //float3 color : COLOR; float rot : ROTATION; + float3 vel : VELOCITY; }; -GS_INPUT main(float3 inPos : POSITION, float4 inSizOpMir : TEXCOORD1/*, float4 inCol : TEXCOORD2*/, float rot :TEXCOORD2) +GS_INPUT main(float3 inPos : POSITION, float4 inSizOpMir : TEXCOORD0/*, float4 inCol : TEXCOORD2*/, float rot : ROTATION, float3 vel : VELOCITY) { GS_INPUT Out = (GS_INPUT)0; @@ -14,6 +15,7 @@ GS_INPUT main(float3 inPos : POSITION, float4 inSizOpMir : TEXCOORD1/*, float4 i //Out.color=inCol.xyz; //Out.rot=inCol.w; Out.rot=rot; + Out.vel = vel; return Out; } \ No newline at end of file diff --git a/WickedEngine/wiEmittedParticle.cpp b/WickedEngine/wiEmittedParticle.cpp index 920e38e5a..c257ab532 100644 --- a/WickedEngine/wiEmittedParticle.cpp +++ b/WickedEngine/wiEmittedParticle.cpp @@ -66,6 +66,7 @@ wiEmittedParticle::wiEmittedParticle(std::string newName, std::string newMat, Ob ,transform._31,transform._32,transform._33 ); + motionBlurAmount = 0.0f; } long wiEmittedParticle::getCount(){return points.size();} @@ -258,7 +259,7 @@ void wiEmittedParticle::Draw(Camera* camera, ID3D11DeviceContext *context, ID3D1 cb.mCamPos = camera->GetEye(); cb.mAdd.x = additive; cb.mAdd.y = (FLAG==DRAW_DARK?true:false); - + cb.mMotionBlurAmount = motionBlurAmount; //context->UpdateSubresource( constantBuffer, 0, NULL, &cb, 0, 0 ); @@ -331,18 +332,6 @@ void wiEmittedParticle::CleanUp() //delete(this); } -void* wiEmittedParticle::operator new(size_t size) -{ - void* result = _aligned_malloc(size,16); - return result; -} -void wiEmittedParticle::operator delete(void* p) -{ - if(p) _aligned_free(p); -} - - - void wiEmittedParticle::LoadShaders() @@ -350,9 +339,10 @@ void wiEmittedParticle::LoadShaders() D3D11_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, - { "TEXCOORD", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, //{ "TEXCOORD", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, - { "TEXCOORD", 2, DXGI_FORMAT_R32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "ROTATION", 0, DXGI_FORMAT_R32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "VELOCITY", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = ARRAYSIZE(layout); wiRenderer::VertexShaderInfo* vsinfo = static_cast(wiResourceManager::GetGlobal()->add("shaders/pointspriteVS.cso", wiResourceManager::VERTEXSHADER, layout, numElements)); diff --git a/WickedEngine/wiEmittedParticle.h b/WickedEngine/wiEmittedParticle.h index 27c0c306e..e061b49b1 100644 --- a/WickedEngine/wiEmittedParticle.h +++ b/WickedEngine/wiEmittedParticle.h @@ -15,8 +15,9 @@ private: XMFLOAT3 pos; XMFLOAT4 sizOpaMir; //XMFLOAT3 col; - float rot,rotVel; + float rot; XMFLOAT3 vel; + float rotVel; float life; float maxLife; float sizBeginEnd[2]; @@ -35,15 +36,19 @@ private: sizBeginEnd[0] = sizOpaMir.x; sizBeginEnd[1] = sizOpaMir.x*scaleX; } + + ALIGN_16 }; deque points; struct ConstantBuffer { - XMMATRIX mView; - XMMATRIX mProjection; - XMVECTOR mCamPos; - XMFLOAT4 mAdd; + XMMATRIX mView; + XMMATRIX mProjection; + XMVECTOR mCamPos; + XMFLOAT2 mAdd; + float mMotionBlurAmount; + float padding; ALIGN_16 }; @@ -85,8 +90,6 @@ public: wiEmittedParticle(){}; wiEmittedParticle(std::string newName, std::string newMat, Object* newObject, float newSize, float newRandomFac, float newNormalFac ,float newCount, float newLife, float newRandLife, float newScaleX, float newScaleY, float newRot); - void* operator new(size_t); - void operator delete(void*); static void SetUpStatic(); static void CleanUpStatic(); @@ -114,5 +117,6 @@ public: float size,random_factor,normal_factor; float count,life,random_life; float scaleX,scaleY,rotation; + float motionBlurAmount; }; diff --git a/WickedEngine/wiLoader_BindLua.cpp b/WickedEngine/wiLoader_BindLua.cpp index ae2a21831..cbe6af962 100644 --- a/WickedEngine/wiLoader_BindLua.cpp +++ b/WickedEngine/wiLoader_BindLua.cpp @@ -1,6 +1,7 @@ #include "wiLoader_BindLua.h" #include "Vector_BindLua.h" #include "Matrix_BindLua.h" +#include "wiEmittedParticle.h" namespace wiLoader_BindLua { @@ -13,6 +14,7 @@ namespace wiLoader_BindLua Armature_BindLua::Bind(); Ray_BindLua::Bind(); AABB_BindLua::Bind(); + EmittedParticle_BindLua::Bind(); } } @@ -500,6 +502,7 @@ Luna::FunctionType Object_BindLua::methods[] = { lunamethod(Object_BindLua, GetTransparency), lunamethod(Object_BindLua, SetColor), lunamethod(Object_BindLua, GetColor), + lunamethod(Object_BindLua, GetEmitter), lunamethod(Object_BindLua, IsValid), { NULL, NULL } }; @@ -555,17 +558,25 @@ int Object_BindLua::SetColor(lua_State *L) { if (object == nullptr) { - wiLua::SError(L, "SetColor(float r, float g, float b) object is null!"); + wiLua::SError(L, "SetColor(Vector rgb) object is null!"); return 0; } int argc = wiLua::SGetArgCount(L); if (argc > 2) { - object->color = wiLua::SGetFloat3(L, 1); + Vector_BindLua* vec = Luna::lightcheck(L, 1); + if (vec != nullptr) + { + XMStoreFloat3(&object->color, vec->vector); + } + else + { + wiLua::SError(L, "SetColor(Vector rgb) argument is not a Vector!"); + } } else { - wiLua::SError(L, "SetColor(float r, float g, float b) not enough arguments!"); + wiLua::SError(L, "SetColor(Vector rgb) not enough arguments!"); } return 0; } @@ -576,8 +587,28 @@ int Object_BindLua::GetColor(lua_State *L) wiLua::SError(L, "GetColor() object is null!"); return 0; } - wiLua::SSetFloat3(L, object->color); - return 3; + Luna::push(L, new Vector_BindLua(XMLoadFloat3(&object->color))); + return 1; +} +int Object_BindLua::GetEmitter(lua_State *L) +{ + if (object == nullptr) + { + wiLua::SError(L, "GetEmitter(opt int id = 0) object is null!"); + return 0; + } + int argc = wiLua::SGetArgCount(L); + int id = 0; + if (argc > 0) + { + id = wiLua::SGetInt(L, 1); + } + if (object->eParticleSystems.size() > id) + { + Luna::push(L, new EmittedParticle_BindLua(object->eParticleSystems[id])); + } + Luna::push(L, new EmittedParticle_BindLua(L)); + return 1; } int Object_BindLua::IsValid(lua_State *L) { @@ -968,4 +999,117 @@ void AABB_BindLua::Bind() initialized = true; Luna::Register(wiLua::GetGlobal()->GetLuaState()); } +} + + + + +const char EmittedParticle_BindLua::className[] = "Emitter"; + +Luna::FunctionType EmittedParticle_BindLua::methods[] = { + lunamethod(EmittedParticle_BindLua, GetName), + lunamethod(EmittedParticle_BindLua, SetName), + lunamethod(EmittedParticle_BindLua, GetMotionBlur), + lunamethod(EmittedParticle_BindLua, SetMotionBlur), + lunamethod(EmittedParticle_BindLua, Burst), + lunamethod(EmittedParticle_BindLua, IsValid), + { NULL, NULL } +}; +Luna::PropertyType EmittedParticle_BindLua::properties[] = { + { NULL, NULL } +}; + +EmittedParticle_BindLua::EmittedParticle_BindLua(wiEmittedParticle* ps) :ps(ps) +{ +} +EmittedParticle_BindLua::EmittedParticle_BindLua(lua_State *L) +{ + ps = nullptr; +} +EmittedParticle_BindLua::~EmittedParticle_BindLua() +{ +} + +int EmittedParticle_BindLua::GetName(lua_State* L) +{ + if (ps == nullptr) + { + wiLua::SError(L, "GetName() particle system is null!"); + return 0; + } + wiLua::SSetString(L, ps->name); + return 1; +} +int EmittedParticle_BindLua::SetName(lua_State* L) +{ + if (ps == nullptr) + { + wiLua::SError(L, "SetName(string name) particle system is null!"); + return 0; + } + int argc = wiLua::SGetArgCount(L); + if (argc > 0) + { + ps->name = wiLua::SGetString(L, 1); + } + else + wiLua::SError(L, "SetName(string name) not enough arguments!"); + return 0; +} +int EmittedParticle_BindLua::GetMotionBlur(lua_State* L) +{ + if (ps == nullptr) + { + wiLua::SError(L, "GetMotionBlur() particle system is null!"); + return 0; + } + wiLua::SSetFloat(L, ps->motionBlurAmount); + return 1; +} +int EmittedParticle_BindLua::SetMotionBlur(lua_State* L) +{ + if (ps == nullptr) + { + wiLua::SError(L, "SetMotionBlur(float amount) particle system is null!"); + return 0; + } + int argc = wiLua::SGetArgCount(L); + if (argc > 0) + { + ps->motionBlurAmount = wiLua::SGetFloat(L, 1); + } + else + wiLua::SError(L, "SetMotionBlur(float amount) not enough arguments!"); + return 0; +} +int EmittedParticle_BindLua::Burst(lua_State* L) +{ + if (ps == nullptr) + { + wiLua::SError(L, "Burst(float num) particle system is null!"); + return 0; + } + int argc = wiLua::SGetArgCount(L); + if (argc > 0) + { + ps->Burst(wiLua::SGetFloat(L, 1)); + } + else + wiLua::SError(L, "Burst(float num) not enough arguments!"); + return 0; +} +int EmittedParticle_BindLua::IsValid(lua_State *L) +{ + wiLua::SSetBool(L, ps != nullptr); + return 1; +} + +void EmittedParticle_BindLua::Bind() +{ + static bool initialized = false; + if (!initialized) + { + initialized = true; + Luna::Register(wiLua::GetGlobal()->GetLuaState()); + } } \ No newline at end of file diff --git a/WickedEngine/wiLoader_BindLua.h b/WickedEngine/wiLoader_BindLua.h index f167bc73a..5d6930079 100644 --- a/WickedEngine/wiLoader_BindLua.h +++ b/WickedEngine/wiLoader_BindLua.h @@ -95,6 +95,7 @@ public: int GetTransparency(lua_State *L); int SetColor(lua_State *L); int GetColor(lua_State *L); + int GetEmitter(lua_State *L); int IsValid(lua_State *L); static void Bind(); @@ -170,3 +171,26 @@ public: static void Bind(); }; +class EmittedParticle_BindLua +{ +public: + wiEmittedParticle* ps; + + static const char className[]; + static Luna::FunctionType methods[]; + static Luna::PropertyType properties[]; + + EmittedParticle_BindLua(wiEmittedParticle* ps); + EmittedParticle_BindLua(lua_State* L); + ~EmittedParticle_BindLua(); + + int GetName(lua_State* L); + int SetName(lua_State* L); + int GetMotionBlur(lua_State* L); + int SetMotionBlur(lua_State* L); + int Burst(lua_State* L); + int IsValid(lua_State* L); + + static void Bind(); +}; + diff --git a/WickedEngine/wiRenderer_BindLua.cpp b/WickedEngine/wiRenderer_BindLua.cpp index 536f2cede..2712e54be 100644 --- a/WickedEngine/wiRenderer_BindLua.cpp +++ b/WickedEngine/wiRenderer_BindLua.cpp @@ -125,6 +125,44 @@ namespace wiRenderer_BindLua } return 0; } + int GetEmitters(lua_State* L) + { + stringstream ss(""); + for (map >::iterator it = wiRenderer::emitterSystems.begin(); it != wiRenderer::emitterSystems.end(); ++it) + { + ss << it->first << endl; + } + wiLua::SSetString(L, ss.str()); + return 1; + } + int GetEmitter(lua_State* L) + { + int argc = wiLua::SGetArgCount(L); + if (argc > 0) + { + string name = wiLua::SGetString(L, 1); + map >::iterator it = wiRenderer::emitterSystems.find(name); + if (it != wiRenderer::emitterSystems.end()) + { + int i = 0; + for (auto& x : it->second) + { + Luna::push(L, new EmittedParticle_BindLua(x)); + i++; + } + return i; + } + else + { + wiLua::SError(L, "GetEmitter(string name) no emitter by that name!"); + } + } + else + { + wiLua::SError(L, "GetEmitter(string name) not enough arguments!"); + } + return 0; + } int GetMeshes(lua_State* L) { stringstream ss(""); @@ -471,6 +509,8 @@ namespace wiRenderer_BindLua wiLua::GetGlobal()->RegisterFunc("GetArmature", GetArmature); wiLua::GetGlobal()->RegisterFunc("GetObjects", GetObjects); wiLua::GetGlobal()->RegisterFunc("GetObject", GetObjectLua); + wiLua::GetGlobal()->RegisterFunc("GetEmitters", GetEmitters); + wiLua::GetGlobal()->RegisterFunc("GetEmitter", GetEmitter); wiLua::GetGlobal()->RegisterFunc("GetMeshes", GetMeshes); wiLua::GetGlobal()->RegisterFunc("GetLights", GetLights); wiLua::GetGlobal()->RegisterFunc("GetMaterials", GetMaterials);