09_MultipleViewports.lua 9.9 KB

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