10_RenderToTexture.as 10 KB

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