瀏覽代碼

add ModelNode.NORMAL

sunag 4 年之前
父節點
當前提交
500f0c2e2d
共有 1 個文件被更改,包括 63 次插入4 次删除
  1. 63 4
      examples/jsm/renderers/nodes/accessors/ModelNode.js

+ 63 - 4
examples/jsm/renderers/nodes/accessors/ModelNode.js

@@ -1,20 +1,38 @@
 import Node from '../core/Node.js';
 import Matrix4Node from '../inputs/Matrix4Node.js';
+import Matrix3Node from '../inputs/Matrix3Node.js';
 import { NodeUpdateType } from '../core/constants.js';
 
 class ModelNode extends Node {
 
 	static VIEW = 'view';
+	static NORMAL = 'normal';
 
 	constructor( scope = ModelNode.VIEW ) {
 
-		super( 'mat4' );
+		super();
 
 		this.scope = scope;
 
 		this.updateType = NodeUpdateType.Object;
 
-		this._inputNode = new Matrix4Node( null );
+		this._inputNode = null;
+
+	}
+
+	getType() {
+
+		const scope = this.scope;
+
+		if ( scope === ModelNode.VIEW ) {
+
+			return 'mat4';
+
+		} else if ( scope === ModelNode.NORMAL ) {
+
+			return 'mat3';
+
+		}
 
 	}
 
@@ -22,14 +40,55 @@ class ModelNode extends Node {
 
 		const object = frame.object;
 		const inputNode = this._inputNode;
+		const scope = this.scope;
+
+		if ( scope === ModelNode.VIEW ) {
+
+			inputNode.value = object.modelViewMatrix;
+
+		} else if ( scope === ModelNode.NORMAL ) {
 
-		inputNode.value = object.modelViewMatrix;
+			inputNode.value = object.normalMatrix;
+
+		}
 
 	}
 
 	generate( builder, output ) {
 
-		return this._inputNode.build( builder, output );
+		const nodeData = builder.getDataFromNode( this );
+
+		let inputNode = this._inputNode;
+
+		if ( nodeData.inputNode === undefined ) {
+
+			const scope = this.scope;
+
+			if ( scope === ModelNode.VIEW ) {
+
+				if ( inputNode === null || inputNode.isMatrix4Node !== true ) {
+
+					inputNode = new Matrix4Node( null );
+
+				}
+
+			} else if ( scope === ModelNode.NORMAL ) {
+
+				if ( inputNode === null || inputNode.isMatrix3Node !== true ) {
+
+					inputNode = new Matrix3Node( null );
+
+				}
+
+			}
+
+			this._inputNode = inputNode;
+
+			nodeData.inputNode = inputNode;
+
+		}
+
+		return inputNode.build( builder, output );
 
 	}