fourdjs/taperedLink.js
2024-06-09 17:21:16 +10:00

56 lines
1.6 KiB
JavaScript

import * as THREE from 'three';
class TaperedLink extends THREE.Group {
constructor(baseMaterial, n1, n2, r1, r2) {
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 );
this.update(n1, n2, r1, r2);
}
update(n1, n2, r1, r2) {
const kraw = r1 - r2;
const k = ( kraw == 0 ) ? 0.001 : kraw;
let nbase = n1;
let napex = n2;
let rbase = r1;
let rapex = r2;
if( k < 0 ) {
nbase = n2;
napex = n1;
rbase = r2;
rapex = r1;
}
const l = nbase.v3.distanceTo(napex.v3);
const h = l * rbase / k;
//const pos = new THREE.Vector3(0, h/2 - l/2, 0);
//pos.add(nbase.v3);
this.object.scale.copy(new THREE.Vector3(rbase, h, rbase));
this.object.position.copy(nbase.v3);
this.object.lookAt(napex.v3);
this.object.rotation.x = Math.PI / 2.0;
}
// 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 };