10_RenderToTexture.lua 9.6 KB

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