09_MultipleViewports.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. -- Multiple viewports example.
  2. -- This sample demonstrates:
  3. -- - Setting up two viewports with two separate cameras
  4. -- - Adding post processing effects to a viewport's render path and toggling them
  5. require "LuaScripts/Utilities/Sample"
  6. local scene_ = nil
  7. local cameraNode = nil
  8. local rearCameraNode = nil
  9. local yaw = 0.0
  10. local pitch = 0.0
  11. local drawDebug = false
  12. function Start()
  13. -- Execute the common startup for samples
  14. SampleStart()
  15. -- Create the scene content
  16. CreateScene()
  17. -- Create the UI content
  18. CreateInstructions()
  19. -- Setup the viewports for displaying the scene
  20. SetupViewports()
  21. -- Hook up to the frame update and render post-update events
  22. SubscribeToEvents()
  23. end
  24. function CreateScene()
  25. scene_ = Scene()
  26. -- Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  27. -- Also create a DebugRenderer component so that we can draw debug geometry
  28. scene_:CreateComponent("Octree")
  29. scene_:CreateComponent("DebugRenderer")
  30. -- Create scene node & StaticModel component for showing a static plane
  31. local planeNode = scene_:CreateChild("Plane")
  32. planeNode.scale = Vector3(100.0, 1.0, 100.0)
  33. local planeObject = planeNode:CreateComponent("StaticModel")
  34. planeObject.model = cache:GetResource("Model", "Models/Plane.mdl")
  35. planeObject.material = cache:GetResource("Material", "Materials/StoneTiled.xml")
  36. -- Create a Zone component for ambient lighting & fog control
  37. local zoneNode = scene_:CreateChild("Zone")
  38. local zone = zoneNode:CreateComponent("Zone")
  39. zone.boundingBox = BoundingBox(-1000.0, 1000.0)
  40. zone.ambientColor = Color(0.15, 0.15, 0.15)
  41. zone.fogColor = Color(0.5, 0.5, 0.7)
  42. zone.fogStart = 100.0
  43. zone.fogEnd = 300.0
  44. -- Create a directional light to the world. Enable cascaded shadows on it
  45. local lightNode = scene_:CreateChild("DirectionalLight")
  46. lightNode.direction = Vector3(0.6, -1.0, 0.8)
  47. local light = lightNode:CreateComponent("Light")
  48. light.lightType = LIGHT_DIRECTIONAL
  49. light.castShadows = true
  50. light.shadowBias = BiasParameters(0.00025, 0.5)
  51. -- Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
  52. light.shadowCascade = CascadeParameters(10.0, 50.0, 200.0, 0.0, 0.8)
  53. -- Create some mushrooms
  54. local NUM_MUSHROOMS = 240
  55. for i = 1, NUM_MUSHROOMS do
  56. local mushroomNode = scene_:CreateChild("Mushroom")
  57. mushroomNode.position = Vector3(Random(90.0) - 45.0, 0.0, Random(90.0) - 45.0)
  58. mushroomNode.rotation = Quaternion(0.0, Random(360.0), 0.0)
  59. mushroomNode:SetScale(0.5 + Random(2.0))
  60. local mushroomObject = mushroomNode:CreateComponent("StaticModel")
  61. mushroomObject.model = cache:GetResource("Model", "Models/Mushroom.mdl")
  62. mushroomObject.material = cache:GetResource("Material", "Materials/Mushroom.xml")
  63. mushroomObject.castShadows = true
  64. end
  65. -- Create randomly sized boxes. If boxes are big enough, make them occluders. Occluders will be software rasterized before
  66. -- rendering to a low-resolution depth-only buffer to test the objects in the view frustum for visibility
  67. local NUM_BOXES = 20
  68. for i = 1, NUM_BOXES do
  69. local boxNode = scene_:CreateChild("Box")
  70. local size = 1.0 + Random(10.0)
  71. boxNode.position = Vector3(Random(80.0) - 40.0, size * 0.5, Random(80.0) - 40.0)
  72. boxNode:SetScale(size)
  73. local boxObject = boxNode:CreateComponent("StaticModel")
  74. boxObject.model = cache:GetResource("Model", "Models/Box.mdl")
  75. boxObject.material = cache:GetResource("Material", "Materials/Stone.xml")
  76. boxObject.castShadows = true
  77. if size >= 3.0 then
  78. boxObject.occluder = true
  79. end
  80. end
  81. -- Create the camera. Limit far clip distance to match the fog
  82. cameraNode = scene_:CreateChild("Camera")
  83. local camera = cameraNode:CreateComponent("Camera")
  84. camera.farClip = 300.0
  85. -- Parent the rear camera node to the front camera node and turn it 180 degrees to face backward
  86. -- Here, we use the angle-axis constructor for Quaternion instead of the usual Euler angles
  87. rearCameraNode = cameraNode:CreateChild("RearCamera")
  88. rearCameraNode:Rotate(Quaternion(180.0, Vector3(0.0, 1.0, 0.0)))
  89. local rearCamera = rearCameraNode:CreateComponent("Camera")
  90. rearCamera.farClip = 300.0
  91. -- Because the rear viewport is rather small, disable occlusion culling from it. Use the camera's
  92. -- "view override flags" for this. We could also disable eg. shadows or force low material quality
  93. -- if we wanted
  94. rearCamera.viewOverrideFlags = VO_DISABLE_OCCLUSION
  95. -- Set an initial position for the front camera scene node above the plane
  96. cameraNode.position = Vector3(0.0, 5.0, 0.0)
  97. end
  98. function CreateInstructions()
  99. -- Construct new Text object, set string to display and font to use
  100. local instructionText = ui.root:CreateChild("Text")
  101. instructionText.text =
  102. "Use WASD keys and mouse to move\n"..
  103. "B to toggle bloom, F to toggle FXAA\n"..
  104. "Space to toggle debug geometry\n"
  105. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  106. -- The text has multiple rows. Center them in relation to each other
  107. instructionText.textAlignment = HA_CENTER
  108. -- Position the text relative to the screen center
  109. instructionText.horizontalAlignment = HA_CENTER
  110. instructionText.verticalAlignment = VA_CENTER
  111. instructionText:SetPosition(0, ui.root.height / 4)
  112. end
  113. function SetupViewports()
  114. renderer.numViewports = 2
  115. -- Set up the front camera viewport
  116. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  117. renderer:SetViewport(0, viewport)
  118. -- Clone the default render path so that we do not interfere with the other viewport, then add
  119. -- bloom and FXAA post process effects to the front viewport. Render path commands can be tagged
  120. -- for example with the effect name to allow easy toggling on and off. We start with the effects
  121. -- disabled.
  122. local effectRenderPath = viewport:GetRenderPath():Clone()
  123. effectRenderPath:Append(cache:GetResource("XMLFile", "PostProcess/Bloom.xml"))
  124. effectRenderPath:Append(cache:GetResource("XMLFile", "PostProcess/EdgeFilter.xml"))
  125. -- Make the bloom mixing parameter more pronounced
  126. effectRenderPath:SetShaderParameter("BloomMix", Variant(Vector2(0.9, 0.6)))
  127. effectRenderPath:SetEnabled("Bloom", false)
  128. effectRenderPath:SetEnabled("EdgeFilter", false)
  129. viewport:SetRenderPath(effectRenderPath)
  130. -- Set up the rear camera viewport on top of the front view ("rear view mirror")
  131. -- The viewport index must be greater in that case, otherwise the view would be left behind
  132. local rearViewport = Viewport:new(scene_, rearCameraNode:GetComponent("Camera"),
  133. IntRect(graphics.width * 2 / 3, 32, graphics.width - 32, graphics.height / 3))
  134. renderer:SetViewport(1, rearViewport)
  135. end
  136. function SubscribeToEvents()
  137. -- Subscribe HandleUpdate() function for processing update events
  138. SubscribeToEvent("Update", "HandleUpdate")
  139. -- Subscribe HandlePostRenderUpdate() function for processing the post-render update event, during which we request
  140. -- debug geometry
  141. SubscribeToEvent("PostRenderUpdate", "HandlePostRenderUpdate")
  142. end
  143. function MoveCamera(timeStep)
  144. -- Do not move if the UI has a focused element (the console)
  145. if ui.focusElement ~= nil then
  146. return
  147. end
  148. -- Movement speed as world units per second
  149. local MOVE_SPEED = 20.0
  150. -- Mouse sensitivity as degrees per pixel
  151. local MOUSE_SENSITIVITY = 0.1
  152. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  153. local mouseMove = input.mouseMove
  154. yaw = yaw + MOUSE_SENSITIVITY * mouseMove.x
  155. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  156. pitch = Clamp(pitch, -90.0, 90.0)
  157. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  158. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  159. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  160. if input:GetKeyDown(KEY_W) then
  161. cameraNode:TranslateRelative(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  162. end
  163. if input:GetKeyDown(KEY_S) then
  164. cameraNode:TranslateRelative(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  165. end
  166. if input:GetKeyDown(KEY_A) then
  167. cameraNode:TranslateRelative(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  168. end
  169. if input:GetKeyDown(KEY_D) then
  170. cameraNode:TranslateRelative(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  171. end
  172. -- Toggle post processing effects on the front viewport. Note that the rear viewport is unaffected
  173. local effectRenderPath = renderer:GetViewport(0).renderPath
  174. if input:GetKeyPress(KEY_B) then
  175. effectRenderPath:ToggleEnabled("Bloom")
  176. end
  177. if input:GetKeyPress(KEY_F) then
  178. effectRenderPath:ToggleEnabled("EdgeFilter")
  179. end
  180. -- Toggle debug geometry with space
  181. if input:GetKeyPress(KEY_SPACE) then
  182. drawDebug = not drawDebug
  183. end
  184. end
  185. function HandleUpdate(eventType, eventData)
  186. -- Take the frame time step, which is stored as a float
  187. local timeStep = eventData:GetFloat("TimeStep")
  188. -- Move the camera, scale movement with time step
  189. MoveCamera(timeStep)
  190. end
  191. function HandlePostRenderUpdate(eventType, eventData)
  192. -- If draw debug mode is enabled, draw viewport debug geometry. Disable depth test so that we can see the effect of occlusion
  193. if drawDebug then
  194. renderer:DrawDebugGeometry(false)
  195. end
  196. end