Three.js use framebuffer as texture

前端 未结 1 1033
梦如初夏
梦如初夏 2021-01-31 12:14

I\'m using an image in a canvas element as a texture in Three.js, performing image manipulations on the canvas using JavaScript, and then calling needsUpdate() on the texture. T

相关标签:
1条回答
  • Render to texture and Render to another scene as listed above are the same thing, and are the technique you want. To explain:

    In vanilla WebGL the way you do this kind of thing is by creating a framebuffer object (FBO) from scratch, binding a texture to it, and rendering it with the shader of your choice. Concepts like "scene" and "camera" aren't involved, and it's kind of a complicated process. Here's an example:

    http://learningwebgl.com/blog/?p=1786

    But this also happens to be essentially what Three.js does when you use it to render a scene with a camera: the renderer outputs to a framebuffer, which in its basic usage goes straight to the screen. So if you instruct it to render to a new WebGLRenderTarget instead, you can use whatever the camera sees as the input texture of a second material. All the complicated stuff is still happening, but behind the scenes, which is the beauty of Three.js. :)

    So: To replicate a WebGL setup of an FBO containing a single rendered texture, as mentioned in the comments, just make a new scene containing an orthographic camera and a single plane with a material using the desired texture, then render to a new WebGLRenderTarget using your custom shader:

    // new render-to-texture scene
    myScene = new THREE.Scene();
    
    // you may need to modify these parameters
    var renderTargetParams = {
      minFilter:THREE.LinearFilter,
      stencilBuffer:false,
      depthBuffer:false
    };
    
    myImage = THREE.ImageUtils.loadTexture( 'path/to/texture.png',
      new THREE.UVMapping(), function() { myCallbackFunction(); } );
    
    imageWidth = myImage.image.width;
    imageHeight = myImage.image.height;
    
    // create buffer
    myTexture = new THREE.WebGLRenderTarget( width, height, renderTargetParams );
    
    // custom RTT materials
    myUniforms = {
      colorMap: { type: "t", value: myImage },
    };
    myTextureMat = new THREE.ShaderMaterial({
      uniforms: myUniforms,
      vertexShader: document.getElementById( 'my_custom_vs' ).textContent,
      fragmentShader: document.getElementById( 'my_custom_fs' ).textContent
    });
    
    // Setup render-to-texture scene
    myCamera = new THREE.OrthographicCamera( imageWidth / - 2,
      imageWidth / 2,
      imageHeight / 2,
      imageHeight / - 2, -10000, 10000 );
    
    var myTextureGeo = new THREE.PlaneGeometry( imageWidth, imageHeight );
    myTextureMesh = new THREE.Mesh( myTextureGeo, myTextureMat );
    myTextureMesh.position.z = -100;
    myScene.add( myTextureMesh );
    
    renderer.render( myScene, myCamera, myTexture, true );
    

    Once you've rendered the new scene, myTexture will be available for use as a texture in another material in your main scene. Note that you may want to trigger the first render with the callback function in the loadTexture() call, so that it won't try to render until the source image has loaded.

    0 讨论(0)
提交回复
热议问题