10_RenderToTexture.lua 9.7 KB

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