From cc2ca6a43317b3879893e00539ee1ff3f4a6b7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Sun, 9 Jan 2022 21:29:16 +0100 Subject: [PATCH] scripting updates and new sample scripts --- .../ScriptingAPI-Documentation.md | 6 +++ Content/scripts/camera_pointlight.lua | 37 ++++++++++++++++ Content/scripts/camera_spotlight.lua | 42 +++++++++++++++++++ WickedEngine/wiScene_BindLua.cpp | 34 +++++++++++++++ WickedEngine/wiScene_BindLua.h | 4 ++ WickedEngine/wiVersion.cpp | 2 +- 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 Content/scripts/camera_pointlight.lua create mode 100644 Content/scripts/camera_spotlight.lua diff --git a/Content/Documentation/ScriptingAPI-Documentation.md b/Content/Documentation/ScriptingAPI-Documentation.md index 92e04a15a..e96f7fa3c 100644 --- a/Content/Documentation/ScriptingAPI-Documentation.md +++ b/Content/Documentation/ScriptingAPI-Documentation.md @@ -429,6 +429,7 @@ The scene holds components. Entity handles can be used to retrieve associated co - Clear() -- deletes every entity and component inside the scene - Merge(Scene other) -- moves contents from an other scene into this one. The other scene will be empty after this operation (contents are moved, not copied) +- CreateEntity() : int entity -- creates an empty entity and returns it - Entity_FindByName(string value) : int entity -- returns an entity ID if it exists, and 0 otherwise - Entity_Remove(Entity entity) -- removes an entity and deletes all its components if it exists - Entity_Duplicate(Entity entity) : int entity -- duplicates all of an entity's components and creates a new entity with them. Returns the clone entity handle @@ -527,6 +528,9 @@ Describes an orientation in 3D space. - GetInvView() : Matrix result - GetInvProjection() : Matrix result - GetInvViewProjection() : Matrix result +- GetPosition() : Vector result +- GetLookDirection() : Vector result +- GetUpDirection() : Vector result #### AnimationComponent - Play() @@ -565,9 +569,11 @@ Describes an orientation in 3D space. - [outer]DIRECTIONAL : int - [outer]POINT : int - [outer]SPOT : int +- SetRange(float value) - SetEnergy(float value) - SetColor(Vector value) - SetCastShadow(bool value) +- SetFOV(float value) - GetType() : int result #### ObjectComponent diff --git a/Content/scripts/camera_pointlight.lua b/Content/scripts/camera_pointlight.lua new file mode 100644 index 000000000..41da1a7af --- /dev/null +++ b/Content/scripts/camera_pointlight.lua @@ -0,0 +1,37 @@ +-- This script will add a point light fixed to camera + +backlog_post("---> START SCRIPT: camera_pointlight.lua") + +scene = GetScene() + +runProcess(function() + + local light_entity = CreateEntity() + scene.Component_CreateLight(light_entity) + + local light = scene.Component_GetLight(light_entity) + light.SetType(POINT) + light.SetRange(20) + light.SetEnergy(4) + light.SetColor(Vector(1,0.9,0.8)) + + scene.Component_CreateTransform(light_entity) + + while true do + + local camera = GetCamera() + local campos = camera.GetPosition() + local transform = scene.Component_GetTransform(light_entity) + if transform == nil then + backlog_post("light no longer exists, exiting script") + return + else + transform.ClearTransform() + transform.Translate(campos) + end + + update() + end +end) + +backlog_post("---> END SCRIPT: camera_pointlight.lua") diff --git a/Content/scripts/camera_spotlight.lua b/Content/scripts/camera_spotlight.lua new file mode 100644 index 000000000..5ce7f2236 --- /dev/null +++ b/Content/scripts/camera_spotlight.lua @@ -0,0 +1,42 @@ +-- This script will add a spot light fixed to camera + +backlog_post("---> START SCRIPT: camera_spotlight.lua") + +scene = GetScene() + +runProcess(function() + + local light_entity = CreateEntity() + scene.Component_CreateLight(light_entity) + + local light = scene.Component_GetLight(light_entity) + light.SetType(SPOT) + light.SetRange(100) + light.SetEnergy(8) + light.SetFOV(math.pi / 3.0) + light.SetColor(Vector(1,0.7,0.8)) + + scene.Component_CreateTransform(light_entity) + + while true do + + local camera = GetCamera() + local campos = camera.GetPosition() + local camlook = camera.GetLookDirection() + local camup = camera.GetUpDirection() + local transform = scene.Component_GetTransform(light_entity) + if transform == nil then + backlog_post("light no longer exists, exiting script") + return + else + transform.ClearTransform() + transform.Rotate(Vector(-math.pi / 2.0, 0, 0)) -- spot light was facing downwards by default, rotate it to face +Z like camera default + --transform.MatrixTransform(camera.GetInvView()) + transform.MatrixTransform(matrix.Inverse(matrix.LookTo(campos, camlook, camup))) -- This is similar to camera.GetInvView() + end + + update() + end +end) + +backlog_post("---> END SCRIPT: camera_spotlight.lua") diff --git a/WickedEngine/wiScene_BindLua.cpp b/WickedEngine/wiScene_BindLua.cpp index e996a4297..2b981fc3c 100644 --- a/WickedEngine/wiScene_BindLua.cpp +++ b/WickedEngine/wiScene_BindLua.cpp @@ -1487,6 +1487,9 @@ Luna::FunctionType CameraComponent_BindLua::methods[] = lunamethod(CameraComponent_BindLua, GetInvView), lunamethod(CameraComponent_BindLua, GetInvProjection), lunamethod(CameraComponent_BindLua, GetInvViewProjection), + lunamethod(CameraComponent_BindLua, GetPosition), + lunamethod(CameraComponent_BindLua, GetLookDirection), + lunamethod(CameraComponent_BindLua, GetUpDirection), { NULL, NULL } }; Luna::PropertyType CameraComponent_BindLua::properties[] = { @@ -1675,6 +1678,21 @@ int CameraComponent_BindLua::GetInvViewProjection(lua_State* L) Luna::push(L, new Matrix_BindLua(component->GetInvViewProjection())); return 1; } +int CameraComponent_BindLua::GetPosition(lua_State* L) +{ + Luna::push(L, new Vector_BindLua(component->GetEye())); + return 1; +} +int CameraComponent_BindLua::GetLookDirection(lua_State* L) +{ + Luna::push(L, new Vector_BindLua(component->GetAt())); + return 1; +} +int CameraComponent_BindLua::GetUpDirection(lua_State* L) +{ + Luna::push(L, new Vector_BindLua(component->GetUp())); + return 1; +} @@ -2135,6 +2153,7 @@ Luna::FunctionType LightComponent_BindLua::methods[] = { lunamethod(LightComponent_BindLua, SetColor), lunamethod(LightComponent_BindLua, SetCastShadow), lunamethod(LightComponent_BindLua, GetType), + lunamethod(LightComponent_BindLua, SetFOV), { NULL, NULL } }; Luna::PropertyType LightComponent_BindLua::properties[] = { @@ -2235,6 +2254,21 @@ int LightComponent_BindLua::SetCastShadow(lua_State* L) return 0; } +int LightComponent_BindLua::SetFOV(lua_State* L) +{ + int argc = wi::lua::SGetArgCount(L); + if (argc > 0) + { + float value = wi::lua::SGetFloat(L, 1); + component->fov = value; + } + else + { + wi::lua::SError(L, "SetFOV(float value) not enough arguments!"); + } + + return 0; +} int LightComponent_BindLua::GetType(lua_State* L) { diff --git a/WickedEngine/wiScene_BindLua.h b/WickedEngine/wiScene_BindLua.h index 1d5ba445f..caa25a0b8 100644 --- a/WickedEngine/wiScene_BindLua.h +++ b/WickedEngine/wiScene_BindLua.h @@ -179,6 +179,9 @@ namespace wi::lua::scene int GetInvView(lua_State* L); int GetInvProjection(lua_State* L); int GetInvViewProjection(lua_State* L); + int GetPosition(lua_State* L); + int GetLookDirection(lua_State* L); + int GetUpDirection(lua_State* L); }; class AnimationComponent_BindLua @@ -275,6 +278,7 @@ namespace wi::lua::scene int SetEnergy(lua_State* L); int SetColor(lua_State* L); int SetCastShadow(lua_State* L); + int SetFOV(lua_State* L); int GetType(lua_State* L); }; diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index f9fcf9fd9..b28a02ec9 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wi::version // minor features, major updates, breaking compatibility changes const int minor = 60; // minor bug fixes, alterations, refactors, updates - const int revision = 14; + const int revision = 15; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);