How to render clipped surfaces as solid objects

You want to render a clipped surface as if it were a solid — i.e., not hollow.

You can achieve that effect with MeshPhongMaterial — or any three.js material for that matter — with a simple hack to the material shader.

material.onBeforeCompile = function( shader ) {

    shader.fragmentShader = shader.fragmentShader.replace(

        '#include <output_fragment>',

        `
        vec3 backfaceColor = vec3( 0.4, 0.4, 0.4 );
        gl_FragColor = ( gl_FrontFacing ) ? vec4( outgoingLight, diffuseColor.a ) : vec4( backfaceColor, opacity );
        `
    )
};

This should look pretty good. It will require material.side = THREE.DoubleSide;

clipped torus

Alternatively, see https://threejs.org/examples/webgl_clipping_stencil.html.

three.js r.148

Leave a Comment