09_MultipleViewports.lua 10.0 KB

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