Jump to content

RenderTargetTexture a separate scene


mop
 Share

Recommended Posts

Hi,

 

i am experimenting with rendertargettextures and i am trying to render a completely separate scene to a texture.

 

This works but it will remain static :S This might be an utterly stupid question and i am probably missing some very basic flag like "scene.makeItDynamicEvenThoughItsNotTheActiveScene = true".

 

Here is my code

var canvas = document.getElementById("canvas");var engine = new BABYLON.Engine(canvas, true);// Now create a basic Babylon Scene object var scene = new BABYLON.Scene(engine);// This creates and positions a free cameravar camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);// This targets the camera to scene origincamera.setTarget(BABYLON.Vector3.Zero());// This attaches the camera to the canvascamera.attachControl(canvas, false);// This creates a light, aiming 0,1,0 - to the sky.var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);// Dim the light a small amountlight.intensity = .5;// Let's try our built-in 'sphere' shape. Params: name, subdivisions, size, scenevar sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);// Move the sphere upward 1/2 its heightsphere.position.y = 1;// Let's try our built-in 'ground' shape.  Params: name, width, depth, subdivisions, scenevar ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);var renderTarget = new BABYLON.RenderTargetTexture("scene", 1024, scene, false, false);renderTarget.renderList = scene.meshes;var finalScene = new BABYLON.Scene(engine);finalScene.clearColor = new BABYLON.Color3(0.4, 0, 0);// This creates and positions a free camera (non-mesh)var finalCamera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 0, 0), finalScene);finalCamera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;var width = engine.getRenderWidth();var height = engine.getRenderHeight();finalCamera.orthoTop = height / 2;finalCamera.orthoBottom = height / -2;finalCamera.orthoLeft = width / -2;finalCamera.orthoRight = width / 2;BABYLON.Effect.ShadersStore["compositeVertexShader"] = "precision highp float;\n" +"attribute vec3 position;\n" +"attribute vec2 uv;\n" +"uniform mat4 worldViewProjection;\n" +"varying vec2 vUv;\n" +"void main() {\n" +"   vUv = uv;\n"+"   gl_Position = worldViewProjection * vec4( position, 1.0 );\n" +"}\n";BABYLON.Effect.ShadersStore["compositePixelShader"] ="precision highp float;\n" +"varying vec2 vUv;\n" +"uniform sampler2D tDiffuse1;\n" +"void main(void) {\n" +"  gl_FragColor = texture2D( tDiffuse1, vUv );\n" +"}\n";var compositeMaterial = new BABYLON.ShaderMaterial("composite", finalScene, "composite", {    attributes: ["position", "uv"],    uniforms: ["worldViewProjection"]});compositeMaterial.setTexture("tDiffuse1", renderTarget);// Let's try our built-in 'sphere' shape. Params: name, subdivisions, size, scenevar plane = BABYLON.Mesh.CreateGround("plane", width, height, 0, finalScene);plane.rotation.x = -Math.PI/2;plane.material = compositeMaterial;// This targets the camera to scene origincamera.setTarget(new BABYLON.Vector3.Zero());// Register a render loop to repeatedly render the scenevar hmm = 0;engine.runRenderLoop(function () {    sphere.position.y = Math.sin(hmm);    hmm += 0.1;    //scene.render();    renderTarget.render();    finalScene.render();});

If i uncomment scene.render() everything works as expected (sphere bouncing), but when i render to a texture the animation is gone :S

 

Any idea? :S

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...