SceneUtils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.Mesh ) {
  39. object = new THREE.Mesh( source.geometry, source.material );
  40. } else if ( source instanceof THREE.Line ) {
  41. object = new THREE.Line( source.geometry, source.material, source.type );
  42. } else if ( source instanceof THREE.Ribbon ) {
  43. object = new THREE.Ribbon( source.geometry, source.material );
  44. } else if ( source instanceof THREE.ParticleSystem ) {
  45. object = new THREE.ParticleSystem( source.geometry, source.material );
  46. object.sortParticles = source.sortParticles;
  47. } else if ( source instanceof THREE.Particle ) {
  48. object = new THREE.Particle( source.material );
  49. } else if ( source instanceof THREE.Sprite ) {
  50. object = new THREE.Sprite( {} );
  51. object.color.copy( source.color );
  52. object.map = source.map;
  53. object.blending = source.blending;
  54. object.useScreenCoordinates = source.useScreenCoordinates;
  55. object.mergeWith3D = source.mergeWith3D;
  56. object.affectedByDistance = source.affectedByDistance;
  57. object.scaleByViewport = source.scaleByViewport;
  58. object.alignment = source.alignment;
  59. object.rotation3d.copy( source.rotation3d );
  60. object.rotation = source.rotation;
  61. object.opacity = source.opacity;
  62. object.uvOffset.copy( source.uvOffset );
  63. object.uvScale.copy( source.uvScale);
  64. } else if ( source instanceof THREE.LOD ) {
  65. object = new THREE.LOD();
  66. } else if ( source instanceof THREE.SkinnedMesh ) {
  67. object = new THREE.SkinnedMesh();
  68. } else if ( 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.parent = source.parent;
  77. object.up.copy( source.up );
  78. object.position.copy( source.position );
  79. // because of Sprite madness
  80. if ( object.rotation instanceof THREE.Vector3 )
  81. object.rotation.copy( source.rotation );
  82. object.eulerOrder = source.eulerOrder;
  83. object.scale.copy( source.scale );
  84. object.dynamic = source.dynamic;
  85. object.doubleSided = source.doubleSided;
  86. object.flipSided = source.flipSided;
  87. object.renderDepth = source.renderDepth;
  88. object.rotationAutoUpdate = source.rotationAutoUpdate;
  89. object.matrix.copy( source.matrix );
  90. object.matrixWorld.copy( source.matrixWorld );
  91. object.matrixRotationWorld.copy( source.matrixRotationWorld );
  92. object.matrixAutoUpdate = source.matrixAutoUpdate;
  93. object.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  94. object.quaternion.copy( source.quaternion );
  95. object.useQuaternion = source.useQuaternion;
  96. object.boundRadius = source.boundRadius;
  97. object.boundRadiusScale = source.boundRadiusScale;
  98. object.visible = source.visible;
  99. object.castShadow = source.castShadow;
  100. object.receiveShadow = source.receiveShadow;
  101. object.frustumCulled = source.frustumCulled;
  102. // children
  103. for ( var i = 0; i < source.children.length; i ++ ) {
  104. var child = THREE.SceneUtils.cloneObject( source.children[ i ] );
  105. object.children[ i ] = child;
  106. child.parent = object;
  107. }
  108. // LODs need to be patched separately to use cloned children
  109. if ( source instanceof THREE.LOD ) {
  110. for ( var i = 0; i < source.LODs.length; i ++ ) {
  111. var lod = source.LODs[ i ];
  112. object.LODs[ i ] = { visibleAtDistance: lod.visibleAtDistance, object3D: object.children[ i ] };
  113. }
  114. }
  115. return object;
  116. }
  117. };
粤ICP备19079148号