Browse Source

Added C++ / script integration samples, which reimplement AnimatingScene sample, but use a script object instead of a C++ component.
Added Execute() function to LuaScriptInstance.
Fixed LuaScriptInstance::SetScriptFileName() not loading the script.

Lasse Öörni 12 years ago
parent
commit
48532b2509

+ 19 - 0
Bin/Data/LuaScripts/Rotator.lua

@@ -0,0 +1,19 @@
+-- Rotator script object class. Script objects to be added to a scene node must implement the empty ScriptObject interface
+Rotator = ScriptObject()
+
+function Rotator:Start()
+    self.rotationSpeed = {0.0, 0.0, 0.0}
+end
+
+function Rotator:SetRotationSpeed(x, y, z)
+    self.rotationSpeed[1] = x;
+    self.rotationSpeed[2] = y;
+    self.rotationSpeed[3] = z;
+end
+
+function Rotator:Update(timeStep)
+    local x = self.rotationSpeed[1] * timeStep
+    local y = self.rotationSpeed[2] * timeStep
+    local z = self.rotationSpeed[3] * timeStep
+    self.node:RotateXYZ(x, y, z)
+end

+ 16 - 0
Bin/Data/Scripts/Rotator.as

@@ -0,0 +1,16 @@
+// Rotator script object class. Script objects to be added to a scene node must implement the empty ScriptObject interface
+class Rotator : ScriptObject
+{
+    Vector3 rotationSpeed;
+
+    void SetRotationSpeed(const Vector3&in speed)
+    {
+        rotationSpeed = speed;
+    }
+
+    // Update is called during the variable timestep scene update
+    void Update(float timeStep)
+    {
+        node.Rotate(Quaternion(rotationSpeed.x * timeStep, rotationSpeed.y * timeStep, rotationSpeed.z * timeStep));
+    }
+}

+ 1 - 1
Docs/Urho3D.dox

@@ -59,8 +59,8 @@ Urho3D development, contributions and bugfixes by:
 - Chris Friesen
 - Alex Fuller
 - Mika Heinonen
-- Ali Kämäräinen
 - Jason Kinzer
+- Ali Kämäräinen
 - Pete Leigh
 - Paul Noome
 - Vladimir Pobedinsky

+ 1 - 1
Readme.txt

@@ -20,8 +20,8 @@ Urho3D development, contributions and bugfixes by:
 - Chris Friesen
 - Alex Fuller
 - Mika Heinonen
-- Ali Kämäräinen
 - Jason Kinzer
+- Ali Kämäräinen
 - Pete Leigh
 - Paul Noome
 - Vladimir Pobedinsky

+ 94 - 8
Source/Extras/LuaScript/LuaScriptInstance.cpp

@@ -112,20 +112,16 @@ void LuaScriptInstance::SetScriptFileName(const String& scriptFileName)
 
     scriptFileName_ = scriptFileName;
 
-    if (!scriptFileName_.Empty())
+    if (scriptFileName_.Empty())
         return;
 
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     LuaFile* luaFile = cache->GetResource<LuaFile>(scriptFileName_);
     if (!luaFile)
-    {
-        LOGERROR("Get Lua file failed: " + scriptFileName_);
-    }
+        return;
 
     if (!luaFile->LoadAndExecute(luaState_))
-    {
         LOGERROR("Execute Lua file failed: " + scriptFileName_);
-    }
 }
 
 void LuaScriptInstance::SetScriptObjectType(const String& scriptObjectType)
@@ -266,6 +262,18 @@ void LuaScriptInstance::ScriptUnsubscribeFromEvents(void* sender)
     objectToEventTypeToFunctionRefMap_.Erase(it);
 }
 
+bool LuaScriptInstance::Execute(const String& functionName, const VariantVector& parameters)
+{
+    if (scriptObjectRef_ == LUA_REFNIL)
+        return false;
+
+    int functionRef = luaScript_->GetScriptFunctionRef(scriptObjectType_ + "." + functionName);
+    if (functionRef == LUA_REFNIL)
+        return false;
+    
+    return CallScriptObjectFunction(functionRef, parameters);
+}
+
 PODVector<unsigned char> LuaScriptInstance::GetScriptDataAttr() const
 {
     int functionRef = scriptObjectMethodRefs_[LSOM_SAVE];
@@ -439,7 +447,6 @@ void LuaScriptInstance::CallScriptObjectFunction(int functionRef, float timeStep
     }
 }
 
-
 void LuaScriptInstance::CallScriptObjectFunction(int functionRef, Deserializer& deserializer)
 {
     if (functionRef == LUA_REFNIL)
@@ -490,7 +497,7 @@ void LuaScriptInstance::CallScriptObjectFunction(int functionRef, Serializer& se
     }
 }
 
-void LuaScriptInstance::CallScriptObjectFunction(int functionRef, StringHash eventType, VariantMap& eventData )
+void LuaScriptInstance::CallScriptObjectFunction(int functionRef, StringHash eventType, VariantMap& eventData)
 {
     if (functionRef == LUA_REFNIL)
         return;
@@ -518,4 +525,83 @@ void LuaScriptInstance::CallScriptObjectFunction(int functionRef, StringHash eve
     }
 }
 
+bool LuaScriptInstance::CallScriptObjectFunction(int functionRef, const VariantVector& parameters)
+{
+    if (functionRef == LUA_REFNIL)
+        return false;
+
+    int top = lua_gettop(luaState_);
+
+    // Push function.
+    lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
+
+    // Push script object.
+    lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
+
+    unsigned numParams = 1;
+    
+    // Push specified parameters
+    for (unsigned i = 0; i < parameters.Size(); ++i)
+    {
+        switch (parameters[i].GetType())
+        {
+        case VAR_BOOL:
+            tolua_pushboolean(luaState_, parameters[i].GetBool() ? 1 : 0);
+            ++numParams;
+            break;
+            
+        case VAR_INT:
+            tolua_pushnumber(luaState_, (double)parameters[i].GetInt());
+            ++numParams;
+            break;
+            
+        case VAR_FLOAT:
+            tolua_pushnumber(luaState_, parameters[i].GetFloat());
+            ++numParams;
+            break;
+            
+        case VAR_STRING:
+            tolua_pushstring(luaState_, parameters[i].GetString().CString());
+            ++numParams;
+            break;
+            
+        case VAR_VECTOR2:
+            tolua_pushusertype(luaState_, &const_cast<Vector2&>(parameters[i].GetVector2()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+
+        case VAR_VECTOR3:
+            tolua_pushusertype(luaState_, &const_cast<Vector3&>(parameters[i].GetVector3()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+            
+        case VAR_VECTOR4:
+            tolua_pushusertype(luaState_, &const_cast<Vector4&>(parameters[i].GetVector4()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+
+        case VAR_QUATERNION:
+            tolua_pushusertype(luaState_, &const_cast<Quaternion&>(parameters[i].GetQuaternion()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+            
+        case VAR_COLOR:
+            tolua_pushusertype(luaState_, &const_cast<Color&>(parameters[i].GetColor()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+        }
+    }
+    
+    // Call script object function.
+    if (lua_pcall(luaState_, numParams, 0, 0) != 0)
+    {
+        const char* message = lua_tostring(luaState_, -1);
+        LOGERROR("Execute Lua function failed: " + String(message));
+        lua_settop(luaState_, top);
+        return false;
+    }
+    
+    return true;
+}
+
 }

+ 6 - 0
Source/Extras/LuaScript/LuaScriptInstance.h

@@ -101,6 +101,9 @@ public:
     /// Script unsubscribe from a specific sender's all events.
     void ScriptUnsubscribeFromEvents(void* sender);
 
+    /// Execute a script object function with parameters. Return true if successful.
+    bool Execute(const String& functionName, const VariantVector& parameters);
+    
     /// Return script file name.
     const String& GetScriptFileName() const { return scriptFileName_; }
 
@@ -156,6 +159,9 @@ private:
     /// Call script object function.
     void CallScriptObjectFunction(int functionRef, StringHash eventType, VariantMap& eventData);
 
+    /// Call script object function with arbitrary parameters.
+    bool CallScriptObjectFunction(int functionRef, const VariantVector& parameters);
+
     // Lua Script.
     LuaScript* luaScript_;
 

+ 205 - 0
Source/Samples/21_AngelScriptIntegration/AngelScriptIntegration.cpp

@@ -0,0 +1,205 @@
+//
+// Copyright (c) 2008-2013 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "Camera.h"
+#include "CoreEvents.h"
+#include "Engine.h"
+#include "Font.h"
+#include "Graphics.h"
+#include "Input.h"
+#include "Material.h"
+#include "Model.h"
+#include "Octree.h"
+#include "Renderer.h"
+#include "ResourceCache.h"
+#include "Scene.h"
+#include "Script.h"
+#include "ScriptFile.h"
+#include "ScriptInstance.h"
+#include "StaticModel.h"
+#include "Text.h"
+#include "UI.h"
+#include "Zone.h"
+
+#include "AngelScriptIntegration.h"
+
+#include "DebugNew.h"
+
+DEFINE_APPLICATION_MAIN(AngelScriptIntegration)
+
+AngelScriptIntegration::AngelScriptIntegration(Context* context) :
+    Sample(context),
+    yaw_(0.0f),
+    pitch_(0.0f)
+{
+    // Instantiate and register the AngelScript subsystem so that we can use the ScriptInstance component
+    context_->RegisterSubsystem(new Script(context_));
+}
+
+void AngelScriptIntegration::Start()
+{
+    // Execute base class startup
+    Sample::Start();
+
+    // Create the scene content
+    CreateScene();
+    
+    // Create the UI content
+    CreateInstructions();
+    
+    // Setup the viewport for displaying the scene
+    SetupViewport();
+
+    // Hook up to the frame update events
+    SubscribeToEvents();
+}
+
+void AngelScriptIntegration::CreateScene()
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    
+    scene_ = new Scene(context_);
+    
+    // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
+    // (-1000, -1000, -1000) to (1000, 1000, 1000)
+    scene_->CreateComponent<Octree>();
+    
+    // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
+    // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y
+    // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
+    Node* zoneNode = scene_->CreateChild("Zone");
+    Zone* zone = zoneNode->CreateComponent<Zone>();
+    // Set same volume as the Octree, set a close bluish fog and some ambient light
+    zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
+    zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f));
+    zone->SetFogColor(Color(0.1f, 0.2f, 0.3f));
+    zone->SetFogStart(10.0f);
+    zone->SetFogEnd(100.0f);
+    
+    // Create randomly positioned and oriented box StaticModels in the scene
+    const unsigned NUM_OBJECTS = 2000;
+    for (unsigned i = 0; i < NUM_OBJECTS; ++i)
+    {
+        Node* boxNode = scene_->CreateChild("Box");
+        boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f));
+        // Orient using random pitch, yaw and roll Euler angles
+        boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f)));
+        StaticModel* boxObject = boxNode->CreateComponent<StaticModel>();
+        boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
+        boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
+        
+        // Add our custom Rotator script object (using the ScriptInstance C++ component to instantiate / store it) which will
+        // rotate the scene node each frame, when the scene sends its update event
+        ScriptInstance* instance = boxNode->CreateComponent<ScriptInstance>();
+        instance->CreateObject(cache->GetResource<ScriptFile>("Scripts/Rotator.as"), "Rotator");
+        // Call the script object's "SetRotationSpeed" function. Function arguments need to be passed in a VariantVector
+        VariantVector parameters;
+        parameters.Push(Vector3(10.0f, 20.0f, 30.0f));
+        instance->Execute("void SetRotationSpeed(const Vector3&in)", parameters);
+    }
+    
+    // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
+    // bring the far clip plane closer for more effective culling of distant objects
+    cameraNode_ = scene_->CreateChild("Camera");
+    Camera* camera = cameraNode_->CreateComponent<Camera>();
+    camera->SetFarClip(100.0f);
+    
+    // Create a point light to the camera scene node
+    Light* light = cameraNode_->CreateComponent<Light>();
+    light->SetLightType(LIGHT_POINT);
+    light->SetRange(30.0f);
+}
+
+void AngelScriptIntegration::CreateInstructions()
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    UI* ui = GetSubsystem<UI>();
+    
+    // Construct new Text object, set string to display and font to use
+    Text* instructionText = ui->GetRoot()->CreateChild<Text>();
+    instructionText->SetText("Use WASD keys and mouse to move");
+    instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
+    
+    // Position the text relative to the screen center
+    instructionText->SetHorizontalAlignment(HA_CENTER);
+    instructionText->SetVerticalAlignment(VA_CENTER);
+    instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
+}
+
+void AngelScriptIntegration::SetupViewport()
+{
+    Renderer* renderer = GetSubsystem<Renderer>();
+    
+    // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
+    SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
+    renderer->SetViewport(0, viewport);
+}
+
+void AngelScriptIntegration::SubscribeToEvents()
+{
+    // Subscribe HandleUpdate() function for processing update events
+    SubscribeToEvent(E_UPDATE, HANDLER(AngelScriptIntegration, HandleUpdate));
+}
+
+void AngelScriptIntegration::MoveCamera(float timeStep)
+{
+    // Do not move if the UI has a focused element (the console)
+    if (GetSubsystem<UI>()->GetFocusElement())
+        return;
+    
+    Input* input = GetSubsystem<Input>();
+    
+    // Movement speed as world units per second
+    const float MOVE_SPEED = 20.0f;
+    // Mouse sensitivity as degrees per pixel
+    const float MOUSE_SENSITIVITY = 0.1f;
+    
+    // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
+    IntVector2 mouseMove = input->GetMouseMove();
+    yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
+    pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
+    pitch_ = Clamp(pitch_, -90.0f, 90.0f);
+    
+    // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
+    cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
+    
+    // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
+    if (input->GetKeyDown('W'))
+        cameraNode_->TranslateRelative(Vector3::FORWARD * MOVE_SPEED * timeStep);
+    if (input->GetKeyDown('S'))
+        cameraNode_->TranslateRelative(Vector3::BACK * MOVE_SPEED * timeStep);
+    if (input->GetKeyDown('A'))
+        cameraNode_->TranslateRelative(Vector3::LEFT * MOVE_SPEED * timeStep);
+    if (input->GetKeyDown('D'))
+        cameraNode_->TranslateRelative(Vector3::RIGHT * MOVE_SPEED * timeStep);
+}
+
+void AngelScriptIntegration::HandleUpdate(StringHash eventType, VariantMap& eventData)
+{
+    using namespace Update;
+
+    // Take the frame time step, which is stored as a float
+    float timeStep = eventData[P_TIMESTEP].GetFloat();
+    
+    // Move the camera, scale movement with time step
+    MoveCamera(timeStep);
+}

+ 72 - 0
Source/Samples/21_AngelScriptIntegration/AngelScriptIntegration.h

@@ -0,0 +1,72 @@
+//
+// Copyright (c) 2008-2013 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "Sample.h"
+
+namespace Urho3D
+{
+
+class Node;
+class Scene;
+
+}
+
+/// Animating 3D scene example with script integration.
+/// This sample demonstrates:
+///     - Initializing the AngelScript scripting subsystem
+///     - Creating a 3D scene and using a script object to animate the objects
+class AngelScriptIntegration : public Sample
+{
+    OBJECT(AngelScriptIntegration);
+
+public:
+    /// Construct.
+    AngelScriptIntegration(Context* context);
+
+    /// Setup after engine initialization and before running the main loop.
+    virtual void Start();
+
+private:
+    /// Construct the scene content.
+    void CreateScene();
+    /// Construct an instruction text to the UI.
+    void CreateInstructions();
+    /// Set up a viewport for displaying the scene.
+    void SetupViewport();
+    /// Subscribe to application-wide logic update events.
+    void SubscribeToEvents();
+    /// Read input and moves the camera.
+    void MoveCamera(float timeStep);
+    /// Handle the logic update event.
+    void HandleUpdate(StringHash eventType, VariantMap& eventData);
+
+    /// Scene.
+    SharedPtr<Scene> scene_;
+    /// Camera scene node.
+    SharedPtr<Node> cameraNode_;
+    /// Camera yaw angle.
+    float yaw_;
+    /// Camera pitch angle.
+    float pitch_;
+};

+ 35 - 0
Source/Samples/21_AngelScriptIntegration/CMakeLists.txt

@@ -0,0 +1,35 @@
+#
+# Copyright (c) 2008-2013 the Urho3D project.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+#
+
+# Define target name
+set (TARGET_NAME 21_AngelScriptIntegration)
+
+# Define source files
+file (GLOB CPP_FILES *.cpp)
+file (GLOB H_FILES *.h)
+set (SOURCE_FILES ${CPP_FILES} ${H_FILES} ${COMMON_SAMPLE_H_FILES})
+
+# Define dependency libs
+set (LIBS ${LIBS} ../../Engine/Script)
+
+# Setup target with resource copying
+setup_main_executable ()

+ 35 - 0
Source/Samples/22_LuaIntegration/CMakeLists.txt

@@ -0,0 +1,35 @@
+#
+# Copyright (c) 2008-2013 the Urho3D project.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+#
+
+# Define target name
+set (TARGET_NAME 22_LuaIntegration)
+
+# Define source files
+file (GLOB CPP_FILES *.cpp)
+file (GLOB H_FILES *.h)
+set (SOURCE_FILES ${CPP_FILES} ${H_FILES} ${COMMON_SAMPLE_H_FILES})
+
+# Define dependency libs
+set (LIBS ${LIBS} ../../Extras/LuaScript)
+
+# Setup target with resource copying
+setup_main_executable ()

+ 206 - 0
Source/Samples/22_LuaIntegration/LuaIntegration.cpp

@@ -0,0 +1,206 @@
+//
+// Copyright (c) 2008-2013 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "Camera.h"
+#include "CoreEvents.h"
+#include "Engine.h"
+#include "Font.h"
+#include "Graphics.h"
+#include "Input.h"
+#include "LuaScript.h"
+#include "LuaScriptInstance.h"
+#include "Material.h"
+#include "Model.h"
+#include "Octree.h"
+#include "Renderer.h"
+#include "ResourceCache.h"
+#include "Scene.h"
+#include "StaticModel.h"
+#include "Text.h"
+#include "UI.h"
+#include "Zone.h"
+
+#include "LuaIntegration.h"
+
+#include "DebugNew.h"
+
+DEFINE_APPLICATION_MAIN(LuaIntegration)
+
+LuaIntegration::LuaIntegration(Context* context) :
+    Sample(context),
+    yaw_(0.0f),
+    pitch_(0.0f)
+{
+    // Instantiate and register the Lua script subsystem so that we can use the LuaScriptInstance component
+    context_->RegisterSubsystem(new LuaScript(context_));
+}
+
+void LuaIntegration::Start()
+{
+    // Execute base class startup
+    Sample::Start();
+
+    // Create the scene content
+    CreateScene();
+
+    // Create the UI content
+    CreateInstructions();
+
+    // Setup the viewport for displaying the scene
+    SetupViewport();
+
+    // Hook up to the frame update events
+    SubscribeToEvents();
+}
+
+void LuaIntegration::CreateScene()
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+
+    scene_ = new Scene(context_);
+
+    // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
+    // (-1000, -1000, -1000) to (1000, 1000, 1000)
+    scene_->CreateComponent<Octree>();
+
+    // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
+    // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y
+    // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
+    Node* zoneNode = scene_->CreateChild("Zone");
+    Zone* zone = zoneNode->CreateComponent<Zone>();
+    // Set same volume as the Octree, set a close bluish fog and some ambient light
+    zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
+    zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f));
+    zone->SetFogColor(Color(0.1f, 0.2f, 0.3f));
+    zone->SetFogStart(10.0f);
+    zone->SetFogEnd(100.0f);
+    
+    // Create randomly positioned and oriented box StaticModels in the scene
+    const unsigned NUM_OBJECTS = 2000;
+    for (unsigned i = 0; i < NUM_OBJECTS; ++i)
+    {
+        Node* boxNode = scene_->CreateChild("Box");
+        boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f));
+        // Orient using random pitch, yaw and roll Euler angles
+        boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f)));
+        StaticModel* boxObject = boxNode->CreateComponent<StaticModel>();
+        boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
+        boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
+        
+        // Add our custom Rotator script object (using the LuaScriptInstance C++ component to instantiate / store it) which will
+        // rotate the scene node each frame, when the scene sends its update event
+        LuaScriptInstance* instance = boxNode->CreateComponent<LuaScriptInstance>();
+        instance->CreateObject("LuaScripts/Rotator.lua", "Rotator");
+        // Call the script object's "SetRotationSpeed" function. Function arguments need to be passed in a VariantVector
+        VariantVector parameters;
+        parameters.Push(10.0f);
+        parameters.Push(20.0f);
+        parameters.Push(30.0f);
+        instance->Execute("SetRotationSpeed", parameters);
+    }
+    
+    // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
+    // bring the far clip plane closer for more effective culling of distant objects
+    cameraNode_ = scene_->CreateChild("Camera");
+    Camera* camera = cameraNode_->CreateComponent<Camera>();
+    camera->SetFarClip(100.0f);
+    
+    // Create a point light to the camera scene node
+    Light* light = cameraNode_->CreateComponent<Light>();
+    light->SetLightType(LIGHT_POINT);
+    light->SetRange(30.0f);
+}
+
+void LuaIntegration::CreateInstructions()
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    UI* ui = GetSubsystem<UI>();
+    
+    // Construct new Text object, set string to display and font to use
+    Text* instructionText = ui->GetRoot()->CreateChild<Text>();
+    instructionText->SetText("Use WASD keys and mouse to move");
+    instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
+    
+    // Position the text relative to the screen center
+    instructionText->SetHorizontalAlignment(HA_CENTER);
+    instructionText->SetVerticalAlignment(VA_CENTER);
+    instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
+}
+
+void LuaIntegration::SetupViewport()
+{
+    Renderer* renderer = GetSubsystem<Renderer>();
+    
+    // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
+    SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
+    renderer->SetViewport(0, viewport);
+}
+
+void LuaIntegration::SubscribeToEvents()
+{
+    // Subscribe HandleUpdate() function for processing update events
+    SubscribeToEvent(E_UPDATE, HANDLER(LuaIntegration, HandleUpdate));
+}
+
+void LuaIntegration::MoveCamera(float timeStep)
+{
+    // Do not move if the UI has a focused element (the console)
+    if (GetSubsystem<UI>()->GetFocusElement())
+        return;
+    
+    Input* input = GetSubsystem<Input>();
+    
+    // Movement speed as world units per second
+    const float MOVE_SPEED = 20.0f;
+    // Mouse sensitivity as degrees per pixel
+    const float MOUSE_SENSITIVITY = 0.1f;
+    
+    // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
+    IntVector2 mouseMove = input->GetMouseMove();
+    yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
+    pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
+    pitch_ = Clamp(pitch_, -90.0f, 90.0f);
+    
+    // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
+    cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
+    
+    // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
+    if (input->GetKeyDown('W'))
+        cameraNode_->TranslateRelative(Vector3::FORWARD * MOVE_SPEED * timeStep);
+    if (input->GetKeyDown('S'))
+        cameraNode_->TranslateRelative(Vector3::BACK * MOVE_SPEED * timeStep);
+    if (input->GetKeyDown('A'))
+        cameraNode_->TranslateRelative(Vector3::LEFT * MOVE_SPEED * timeStep);
+    if (input->GetKeyDown('D'))
+        cameraNode_->TranslateRelative(Vector3::RIGHT * MOVE_SPEED * timeStep);
+}
+
+void LuaIntegration::HandleUpdate(StringHash eventType, VariantMap& eventData)
+{
+    using namespace Update;
+
+    // Take the frame time step, which is stored as a float
+    float timeStep = eventData[P_TIMESTEP].GetFloat();
+    
+    // Move the camera, scale movement with time step
+    MoveCamera(timeStep);
+}

+ 72 - 0
Source/Samples/22_LuaIntegration/LuaIntegration.h

@@ -0,0 +1,72 @@
+//
+// Copyright (c) 2008-2013 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "Sample.h"
+
+namespace Urho3D
+{
+
+class Node;
+class Scene;
+
+}
+
+/// Animating 3D scene example with script integration.
+/// This sample demonstrates:
+///     - Initializing the Lua scripting subsystem
+///     - Creating a 3D scene and using a script object to animate the objects
+class LuaIntegration : public Sample
+{
+    OBJECT(LuaIntegration);
+
+public:
+    /// Construct.
+    LuaIntegration(Context* context);
+
+    /// Setup after engine initialization and before running the main loop.
+    virtual void Start();
+
+private:
+    /// Construct the scene content.
+    void CreateScene();
+    /// Construct an instruction text to the UI.
+    void CreateInstructions();
+    /// Set up a viewport for displaying the scene.
+    void SetupViewport();
+    /// Subscribe to application-wide logic update events.
+    void SubscribeToEvents();
+    /// Read input and moves the camera.
+    void MoveCamera(float timeStep);
+    /// Handle the logic update event.
+    void HandleUpdate(StringHash eventType, VariantMap& eventData);
+
+    /// Scene.
+    SharedPtr<Scene> scene_;
+    /// Camera scene node.
+    SharedPtr<Node> cameraNode_;
+    /// Camera yaw angle.
+    float yaw_;
+    /// Camera pitch angle.
+    float pitch_;
+};

+ 4 - 0
Source/Samples/CMakeLists.txt

@@ -62,3 +62,7 @@ add_subdirectory (17_SceneReplication)
 add_subdirectory (18_CharacterDemo)
 add_subdirectory (19_VehicleDemo)
 add_subdirectory (20_HugeObjectCount)
+add_subdirectory (21_AngelScriptIntegration)
+if (ENABLE_LUA)
+    add_subdirectory (22_LuaIntegration)
+endif ()