Spaces:
Running
Running
File size: 6,603 Bytes
a28eca3 |
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
import {
BufferAttribute,
BufferGeometry,
Color,
Group,
Matrix4,
Mesh,
Vector3
} from 'three';
import { mergeGroups, deepCloneAttribute } from './BufferGeometryUtils.js';
const _color = /*@__PURE__*/new Color();
const _matrix = /*@__PURE__*/new Matrix4();
function createMeshesFromInstancedMesh( instancedMesh ) {
const group = new Group();
const count = instancedMesh.count;
const geometry = instancedMesh.geometry;
const material = instancedMesh.material;
for ( let i = 0; i < count; i ++ ) {
const mesh = new Mesh( geometry, material );
instancedMesh.getMatrixAt( i, mesh.matrix );
mesh.matrix.decompose( mesh.position, mesh.quaternion, mesh.scale );
group.add( mesh );
}
group.copy( instancedMesh );
group.updateMatrixWorld(); // ensure correct world matrices of meshes
return group;
}
function createMeshesFromMultiMaterialMesh( mesh ) {
if ( Array.isArray( mesh.material ) === false ) {
console.warn( 'THREE.SceneUtils.createMeshesFromMultiMaterialMesh(): The given mesh has no multiple materials.' );
return mesh;
}
const object = new Group();
object.copy( mesh );
// merge groups (which automatically sorts them)
const geometry = mergeGroups( mesh.geometry );
const index = geometry.index;
const groups = geometry.groups;
const attributeNames = Object.keys( geometry.attributes );
// create a mesh for each group by extracting the buffer data into a new geometry
for ( let i = 0; i < groups.length; i ++ ) {
const group = groups[ i ];
const start = group.start;
const end = start + group.count;
const newGeometry = new BufferGeometry();
const newMaterial = mesh.material[ group.materialIndex ];
// process all buffer attributes
for ( let j = 0; j < attributeNames.length; j ++ ) {
const name = attributeNames[ j ];
const attribute = geometry.attributes[ name ];
const itemSize = attribute.itemSize;
const newLength = group.count * itemSize;
const type = attribute.array.constructor;
const newArray = new type( newLength );
const newAttribute = new BufferAttribute( newArray, itemSize );
for ( let k = start, n = 0; k < end; k ++, n ++ ) {
const ind = index.getX( k );
if ( itemSize >= 1 ) newAttribute.setX( n, attribute.getX( ind ) );
if ( itemSize >= 2 ) newAttribute.setY( n, attribute.getY( ind ) );
if ( itemSize >= 3 ) newAttribute.setZ( n, attribute.getZ( ind ) );
if ( itemSize >= 4 ) newAttribute.setW( n, attribute.getW( ind ) );
}
newGeometry.setAttribute( name, newAttribute );
}
const newMesh = new Mesh( newGeometry, newMaterial );
object.add( newMesh );
}
return object;
}
function createMultiMaterialObject( geometry, materials ) {
const group = new Group();
for ( let i = 0, l = materials.length; i < l; i ++ ) {
group.add( new Mesh( geometry, materials[ i ] ) );
}
return group;
}
function reduceVertices( object, func, initialValue ) {
let value = initialValue;
const vertex = new Vector3();
object.updateWorldMatrix( true, true );
object.traverseVisible( ( child ) => {
const { geometry } = child;
if ( geometry !== undefined ) {
const { position } = geometry.attributes;
if ( position !== undefined ) {
for ( let i = 0, l = position.count; i < l; i ++ ) {
if ( child.isMesh ) {
child.getVertexPosition( i, vertex );
} else {
vertex.fromBufferAttribute( position, i );
}
if ( ! child.isSkinnedMesh ) {
vertex.applyMatrix4( child.matrixWorld );
}
value = func( value, vertex );
}
}
}
} );
return value;
}
/**
* @param {InstancedMesh} mesh
* @param {function(int, int):int} compareFn
*/
function sortInstancedMesh( mesh, compareFn ) {
// store copy of instanced attributes for lookups
const instanceMatrixRef = deepCloneAttribute( mesh.instanceMatrix );
const instanceColorRef = mesh.instanceColor ? deepCloneAttribute( mesh.instanceColor ) : null;
const attributeRefs = new Map();
for ( const name in mesh.geometry.attributes ) {
const attribute = mesh.geometry.attributes[ name ];
if ( attribute.isInstancedBufferAttribute ) {
attributeRefs.set( attribute, deepCloneAttribute( attribute ) );
}
}
// compute sort order
const tokens = [];
for ( let i = 0; i < mesh.count; i ++ ) tokens.push( i );
tokens.sort( compareFn );
// apply sort order
for ( let i = 0; i < tokens.length; i ++ ) {
const refIndex = tokens[ i ];
_matrix.fromArray( instanceMatrixRef.array, refIndex * mesh.instanceMatrix.itemSize );
_matrix.toArray( mesh.instanceMatrix.array, i * mesh.instanceMatrix.itemSize );
if ( mesh.instanceColor ) {
_color.fromArray( instanceColorRef.array, refIndex * mesh.instanceColor.itemSize );
_color.toArray( mesh.instanceColor.array, i * mesh.instanceColor.itemSize );
}
for ( const name in mesh.geometry.attributes ) {
const attribute = mesh.geometry.attributes[ name ];
if ( attribute.isInstancedBufferAttribute ) {
const attributeRef = attributeRefs.get( attribute );
attribute.setX( i, attributeRef.getX( refIndex ) );
if ( attribute.itemSize > 1 ) attribute.setY( i, attributeRef.getY( refIndex ) );
if ( attribute.itemSize > 2 ) attribute.setZ( i, attributeRef.getZ( refIndex ) );
if ( attribute.itemSize > 3 ) attribute.setW( i, attributeRef.getW( refIndex ) );
}
}
}
}
/**
* @param {Object3D} object Object to traverse.
* @yields {Object3D} Objects that passed the filter condition.
*/
function* traverseGenerator( object ) {
yield object;
const children = object.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
yield* traverseGenerator( children[ i ] );
}
}
/**
* @param {Object3D} object Object to traverse.
* @yields {Object3D} Objects that passed the filter condition.
*/
function* traverseVisibleGenerator( object ) {
if ( object.visible === false ) return;
yield object;
const children = object.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
yield* traverseVisibleGenerator( children[ i ] );
}
}
/**
* @param {Object3D} object Object to traverse.
* @yields {Object3D} Objects that passed the filter condition.
*/
function* traverseAncestorsGenerator( object ) {
const parent = object.parent;
if ( parent !== null ) {
yield parent;
yield* traverseAncestorsGenerator( parent );
}
}
export {
createMeshesFromInstancedMesh,
createMeshesFromMultiMaterialMesh,
createMultiMaterialObject,
reduceVertices,
sortInstancedMesh,
traverseGenerator,
traverseVisibleGenerator,
traverseAncestorsGenerator
};
|