فهرست منبع

Merge pull request #19955 from martinRenou/basicnodematerial_normals

BasicNodeMaterial: Improve shaders
Mr.doob 4 سال پیش
والد
کامیت
ac858d5df9
1فایلهای تغییر یافته به همراه47 افزوده شده و 5 حذف شده
  1. 47 5
      examples/jsm/nodes/materials/nodes/BasicNode.js

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

@@ -21,22 +21,51 @@ 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 <morphtarget_vertex>",
+			"#include <skinning_vertex>",
+			"#include <project_vertex>",
+			"#include <fog_vertex>",
+			"#include <logdepthbuf_vertex>",
+			"#include <clipping_planes_vertex>",
 
-		}
+			"	vViewPosition = - mvPosition.xyz;",
+
+			"#include <worldpos_vertex>",
+			"#include <shadowmap_vertex>"
+		);
 
 		code = output.join( "\n" );
 
@@ -55,7 +84,20 @@ BasicNode.prototype.generate = function ( builder ) {
 
 		builder.requires.transparent = alpha !== undefined;
 
+		builder.addParsCode( [
+			"varying vec3 vViewPosition;",
+
+			"#ifndef FLAT_SHADED",
+
+			" varying vec3 vNormal;",
+
+			"#endif",
+		].join( "\n" ) );
+
 		var output = [
+			// add before: prevent undeclared normal
+			"#include <normal_fragment_begin>",
+
 			color.code,
 		];