Object3D.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. import { Quaternion } from '../math/Quaternion.js';
  2. import { Vector3 } from '../math/Vector3.js';
  3. import { Matrix4 } from '../math/Matrix4.js';
  4. import { EventDispatcher } from './EventDispatcher.js';
  5. import { Euler } from '../math/Euler.js';
  6. import { Layers } from './Layers.js';
  7. import { Matrix3 } from '../math/Matrix3.js';
  8. import { generateUUID } from '../math/MathUtils.js';
  9. import { error } from '../utils.js';
  10. let _object3DId = 0;
  11. const _v1 = /*@__PURE__*/ new Vector3();
  12. const _q1 = /*@__PURE__*/ new Quaternion();
  13. const _m1 = /*@__PURE__*/ new Matrix4();
  14. const _target = /*@__PURE__*/ new Vector3();
  15. const _position = /*@__PURE__*/ new Vector3();
  16. const _scale = /*@__PURE__*/ new Vector3();
  17. const _quaternion = /*@__PURE__*/ new Quaternion();
  18. const _xAxis = /*@__PURE__*/ new Vector3( 1, 0, 0 );
  19. const _yAxis = /*@__PURE__*/ new Vector3( 0, 1, 0 );
  20. const _zAxis = /*@__PURE__*/ new Vector3( 0, 0, 1 );
  21. /**
  22. * Fires when the object has been added to its parent object.
  23. *
  24. * @event Object3D#added
  25. * @type {Object}
  26. */
  27. const _addedEvent = { type: 'added' };
  28. /**
  29. * Fires when the object has been removed from its parent object.
  30. *
  31. * @event Object3D#removed
  32. * @type {Object}
  33. */
  34. const _removedEvent = { type: 'removed' };
  35. /**
  36. * Fires when a new child object has been added.
  37. *
  38. * @event Object3D#childadded
  39. * @type {Object}
  40. */
  41. const _childaddedEvent = { type: 'childadded', child: null };
  42. /**
  43. * Fires when a child object has been removed.
  44. *
  45. * @event Object3D#childremoved
  46. * @type {Object}
  47. */
  48. const _childremovedEvent = { type: 'childremoved', child: null };
  49. /**
  50. * This is the base class for most objects in three.js and provides a set of
  51. * properties and methods for manipulating objects in 3D space.
  52. *
  53. * @augments EventDispatcher
  54. */
  55. class Object3D extends EventDispatcher {
  56. /**
  57. * Constructs a new 3D object.
  58. */
  59. constructor() {
  60. super();
  61. /**
  62. * This flag can be used for type testing.
  63. *
  64. * @type {boolean}
  65. * @readonly
  66. * @default true
  67. */
  68. this.isObject3D = true;
  69. /**
  70. * The ID of the 3D object.
  71. *
  72. * @name Object3D#id
  73. * @type {number}
  74. * @readonly
  75. */
  76. Object.defineProperty( this, 'id', { value: _object3DId ++ } );
  77. /**
  78. * The UUID of the 3D object.
  79. *
  80. * @type {string}
  81. * @readonly
  82. */
  83. this.uuid = generateUUID();
  84. /**
  85. * The name of the 3D object.
  86. *
  87. * @type {string}
  88. */
  89. this.name = '';
  90. /**
  91. * The type property is used for detecting the object type
  92. * in context of serialization/deserialization.
  93. *
  94. * @type {string}
  95. * @readonly
  96. */
  97. this.type = 'Object3D';
  98. /**
  99. * A reference to the parent object.
  100. *
  101. * @type {?Object3D}
  102. * @default null
  103. */
  104. this.parent = null;
  105. /**
  106. * An array holding the child 3D objects of this instance.
  107. *
  108. * @type {Array<Object3D>}
  109. */
  110. this.children = [];
  111. /**
  112. * Defines the `up` direction of the 3D object which influences
  113. * the orientation via methods like {@link Object3D#lookAt}.
  114. *
  115. * The default values for all 3D objects is defined by `Object3D.DEFAULT_UP`.
  116. *
  117. * @type {Vector3}
  118. */
  119. this.up = Object3D.DEFAULT_UP.clone();
  120. const position = new Vector3();
  121. const rotation = new Euler();
  122. const quaternion = new Quaternion();
  123. const scale = new Vector3( 1, 1, 1 );
  124. function onRotationChange() {
  125. quaternion.setFromEuler( rotation, false );
  126. }
  127. function onQuaternionChange() {
  128. rotation.setFromQuaternion( quaternion, undefined, false );
  129. }
  130. rotation._onChange( onRotationChange );
  131. quaternion._onChange( onQuaternionChange );
  132. Object.defineProperties( this, {
  133. /**
  134. * Represents the object's local position.
  135. *
  136. * @name Object3D#position
  137. * @type {Vector3}
  138. * @default (0,0,0)
  139. */
  140. position: {
  141. configurable: true,
  142. enumerable: true,
  143. value: position
  144. },
  145. /**
  146. * Represents the object's local rotation as Euler angles, in radians.
  147. *
  148. * @name Object3D#rotation
  149. * @type {Euler}
  150. * @default (0,0,0)
  151. */
  152. rotation: {
  153. configurable: true,
  154. enumerable: true,
  155. value: rotation
  156. },
  157. /**
  158. * Represents the object's local rotation as Quaternions.
  159. *
  160. * @name Object3D#quaternion
  161. * @type {Quaternion}
  162. */
  163. quaternion: {
  164. configurable: true,
  165. enumerable: true,
  166. value: quaternion
  167. },
  168. /**
  169. * Represents the object's local scale.
  170. *
  171. * @name Object3D#scale
  172. * @type {Vector3}
  173. * @default (1,1,1)
  174. */
  175. scale: {
  176. configurable: true,
  177. enumerable: true,
  178. value: scale
  179. },
  180. /**
  181. * Represents the object's model-view matrix.
  182. *
  183. * @name Object3D#modelViewMatrix
  184. * @type {Matrix4}
  185. */
  186. modelViewMatrix: {
  187. value: new Matrix4()
  188. },
  189. /**
  190. * Represents the object's normal matrix.
  191. *
  192. * @name Object3D#normalMatrix
  193. * @type {Matrix3}
  194. */
  195. normalMatrix: {
  196. value: new Matrix3()
  197. }
  198. } );
  199. /**
  200. * Represents the object's transformation matrix in local space.
  201. *
  202. * @type {Matrix4}
  203. */
  204. this.matrix = new Matrix4();
  205. /**
  206. * Represents the object's transformation matrix in world space.
  207. * If the 3D object has no parent, then it's identical to the local transformation matrix
  208. *
  209. * @type {Matrix4}
  210. */
  211. this.matrixWorld = new Matrix4();
  212. /**
  213. * When set to `true`, the engine automatically computes the local matrix from position,
  214. * rotation and scale every frame. If set to `false`, the app is responsible for recomputing
  215. * the local matrix by calling `updateMatrix()`.
  216. *
  217. * The default values for all 3D objects is defined by `Object3D.DEFAULT_MATRIX_AUTO_UPDATE`.
  218. *
  219. * @type {boolean}
  220. * @default true
  221. */
  222. this.matrixAutoUpdate = Object3D.DEFAULT_MATRIX_AUTO_UPDATE;
  223. /**
  224. * When set to `true`, the engine automatically computes the world matrix from the current local
  225. * matrix and the object's transformation hierarchy. If set to `false`, the app is responsible for
  226. * recomputing the world matrix by directly updating the `matrixWorld` property.
  227. *
  228. * The default values for all 3D objects is defined by `Object3D.DEFAULT_MATRIX_WORLD_AUTO_UPDATE`.
  229. *
  230. * @type {boolean}
  231. * @default true
  232. */
  233. this.matrixWorldAutoUpdate = Object3D.DEFAULT_MATRIX_WORLD_AUTO_UPDATE; // checked by the renderer
  234. /**
  235. * When set to `true`, it calculates the world matrix in that frame and resets this property
  236. * to `false`.
  237. *
  238. * @type {boolean}
  239. * @default false
  240. */
  241. this.matrixWorldNeedsUpdate = false;
  242. /**
  243. * The layer membership of the 3D object. The 3D object is only visible if it has
  244. * at least one layer in common with the camera in use. This property can also be
  245. * used to filter out unwanted objects in ray-intersection tests when using {@link Raycaster}.
  246. *
  247. * @type {Layers}
  248. */
  249. this.layers = new Layers();
  250. /**
  251. * When set to `true`, the 3D object gets rendered.
  252. *
  253. * @type {boolean}
  254. * @default true
  255. */
  256. this.visible = true;
  257. /**
  258. * When set to `true`, the 3D object gets rendered into shadow maps.
  259. *
  260. * @type {boolean}
  261. * @default false
  262. */
  263. this.castShadow = false;
  264. /**
  265. * When set to `true`, the 3D object is affected by shadows in the scene.
  266. *
  267. * @type {boolean}
  268. * @default false
  269. */
  270. this.receiveShadow = false;
  271. /**
  272. * When set to `true`, the 3D object is honored by view frustum culling.
  273. *
  274. * @type {boolean}
  275. * @default true
  276. */
  277. this.frustumCulled = true;
  278. /**
  279. * This value allows the default rendering order of scene graph objects to be
  280. * overridden although opaque and transparent objects remain sorted independently.
  281. * When this property is set for an instance of {@link Group},all descendants
  282. * objects will be sorted and rendered together. Sorting is from lowest to highest
  283. * render order.
  284. *
  285. * @type {number}
  286. * @default 0
  287. */
  288. this.renderOrder = 0;
  289. /**
  290. * An array holding the animation clips of the 3D object.
  291. *
  292. * @type {Array<AnimationClip>}
  293. */
  294. this.animations = [];
  295. /**
  296. * Custom depth material to be used when rendering to the depth map. Can only be used
  297. * in context of meshes. When shadow-casting with a {@link DirectionalLight} or {@link SpotLight},
  298. * if you are modifying vertex positions in the vertex shader you must specify a custom depth
  299. * material for proper shadows.
  300. *
  301. * Only relevant in context of {@link WebGLRenderer}.
  302. *
  303. * @type {(Material|undefined)}
  304. * @default undefined
  305. */
  306. this.customDepthMaterial = undefined;
  307. /**
  308. * Same as {@link Object3D#customDepthMaterial}, but used with {@link PointLight}.
  309. *
  310. * Only relevant in context of {@link WebGLRenderer}.
  311. *
  312. * @type {(Material|undefined)}
  313. * @default undefined
  314. */
  315. this.customDistanceMaterial = undefined;
  316. /**
  317. * Whether the 3D object is supposed to be static or not. If set to `true`, it means
  318. * the 3D object is not going to be changed after the initial renderer. This includes
  319. * geometry and material settings. A static 3D object can be processed by the renderer
  320. * slightly faster since certain state checks can be bypassed.
  321. *
  322. * Only relevant in context of {@link WebGPURenderer}.
  323. *
  324. * @type {boolean}
  325. * @default false
  326. */
  327. this.static = false;
  328. /**
  329. * An object that can be used to store custom data about the 3D object. It
  330. * should not hold references to functions as these will not be cloned.
  331. *
  332. * @type {Object}
  333. */
  334. this.userData = {};
  335. /**
  336. * The pivot point for rotation and scale transformations.
  337. * When set, rotation and scale are applied around this point
  338. * instead of the object's origin.
  339. *
  340. * @type {?Vector3}
  341. * @default null
  342. */
  343. this.pivot = null;
  344. }
  345. /**
  346. * A callback that is executed immediately before a 3D object is rendered to a shadow map.
  347. *
  348. * @param {Renderer|WebGLRenderer} renderer - The renderer.
  349. * @param {Object3D} object - The 3D object.
  350. * @param {Camera} camera - The camera that is used to render the scene.
  351. * @param {Camera} shadowCamera - The shadow camera.
  352. * @param {BufferGeometry} geometry - The 3D object's geometry.
  353. * @param {Material} depthMaterial - The depth material.
  354. * @param {Object} group - The geometry group data.
  355. */
  356. onBeforeShadow( /* renderer, object, camera, shadowCamera, geometry, depthMaterial, group */ ) {}
  357. /**
  358. * A callback that is executed immediately after a 3D object is rendered to a shadow map.
  359. *
  360. * @param {Renderer|WebGLRenderer} renderer - The renderer.
  361. * @param {Object3D} object - The 3D object.
  362. * @param {Camera} camera - The camera that is used to render the scene.
  363. * @param {Camera} shadowCamera - The shadow camera.
  364. * @param {BufferGeometry} geometry - The 3D object's geometry.
  365. * @param {Material} depthMaterial - The depth material.
  366. * @param {Object} group - The geometry group data.
  367. */
  368. onAfterShadow( /* renderer, object, camera, shadowCamera, geometry, depthMaterial, group */ ) {}
  369. /**
  370. * A callback that is executed immediately before a 3D object is rendered.
  371. *
  372. * @param {Renderer|WebGLRenderer} renderer - The renderer.
  373. * @param {Object3D} object - The 3D object.
  374. * @param {Camera} camera - The camera that is used to render the scene.
  375. * @param {BufferGeometry} geometry - The 3D object's geometry.
  376. * @param {Material} material - The 3D object's material.
  377. * @param {Object} group - The geometry group data.
  378. */
  379. onBeforeRender( /* renderer, scene, camera, geometry, material, group */ ) {}
  380. /**
  381. * A callback that is executed immediately after a 3D object is rendered.
  382. *
  383. * @param {Renderer|WebGLRenderer} renderer - The renderer.
  384. * @param {Object3D} object - The 3D object.
  385. * @param {Camera} camera - The camera that is used to render the scene.
  386. * @param {BufferGeometry} geometry - The 3D object's geometry.
  387. * @param {Material} material - The 3D object's material.
  388. * @param {Object} group - The geometry group data.
  389. */
  390. onAfterRender( /* renderer, scene, camera, geometry, material, group */ ) {}
  391. /**
  392. * Applies the given transformation matrix to the object and updates the object's position,
  393. * rotation and scale.
  394. *
  395. * @param {Matrix4} matrix - The transformation matrix.
  396. */
  397. applyMatrix4( matrix ) {
  398. if ( this.matrixAutoUpdate ) this.updateMatrix();
  399. this.matrix.premultiply( matrix );
  400. this.matrix.decompose( this.position, this.quaternion, this.scale );
  401. }
  402. /**
  403. * Applies a rotation represented by given the quaternion to the 3D object.
  404. *
  405. * @param {Quaternion} q - The quaternion.
  406. * @return {Object3D} A reference to this instance.
  407. */
  408. applyQuaternion( q ) {
  409. this.quaternion.premultiply( q );
  410. return this;
  411. }
  412. /**
  413. * Sets the given rotation represented as an axis/angle couple to the 3D object.
  414. *
  415. * @param {Vector3} axis - The (normalized) axis vector.
  416. * @param {number} angle - The angle in radians.
  417. */
  418. setRotationFromAxisAngle( axis, angle ) {
  419. // assumes axis is normalized
  420. this.quaternion.setFromAxisAngle( axis, angle );
  421. }
  422. /**
  423. * Sets the given rotation represented as Euler angles to the 3D object.
  424. *
  425. * @param {Euler} euler - The Euler angles.
  426. */
  427. setRotationFromEuler( euler ) {
  428. this.quaternion.setFromEuler( euler, true );
  429. }
  430. /**
  431. * Sets the given rotation represented as rotation matrix to the 3D object.
  432. *
  433. * @param {Matrix4} m - Although a 4x4 matrix is expected, the upper 3x3 portion must be
  434. * a pure rotation matrix (i.e, unscaled).
  435. */
  436. setRotationFromMatrix( m ) {
  437. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  438. this.quaternion.setFromRotationMatrix( m );
  439. }
  440. /**
  441. * Sets the given rotation represented as a Quaternion to the 3D object.
  442. *
  443. * @param {Quaternion} q - The Quaternion
  444. */
  445. setRotationFromQuaternion( q ) {
  446. // assumes q is normalized
  447. this.quaternion.copy( q );
  448. }
  449. /**
  450. * Rotates the 3D object along an axis in local space.
  451. *
  452. * @param {Vector3} axis - The (normalized) axis vector.
  453. * @param {number} angle - The angle in radians.
  454. * @return {Object3D} A reference to this instance.
  455. */
  456. rotateOnAxis( axis, angle ) {
  457. // rotate object on axis in object space
  458. // axis is assumed to be normalized
  459. _q1.setFromAxisAngle( axis, angle );
  460. this.quaternion.multiply( _q1 );
  461. return this;
  462. }
  463. /**
  464. * Rotates the 3D object along an axis in world space.
  465. *
  466. * @param {Vector3} axis - The (normalized) axis vector.
  467. * @param {number} angle - The angle in radians.
  468. * @return {Object3D} A reference to this instance.
  469. */
  470. rotateOnWorldAxis( axis, angle ) {
  471. // rotate object on axis in world space
  472. // axis is assumed to be normalized
  473. // method assumes no rotated parent
  474. _q1.setFromAxisAngle( axis, angle );
  475. this.quaternion.premultiply( _q1 );
  476. return this;
  477. }
  478. /**
  479. * Rotates the 3D object around its X axis in local space.
  480. *
  481. * @param {number} angle - The angle in radians.
  482. * @return {Object3D} A reference to this instance.
  483. */
  484. rotateX( angle ) {
  485. return this.rotateOnAxis( _xAxis, angle );
  486. }
  487. /**
  488. * Rotates the 3D object around its Y axis in local space.
  489. *
  490. * @param {number} angle - The angle in radians.
  491. * @return {Object3D} A reference to this instance.
  492. */
  493. rotateY( angle ) {
  494. return this.rotateOnAxis( _yAxis, angle );
  495. }
  496. /**
  497. * Rotates the 3D object around its Z axis in local space.
  498. *
  499. * @param {number} angle - The angle in radians.
  500. * @return {Object3D} A reference to this instance.
  501. */
  502. rotateZ( angle ) {
  503. return this.rotateOnAxis( _zAxis, angle );
  504. }
  505. /**
  506. * Translate the 3D object by a distance along the given axis in local space.
  507. *
  508. * @param {Vector3} axis - The (normalized) axis vector.
  509. * @param {number} distance - The distance in world units.
  510. * @return {Object3D} A reference to this instance.
  511. */
  512. translateOnAxis( axis, distance ) {
  513. // translate object by distance along axis in object space
  514. // axis is assumed to be normalized
  515. _v1.copy( axis ).applyQuaternion( this.quaternion );
  516. this.position.add( _v1.multiplyScalar( distance ) );
  517. return this;
  518. }
  519. /**
  520. * Translate the 3D object by a distance along its X-axis in local space.
  521. *
  522. * @param {number} distance - The distance in world units.
  523. * @return {Object3D} A reference to this instance.
  524. */
  525. translateX( distance ) {
  526. return this.translateOnAxis( _xAxis, distance );
  527. }
  528. /**
  529. * Translate the 3D object by a distance along its Y-axis in local space.
  530. *
  531. * @param {number} distance - The distance in world units.
  532. * @return {Object3D} A reference to this instance.
  533. */
  534. translateY( distance ) {
  535. return this.translateOnAxis( _yAxis, distance );
  536. }
  537. /**
  538. * Translate the 3D object by a distance along its Z-axis in local space.
  539. *
  540. * @param {number} distance - The distance in world units.
  541. * @return {Object3D} A reference to this instance.
  542. */
  543. translateZ( distance ) {
  544. return this.translateOnAxis( _zAxis, distance );
  545. }
  546. /**
  547. * Converts the given vector from this 3D object's local space to world space.
  548. *
  549. * @param {Vector3} vector - The vector to convert.
  550. * @return {Vector3} The converted vector.
  551. */
  552. localToWorld( vector ) {
  553. this.updateWorldMatrix( true, false );
  554. return vector.applyMatrix4( this.matrixWorld );
  555. }
  556. /**
  557. * Converts the given vector from this 3D object's world space to local space.
  558. *
  559. * @param {Vector3} vector - The vector to convert.
  560. * @return {Vector3} The converted vector.
  561. */
  562. worldToLocal( vector ) {
  563. this.updateWorldMatrix( true, false );
  564. return vector.applyMatrix4( _m1.copy( this.matrixWorld ).invert() );
  565. }
  566. /**
  567. * Rotates the object to face a point in world space.
  568. *
  569. * This method does not support objects having non-uniformly-scaled parent(s).
  570. *
  571. * @param {number|Vector3} x - The x coordinate in world space. Alternatively, a vector representing a position in world space
  572. * @param {number} [y] - The y coordinate in world space.
  573. * @param {number} [z] - The z coordinate in world space.
  574. */
  575. lookAt( x, y, z ) {
  576. // This method does not support objects having non-uniformly-scaled parent(s)
  577. if ( x.isVector3 ) {
  578. _target.copy( x );
  579. } else {
  580. _target.set( x, y, z );
  581. }
  582. const parent = this.parent;
  583. this.updateWorldMatrix( true, false );
  584. _position.setFromMatrixPosition( this.matrixWorld );
  585. if ( this.isCamera || this.isLight ) {
  586. _m1.lookAt( _position, _target, this.up );
  587. } else {
  588. _m1.lookAt( _target, _position, this.up );
  589. }
  590. this.quaternion.setFromRotationMatrix( _m1 );
  591. if ( parent ) {
  592. _m1.extractRotation( parent.matrixWorld );
  593. _q1.setFromRotationMatrix( _m1 );
  594. this.quaternion.premultiply( _q1.invert() );
  595. }
  596. }
  597. /**
  598. * Adds the given 3D object as a child to this 3D object. An arbitrary number of
  599. * objects may be added. Any current parent on an object passed in here will be
  600. * removed, since an object can have at most one parent.
  601. *
  602. * @fires Object3D#added
  603. * @fires Object3D#childadded
  604. * @param {Object3D} object - The 3D object to add.
  605. * @return {Object3D} A reference to this instance.
  606. */
  607. add( object ) {
  608. if ( arguments.length > 1 ) {
  609. for ( let i = 0; i < arguments.length; i ++ ) {
  610. this.add( arguments[ i ] );
  611. }
  612. return this;
  613. }
  614. if ( object === this ) {
  615. error( 'Object3D.add: object can\'t be added as a child of itself.', object );
  616. return this;
  617. }
  618. if ( object && object.isObject3D ) {
  619. object.removeFromParent();
  620. object.parent = this;
  621. this.children.push( object );
  622. object.dispatchEvent( _addedEvent );
  623. _childaddedEvent.child = object;
  624. this.dispatchEvent( _childaddedEvent );
  625. _childaddedEvent.child = null;
  626. } else {
  627. error( 'Object3D.add: object not an instance of THREE.Object3D.', object );
  628. }
  629. return this;
  630. }
  631. /**
  632. * Removes the given 3D object as child from this 3D object.
  633. * An arbitrary number of objects may be removed.
  634. *
  635. * @fires Object3D#removed
  636. * @fires Object3D#childremoved
  637. * @param {Object3D} object - The 3D object to remove.
  638. * @return {Object3D} A reference to this instance.
  639. */
  640. remove( object ) {
  641. if ( arguments.length > 1 ) {
  642. for ( let i = 0; i < arguments.length; i ++ ) {
  643. this.remove( arguments[ i ] );
  644. }
  645. return this;
  646. }
  647. const index = this.children.indexOf( object );
  648. if ( index !== - 1 ) {
  649. object.parent = null;
  650. this.children.splice( index, 1 );
  651. object.dispatchEvent( _removedEvent );
  652. _childremovedEvent.child = object;
  653. this.dispatchEvent( _childremovedEvent );
  654. _childremovedEvent.child = null;
  655. }
  656. return this;
  657. }
  658. /**
  659. * Removes this 3D object from its current parent.
  660. *
  661. * @fires Object3D#removed
  662. * @fires Object3D#childremoved
  663. * @return {Object3D} A reference to this instance.
  664. */
  665. removeFromParent() {
  666. const parent = this.parent;
  667. if ( parent !== null ) {
  668. parent.remove( this );
  669. }
  670. return this;
  671. }
  672. /**
  673. * Removes all child objects.
  674. *
  675. * @fires Object3D#removed
  676. * @fires Object3D#childremoved
  677. * @return {Object3D} A reference to this instance.
  678. */
  679. clear() {
  680. return this.remove( ... this.children );
  681. }
  682. /**
  683. * Adds the given 3D object as a child of this 3D object, while maintaining the object's world
  684. * transform. This method does not support scene graphs having non-uniformly-scaled nodes(s).
  685. *
  686. * @fires Object3D#added
  687. * @fires Object3D#childadded
  688. * @param {Object3D} object - The 3D object to attach.
  689. * @return {Object3D} A reference to this instance.
  690. */
  691. attach( object ) {
  692. // adds object as a child of this, while maintaining the object's world transform
  693. // Note: This method does not support scene graphs having non-uniformly-scaled nodes(s)
  694. this.updateWorldMatrix( true, false );
  695. _m1.copy( this.matrixWorld ).invert();
  696. if ( object.parent !== null ) {
  697. object.parent.updateWorldMatrix( true, false );
  698. _m1.multiply( object.parent.matrixWorld );
  699. }
  700. object.applyMatrix4( _m1 );
  701. object.removeFromParent();
  702. object.parent = this;
  703. this.children.push( object );
  704. object.updateWorldMatrix( false, true );
  705. object.dispatchEvent( _addedEvent );
  706. _childaddedEvent.child = object;
  707. this.dispatchEvent( _childaddedEvent );
  708. _childaddedEvent.child = null;
  709. return this;
  710. }
  711. /**
  712. * Searches through the 3D object and its children, starting with the 3D object
  713. * itself, and returns the first with a matching ID.
  714. *
  715. * @param {number} id - The id.
  716. * @return {Object3D|undefined} The found 3D object. Returns `undefined` if no 3D object has been found.
  717. */
  718. getObjectById( id ) {
  719. return this.getObjectByProperty( 'id', id );
  720. }
  721. /**
  722. * Searches through the 3D object and its children, starting with the 3D object
  723. * itself, and returns the first with a matching name.
  724. *
  725. * @param {string} name - The name.
  726. * @return {Object3D|undefined} The found 3D object. Returns `undefined` if no 3D object has been found.
  727. */
  728. getObjectByName( name ) {
  729. return this.getObjectByProperty( 'name', name );
  730. }
  731. /**
  732. * Searches through the 3D object and its children, starting with the 3D object
  733. * itself, and returns the first with a matching property value.
  734. *
  735. * @param {string} name - The name of the property.
  736. * @param {any} value - The value.
  737. * @return {Object3D|undefined} The found 3D object. Returns `undefined` if no 3D object has been found.
  738. */
  739. getObjectByProperty( name, value ) {
  740. if ( this[ name ] === value ) return this;
  741. for ( let i = 0, l = this.children.length; i < l; i ++ ) {
  742. const child = this.children[ i ];
  743. const object = child.getObjectByProperty( name, value );
  744. if ( object !== undefined ) {
  745. return object;
  746. }
  747. }
  748. return undefined;
  749. }
  750. /**
  751. * Searches through the 3D object and its children, starting with the 3D object
  752. * itself, and returns all 3D objects with a matching property value.
  753. *
  754. * @param {string} name - The name of the property.
  755. * @param {any} value - The value.
  756. * @param {Array<Object3D>} result - The method stores the result in this array.
  757. * @return {Array<Object3D>} The found 3D objects.
  758. */
  759. getObjectsByProperty( name, value, result = [] ) {
  760. if ( this[ name ] === value ) result.push( this );
  761. const children = this.children;
  762. for ( let i = 0, l = children.length; i < l; i ++ ) {
  763. children[ i ].getObjectsByProperty( name, value, result );
  764. }
  765. return result;
  766. }
  767. /**
  768. * Returns a vector representing the position of the 3D object in world space.
  769. *
  770. * @param {Vector3} target - The target vector the result is stored to.
  771. * @return {Vector3} The 3D object's position in world space.
  772. */
  773. getWorldPosition( target ) {
  774. this.updateWorldMatrix( true, false );
  775. return target.setFromMatrixPosition( this.matrixWorld );
  776. }
  777. /**
  778. * Returns a Quaternion representing the position of the 3D object in world space.
  779. *
  780. * @param {Quaternion} target - The target Quaternion the result is stored to.
  781. * @return {Quaternion} The 3D object's rotation in world space.
  782. */
  783. getWorldQuaternion( target ) {
  784. this.updateWorldMatrix( true, false );
  785. this.matrixWorld.decompose( _position, target, _scale );
  786. return target;
  787. }
  788. /**
  789. * Returns a vector representing the scale of the 3D object in world space.
  790. *
  791. * @param {Vector3} target - The target vector the result is stored to.
  792. * @return {Vector3} The 3D object's scale in world space.
  793. */
  794. getWorldScale( target ) {
  795. this.updateWorldMatrix( true, false );
  796. this.matrixWorld.decompose( _position, _quaternion, target );
  797. return target;
  798. }
  799. /**
  800. * Returns a vector representing the ("look") direction of the 3D object in world space.
  801. *
  802. * @param {Vector3} target - The target vector the result is stored to.
  803. * @return {Vector3} The 3D object's direction in world space.
  804. */
  805. getWorldDirection( target ) {
  806. this.updateWorldMatrix( true, false );
  807. const e = this.matrixWorld.elements;
  808. return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize();
  809. }
  810. /**
  811. * Abstract method to get intersections between a casted ray and this
  812. * 3D object. Renderable 3D objects such as {@link Mesh}, {@link Line} or {@link Points}
  813. * implement this method in order to use raycasting.
  814. *
  815. * @abstract
  816. * @param {Raycaster} raycaster - The raycaster.
  817. * @param {Array<Object>} intersects - An array holding the result of the method.
  818. */
  819. raycast( /* raycaster, intersects */ ) {}
  820. /**
  821. * Executes the callback on this 3D object and all descendants.
  822. *
  823. * Note: Modifying the scene graph inside the callback is discouraged.
  824. *
  825. * @param {Function} callback - A callback function that allows to process the current 3D object.
  826. */
  827. traverse( callback ) {
  828. callback( this );
  829. const children = this.children;
  830. for ( let i = 0, l = children.length; i < l; i ++ ) {
  831. children[ i ].traverse( callback );
  832. }
  833. }
  834. /**
  835. * Like {@link Object3D#traverse}, but the callback will only be executed for visible 3D objects.
  836. * Descendants of invisible 3D objects are not traversed.
  837. *
  838. * Note: Modifying the scene graph inside the callback is discouraged.
  839. *
  840. * @param {Function} callback - A callback function that allows to process the current 3D object.
  841. */
  842. traverseVisible( callback ) {
  843. if ( this.visible === false ) return;
  844. callback( this );
  845. const children = this.children;
  846. for ( let i = 0, l = children.length; i < l; i ++ ) {
  847. children[ i ].traverseVisible( callback );
  848. }
  849. }
  850. /**
  851. * Like {@link Object3D#traverse}, but the callback will only be executed for all ancestors.
  852. *
  853. * Note: Modifying the scene graph inside the callback is discouraged.
  854. *
  855. * @param {Function} callback - A callback function that allows to process the current 3D object.
  856. */
  857. traverseAncestors( callback ) {
  858. const parent = this.parent;
  859. if ( parent !== null ) {
  860. callback( parent );
  861. parent.traverseAncestors( callback );
  862. }
  863. }
  864. /**
  865. * Updates the transformation matrix in local space by computing it from the current
  866. * position, rotation and scale values.
  867. */
  868. updateMatrix() {
  869. this.matrix.compose( this.position, this.quaternion, this.scale );
  870. const pivot = this.pivot;
  871. if ( pivot !== null ) {
  872. const px = pivot.x, py = pivot.y, pz = pivot.z;
  873. const te = this.matrix.elements;
  874. te[ 12 ] += px - te[ 0 ] * px - te[ 4 ] * py - te[ 8 ] * pz;
  875. te[ 13 ] += py - te[ 1 ] * px - te[ 5 ] * py - te[ 9 ] * pz;
  876. te[ 14 ] += pz - te[ 2 ] * px - te[ 6 ] * py - te[ 10 ] * pz;
  877. }
  878. this.matrixWorldNeedsUpdate = true;
  879. }
  880. /**
  881. * Updates the transformation matrix in world space of this 3D objects and its descendants.
  882. *
  883. * To ensure correct results, this method also recomputes the 3D object's transformation matrix in
  884. * local space. The computation of the local and world matrix can be controlled with the
  885. * {@link Object3D#matrixAutoUpdate} and {@link Object3D#matrixWorldAutoUpdate} flags which are both
  886. * `true` by default. Set these flags to `false` if you need more control over the update matrix process.
  887. *
  888. * @param {boolean} [force=false] - When set to `true`, a recomputation of world matrices is forced even
  889. * when {@link Object3D#matrixWorldNeedsUpdate} is `false`.
  890. */
  891. updateMatrixWorld( force ) {
  892. if ( this.matrixAutoUpdate ) this.updateMatrix();
  893. if ( this.matrixWorldNeedsUpdate || force ) {
  894. if ( this.matrixWorldAutoUpdate === true ) {
  895. if ( this.parent === null ) {
  896. this.matrixWorld.copy( this.matrix );
  897. } else {
  898. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  899. }
  900. }
  901. this.matrixWorldNeedsUpdate = false;
  902. force = true;
  903. }
  904. // make sure descendants are updated if required
  905. const children = this.children;
  906. for ( let i = 0, l = children.length; i < l; i ++ ) {
  907. const child = children[ i ];
  908. child.updateMatrixWorld( force );
  909. }
  910. }
  911. /**
  912. * An alternative version of {@link Object3D#updateMatrixWorld} with more control over the
  913. * update of ancestor and descendant nodes.
  914. *
  915. * @param {boolean} [updateParents=false] Whether ancestor nodes should be updated or not.
  916. * @param {boolean} [updateChildren=false] Whether descendant nodes should be updated or not.
  917. * @param {boolean} [force=false] - When set to `true`, a recomputation of world matrices is forced even
  918. * when {@link Object3D#matrixWorldNeedsUpdate} is `false`.
  919. */
  920. updateWorldMatrix( updateParents, updateChildren, force = false ) {
  921. const parent = this.parent;
  922. if ( updateParents === true && parent !== null ) {
  923. parent.updateWorldMatrix( true, false );
  924. }
  925. if ( this.matrixAutoUpdate ) this.updateMatrix();
  926. if ( this.matrixWorldNeedsUpdate || force ) {
  927. if ( this.matrixWorldAutoUpdate === true ) {
  928. if ( this.parent === null ) {
  929. this.matrixWorld.copy( this.matrix );
  930. } else {
  931. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  932. }
  933. }
  934. this.matrixWorldNeedsUpdate = false;
  935. force = true;
  936. }
  937. // make sure descendants are updated
  938. if ( updateChildren === true ) {
  939. const children = this.children;
  940. for ( let i = 0, l = children.length; i < l; i ++ ) {
  941. const child = children[ i ];
  942. child.updateWorldMatrix( false, true, force );
  943. }
  944. }
  945. }
  946. /**
  947. * Serializes the 3D object into JSON.
  948. *
  949. * @param {?(Object|string)} meta - An optional value holding meta information about the serialization.
  950. * @return {Object} A JSON object representing the serialized 3D object.
  951. * @see {@link ObjectLoader#parse}
  952. */
  953. toJSON( meta ) {
  954. // meta is a string when called from JSON.stringify
  955. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  956. const output = {};
  957. // meta is a hash used to collect geometries, materials.
  958. // not providing it implies that this is the root object
  959. // being serialized.
  960. if ( isRootObject ) {
  961. // initialize meta obj
  962. meta = {
  963. geometries: {},
  964. materials: {},
  965. textures: {},
  966. images: {},
  967. shapes: {},
  968. skeletons: {},
  969. animations: {},
  970. nodes: {}
  971. };
  972. output.metadata = {
  973. version: 4.7,
  974. type: 'Object',
  975. generator: 'Object3D.toJSON'
  976. };
  977. }
  978. // standard Object3D serialization
  979. const object = {};
  980. object.uuid = this.uuid;
  981. object.type = this.type;
  982. if ( this.name !== '' ) object.name = this.name;
  983. if ( this.castShadow === true ) object.castShadow = true;
  984. if ( this.receiveShadow === true ) object.receiveShadow = true;
  985. if ( this.visible === false ) object.visible = false;
  986. if ( this.frustumCulled === false ) object.frustumCulled = false;
  987. if ( this.renderOrder !== 0 ) object.renderOrder = this.renderOrder;
  988. if ( this.static !== false ) object.static = this.static;
  989. if ( Object.keys( this.userData ).length > 0 ) object.userData = this.userData;
  990. object.layers = this.layers.mask;
  991. object.matrix = this.matrix.toArray();
  992. object.up = this.up.toArray();
  993. if ( this.pivot !== null ) object.pivot = this.pivot.toArray();
  994. if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
  995. if ( this.morphTargetDictionary !== undefined ) object.morphTargetDictionary = Object.assign( {}, this.morphTargetDictionary );
  996. if ( this.morphTargetInfluences !== undefined ) object.morphTargetInfluences = this.morphTargetInfluences.slice();
  997. // object specific properties
  998. if ( this.isInstancedMesh ) {
  999. object.type = 'InstancedMesh';
  1000. object.count = this.count;
  1001. object.instanceMatrix = this.instanceMatrix.toJSON();
  1002. if ( this.instanceColor !== null ) object.instanceColor = this.instanceColor.toJSON();
  1003. }
  1004. if ( this.isBatchedMesh ) {
  1005. object.type = 'BatchedMesh';
  1006. object.perObjectFrustumCulled = this.perObjectFrustumCulled;
  1007. object.sortObjects = this.sortObjects;
  1008. object.drawRanges = this._drawRanges;
  1009. object.reservedRanges = this._reservedRanges;
  1010. object.geometryInfo = this._geometryInfo.map( info => ( {
  1011. ...info,
  1012. boundingBox: info.boundingBox ? info.boundingBox.toJSON() : undefined,
  1013. boundingSphere: info.boundingSphere ? info.boundingSphere.toJSON() : undefined
  1014. } ) );
  1015. object.instanceInfo = this._instanceInfo.map( info => ( { ...info } ) );
  1016. object.availableInstanceIds = this._availableInstanceIds.slice();
  1017. object.availableGeometryIds = this._availableGeometryIds.slice();
  1018. object.nextIndexStart = this._nextIndexStart;
  1019. object.nextVertexStart = this._nextVertexStart;
  1020. object.geometryCount = this._geometryCount;
  1021. object.maxInstanceCount = this._maxInstanceCount;
  1022. object.maxVertexCount = this._maxVertexCount;
  1023. object.maxIndexCount = this._maxIndexCount;
  1024. object.geometryInitialized = this._geometryInitialized;
  1025. object.matricesTexture = this._matricesTexture.toJSON( meta );
  1026. object.indirectTexture = this._indirectTexture.toJSON( meta );
  1027. if ( this._colorsTexture !== null ) {
  1028. object.colorsTexture = this._colorsTexture.toJSON( meta );
  1029. }
  1030. if ( this.boundingSphere !== null ) {
  1031. object.boundingSphere = this.boundingSphere.toJSON();
  1032. }
  1033. if ( this.boundingBox !== null ) {
  1034. object.boundingBox = this.boundingBox.toJSON();
  1035. }
  1036. }
  1037. //
  1038. function serialize( library, element ) {
  1039. if ( library[ element.uuid ] === undefined ) {
  1040. library[ element.uuid ] = element.toJSON( meta );
  1041. }
  1042. return element.uuid;
  1043. }
  1044. if ( this.isScene ) {
  1045. if ( this.background ) {
  1046. if ( this.background.isColor ) {
  1047. object.background = this.background.toJSON();
  1048. } else if ( this.background.isTexture ) {
  1049. object.background = this.background.toJSON( meta ).uuid;
  1050. }
  1051. }
  1052. if ( this.environment && this.environment.isTexture && this.environment.isRenderTargetTexture !== true ) {
  1053. object.environment = this.environment.toJSON( meta ).uuid;
  1054. }
  1055. } else if ( this.isMesh || this.isLine || this.isPoints ) {
  1056. object.geometry = serialize( meta.geometries, this.geometry );
  1057. const parameters = this.geometry.parameters;
  1058. if ( parameters !== undefined && parameters.shapes !== undefined ) {
  1059. const shapes = parameters.shapes;
  1060. if ( Array.isArray( shapes ) ) {
  1061. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  1062. const shape = shapes[ i ];
  1063. serialize( meta.shapes, shape );
  1064. }
  1065. } else {
  1066. serialize( meta.shapes, shapes );
  1067. }
  1068. }
  1069. }
  1070. if ( this.isSkinnedMesh ) {
  1071. object.bindMode = this.bindMode;
  1072. object.bindMatrix = this.bindMatrix.toArray();
  1073. if ( this.skeleton !== undefined ) {
  1074. serialize( meta.skeletons, this.skeleton );
  1075. object.skeleton = this.skeleton.uuid;
  1076. }
  1077. }
  1078. if ( this.material !== undefined ) {
  1079. if ( Array.isArray( this.material ) ) {
  1080. const uuids = [];
  1081. for ( let i = 0, l = this.material.length; i < l; i ++ ) {
  1082. uuids.push( serialize( meta.materials, this.material[ i ] ) );
  1083. }
  1084. object.material = uuids;
  1085. } else {
  1086. object.material = serialize( meta.materials, this.material );
  1087. }
  1088. }
  1089. //
  1090. if ( this.children.length > 0 ) {
  1091. object.children = [];
  1092. for ( let i = 0; i < this.children.length; i ++ ) {
  1093. object.children.push( this.children[ i ].toJSON( meta ).object );
  1094. }
  1095. }
  1096. //
  1097. if ( this.animations.length > 0 ) {
  1098. object.animations = [];
  1099. for ( let i = 0; i < this.animations.length; i ++ ) {
  1100. const animation = this.animations[ i ];
  1101. object.animations.push( serialize( meta.animations, animation ) );
  1102. }
  1103. }
  1104. if ( isRootObject ) {
  1105. const geometries = extractFromCache( meta.geometries );
  1106. const materials = extractFromCache( meta.materials );
  1107. const textures = extractFromCache( meta.textures );
  1108. const images = extractFromCache( meta.images );
  1109. const shapes = extractFromCache( meta.shapes );
  1110. const skeletons = extractFromCache( meta.skeletons );
  1111. const animations = extractFromCache( meta.animations );
  1112. const nodes = extractFromCache( meta.nodes );
  1113. if ( geometries.length > 0 ) output.geometries = geometries;
  1114. if ( materials.length > 0 ) output.materials = materials;
  1115. if ( textures.length > 0 ) output.textures = textures;
  1116. if ( images.length > 0 ) output.images = images;
  1117. if ( shapes.length > 0 ) output.shapes = shapes;
  1118. if ( skeletons.length > 0 ) output.skeletons = skeletons;
  1119. if ( animations.length > 0 ) output.animations = animations;
  1120. if ( nodes.length > 0 ) output.nodes = nodes;
  1121. }
  1122. output.object = object;
  1123. return output;
  1124. // extract data from the cache hash
  1125. // remove metadata on each item
  1126. // and return as array
  1127. function extractFromCache( cache ) {
  1128. const values = [];
  1129. for ( const key in cache ) {
  1130. const data = cache[ key ];
  1131. delete data.metadata;
  1132. values.push( data );
  1133. }
  1134. return values;
  1135. }
  1136. }
  1137. /**
  1138. * Returns a new 3D object with copied values from this instance.
  1139. *
  1140. * @param {boolean} [recursive=true] - When set to `true`, descendants of the 3D object are also cloned.
  1141. * @return {Object3D} A clone of this instance.
  1142. */
  1143. clone( recursive ) {
  1144. return new this.constructor().copy( this, recursive );
  1145. }
  1146. /**
  1147. * Copies the values of the given 3D object to this instance.
  1148. *
  1149. * @param {Object3D} source - The 3D object to copy.
  1150. * @param {boolean} [recursive=true] - When set to `true`, descendants of the 3D object are cloned.
  1151. * @return {Object3D} A reference to this instance.
  1152. */
  1153. copy( source, recursive = true ) {
  1154. this.name = source.name;
  1155. this.up.copy( source.up );
  1156. this.position.copy( source.position );
  1157. this.rotation.order = source.rotation.order;
  1158. this.quaternion.copy( source.quaternion );
  1159. this.scale.copy( source.scale );
  1160. this.pivot = ( source.pivot !== null ) ? source.pivot.clone() : null;
  1161. this.matrix.copy( source.matrix );
  1162. this.matrixWorld.copy( source.matrixWorld );
  1163. this.matrixAutoUpdate = source.matrixAutoUpdate;
  1164. this.matrixWorldAutoUpdate = source.matrixWorldAutoUpdate;
  1165. this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  1166. this.layers.mask = source.layers.mask;
  1167. this.visible = source.visible;
  1168. this.castShadow = source.castShadow;
  1169. this.receiveShadow = source.receiveShadow;
  1170. this.frustumCulled = source.frustumCulled;
  1171. this.renderOrder = source.renderOrder;
  1172. this.static = source.static;
  1173. this.animations = source.animations.slice();
  1174. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  1175. if ( recursive === true ) {
  1176. for ( let i = 0; i < source.children.length; i ++ ) {
  1177. const child = source.children[ i ];
  1178. this.add( child.clone() );
  1179. }
  1180. }
  1181. return this;
  1182. }
  1183. }
  1184. /**
  1185. * The default up direction for objects, also used as the default
  1186. * position for {@link DirectionalLight} and {@link HemisphereLight}.
  1187. *
  1188. * @static
  1189. * @type {Vector3}
  1190. * @default (0,1,0)
  1191. */
  1192. Object3D.DEFAULT_UP = /*@__PURE__*/ new Vector3( 0, 1, 0 );
  1193. /**
  1194. * The default setting for {@link Object3D#matrixAutoUpdate} for
  1195. * newly created 3D objects.
  1196. *
  1197. * @static
  1198. * @type {boolean}
  1199. * @default true
  1200. */
  1201. Object3D.DEFAULT_MATRIX_AUTO_UPDATE = true;
  1202. /**
  1203. * The default setting for {@link Object3D#matrixWorldAutoUpdate} for
  1204. * newly created 3D objects.
  1205. *
  1206. * @static
  1207. * @type {boolean}
  1208. * @default true
  1209. */
  1210. Object3D.DEFAULT_MATRIX_WORLD_AUTO_UPDATE = true;
  1211. export { Object3D };
粤ICP备19079148号