05_AnimatingScene.lua 6.5 KB

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