I was looking for any way to slice the geometry at for example, every vertex greater than Z:100 will be removed, and every X,Y bigger than 200 for example.
I was using V127 of threejs.
Actually I was trying with this code, but I was missing some step:
Before “Slice at z: 100”
After applying my actual code
As you can see it connects the removed vertex with the 0,0,0 position.
There is my code:
const positions = (this.mesh.geometry as THREE.BufferGeometry).attributes.position.array as Array<number>
for (let i = 0; i < positions.length; i += 3) {
if(positions[i +2] > 100) {
positions[i] = null;
positions[i + 1] = null;
positions[i + 2] = null;
}
}
(this.mesh.geometry as THREE.BufferGeometry).attributes.position.needsUpdate = true
This kind of vertex manipulating is problematic since you will mess up the face definitions of your geometry. Faces require three vertices in three.js (since they are always triangles). If you remove a vertex without defining a replacement, you produce undefined triangle data.
Since buffer attributes are fixed sized, you can not remove data from a buffer or perform any resizing operation in general. You have to create new buffer attributes and a new geometry. Alternatively, you move all “removed” vertices at the end of the buffers and define via drawRange what parts of the geometry should be rendered.
And the best way to move the vertex is inside the loop? I was trying to do that, but without success, i’ll keep trying, if you have some fast idea how to do can you tell me?
const positions: Float32Array = (this.mesh.geometry as THREE.BufferGeometry).attributes.position.array as Float32Array
let total = positions.length;
for (let i = 0; i < positions.length; i += 3) {
let x = positions[i];
let y = positions[i + 1];
let z = positions[i + 2];
let nextX = positions[i + 3];
let nextY = positions[i + 4];
let nextZ = positions[i + 5];
if(z > 100) {
positions[i] = nextX;
positions[i + 1] = nextY;
positions[i + 2] = nextZ;
positions[i + 3] = null;
positions[i + 4] = null;
positions[i + 5] = null;
total -= 3;
}
if(z == null) {
positions[i] = nextX;
positions[i + 1] = nextY;
positions[i + 2] = nextZ;
positions[i+3] = null;
positions[i+4] = null;
positions[i+5] = null;
}
}
(this.mesh.geometry as THREE.BufferGeometry).attributes.position.needsUpdate = true
this.mesh.geometry.setDrawRange(0, total);