SceneUtils.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.SceneUtils = {
  5. showHierarchy : function ( root, visible ) {
  6. THREE.SceneUtils.traverseHierarchy( root, function( node ) { node.visible = visible; } );
  7. },
  8. traverseHierarchy : function ( root, callback ) {
  9. var n, i, l = root.children.length;
  10. for ( i = 0; i < l; i ++ ) {
  11. n = root.children[ i ];
  12. callback( n );
  13. THREE.SceneUtils.traverseHierarchy( n, callback );
  14. }
  15. },
  16. createMultiMaterialObject : function ( geometry, materials ) {
  17. var i, il = materials.length,
  18. group = new THREE.Object3D();
  19. for ( i = 0; i < il; i ++ ) {
  20. var object = new THREE.Mesh( geometry, materials[ i ] );
  21. group.add( object );
  22. }
  23. return group;
  24. },
  25. cloneObject: function ( source ) {
  26. var object;
  27. // subclass specific properties
  28. // (must process in order from more specific subclasses to more abstract classes)
  29. if ( source instanceof THREE.MorphAnimMesh ) {
  30. object = new THREE.MorphAnimMesh( source.geometry, source.material );
  31. object.duration = source.duration;
  32. object.mirroredLoop = source.mirroredLoop;
  33. object.time = source.time;
  34. object.lastKeyframe = source.lastKeyframe;
  35. object.currentKeyframe = source.currentKeyframe;
  36. object.direction = source.direction;
  37. object.directionBackwards = source.directionBackwards;
  38. } else if ( source instanceof THREE.SkinnedMesh ) {
  39. object = new THREE.SkinnedMesh( source.geometry, source.material );
  40. } else if ( source instanceof THREE.Mesh ) {
  41. object = new THREE.Mesh( source.geometry, source.material );
  42. } else if ( source instanceof THREE.Line ) {
  43. object = new THREE.Line( source.geometry, source.material, source.type );
  44. } else if ( source instanceof THREE.Ribbon ) {
  45. object = new THREE.Ribbon( source.geometry, source.material );
  46. } else if ( source instanceof THREE.ParticleSystem ) {
  47. object = new THREE.ParticleSystem( source.geometry, source.material );
  48. object.sortParticles = source.sortParticles;
  49. } else if ( source instanceof THREE.Particle ) {
  50. object = new THREE.Particle( source.material );
  51. } else if ( source instanceof THREE.Sprite ) {
  52. object = new THREE.Sprite( {} );
  53. object.color.copy( source.color );
  54. object.map = source.map;
  55. object.blending = source.blending;
  56. object.useScreenCoordinates = source.useScreenCoordinates;
  57. object.mergeWith3D = source.mergeWith3D;
  58. object.affectedByDistance = source.affectedByDistance;
  59. object.scaleByViewport = source.scaleByViewport;
  60. object.alignment = source.alignment;
  61. object.rotation3d.copy( source.rotation3d );
  62. object.rotation = source.rotation;
  63. object.opacity = source.opacity;
  64. object.uvOffset.copy( source.uvOffset );
  65. object.uvScale.copy( source.uvScale);
  66. } else if ( source instanceof THREE.LOD ) {
  67. object = new THREE.LOD();
  68. } else if ( THREE.MarchingCubes && source instanceof THREE.MarchingCubes ) {
  69. object = new THREE.MarchingCubes( source.resolution, source.material );
  70. object.field.set( source.field );
  71. object.isolation = source.isolation;
  72. } else if ( source instanceof THREE.Object3D ) {
  73. object = new THREE.Object3D();
  74. }
  75. // base class properties
  76. object.name = source.name;
  77. object.parent = source.parent;
  78. object.up.copy( source.up );
  79. object.position.copy( source.position );
  80. // because of Sprite madness
  81. if ( object.rotation instanceof THREE.Vector3 )
  82. object.rotation.copy( source.rotation );
  83. object.eulerOrder = source.eulerOrder;
  84. object.scale.copy( source.scale );
  85. object.dynamic = source.dynamic;
  86. object.doubleSided = source.doubleSided;
  87. object.flipSided = source.flipSided;
  88. object.renderDepth = source.renderDepth;
  89. object.rotationAutoUpdate = source.rotationAutoUpdate;
  90. object.matrix.copy( source.matrix );
  91. object.matrixWorld.copy( source.matrixWorld );
  92. object.matrixRotationWorld.copy( source.matrixRotationWorld );
  93. object.matrixAutoUpdate = source.matrixAutoUpdate;
  94. object.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  95. object.quaternion.copy( source.quaternion );
  96. object.useQuaternion = source.useQuaternion;
  97. object.boundRadius = source.boundRadius;
  98. object.boundRadiusScale = source.boundRadiusScale;
  99. object.visible = source.visible;
  100. object.castShadow = source.castShadow;
  101. object.receiveShadow = source.receiveShadow;
  102. object.frustumCulled = source.frustumCulled;
  103. // children
  104. for ( var i = 0; i < source.children.length; i ++ ) {
  105. var child = THREE.SceneUtils.cloneObject( source.children[ i ] );
  106. object.children[ i ] = child;
  107. child.parent = object;
  108. }
  109. // LODs need to be patched separately to use cloned children
  110. if ( source instanceof THREE.LOD ) {
  111. for ( var i = 0; i < source.LODs.length; i ++ ) {
  112. var lod = source.LODs[ i ];
  113. object.LODs[ i ] = { visibleAtDistance: lod.visibleAtDistance, object3D: object.children[ i ] };
  114. }
  115. }
  116. return object;
  117. },
  118. detach : function ( child, parent, scene ) {
  119. child.applyMatrix( parent.matrixWorld );
  120. parent.remove( child );
  121. scene.add( child );
  122. },
  123. attach: function ( child, scene, parent ) {
  124. var matrixWorldInverse = new THREE.Matrix4();
  125. matrixWorldInverse.getInverse( parent.matrixWorld );
  126. child.applyMatrix( matrixWorldInverse );
  127. scene.remove( child );
  128. parent.add( child );
  129. }
  130. };