mop Posted December 1, 2014 Share Posted December 1, 2014 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 codevar 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 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 1, 2014 Share Posted December 1, 2014 Excellent idea. Please create a playground repro and I'll fix it for you Quote Link to comment Share on other sites More sharing options...
mop Posted December 1, 2014 Author Share Posted December 1, 2014 I am delighted that my question is not completely stupid http://www.babylonjs-playground.com/#1XAW5G#2 hope i didn't mess up the scene ordering. the return value seems to be irrelevant and the scene creation order seems to be the relevant stuff :S Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 1, 2014 Share Posted December 1, 2014 Ok this is because scene.render is not called. So cache is not updated. You can force cache update like this:http://www.babylonjs-playground.com/#1XAW5G#4 Quote Link to comment Share on other sites More sharing options...
mop Posted December 2, 2014 Author Share Posted December 2, 2014 i knew i must have been missing something although maybe that should be triggered by babylon.js internally. What do you think? Thank you for the quick fix Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted December 2, 2014 Share Posted December 2, 2014 Actually this is not easy to check Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.