JSFiddle - React, Tailwind, and code Playground
JavaScript
import * as THREE from 'https://unpkg.com/[email protected]/build/three.module.js'
import { ConvexGeometry } from 'https://unpkg.com/[email protected]/examples/jsm/geometries/ConvexGeometry.js'
import { Geometry } from 'https://unpkg.com/[email protected]/examples/jsm/deprecated/Geometry.js'
import { BufferGeometryUtils } from 'https://unpkg.com/[email protected]/examples/jsm/utils/BufferGeometryUtils.js'
let camera, scene, renderer;
init();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 3;
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animation );
document.body.appendChild( renderer.domElement );
// -------------------------------------
const torusGeometry = new THREE.TorusKnotGeometry()
const torusMaterial = new THREE.MeshNormalMaterial()
const torus = new THREE.Mesh(torusGeometry, torusMaterial)
scene.add(torus)
// extract Vector3 vertices
const vertices = []
const positionAttribute = torusGeometry.attributes.position
for (let i = 0; i < positionAttribute.count; i++) {
const vertex = new THREE.Vector3().fromBufferAttribute(positionAttribute, i)
vertices.push(vertex)
}
// compute convex hull
const hull = new ConvexGeometry(vertices) // this is a BufferGeometry
hull.deleteAttribute( 'normal' );
console.log(hull.attributes.position.count) // --> 558
// Geometry mergeVertices
const hullGeometry = new Geometry().fromBufferGeometry(hull)
hullGeometry.mergeVertices()
console.log(hullGeometry.vertices.length) // --> 95
// BufferGeometry mergeVertices
const hullBufferGeometry = BufferGeometryUtils.mergeVertices(hull)
console.log(hullBufferGeometry.attributes.position.count) // --> 556 but it should be 95
...