Bladeren bron

gethash and customProgramCacheKey

sunag 5 jaren geleden
bovenliggende
commit
cf0649f407
2 gewijzigde bestanden met toevoegingen van 59 en 26 verwijderingen
  1. 22 0
      examples/jsm/nodes/core/Node.js
  2. 37 26
      examples/jsm/nodes/materials/NodeMaterial.js

+ 22 - 0
examples/jsm/nodes/core/Node.js

@@ -100,6 +100,28 @@ Node.prototype = {
 
 	},
 
+	getHash: function() {
+		
+		var hash = '{';
+		
+		for(var prop in this) {
+
+			var obj = this[ prop ];
+			
+			if (obj instanceof Node) {
+				
+				hash += '"' + prop + '":' + obj.getHash() + ',';
+				
+			}
+
+		}
+		
+		hash += '"id":"' + this.uuid + '"}';
+		
+		return hash;		
+		
+	},
+
 	appendDepsNode: function ( builder, data, output ) {
 
 		data.deps = ( data.deps || 0 ) + 1;

+ 37 - 26
examples/jsm/nodes/materials/NodeMaterial.js

@@ -26,32 +26,6 @@ function NodeMaterial( vertex, fragment ) {
 
 	this.updaters = [];
 
-	// onBeforeCompile can't be in the prototype because onBeforeCompile.toString varies per material
-
-	this.onBeforeCompile = function ( shader, renderer ) {
-
-		var materialProperties = renderer.properties.get( this );
-
-		if ( this.version !== materialProperties.__version ) {
-
-			this.build( { renderer: renderer } );
-
-			shader.uniforms = this.uniforms;
-			shader.vertexShader = this.vertexShader;
-			shader.fragmentShader = this.fragmentShader;
-
-		}
-
-	};
-
-	// it fix the programCache and share the code with others materials
-
-	this.onBeforeCompile.toString = function () {
-
-		return scope.needsCompile;
-
-	};
-
 }
 
 NodeMaterial.prototype = Object.create( ShaderMaterial.prototype );
@@ -91,6 +65,43 @@ Object.defineProperties( NodeMaterial.prototype, {
 
 } );
 
+NodeMaterial.prototype.onBeforeCompile = function ( shader, renderer ) {
+
+	var materialProperties = renderer.properties.get( this );
+
+	if ( this.version !== materialProperties.__version ) {
+
+		this.build( { renderer: renderer } );
+
+		shader.uniforms = this.uniforms;
+		shader.vertexShader = this.vertexShader;
+		shader.fragmentShader = this.fragmentShader;
+
+	}
+
+};
+
+NodeMaterial.prototype.customProgramCacheKey = function () {
+
+	var hash = this.getHash();
+
+	return hash;
+
+};
+
+NodeMaterial.prototype.getHash = function () {
+
+	var hash = '{';
+
+	hash += '"vertex":' + this.vertex.getHash() + ',\n';
+	hash += '"fragment":' + this.fragment.getHash() + '\n';
+
+	hash += '}'
+
+	return hash;
+	
+};
+
 NodeMaterial.prototype.updateFrame = function ( frame ) {
 
 	for ( var i = 0; i < this.updaters.length; ++ i ) {