Transparent vertex colors in bufferattribute?

I am trying to render a geometry using buffergeometry. The geometry itself is rendered using BufferAttribute as such:

var geometry = new THREE.BufferGeometry();
geometry.addAttribute(‘position’, new THREE.BufferAttribute(positions, 3));
geometry.addAttribute(‘normal’, new THREE.BufferAttribute(normals, 3, true));
geometry.addAttribute(‘color’, new THREE.BufferAttribute(colors, 3, true));

Now assuming the geometry is a cube with 6 faces labeled A to F. How can I cause the vertex colors of only a particular face, say face A to be rendered transparent and other faces to be set to opaque or visible at any time? I searched and found other similar questions but non specify doing this for buffer attribute. Please also note that the final geometry is a mesh created using MeshPhongMaterial.

var material = new THREE.MeshPhongMaterial({
vertexColors: THREE.VertexColors,
});
var mesh = new THREE.Mesh(geometry, material);

There is no way to achieve this effect with build in materials since vertex colors are always RGB values (without alpha). More information in the following issue at GitHub:

You can still achieve your intended result by an additional buffer attribute that controls the opacity per face. It’s then necessary to enhance the shader code of MeshPhongMaterial e.g. via Material.onBeforeCompile() so the new attribute is actually used.

1 Like

Is there any example code on how to do this ? I am not very familiar with this:

It’s then necessary to enhance the shader code of MeshPhongMaterial e.g. via Material.onBeforeCompile() so the new attribute is actually used.

Yes, it’s webgl_materials_modified.

1 Like

Sorry I am not so familiar with webgl and this is properly not the right place but how do I change the color property ? I tried doing this but it did not render my geometry in the red color . It was always white no matter what numbers i gave in the vec4:

var phongShader = THREE.ShaderLib['phong'];

var material = new THREE.MeshPhongMaterial();
var fs = phongShader.fragmentShader;
        fs.replace(
            'gl_FragColor = vec4( outgoingLight, diffuseColor.a );',
            
                ['gl_FragColor = vec4( 0,0,1,0.5 );'
            
        ].join('\n'));
 material.onBeforeCompile = function (shader) {
            shader.uniforms.reflectivity = { value: 0.5 };
            shader.fragmentShader = fs;
            shader.vertexShader = phongShader.vertexShader;

        };

It was implemented as vertexAlphas in r127 :tada:

1 Like