RenderBox.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Render to texture example component
  2. var game = Atomic.game;
  3. var cache = game.cache;
  4. var node = self.node;
  5. // we're be setting up our render texture with a chest scene
  6. var chestScene;
  7. var chestNode;
  8. var chestCamera;
  9. // Create a scene which get's rendered to texture
  10. function createChestScene() {
  11. chestScene = new Atomic.Scene();
  12. chestScene.createComponent("Octree");
  13. // create a zone
  14. var zoneNode = chestScene.createChild("Zone")
  15. var zone = zoneNode.createComponent("Zone");
  16. zone.ambientColor = [0.0, 0.0, 0.0];
  17. zone.fogColor = [.2, .2, .2, 1.0];
  18. zone.fogStart = 10;
  19. zone.fogEnd = 100;
  20. // chest node where we will attach the model
  21. chestNode = chestScene.createChild("Chest");
  22. chestNode.pitch(-90);
  23. var model = chestNode.createComponent("StaticModel");
  24. model.setModel(cache.getResource("Model", "Models/Chest.mdl"));
  25. model.setMaterial(cache.getResource("Material", "Materials/Chest.xml"));
  26. // Create a camera for the render-to-texture scene. Simply leave it at the world origin and let it observe the scene
  27. var cameraNode = chestScene.createChild("Camera");
  28. chestCamera = cameraNode.createComponent("Camera");
  29. cameraNode.position = [0, .5, -4];
  30. chestCamera.farClip = 100;
  31. // Create a point light to the camera scene node
  32. var light = cameraNode.createComponent("Light");
  33. light.lightType = Atomic.LIGHT_POINT;
  34. light.range = 30;
  35. }
  36. function start() {
  37. // create the chest render to texture scene
  38. createChestScene();
  39. // add a box which will have the chest scene rendered on it
  40. var model = node.createComponent("StaticModel");
  41. model.setModel(cache.getResource("Model", "Models/Box.mdl"));
  42. // Create a renderable texture (1024x1024, RGB format), enable bilinear filtering on it
  43. var renderTexture = new Atomic.Texture2D();
  44. renderTexture.setSize(1024, 1024, game.graphics.getRGBFormat(), Atomic.TEXTURE_RENDERTARGET);
  45. renderTexture.filterMode = Atomic.FILTER_BILINEAR;
  46. // Create a new material from scratch, use the diffuse unlit technique, assign the render texture
  47. // as its diffuse texture, then assign the material box object
  48. var renderMaterial = new Atomic.Material();
  49. renderMaterial.setTechnique(0, cache.getResource("Technique", "Techniques/Diff.xml"));
  50. renderMaterial.setTexture(Atomic.TU_DIFFUSE, renderTexture);
  51. model.setMaterial(renderMaterial);
  52. // Get the texture's RenderSurface object (exists when the texture has been created in rendertarget mode)
  53. // and define the viewport for rendering the second scene, similarly as how backbuffer viewports are defined
  54. // to the Renderer subsystem. By default the texture viewport will be updated when the texture is visible
  55. // in the main view
  56. var surface = renderTexture.getRenderSurface();
  57. var rttViewport = new Atomic.Viewport(chestScene, chestCamera);
  58. surface.setViewport(0, rttViewport);
  59. node.pitch(-90);
  60. }
  61. function update(timeStep) {
  62. // Let's roll
  63. node.roll(timeStep * 15);
  64. chestNode.roll(timeStep * 75);
  65. }