fourdjs/taperedLink.js
2024-05-28 17:40:30 +10:00

37 lines
1.1 KiB
JavaScript

import * as THREE from 'three';
class TaperedLink extends THREE.Group {
constructor(baseMaterial) {
super();
const geometry = new THREE.ConeGeometry( 0.75, 1, 32, true );
const cplane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 0.5);
const material = baseMaterial.clone();
material.clippingPlanes = [ cplane ];
this.object = new THREE.Mesh( geometry, material );
this.add( this.object );
}
update(r1, r2, l) {
const kraw = ( r1 - r2 );
const k = ( kraw == 0 ) ? 0.001 : kraw;
if( k > 0 ) {
const h = l * r1 / k;
this.object.scale.copy(new THREE.Vector3(r1, h, r1));
this.object.material.clippingPlanes[0].normal.y = -1;
this.object.material.clippingPlanes[0].constant = l / 2;
this.object.position.copy(new THREE.Vector3(0, h/2 - l/2, 0));
} else {
const h = l * r2 / k;
this.object.scale.copy(new THREE.Vector3(r2, h, r2));
this.object.material.clippingPlanes[0].normal.y = 1;
this.object.material.clippingPlanes[0].constant = l / 2;
this.object.position.copy(new THREE.Vector3(0, h / 2 + l / 2, 0));
}
}
}
export { TaperedLink };