04_StaticScene.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. -- Static 3D scene example.
  2. -- This sample demonstrates:
  3. -- - Creating a 3D scene with static content
  4. -- - Displaying the scene using the Renderer subsystem
  5. -- - Handling keyboard and mouse input to move a freelook 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 cache = GetCache()
  12. local input = GetInput()
  13. local renderer = GetRenderer()
  14. local ui = GetUI()
  15. function Start()
  16. -- Execute the common startup for samples
  17. SampleStart()
  18. -- Create the scene content
  19. CreateScene()
  20. -- Create the UI content
  21. CreateInstructions()
  22. -- Setup the viewport for displaying the scene
  23. SetupViewport()
  24. -- Hook up to the frame update events
  25. SubscribeToEvents()
  26. end
  27. function CreateScene()
  28. scene_ = Scene()
  29. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  30. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  31. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  32. -- optimizing manner
  33. scene_:CreateComponent("Octree")
  34. -- Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  35. -- plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  36. -- (100 x 100 world units)
  37. local planeNode = scene_:CreateChild("Plane")
  38. planeNode.scale = Vector3(100.0, 1.0, 100.0)
  39. local planeObject = planeNode:CreateComponent("StaticModel")
  40. planeObject.model = cache:GetResource("Model", "Models/Plane.mdl")
  41. planeObject.material = cache:GetResource("Material", "Materials/StoneTiled.xml")
  42. -- Create a directional light to the world so that we can see something. The light scene node's orientation controls the
  43. -- light direction we will use the SetDirection() function which calculates the orientation from a forward direction vector.
  44. -- The light will use default settings (white light, no shadows)
  45. local lightNode = scene_:CreateChild("DirectionalLight")
  46. lightNode.direction = Vector3(0.6, -1.0, 0.8) -- The direction vector does not need to be normalized
  47. local light = lightNode:CreateComponent("Light")
  48. light.lightType = LIGHT_DIRECTIONAL
  49. -- Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  50. -- quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  51. -- LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  52. -- see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  53. -- same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  54. -- scene.
  55. local NUM_OBJECTS = 200
  56. for i = 1, NUM_OBJECTS do
  57. local mushroomNode = scene_:CreateChild("Mushroom")
  58. mushroomNode.position = Vector3(Random(90.0) - 45.0, 0.0, Random(90.0) - 45.0)
  59. mushroomNode.rotation = Quaternion(0.0, Random(360.0), 0.0)
  60. mushroomNode:SetScale(0.5 + Random(2.0))
  61. local mushroomObject = mushroomNode:CreateComponent("StaticModel")
  62. mushroomObject.model = cache:GetResource("Model", "Models/Mushroom.mdl")
  63. mushroomObject.material = cache:GetResource("Material", "Materials/Mushroom.xml")
  64. end
  65. -- Create a scene node for the camera, which we will move around
  66. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  67. cameraNode = scene_:CreateChild("Camera")
  68. cameraNode:CreateComponent("Camera")
  69. -- Set an initial position for the camera scene node above the plane
  70. cameraNode.position = Vector3(0.0, 5.0, 0.0)
  71. end
  72. function CreateInstructions()
  73. -- Construct new Text object, set string to display and font to use
  74. local instructionText = ui.root:CreateChild("Text")
  75. instructionText:SetText("Use WASD keys and mouse to move")
  76. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  77. -- Position the text relative to the screen center
  78. instructionText.horizontalAlignment = HA_CENTER
  79. instructionText.verticalAlignment = VA_CENTER
  80. instructionText:SetPosition(0, ui.root.height / 4)
  81. end
  82. function SetupViewport()
  83. -- 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
  84. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  85. -- use, but now we just use full screen and default render path configured in the engine command line options
  86. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  87. renderer:SetViewport(0, viewport)
  88. end
  89. function MoveCamera(timeStep)
  90. -- Do not move if the UI has a focused element (the console)
  91. if ui.focusElement ~= nil then
  92. return
  93. end
  94. -- Movement speed as world units per second
  95. local MOVE_SPEED = 20.0
  96. -- Mouse sensitivity as degrees per pixel
  97. local MOUSE_SENSITIVITY = 0.1
  98. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  99. local mouseMove = input.mouseMove
  100. yaw = yaw +MOUSE_SENSITIVITY * mouseMove.x
  101. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  102. pitch = Clamp(pitch, -90.0, 90.0)
  103. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  104. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  105. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  106. -- Use the TranslateRelative() function to move relative to the node's orientation. Alternatively we could
  107. -- multiply the desired direction with the node's orientation quaternion, and use just Translate()
  108. if input:GetKeyDown(KEY_W) then
  109. cameraNode:TranslateRelative(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  110. end
  111. if input:GetKeyDown(KEY_S) then
  112. cameraNode:TranslateRelative(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  113. end
  114. if input:GetKeyDown(KEY_A) then
  115. cameraNode:TranslateRelative(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  116. end
  117. if input:GetKeyDown(KEY_D) then
  118. cameraNode:TranslateRelative(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  119. end
  120. end
  121. function SubscribeToEvents()
  122. -- Subscribe HandleUpdate() function for processing update events
  123. SubscribeToEvent("Update", "HandleUpdate")
  124. end
  125. function HandleUpdate(eventType, eventData)
  126. -- Take the frame time step, which is stored as a float
  127. local timeStep = eventData:GetFloat("TimeStep")
  128. -- Move the camera, scale movement with time step
  129. MoveCamera(timeStep)
  130. end