10_RenderToTexture.as 10 KB

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