RenderBox.js 2.8 KB

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