Hi. I am a bit of a novice when it comes to coding on three.js so I wanted some help on this:
I am working on a project that requires clipping planes on a geometry and then capping up the cut portion. I saw many examples and questions and found this to be the closest to what I need: https://threejs.org/examples/?q=stenc#webgl_clipping_stencil . However I tried repeating the same for my project using the code below (but it does not create the cap) . Am I missing something ?
this.partGroup = new THREE.Group();
var object = new THREE.Group();
var poGroup = new THREE.Group();
this.renderer.localClippingEnabled = true;
for(var i = 0; i < parts.length; i++){
var geometry = new THREE.BufferGeometry();
geometry.setIndex(new THREE.BufferAttribute(indices, 1));
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
geometry.setAttribute('normal', new THREE.BufferAttribute(normals, 3, true));
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3, true));
var planes = [
new THREE.Plane(new THREE.Vector3(1, 0, 0), -10),
new THREE.Plane(new THREE.Vector3(0, 1, 0), -1)
];
var plane = planes[0];
var stencilGroup = this.createPlaneStencilGroup(geometry, plane, 1);
var planeMat =
new THREE.MeshStandardMaterial({
color: 0xE91E63,
metalness: 0.1,
roughness: 0.75,
clippingPlanes: planes.filter(p => p !== plane),
stencilWrite: true,
stencilRef: 0,
stencilFunc: THREE.NotEqualStencilFunc,
stencilFail: THREE.ReplaceStencilOp,
stencilZFail: THREE.ReplaceStencilOp,
stencilZPass: THREE.ReplaceStencilOp,
});
var planeGeom = new THREE.PlaneBufferGeometry(40, 40);
var po = new THREE.Mesh(planeGeom, planeMat);
po.onAfterRender = function (renderer) {
renderer.clearStencil();
};
poGroup.add(po);
object.add(stencilGroup);
var material = new THREE.MeshPhongMaterial({
vertexColors: THREE.VertexColors,
side: THREE.DoubleSide,
transparent: transparent,
opacity: opacity,
shininess: 30,
reflectivity: 0.5,
clippingPlanes: planes,
clipIntersection: false
});
var mesh = new THREE.Mesh(geometry, material);
mesh.partID = part.id;
mesh.faces = faceList;
mesh.color = (!randomColors ? null : color.getHex());
mesh.visible = false;
this.partGroup.add(mesh);
}
this.scene.add(object);
this.scene.add(poGroup);
this.scene.add(this.partGroup);
this.updateGL();
I followed the exact code from the examples (except the creation of my original mesh is done using BufferGeometry(). I still end up getting the part without any cap (however the cutting planes cut through fine). I’d really appreciate if I could get help on this. Note that the createPlaneStencilGroup() function is unchanged from the examples: