How to update this old code? Not supported by the current release of Threejs..please help

Help to rewrite part2

let geometry = new THREE.PlaneGeometry(0.4, 0.2)

#part2

geometry.faces.forEach(function(face){
face.vertexNormals.forEach(function(normal){
normal.set(0.0,1.0,0.0).normalize()
})
})

thanks in advance!

Hello! Try this

let geometry = new THREE.PlaneGeometry(0.4, 0.2);
var item=geometry.attributes.normal.array;
var max=geometry.attributes.normal.count*3;
for(var n=0;n<max;n+=3){
item[n]=0;
item[n+1]=1;
item[n+2]=0;
}
geometry.normalizeNormals();
geometry.attributes.normal.needsUpdate=true;
1 Like

Do it like so:

const geometry = new THREE.PlaneGeometry(0.4, 0.2);
const normalAttribute = geometry.getAttribute( 'normal' );

for ( let i = 0; i < normalAttribute.count; i ++ ) {

    normalAttribute.setXYZ( i, 0, 1, 0 );

}

@Chaser_Code It’s not recommended to directly work with the array property of buffer attributes. Your approach breaks with interleaved data.

It’s also not necessary to set needsUpdate to true if you have not rendered the data yet. And the call of normalizeNormals() is also not required since the new normals are already unit vectors.

3 Likes

Thank you so much for the reply! I was struggling with this…once again thank you!