SceneUtils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ( function () {
  2. function createMeshesFromInstancedMesh( instancedMesh ) {
  3. const group = new THREE.Group();
  4. const count = instancedMesh.count;
  5. const geometry = instancedMesh.geometry;
  6. const material = instancedMesh.material;
  7. for ( let i = 0; i < count; i ++ ) {
  8. const mesh = new THREE.Mesh( geometry, material );
  9. instancedMesh.getMatrixAt( i, mesh.matrix );
  10. mesh.matrix.decompose( mesh.position, mesh.quaternion, mesh.scale );
  11. group.add( mesh );
  12. }
  13. group.copy( instancedMesh );
  14. group.updateMatrixWorld(); // ensure correct world matrices of meshes
  15. return group;
  16. }
  17. function createMeshesFromMultiMaterialMesh( mesh ) {
  18. if ( Array.isArray( mesh.material ) === false ) {
  19. console.warn( 'THREE.SceneUtils.createMeshesFromMultiMaterialMesh(): The given mesh has no multiple materials.' );
  20. return mesh;
  21. }
  22. const object = new THREE.Group();
  23. object.copy( mesh ); // merge groups (which automatically sorts them)
  24. const geometry = THREE.mergeGroups( mesh.geometry );
  25. const index = geometry.index;
  26. const groups = geometry.groups;
  27. const attributeNames = Object.keys( geometry.attributes ); // create a mesh for each group by extracting the buffer data into a new geometry
  28. for ( let i = 0; i < groups.length; i ++ ) {
  29. const group = groups[ i ];
  30. const start = group.start;
  31. const end = start + group.count;
  32. const newGeometry = new THREE.BufferGeometry();
  33. const newMaterial = mesh.material[ group.materialIndex ]; // process all buffer attributes
  34. for ( let j = 0; j < attributeNames.length; j ++ ) {
  35. const name = attributeNames[ j ];
  36. const attribute = geometry.attributes[ name ];
  37. const itemSize = attribute.itemSize;
  38. const newLength = group.count * itemSize;
  39. const type = attribute.array.constructor;
  40. const newArray = new type( newLength );
  41. const newAttribute = new THREE.BufferAttribute( newArray, itemSize );
  42. for ( let k = start, n = 0; k < end; k ++, n ++ ) {
  43. const ind = index.getX( k );
  44. if ( itemSize >= 1 ) newAttribute.setX( n, attribute.getX( ind ) );
  45. if ( itemSize >= 2 ) newAttribute.setY( n, attribute.getY( ind ) );
  46. if ( itemSize >= 3 ) newAttribute.setZ( n, attribute.getZ( ind ) );
  47. if ( itemSize >= 4 ) newAttribute.setW( n, attribute.getW( ind ) );
  48. }
  49. newGeometry.setAttribute( name, newAttribute );
  50. }
  51. const newMesh = new THREE.Mesh( newGeometry, newMaterial );
  52. object.add( newMesh );
  53. }
  54. return object;
  55. }
  56. function createMultiMaterialObject( geometry, materials ) {
  57. const group = new THREE.Group();
  58. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  59. group.add( new THREE.Mesh( geometry, materials[ i ] ) );
  60. }
  61. return group;
  62. }
  63. function detach( child, parent, scene ) {
  64. console.warn( 'THREE.SceneUtils: detach() has been deprecated. Use scene.attach( child ) instead.' );
  65. scene.attach( child );
  66. }
  67. function attach( child, scene, parent ) {
  68. console.warn( 'THREE.SceneUtils: attach() has been deprecated. Use parent.attach( child ) instead.' );
  69. parent.attach( child );
  70. }
  71. THREE.SceneUtils = {};
  72. THREE.SceneUtils.attach = attach;
  73. THREE.SceneUtils.createMeshesFromInstancedMesh = createMeshesFromInstancedMesh;
  74. THREE.SceneUtils.createMeshesFromMultiMaterialMesh = createMeshesFromMultiMaterialMesh;
  75. THREE.SceneUtils.createMultiMaterialObject = createMultiMaterialObject;
  76. THREE.SceneUtils.detach = detach;
  77. } )();