RenderToTexture.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "atomic component";
  2. //RenderToTexture component
  3. exports.component = function(self) {
  4. //Load the current scene, but don't set it as main scene to the player
  5. var scene = Atomic.player.loadScene("Scenes/RenderToTextureScene.scene");
  6. self.start = function() {
  7. //link to the current node
  8. var node = self.node;
  9. var model = node.getComponent("StaticModel");
  10. // Create a renderable texture (1024x1024, RGB format), enable bilinear filtering on it
  11. var renderTexture = new Atomic.Texture2D();
  12. renderTexture.setSize(1024, 1024, Atomic.graphics.getRGBFormat(), Atomic.TextureUsage.TEXTURE_RENDERTARGET);
  13. renderTexture.filterMode = Atomic.TextureFilterMode.FILTER_BILINEAR;
  14. // Create a new material from scratch, use the diffuse unlit technique, assign the render texture
  15. // as its diffuse texture, then assign the material box object
  16. var renderMaterial = new Atomic.Material();
  17. renderMaterial.setTechnique(0, Atomic.cache.getResource("Technique", "Techniques/Diff.xml"));
  18. renderMaterial.setTexture(Atomic.TextureUnit.TU_DIFFUSE, renderTexture);
  19. model.setMaterial(renderMaterial);
  20. // Get the texture's RenderSurface object (exists when the texture has been created in rendertarget mode)
  21. // and define the viewport for rendering the second scene, similarly as how backbuffer viewports are defined
  22. // to the Renderer subsystem. By default the texture viewport will be updated when the texture is visible
  23. // in the main view
  24. var camera = scene.getComponents("Camera", true)[0];
  25. //get surface of the render texture
  26. var surface = renderTexture.getRenderSurface();
  27. //create a new viewport
  28. var rttViewport = new Atomic.Viewport(scene, camera);
  29. //set rttViewport as a main viewport in the surface
  30. surface.setViewport(0, rttViewport);
  31. };
  32. };