SceneUtils.js 4.7 KB

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