瀏覽代碼

Core: Use `structuredClone()` when available. (#25535)

* copy with structuredClone when available

* copy userData on BufferGeometry, Material and Texture

* add deepClone function
oyar 2 年之前
父節點
當前提交
cfe9eaf394
共有 5 個文件被更改,包括 21 次插入6 次删除
  1. 2 2
      src/core/BufferGeometry.js
  2. 2 1
      src/core/Object3D.js
  3. 2 1
      src/materials/Material.js
  4. 2 1
      src/textures/Texture.js
  5. 13 1
      src/utils.js

+ 2 - 2
src/core/BufferGeometry.js

@@ -8,7 +8,7 @@ import { Object3D } from './Object3D.js';
 import { Matrix4 } from '../math/Matrix4.js';
 import { Matrix3 } from '../math/Matrix3.js';
 import * as MathUtils from '../math/MathUtils.js';
-import { arrayNeedsUint32 } from '../utils.js';
+import { arrayNeedsUint32, deepClone } from '../utils.js';
 
 let _id = 0;
 
@@ -1071,7 +1071,7 @@ class BufferGeometry extends EventDispatcher {
 
 		// user data
 
-		this.userData = source.userData;
+		this.userData = deepClone( source.userData );
 
 		return this;
 

+ 2 - 1
src/core/Object3D.js

@@ -6,6 +6,7 @@ import { Euler } from '../math/Euler.js';
 import { Layers } from './Layers.js';
 import { Matrix3 } from '../math/Matrix3.js';
 import * as MathUtils from '../math/MathUtils.js';
+import { deepClone } from '../utils.js';
 
 let _object3DId = 0;
 
@@ -944,7 +945,7 @@ class Object3D extends EventDispatcher {
 		this.frustumCulled = source.frustumCulled;
 		this.renderOrder = source.renderOrder;
 
-		this.userData = JSON.parse( JSON.stringify( source.userData ) );
+		this.userData = deepClone( source.userData );
 
 		if ( recursive === true ) {
 

+ 2 - 1
src/materials/Material.js

@@ -1,6 +1,7 @@
 import { EventDispatcher } from '../core/EventDispatcher.js';
 import { FrontSide, NormalBlending, LessEqualDepth, AddEquation, OneMinusSrcAlphaFactor, SrcAlphaFactor, AlwaysStencilFunc, KeepStencilOp } from '../constants.js';
 import * as MathUtils from '../math/MathUtils.js';
+import { deepClone } from '../utils.js';
 
 let materialId = 0;
 
@@ -473,7 +474,7 @@ class Material extends EventDispatcher {
 
 		this.toneMapped = source.toneMapped;
 
-		this.userData = JSON.parse( JSON.stringify( source.userData ) );
+		this.userData = deepClone( source.userData );
 
 		return this;
 

+ 2 - 1
src/textures/Texture.js

@@ -14,6 +14,7 @@ import * as MathUtils from '../math/MathUtils.js';
 import { Vector2 } from '../math/Vector2.js';
 import { Matrix3 } from '../math/Matrix3.js';
 import { Source } from './Source.js';
+import { deepClone } from '../utils.js';
 
 let textureId = 0;
 
@@ -136,7 +137,7 @@ class Texture extends EventDispatcher {
 		this.unpackAlignment = source.unpackAlignment;
 		this.encoding = source.encoding;
 
-		this.userData = JSON.parse( JSON.stringify( source.userData ) );
+		this.userData = deepClone( source.userData );
 
 		this.needsUpdate = true;
 

+ 13 - 1
src/utils.js

@@ -68,4 +68,16 @@ function createElementNS( name ) {
 
 }
 
-export { arrayMin, arrayMax, arrayNeedsUint32, getTypedArray, createElementNS };
+function deepClone( value ) {
+
+	if ( typeof structuredClone === 'function' ) {
+
+		return structuredClone( value );
+
+	}
+
+	return JSON.parse( JSON.stringify( value ) );
+
+}
+
+export { arrayMin, arrayMax, arrayNeedsUint32, getTypedArray, createElementNS, deepClone };