10_RenderToTexture.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. -- Render to texture example
  2. -- This sample demonstrates:
  3. -- - Creating two 3D scenes and rendering the other into a texture
  4. -- - Creating rendertarget textures and materials programmatically
  5. require "LuaScripts/Utilities/Sample"
  6. local rttScene_ = nil
  7. local rttCameraNode = nil
  8. function Start()
  9. -- Execute the common startup for samples
  10. SampleStart()
  11. -- Create the scene content
  12. CreateScene()
  13. -- Create the UI content
  14. CreateInstructions()
  15. -- Setup the viewport for displaying the scene
  16. SetupViewport()
  17. -- Set the mouse mode to use in the sample
  18. SampleInitMouseMode(MM_RELATIVE)
  19. -- Hook up to the frame update events
  20. SubscribeToEvents()
  21. end
  22. function CreateScene()
  23. -- Create the scene which will be rendered to a texture
  24. rttScene_ = Scene()
  25. -- Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  26. rttScene_:CreateComponent("Octree")
  27. -- Create a Zone for ambient light & fog control
  28. local zoneNode = rttScene_:CreateChild("Zone")
  29. local zone = zoneNode:CreateComponent("Zone")
  30. -- Set same volume as the Octree, set a close bluish fog and some ambient light
  31. zone.boundingBox = BoundingBox(-1000.0, 1000.0)
  32. zone.ambientColor = Color(0.05, 0.1, 0.15)
  33. zone.fogColor = Color(0.1, 0.2, 0.3)
  34. zone.fogStart = 10.0
  35. zone.fogEnd = 100.0
  36. -- Create randomly positioned and oriented box StaticModels in the scene
  37. local NUM_OBJECTS = 2000
  38. for i = 1, NUM_OBJECTS do
  39. local boxNode = rttScene_:CreateChild("Box")
  40. boxNode.position = Vector3(Random(200.0) - 100.0, Random(200.0) - 100.0, Random(200.0) - 100.0)
  41. -- Orient using random pitch, yaw and roll Euler angles
  42. boxNode.rotation = Quaternion(Random(360.0), Random(360.0), Random(360.0))
  43. local boxObject = boxNode:CreateComponent("StaticModel")
  44. boxObject.model = cache:GetResource("Model", "Models/Box.mdl")
  45. boxObject.material = cache:GetResource("Material", "Materials/Stone.xml")
  46. -- Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event.
  47. -- Simply set same rotation speed for all objects
  48. local rotator = boxNode:CreateScriptObject("Rotator")
  49. rotator.rotationSpeed = { 10.0, 20.0, 30.0 }
  50. end
  51. -- Create a camera for the render-to-texture scene. Simply leave it at the world origin and let it observe the scene
  52. rttCameraNode = rttScene_:CreateChild("Camera")
  53. local camera = rttCameraNode:CreateComponent("Camera")
  54. camera.farClip = 100.0
  55. -- Create a point light to the camera scene node
  56. local light = rttCameraNode:CreateComponent("Light")
  57. light.lightType = LIGHT_POINT
  58. light.range = 30.0
  59. -- Create the scene in which we move around
  60. scene_ = Scene()
  61. -- Create octree, use also default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  62. scene_:CreateComponent("Octree")
  63. -- Create a Zone component for ambient lighting & fog control
  64. local zoneNode = scene_:CreateChild("Zone")
  65. local zone = zoneNode:CreateComponent("Zone")
  66. zone.boundingBox = BoundingBox(-1000.0, 1000.0)
  67. zone.ambientColor = Color(0.1, 0.1, 0.1)
  68. zone.fogStart = 100.0
  69. zone.fogEnd = 300.0
  70. -- Create a directional light without shadows
  71. local lightNode = scene_:CreateChild("DirectionalLight")
  72. lightNode.direction = Vector3(0.5, -1.0, 0.5)
  73. local light = lightNode:CreateComponent("Light")
  74. light.lightType = LIGHT_DIRECTIONAL
  75. light.color = Color(0.2, 0.2, 0.2)
  76. light.specularIntensity = 1.0
  77. -- Create a "floor" consisting of several tiles
  78. for y = -5, 5 do
  79. for x = -5, 5 do
  80. local floorNode = scene_:CreateChild("FloorTile")
  81. floorNode.position = Vector3(x * 20.5, -0.5, y * 20.5)
  82. floorNode.scale = Vector3(20.0, 1.0, 20.)
  83. local floorObject = floorNode:CreateComponent("StaticModel")
  84. floorObject.model = cache:GetResource("Model", "Models/Box.mdl")
  85. floorObject.material = cache:GetResource("Material", "Materials/Stone.xml")
  86. end
  87. end
  88. -- Create a "screen" like object for viewing the second scene. Construct it from two StaticModels, a box for the frame
  89. -- and a plane for the actual view
  90. local boxNode = scene_:CreateChild("ScreenBox")
  91. boxNode.position = Vector3(0.0, 10.0, 0.0)
  92. boxNode.scale = Vector3(21.0, 16.0, 0.5)
  93. local boxObject = boxNode:CreateComponent("StaticModel")
  94. boxObject.model = cache:GetResource("Model", "Models/Box.mdl")
  95. boxObject.material = cache:GetResource("Material", "Materials/Stone.xml")
  96. local screenNode = scene_:CreateChild("Screen")
  97. screenNode.position = Vector3(0.0, 10.0, -0.27)
  98. screenNode.rotation = Quaternion(-90.0, 0.0, 0.0)
  99. screenNode.scale = Vector3(20.0, 0.0, 15.0)
  100. local screenObject = screenNode:CreateComponent("StaticModel")
  101. screenObject.model = cache:GetResource("Model", "Models/Plane.mdl")
  102. -- Create a renderable texture (1024x768, RGB format), enable bilinear filtering on it
  103. local renderTexture = Texture2D:new()
  104. renderTexture:SetSize(1024, 768, Graphics:GetRGBFormat(), TEXTURE_RENDERTARGET)
  105. renderTexture.filterMode = FILTER_BILINEAR
  106. -- Create a new material from scratch, use the diffuse unlit technique, assign the render texture
  107. -- as its diffuse texture, then assign the material to the screen plane object
  108. local renderMaterial = Material:new()
  109. renderMaterial:SetTechnique(0, cache:GetResource("Technique", "Techniques/DiffUnlit.xml"))
  110. renderMaterial:SetTexture(TU_DIFFUSE, renderTexture)
  111. -- Since the screen material is on top of the box model and may Z-fight, use negative depth bias
  112. -- to push it forward (particularly necessary on mobiles with possibly less Z resolution)
  113. renderMaterial.depthBias = BiasParameters(-0.001, 0.0)
  114. screenObject.material = renderMaterial
  115. -- Get the texture's RenderSurface object (exists when the texture has been created in rendertarget mode)
  116. -- and define the viewport for rendering the second scene, similarly as how backbuffer viewports are defined
  117. -- to the Renderer subsystem. By default the texture viewport will be updated when the texture is visible
  118. -- in the main view
  119. local surface = renderTexture.renderSurface
  120. local rttViewport = Viewport:new(rttScene_, rttCameraNode:GetComponent("Camera"))
  121. surface:SetViewport(0, rttViewport)
  122. -- Create the camera which we will move around. Limit far clip distance to match the fog
  123. cameraNode = scene_:CreateChild("Camera")
  124. local camera = cameraNode:CreateComponent("Camera")
  125. camera.farClip = 300.0
  126. -- Set an initial position for the camera scene node above the plane
  127. cameraNode.position = Vector3(0.0, 7.0, -30.0)
  128. end
  129. function CreateInstructions()
  130. -- Construct new Text object, set string to display and font to use
  131. local instructionText = ui.root:CreateChild("Text")
  132. instructionText.text = "Use WASD keys and mouse to move"
  133. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  134. -- Position the text relative to the screen center
  135. instructionText.horizontalAlignment = HA_CENTER
  136. instructionText.verticalAlignment = VA_CENTER
  137. instructionText:SetPosition(0, ui.root.height / 4)
  138. end
  139. function SetupViewport()
  140. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  141. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  142. renderer:SetViewport(0, viewport)
  143. end
  144. function MoveCamera(timeStep)
  145. -- Do not move if the UI has a focused element (the console)
  146. if ui.focusElement ~= nil then
  147. return
  148. end
  149. -- Movement speed as world units per second
  150. local MOVE_SPEED = 20.0
  151. -- Mouse sensitivity as degrees per pixel
  152. local MOUSE_SENSITIVITY = 0.1
  153. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  154. local mouseMove = input.mouseMove
  155. yaw = yaw + MOUSE_SENSITIVITY * mouseMove.x
  156. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  157. pitch = Clamp(pitch, -90.0, 90.0)
  158. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  159. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  160. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  161. local delta = MOVE_SPEED * timeStep
  162. if input:GetKeyDown(KEY_W) then
  163. cameraNode:Translate(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  164. end
  165. if input:GetKeyDown(KEY_S) then
  166. cameraNode:Translate(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  167. end
  168. if input:GetKeyDown(KEY_A) then
  169. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  170. end
  171. if input:GetKeyDown(KEY_D) then
  172. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  173. end
  174. end
  175. function SubscribeToEvents()
  176. -- Subscribe HandleUpdate() function for processing update events
  177. SubscribeToEvent("Update", "HandleUpdate")
  178. end
  179. function HandleUpdate(eventType, eventData)
  180. -- Take the frame time step, which is stored as a float
  181. local timeStep = eventData["TimeStep"]:GetFloat()
  182. -- Move the camera, scale movement with time step
  183. MoveCamera(timeStep)
  184. end
  185. -- Rotator script object class. Script objects to be added to a scene node must implement the empty ScriptObject interface
  186. Rotator = ScriptObject()
  187. function Rotator:Start()
  188. self.rotationSpeed = {0.0, 0.0, 0.0}
  189. end
  190. -- Update is called during the variable timestep scene update
  191. function Rotator:Update(timeStep)
  192. local x = self.rotationSpeed[1] * timeStep
  193. local y = self.rotationSpeed[2] * timeStep
  194. local z = self.rotationSpeed[3] * timeStep
  195. self.node:Rotate(Quaternion(x, y, z))
  196. end