05_AnimatingScene.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. -- Animating 3D scene example.
  2. -- This sample demonstrates:
  3. -- - Creating a 3D scene and using a script component to animate the objects;
  4. -- - Controlling scene ambience with the Zone component;
  5. -- - Attaching a light to an object (the camera);
  6. require "LuaScripts/Utilities/Sample"
  7. local scene_ = nil
  8. local cameraNode = nil
  9. local yaw = 0.0
  10. local pitch = 0.0
  11. local context = GetContext()
  12. local cache = GetCache()
  13. local input = GetInput()
  14. local renderer = GetRenderer()
  15. local ui = GetUI()
  16. function Start()
  17. -- Execute the common startup for samples
  18. SampleStart()
  19. -- Create the scene content
  20. CreateScene()
  21. -- Create the UI content
  22. CreateInstructions()
  23. -- Setup the viewport for displaying the scene
  24. SetupViewport()
  25. -- Hook up to the frame update events
  26. SubscribeToEvents()
  27. end
  28. function Stop()
  29. end
  30. function CreateScene()
  31. scene_ = Scene(context)
  32. -- Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  33. -- (-1000, -1000, -1000) to (1000, 1000, 1000)
  34. scene_:CreateComponent("Octree")
  35. -- Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
  36. -- 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
  37. -- and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering several zones can exist
  38. local zoneNode = scene_:CreateChild("Zone")
  39. local zone = zoneNode:CreateComponent("Zone")
  40. -- Set same volume as the Octree, set a close bluish fog and some ambient light
  41. zone.boundingBox = BoundingBox(-1000.0, 1000.0)
  42. zone.ambientColor = Color(0.05, 0.1, 0.15)
  43. zone.fogColor = Color(0.1, 0.2, 0.3)
  44. zone.fogStart = 10.0
  45. zone.fogEnd = 100.0
  46. -- Create randomly positioned and oriented box StaticModels in the scene
  47. local NUM_OBJECTS = 2000
  48. for i = 1, NUM_OBJECTS do
  49. local boxNode = scene_:CreateChild("Box")
  50. boxNode.position = Vector3(Random(200.0) - 100.0, Random(200.0) - 100.0, Random(200.0) - 100.0)
  51. -- Orient using random pitch, yaw and roll Euler angles
  52. boxNode.rotation = Quaternion(Random(360.0), Random(360.0), Random(360.0))
  53. local boxObject = boxNode:CreateComponent("StaticModel")
  54. boxObject.model = cache:GetResource("Model", "Models/Box.mdl")
  55. boxObject.material = cache:GetResource("Material", "Materials/Stone.xml")
  56. -- Add the Rotator script object which will rotate the scene node each frame, when the scene sends its update event.
  57. -- This requires the C++ component LuaScriptInstance in the scene node, which acts as a container. We need to tell the
  58. -- script file and class name to instantiate the object (scriptFile is a global property which refers to the currently
  59. -- executing script file.) There is also a shortcut for creating the LuaScriptInstance component and the script object,
  60. -- which is shown in a later sample, but this is what happens "under the hood."
  61. local instance = boxNode:CreateComponent("LuaScriptInstance")
  62. instance:CreateObject("Rotator")
  63. instance.object.rotationSpeed = Vector3(10.0, 20.0, 30.0)
  64. end
  65. -- Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
  66. -- bring the far clip plane closer for more effective culling of distant objects
  67. cameraNode = scene_:CreateChild("Camera")
  68. local camera = cameraNode:CreateComponent("Camera")
  69. camera.farClip = 100.0
  70. -- Create a point light to the camera scene node
  71. light = cameraNode:CreateComponent("Light")
  72. light.lightType = LIGHT_POINT
  73. light.range = 30.0
  74. end
  75. function CreateInstructions()
  76. -- Construct new Text object, set string to display and font to use
  77. local instructionText = ui.root:CreateChild("Text")
  78. instructionText:SetText("Use WASD keys and mouse to move")
  79. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  80. -- Position the text relative to the screen center
  81. instructionText.horizontalAlignment = HA_CENTER
  82. instructionText.verticalAlignment= VA_CENTER
  83. instructionText:SetPosition(0, ui.root.height / 4)
  84. end
  85. function SetupViewport()
  86. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  87. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  88. -- use, but now we just use full screen and default render path configured in the engine command line options
  89. local viewport = Viewport:new(context, scene_, cameraNode:GetComponent("Camera"))
  90. renderer:SetViewport(0, viewport)
  91. end
  92. function MoveCamera(timeStep)
  93. -- Do not move if the UI has a focused element (the console)
  94. if ui.focusElement ~= nil then
  95. return
  96. end
  97. -- Movement speed as world units per second
  98. local MOVE_SPEED = 20.0
  99. -- Mouse sensitivity as degrees per pixel
  100. local MOUSE_SENSITIVITY = 0.1
  101. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  102. local mouseMove = input.mouseMove
  103. yaw = yaw +MOUSE_SENSITIVITY * mouseMove.x
  104. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  105. pitch = Clamp(pitch, -90.0, 90.0)
  106. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  107. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  108. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  109. if input:GetKeyDown(KEY_W) then
  110. cameraNode:TranslateRelative(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  111. end
  112. if input:GetKeyDown(KEY_S) then
  113. cameraNode:TranslateRelative(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  114. end
  115. if input:GetKeyDown(KEY_A) then
  116. cameraNode:TranslateRelative(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  117. end
  118. if input:GetKeyDown(KEY_D) then
  119. cameraNode:TranslateRelative(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  120. end
  121. end
  122. function SubscribeToEvents()
  123. -- Subscribe HandleUpdate() function for processing update events
  124. SubscribeToEvent("Update", "HandleUpdate")
  125. end
  126. function HandleUpdate(eventType, eventData)
  127. -- Take the frame time step, which is stored as a float
  128. local timeStep = eventData:GetFloat("TimeStep")
  129. -- Move the camera, scale movement with time step
  130. MoveCamera(timeStep)
  131. end
  132. -- Rotator script object class. Script objects to be added to a scene node must implement the empty ScriptObject interface
  133. Rotator = ScriptObject()
  134. function Rotator:Start()
  135. self.rotationSpeed = Vector3(0.0, 0.0, 0.0)
  136. self:SubscribeToEvent("Update", "Rotator.Update")
  137. end
  138. function Rotator:Stop()
  139. end
  140. -- Update is called during the variable timestep scene update
  141. function Rotator.Update(self, eventType, eventData)
  142. local timeStep = eventData:GetFloat("TimeStep")
  143. local x = self.rotationSpeed.x * timeStep
  144. local y = self.rotationSpeed.y * timeStep
  145. local z = self.rotationSpeed.z * timeStep
  146. self:GetNode():Rotate(Quaternion(x, y, z))
  147. end