10_RenderToTexture.lua 9.5 KB

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