10_RenderToTexture.lua 9.6 KB

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