Browse Source

add Matrix3Node and optimize UVTransformNode

sunag 7 năm trước cách đây
mục cha
commit
5f4b4925d8

+ 2 - 1
examples/js/loaders/NodeMaterialLoader.js

@@ -214,6 +214,7 @@ Object.assign( THREE.NodeMaterialLoader.prototype, {
 
 					break;
 
+				case "Matrix3Node":
 				case "Matrix4Node":
 
 					object.value.fromArray( node.elements );
@@ -321,7 +322,7 @@ Object.assign( THREE.NodeMaterialLoader.prototype, {
 				case "UVTransformNode":
 
 					object.uv = this.getNode( node.uv );
-					object.transform.value.fromArray( node.elements );
+					object.transform = this.getNode( node.transform );
 
 					break;
 

+ 1 - 0
examples/js/nodes/NodeMaterial.js

@@ -21,6 +21,7 @@ THREE.NodeMaterial.types = {
 	v2: 'vec2',
 	v3: 'vec3',
 	v4: 'vec4',
+	m3: 'mat3',
 	m4: 'mat4'
 };
 

+ 31 - 0
examples/js/nodes/inputs/Matrix3Node.js

@@ -0,0 +1,31 @@
+/**
+ * @author sunag / http://www.sunag.com.br/
+ */
+
+THREE.Matrix3Node = function ( matrix ) {
+
+	THREE.InputNode.call( this, 'm3' );
+
+	this.value = matrix || new THREE.Matrix3();
+
+};
+
+THREE.Matrix3Node.prototype = Object.create( THREE.InputNode.prototype );
+THREE.Matrix3Node.prototype.constructor = THREE.Matrix3Node;
+THREE.Matrix3Node.prototype.nodeType = "Matrix3";
+
+THREE.Matrix3Node.prototype.toJSON = function ( meta ) {
+
+	var data = this.getJSONNode( meta );
+
+	if ( ! data ) {
+
+		data = this.createJSONNode( meta );
+
+		data.elements = this.value.elements.concat();
+
+	}
+
+	return data;
+
+};

+ 6 - 14
examples/js/nodes/utils/UVTransformNode.js

@@ -4,10 +4,10 @@
 
 THREE.UVTransformNode = function () {
 
-	THREE.FunctionNode.call( this, "( uvTransform * vec4( uvNode, 0, 1 ) ).xy", "vec2" );
+	THREE.FunctionNode.call( this, "( uvTransform * vec3( uvNode, 1 ) ).xy", "vec2" );
 
 	this.uv = new THREE.UVNode();
-	this.transform = new THREE.Matrix4Node();
+	this.transform = new THREE.Matrix3Node();
 
 };
 
@@ -26,21 +26,13 @@ THREE.UVTransformNode.prototype.generate = function ( builder, output ) {
 
 THREE.UVTransformNode.prototype.compose = function () {
 
-	var defaultPivot = new THREE.Vector2( .5, .5 ),
-		tempVector = new THREE.Vector3(),
-		tempMatrix = new THREE.Matrix4();
+	var defaultPivot = new THREE.Vector2( .5, .5 );
 
-	return function compose( translate, rotate, scale, optionalCenter ) {
+	return function compose( translate, scale, rotate, optionalCenter ) {
 
 		optionalCenter = optionalCenter !== undefined ? optionalCenter : defaultPivot;
 
-		var matrix = this.transform.value;
-
-		matrix.identity()
-			.setPosition( tempVector.set( - optionalCenter.x, - optionalCenter.y, 0 ) )
-			.premultiply( tempMatrix.makeRotationZ( rotate ) )
-			.multiply( tempMatrix.makeScale( scale.x, scale.y, 0 ) )
-			.multiply( tempMatrix.makeTranslation( translate.x, translate.y, 0 ) );
+		this.transform.value.setUvTransform( translate.x, translate.y, scale.x, scale.y, rotate, optionalCenter.x, optionalCenter.y );
 
 		return this;
 
@@ -57,7 +49,7 @@ THREE.UVTransformNode.prototype.toJSON = function ( meta ) {
 		data = this.createJSONNode( meta );
 
 		data.uv = this.uv.toJSON( meta ).uuid;
-		data.elements = this.transform.value.elements.concat();
+		data.transform = this.transform.toJSON( meta ).uuid;
 
 	}
 

+ 2 - 1
examples/webgl_materials_nodes.html

@@ -72,6 +72,7 @@
 		<script src="js/nodes/inputs/Vector3Node.js"></script>
 		<script src="js/nodes/inputs/Vector4Node.js"></script>
 		<script src="js/nodes/inputs/TextureNode.js"></script>
+		<script src="js/nodes/inputs/Matrix3Node.js"></script>
 		<script src="js/nodes/inputs/Matrix4Node.js"></script>
 		<script src="js/nodes/inputs/CubeTextureNode.js"></script>
 
@@ -860,7 +861,7 @@
 
 					function updateUVTransform() {
 
-						texture.coord.compose( translate, THREE.Math.degToRad( rotate ), scale );
+						texture.coord.compose( translate, scale, THREE.Math.degToRad( rotate ) );
 
 					}