Spaces:
Running
Running
File size: 3,528 Bytes
6cd9596 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
/**
* @author WestLangley / http://github.com/WestLangley
* @author zz85 / http://github.com/zz85
* @author bhouston / http://clara.io
*
* Creates an arrow for visualizing directions
*
* Parameters:
* dir - Vector3
* origin - Vector3
* length - Number
* color - color in hex value
* headLength - Number
* headWidth - Number
*/
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
import { Object3D } from '../core/Object3D.js';
import { CylinderBufferGeometry } from '../geometries/CylinderGeometry.js';
import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
import { Mesh } from '../objects/Mesh.js';
import { Line } from '../objects/Line.js';
import { Vector3 } from '../math/Vector3.js';
var lineGeometry, coneGeometry;
function ArrowHelper( dir, origin, length, color, headLength, headWidth ) {
// dir is assumed to be normalized
Object3D.call( this );
if ( dir === undefined ) dir = new Vector3( 0, 0, 1 );
if ( origin === undefined ) origin = new Vector3( 0, 0, 0 );
if ( length === undefined ) length = 1;
if ( color === undefined ) color = 0xffff00;
if ( headLength === undefined ) headLength = 0.2 * length;
if ( headWidth === undefined ) headWidth = 0.2 * headLength;
if ( lineGeometry === undefined ) {
lineGeometry = new BufferGeometry();
lineGeometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
coneGeometry = new CylinderBufferGeometry( 0, 0.5, 1, 5, 1 );
coneGeometry.translate( 0, - 0.5, 0 );
}
this.position.copy( origin );
this.line = new Line( lineGeometry, new LineBasicMaterial( { color: color } ) );
this.line.matrixAutoUpdate = false;
this.add( this.line );
this.cone = new Mesh( coneGeometry, new MeshBasicMaterial( { color: color } ) );
this.cone.matrixAutoUpdate = false;
this.add( this.cone );
this.setDirection( dir );
this.setLength( length, headLength, headWidth );
}
ArrowHelper.prototype = Object.create( Object3D.prototype );
ArrowHelper.prototype.constructor = ArrowHelper;
ArrowHelper.prototype.setDirection = ( function () {
var axis = new Vector3();
var radians;
return function setDirection( dir ) {
// dir is assumed to be normalized
if ( dir.y > 0.99999 ) {
this.quaternion.set( 0, 0, 0, 1 );
} else if ( dir.y < - 0.99999 ) {
this.quaternion.set( 1, 0, 0, 0 );
} else {
axis.set( dir.z, 0, - dir.x ).normalize();
radians = Math.acos( dir.y );
this.quaternion.setFromAxisAngle( axis, radians );
}
};
}() );
ArrowHelper.prototype.setLength = function ( length, headLength, headWidth ) {
if ( headLength === undefined ) headLength = 0.2 * length;
if ( headWidth === undefined ) headWidth = 0.2 * headLength;
this.line.scale.set( 1, Math.max( 0, length - headLength ), 1 );
this.line.updateMatrix();
this.cone.scale.set( headWidth, headLength, headWidth );
this.cone.position.y = length;
this.cone.updateMatrix();
};
ArrowHelper.prototype.setColor = function ( color ) {
this.line.material.color.copy( color );
this.cone.material.color.copy( color );
};
ArrowHelper.prototype.copy = function ( source ) {
Object3D.prototype.copy.call( this, source, false );
this.line.copy( source.line );
this.cone.copy( source.cone );
return this;
};
ArrowHelper.prototype.clone = function () {
return new this.constructor().copy( this );
};
export { ArrowHelper };
|