diff --git a/examples/jsm/nodes/core/Node.js b/examples/jsm/nodes/core/Node.js index 6a711f742e5e894c267e7f6daa36a0d4ca94b860..7435c07b49a71685fc013dd7a43a447b21a4dd6d 100644 --- a/examples/jsm/nodes/core/Node.js +++ b/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; diff --git a/examples/jsm/nodes/materials/NodeMaterial.js b/examples/jsm/nodes/materials/NodeMaterial.js index 98cb141cb24316b9e9d896bdd51903a4a5414152..5ef4a815ecc3b46283955c95013b68db07e4e04c 100644 --- a/examples/jsm/nodes/materials/NodeMaterial.js +++ b/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 ) {