10_RenderToTexture.as 11 KB

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