Переглянути джерело

BasicNodeMaterial: Improve vertex shader

Use some shader chunks for the vertex initialization and for the
projection

Signed-off-by: martinRenou <[email protected]>
martinRenou 5 роки тому
батько
коміт
3ec082ab55
1 змінених файлів з 28 додано та 5 видалено
  1. 28 5
      examples/jsm/nodes/materials/nodes/BasicNode.js

+ 28 - 5
examples/jsm/nodes/materials/nodes/BasicNode.js

@@ -21,22 +21,45 @@ BasicNode.prototype.generate = function ( builder ) {
 
 		var position = this.position ? this.position.analyzeAndFlow( builder, 'v3', { cache: 'position' } ) : undefined;
 
+		builder.addParsCode( [
+			"varying vec3 vViewPosition;",
+
+			"#ifndef FLAT_SHADED",
+
+			" varying vec3 vNormal;",
+
+			"#endif",
+		].join( "\n" ) );
+
 		var output = [
-			"vec3 transformed = position;"
+			"#include <beginnormal_vertex>",
+			"#include <defaultnormal_vertex>",
+
+			"#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
+
+			" vNormal = normalize( transformedNormal );",
+
+			"#endif",
+
+			"#include <begin_vertex>",
 		];
 
 		if ( position ) {
 
 			output.push(
 				position.code,
-				position.result ? "gl_Position = projectionMatrix * modelViewMatrix * vec4(" + position.result + ", 1.0);" : ''
+				position.result ? "transformed = " + position.result + ";" : ''
 			);
 
-		} else {
+		}
 
-			output.push( "gl_Position = projectionMatrix * modelViewMatrix * vec4(transformed, 1.0);" );
+		output.push(
+			"#include <project_vertex>",
 
-		}
+			" vViewPosition = - mvPosition.xyz;",
+
+			"#include <worldpos_vertex>",
+		);
 
 		code = output.join( "\n" );