Spaces:
Running
Running
File size: 3,386 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
/**
* @author mrdoob / http://mrdoob.com/
* @author WestLangley / http://github.com/WestLangley
*/
import { Matrix3 } from '../math/Matrix3.js';
import { Vector3 } from '../math/Vector3.js';
import { LineSegments } from '../objects/LineSegments.js';
import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
function VertexNormalsHelper( object, size, hex, linewidth ) {
this.object = object;
this.size = ( size !== undefined ) ? size : 1;
var color = ( hex !== undefined ) ? hex : 0xff0000;
var width = ( linewidth !== undefined ) ? linewidth : 1;
//
var nNormals = 0;
var objGeometry = this.object.geometry;
if ( objGeometry && objGeometry.isGeometry ) {
nNormals = objGeometry.faces.length * 3;
} else if ( objGeometry && objGeometry.isBufferGeometry ) {
nNormals = objGeometry.attributes.normal.count;
}
//
var geometry = new BufferGeometry();
var positions = new Float32BufferAttribute( nNormals * 2 * 3, 3 );
geometry.addAttribute( 'position', positions );
LineSegments.call( this, geometry, new LineBasicMaterial( { color: color, linewidth: width } ) );
//
this.matrixAutoUpdate = false;
this.update();
}
VertexNormalsHelper.prototype = Object.create( LineSegments.prototype );
VertexNormalsHelper.prototype.constructor = VertexNormalsHelper;
VertexNormalsHelper.prototype.update = ( function () {
var v1 = new Vector3();
var v2 = new Vector3();
var normalMatrix = new Matrix3();
return function update() {
var keys = [ 'a', 'b', 'c' ];
this.object.updateMatrixWorld( true );
normalMatrix.getNormalMatrix( this.object.matrixWorld );
var matrixWorld = this.object.matrixWorld;
var position = this.geometry.attributes.position;
//
var objGeometry = this.object.geometry;
if ( objGeometry && objGeometry.isGeometry ) {
var vertices = objGeometry.vertices;
var faces = objGeometry.faces;
var idx = 0;
for ( var i = 0, l = faces.length; i < l; i ++ ) {
var face = faces[ i ];
for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
var vertex = vertices[ face[ keys[ j ] ] ];
var normal = face.vertexNormals[ j ];
v1.copy( vertex ).applyMatrix4( matrixWorld );
v2.copy( normal ).applyMatrix3( normalMatrix ).normalize().multiplyScalar( this.size ).add( v1 );
position.setXYZ( idx, v1.x, v1.y, v1.z );
idx = idx + 1;
position.setXYZ( idx, v2.x, v2.y, v2.z );
idx = idx + 1;
}
}
} else if ( objGeometry && objGeometry.isBufferGeometry ) {
var objPos = objGeometry.attributes.position;
var objNorm = objGeometry.attributes.normal;
var idx = 0;
// for simplicity, ignore index and drawcalls, and render every normal
for ( var j = 0, jl = objPos.count; j < jl; j ++ ) {
v1.set( objPos.getX( j ), objPos.getY( j ), objPos.getZ( j ) ).applyMatrix4( matrixWorld );
v2.set( objNorm.getX( j ), objNorm.getY( j ), objNorm.getZ( j ) );
v2.applyMatrix3( normalMatrix ).normalize().multiplyScalar( this.size ).add( v1 );
position.setXYZ( idx, v1.x, v1.y, v1.z );
idx = idx + 1;
position.setXYZ( idx, v2.x, v2.y, v2.z );
idx = idx + 1;
}
}
position.needsUpdate = true;
};
}() );
export { VertexNormalsHelper };
|