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