Skip to content

ClippingGroup stops working when adding these specific meshes #31708

@DomenicoBruzzese

Description

@DomenicoBruzzese

Description

I think I'm looking at a bug with ClippingGroup, I'm running a very simple example with 3 objects:
a simple green sphere, created with threejs
a .glb model with two red meshes

If I add the sphere to a clipping group, it will always work
If I add only one of the two children of the glb model attached in the jsfiddle to the clipping group (along with the sphere) then it will work.
If I add both children of the glb model (along with the sphere) the interaction breaks for the two models, but never for the sphere. I'll attach a jsfiddle to demonstrate the issue, which is replicable on both chrome/windows/amd and chrome/mac/m4

In the screenshots, the green sphere is always correctly clipped to the plane, whereas it seems that the red meshes are being clipped against a different plane, this "ghost plane" is also inconsistent and changes with the camera view direction

loom / video

Reproduction steps

  1. Open the fiddle
  2. Move the camera
  3. Notice how the clipping behaviour is always consistent for the green sphere, but never for the red meshes

Code

import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { ClippingGroup } from 'three/webgpu';

let camera, scene, renderer, controls, clock;

let clippingPlane = new THREE.Plane(new THREE.Vector3(1, 0, 0), 0);
let globalClippingGroup = new ClippingGroup();
let planeHelper;

async function init() {
    // Renderer
    renderer = new THREE.WebGPURenderer({ antialias: true });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    // Scene and Camera
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.set(-50, 50, 50);
    
    clock = new THREE.Clock();

    // Orbit Controls
    controls = new OrbitControls(camera, renderer.domElement);
    controls.enableDamping = true;
    
    window.addEventListener('resize', onWindowResize);

    // Visualize clipping plane
    planeHelper = new THREE.PlaneHelper(clippingPlane, 50, 0x00ff00);
    scene.add(planeHelper);

    // Load GLB model from public folder
    const gltfLoader = new GLTFLoader();
    gltfLoader.load(
    	'https://domenicobrz.github.io/temp/clipping-group-issue/cross-section-copies.glb',
    	(gltf) => {
            
    		const model = gltf.scene || (gltf.scenes && gltf.scenes[0]);
    		if (model) {
    			model.position.set(0, 0, 0);

                let mesh0 = model.children[0];
                let mesh1 = model.children[1];

                globalClippingGroup.add(mesh0);
                globalClippingGroup.add(mesh1);

                globalClippingGroup.clippingPlanes = [clippingPlane];
                globalClippingGroup.clipIntersection = true;

                console.log(globalClippingGroup);

                let sphereMesh = new THREE.Mesh(new THREE.SphereGeometry(5, 32, 32), new THREE.MeshBasicMaterial({ color: 0x00ff00 }));
                sphereMesh.position.set(0, 10, 0);
                globalClippingGroup.add(sphereMesh);

    			scene.add(globalClippingGroup);
    		}
    	},
    	undefined,
    	(error) => {
    		console.error('Error loading GLB:', error);
    	}
    );

    animate();
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
}

async function animate() {
    requestAnimationFrame(animate);
    if (controls) controls.update();
    renderer.render(scene, camera);
}

init();

Live example

jsfiddle:
https://jsfiddle.net/spd5mfvz/3/

Screenshots

Image Image

Version

latest

Device

Desktop

Browser

Chrome

OS

MacOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions