Cut holes out of a plane three.js

47 views Asked by At

I made a plane that uses simplex noise to elevate some of the vertices. The elevated vertices represent areas I want to cut out of the plane (think swiss cheese). I need an actual geometry with holes, so no shaders.

How can I cut these holes out?

function NoisePlane() {
  const meshRef = useRef();
  const [widthSegments, heightSegments] = [100, 100];

  const vertices = useMemo(() => {
    const geometry = new PlaneGeometry(10, 10, widthSegments, heightSegments);
    const simplex = createNoise2D();
    const threshold = 0.5;
    const positions = geometry.attributes.position.array;

    for (let i = 0; i < positions.length; i += 3) {
      const x = positions[i];
      const y = positions[i + 1];
      const z = positions[i + 2];
    
      const noiseValue = simplex(x, y);
      console.log(noiseValue);
    
      positions[i + 2] = noiseValue > threshold ? 0.5 : 0;
    }

    geometry.computeVertexNormals();

    return geometry;
  }, [widthSegments, heightSegments]);

  return (
    <mesh ref={meshRef} geometry={vertices}>
      <meshBasicMaterial color={0x00ff00} wireframe />
    </mesh>
  );
}

wireframe of plane modified by simplex noise.

0

There are 0 answers