Просмотр исходного кода

NodeUtils: Add `getDataFromObject()` (#30171)

sunag 1 год назад
Родитель
Сommit
083f23323c

+ 2 - 1
src/nodes/accessors/SkinningNode.js

@@ -9,6 +9,7 @@ import { positionLocal, positionPrevious } from './Position.js';
 import { tangentLocal } from './Tangent.js';
 import { uniform } from '../core/UniformNode.js';
 import { buffer } from './BufferNode.js';
+import { getDataFromObject } from '../core/NodeUtils.js';
 
 /** @module SkinningNode **/
 
@@ -218,7 +219,7 @@ class SkinningNode extends Node {
 
 		const mrt = builder.renderer.getMRT();
 
-		return ( mrt && mrt.has( 'velocity' ) ) || builder.object.userData.useVelocity === true;
+		return ( mrt && mrt.has( 'velocity' ) ) || getDataFromObject( builder.object ).useVelocity === true;
 
 	}
 

+ 23 - 0
src/nodes/core/NodeUtils.js

@@ -168,6 +168,8 @@ const typeFromLength = /*@__PURE__*/ new Map( [
 	[ 16, 'mat4' ]
 ] );
 
+const dataFromObject = /*@__PURE__*/ new WeakMap();
+
 /**
  * Returns the data type for the given the length.
  *
@@ -334,6 +336,27 @@ export function getValueFromType( type, ...params ) {
 
 }
 
+/**
+ * Gets the object data that can be shared between different rendering steps.
+ *
+ * @param {Object} object - The object to get the data for.
+ * @return {Object} The object data.
+ */
+export function getDataFromObject( object ) {
+
+	let data = dataFromObject.get( object );
+
+	if ( data === undefined ) {
+
+		data = {};
+		dataFromObject.set( object, data );
+
+	}
+
+	return data;
+
+}
+
 /**
  * Converts the given array buffer to a Base64 string.
  *

+ 2 - 2
src/nodes/lighting/ShadowNode.js

@@ -17,6 +17,7 @@ import { viewZToLogarithmicDepth } from '../display/ViewportDepthNode.js';
 import { objectPosition } from '../accessors/Object3DNode.js';
 import { lightShadowMatrix } from '../accessors/Lights.js';
 import { resetRendererAndSceneState, restoreRendererAndSceneState } from '../../renderers/common/RendererUtils.js';
+import { getDataFromObject } from '../core/NodeUtils.js';
 
 /** @module ShadowNode **/
 
@@ -671,8 +672,7 @@ class ShadowNode extends ShadowBaseNode {
 
 				if ( useVelocity ) {
 
-					object.userData = object.userData || {};
-					object.userData.useVelocity = true;
+					getDataFromObject( object ).useVelocity = true;
 
 				}
 

粤ICP备19079148号