09_MultipleViewports.lua 13 KB

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