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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/**
* @author mr.doob / http://mrdoob.com/
*/
import {
BufferAttribute,
BufferGeometry,
Color,
DynamicDrawUsage,
Mesh,
MeshStandardMaterial,
Vector3,
VertexColors
} from '../../../build/three.module.js';
function TubePainter() {
const BUFFER_SIZE = 1000000 * 3;
let positions = new BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
positions.usage = DynamicDrawUsage;
let normals = new BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
normals.usage = DynamicDrawUsage;
let colors = new BufferAttribute( new Float32Array( BUFFER_SIZE ), 3 );
colors.usage = DynamicDrawUsage;
let geometry = new BufferGeometry();
geometry.setAttribute( 'position', positions );
geometry.setAttribute( 'normal', normals );
geometry.setAttribute( 'color', colors );
geometry.drawRange.count = 0;
let material = new MeshStandardMaterial( {
roughness: 0.9,
metalness: 0.0,
vertexColors: VertexColors
} );
let mesh = new Mesh( geometry, material );
mesh.frustumCulled = false;
//
function getPoints( size ) {
let PI2 = Math.PI * 2;
let sides = 10;
let array = [];
let radius = 0.01 * size;
for ( let i = 0; i < sides; i ++ ) {
let angle = ( i / sides ) * PI2;
array.push( new Vector3( Math.sin( angle ) * radius, Math.cos( angle ) * radius, 0 ) );
}
return array;
}
let vector1 = new Vector3();
let vector2 = new Vector3();
let vector3 = new Vector3();
let vector4 = new Vector3();
let color = new Color( 0xffffff );
let size = 1;
function stroke( position1, position2, matrix1, matrix2 ) {
if ( position1.distanceToSquared( position2 ) === 0 ) return;
let count = geometry.drawRange.count;
let points = getPoints( size );
for ( let i = 0, il = points.length; i < il; i ++ ) {
let vertex1 = points[ i ];
let vertex2 = points[ ( i + 1 ) % il ];
// positions
vector1.copy( vertex1 ).applyMatrix4( matrix2 ).add( position2 );
vector2.copy( vertex2 ).applyMatrix4( matrix2 ).add( position2 );
vector3.copy( vertex2 ).applyMatrix4( matrix1 ).add( position1 );
vector4.copy( vertex1 ).applyMatrix4( matrix1 ).add( position1 );
vector1.toArray( positions.array, ( count + 0 ) * 3 );
vector2.toArray( positions.array, ( count + 1 ) * 3 );
vector4.toArray( positions.array, ( count + 2 ) * 3 );
vector2.toArray( positions.array, ( count + 3 ) * 3 );
vector3.toArray( positions.array, ( count + 4 ) * 3 );
vector4.toArray( positions.array, ( count + 5 ) * 3 );
// normals
vector1.copy( vertex1 ).applyMatrix4( matrix2 ).normalize();
vector2.copy( vertex2 ).applyMatrix4( matrix2 ).normalize();
vector3.copy( vertex2 ).applyMatrix4( matrix1 ).normalize();
vector4.copy( vertex1 ).applyMatrix4( matrix1 ).normalize();
vector1.toArray( normals.array, ( count + 0 ) * 3 );
vector2.toArray( normals.array, ( count + 1 ) * 3 );
vector4.toArray( normals.array, ( count + 2 ) * 3 );
vector2.toArray( normals.array, ( count + 3 ) * 3 );
vector3.toArray( normals.array, ( count + 4 ) * 3 );
vector4.toArray( normals.array, ( count + 5 ) * 3 );
// colors
color.toArray( colors.array, ( count + 0 ) * 3 );
color.toArray( colors.array, ( count + 1 ) * 3 );
color.toArray( colors.array, ( count + 2 ) * 3 );
color.toArray( colors.array, ( count + 3 ) * 3 );
color.toArray( colors.array, ( count + 4 ) * 3 );
color.toArray( colors.array, ( count + 5 ) * 3 );
count += 6;
}
geometry.drawRange.count = count;
}
function setSize( value ) {
size = value;
}
function updateGeometry( start, end ) {
if ( start === end ) return;
let offset = start * 3;
let count = ( end - start ) * 3;
positions.updateRange.offset = offset;
positions.updateRange.count = count;
positions.needsUpdate = true;
normals.updateRange.offset = offset;
normals.updateRange.count = count;
normals.needsUpdate = true;
colors.updateRange.offset = offset;
colors.updateRange.count = count;
colors.needsUpdate = true;
}
return {
mesh: mesh,
stroke: stroke,
setSize: setSize,
updateGeometry: updateGeometry
};
}
export { TubePainter };
|