StandardNode.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.StandardNode = function() {
  5. THREE.GLNode.call( this );
  6. this.color = new THREE.ColorNode( 0xEEEEEE );
  7. this.roughness = new THREE.FloatNode( 0.5 );
  8. this.metalness = new THREE.FloatNode( 0.5 );
  9. };
  10. THREE.StandardNode.prototype = Object.create( THREE.GLNode.prototype );
  11. THREE.StandardNode.prototype.constructor = THREE.StandardNode;
  12. THREE.StandardNode.prototype.build = function( builder ) {
  13. var material = builder.material;
  14. var code;
  15. material.define( 'PHYSICAL' );
  16. if ( !this.clearCoat && !this.clearCoatRoughness ) material.define( 'STANDARD' );
  17. material.define( 'ALPHATEST', '0.0' );
  18. material.requestAttribs.light = true;
  19. material.extensions.shaderTextureLOD = true;
  20. if ( builder.isShader( 'vertex' ) ) {
  21. var transform = this.transform ? this.transform.parseAndBuildCode( builder, 'v3', { cache : 'transform' } ) : undefined;
  22. material.mergeUniform( Object.assign( {},
  23. THREE.UniformsLib[ "fog" ],
  24. THREE.UniformsLib[ "lights" ]
  25. ) );
  26. material.addVertexPars( [
  27. "varying vec3 vViewPosition;",
  28. "#ifndef FLAT_SHADED",
  29. " varying vec3 vNormal;",
  30. "#endif",
  31. THREE.ShaderChunk[ "common" ],
  32. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  33. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  34. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  35. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ]
  36. ].join( "\n" ) );
  37. var output = [
  38. THREE.ShaderChunk[ "beginnormal_vertex" ],
  39. THREE.ShaderChunk[ "morphnormal_vertex" ],
  40. THREE.ShaderChunk[ "skinbase_vertex" ],
  41. THREE.ShaderChunk[ "skinnormal_vertex" ],
  42. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  43. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  44. " vNormal = normalize( transformedNormal );",
  45. "#endif",
  46. THREE.ShaderChunk[ "begin_vertex" ]
  47. ];
  48. if ( transform ) {
  49. output.push(
  50. transform.code,
  51. "transformed = " + transform.result + ";"
  52. );
  53. }
  54. output.push(
  55. THREE.ShaderChunk[ "morphtarget_vertex" ],
  56. THREE.ShaderChunk[ "skinning_vertex" ],
  57. THREE.ShaderChunk[ "project_vertex" ],
  58. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  59. " vViewPosition = - mvPosition.xyz;",
  60. THREE.ShaderChunk[ "worldpos_vertex" ],
  61. THREE.ShaderChunk[ "shadowmap_vertex" ]
  62. );
  63. code = output.join( "\n" );
  64. } else {
  65. // blur textures for PBR effect
  66. var requires = {
  67. bias : new THREE.RoughnessToBlinnExponentNode(),
  68. offsetU : 0,
  69. offsetV : 0
  70. };
  71. var useClearCoat = !material.isDefined( 'STANDARD' );
  72. // parse all nodes to reuse generate codes
  73. this.color.parse( builder, { slot : 'color' } );
  74. this.roughness.parse( builder );
  75. this.metalness.parse( builder );
  76. if ( this.alpha ) this.alpha.parse( builder );
  77. if ( this.normal ) this.normal.parse( builder );
  78. if ( this.normalScale && this.normal ) this.normalScale.parse( builder );
  79. if (this.clearCoat) this.clearCoat.parse( builder );
  80. if (this.clearCoatRoughness) this.clearCoatRoughness.parse( builder );
  81. if ( this.reflectivity ) this.reflectivity.parse( builder );
  82. if ( this.light ) this.light.parse( builder, { cache : 'light' } );
  83. if ( this.ao ) this.ao.parse( builder );
  84. if ( this.ambient ) this.ambient.parse( builder );
  85. if ( this.shadow ) this.shadow.parse( builder );
  86. if ( this.emissive ) this.emissive.parse( builder, { slot : 'emissive' } );
  87. if ( this.environment ) this.environment.parse( builder, { cache : 'env', requires : requires, slot : 'environment' } ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
  88. // build code
  89. var color = this.color.buildCode( builder, 'c', { slot : 'color' } );
  90. var roughness = this.roughness.buildCode( builder, 'fv1' );
  91. var metalness = this.metalness.buildCode( builder, 'fv1' );
  92. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  93. var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
  94. var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
  95. var clearCoat = this.clearCoat ? this.clearCoat.buildCode( builder, 'fv1' ) : undefined;
  96. var clearCoatRoughness = this.clearCoatRoughness ? this.clearCoatRoughness.buildCode( builder, 'fv1' ) : undefined;
  97. var reflectivity = this.reflectivity ? this.reflectivity.buildCode( builder, 'fv1' ) : undefined;
  98. var light = this.light ? this.light.buildCode( builder, 'v3', { cache : 'light' } ) : undefined;
  99. var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
  100. var ambient = this.ambient ? this.ambient.buildCode( builder, 'c' ) : undefined;
  101. var shadow = this.shadow ? this.shadow.buildCode( builder, 'c' ) : undefined;
  102. var emissive = this.emissive ? this.emissive.buildCode( builder, 'c', { slot : 'emissive' } ) : undefined;
  103. var environment = this.environment ? this.environment.buildCode( builder, 'c', { cache : 'env', requires : requires, slot : 'environment' } ) : undefined;
  104. var clearCoatEnv = useClearCoat && environment ? this.environment.buildCode( builder, 'c', { cache : 'clearCoat', requires : requires, slot : 'environment' } ) : undefined;
  105. material.requestAttribs.transparent = alpha != undefined;
  106. material.addFragmentPars( [
  107. "varying vec3 vViewPosition;",
  108. "#ifndef FLAT_SHADED",
  109. " varying vec3 vNormal;",
  110. "#endif",
  111. THREE.ShaderChunk[ "common" ],
  112. THREE.ShaderChunk[ "fog_pars_fragment" ],
  113. THREE.ShaderChunk[ "bsdfs" ],
  114. THREE.ShaderChunk[ "lights_pars" ],
  115. THREE.ShaderChunk[ "lights_physical_pars_fragment" ],
  116. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  117. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ]
  118. ].join( "\n" ) );
  119. var output = [
  120. // prevent undeclared normal
  121. THREE.ShaderChunk[ "normal_flip" ],
  122. THREE.ShaderChunk[ "normal_fragment" ],
  123. // prevent undeclared material
  124. " PhysicalMaterial material;",
  125. " material.diffuseColor = vec3( 1.0 );",
  126. color.code,
  127. " vec3 diffuseColor = " + color.result + ";",
  128. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  129. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  130. roughness.code,
  131. " float roughnessFactor = " + roughness.result + ";",
  132. metalness.code,
  133. " float metalnessFactor = " + metalness.result + ";"
  134. ];
  135. if ( alpha ) {
  136. output.push(
  137. alpha.code,
  138. 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
  139. );
  140. }
  141. if ( normal ) {
  142. builder.include( 'perturbNormal2Arb' );
  143. output.push( normal.code );
  144. if ( normalScale ) output.push( normalScale.code );
  145. output.push(
  146. 'normal = perturbNormal2Arb(-vViewPosition,normal,' +
  147. normal.result + ',' +
  148. new THREE.UVNode().build( builder, 'v2' ) + ',' +
  149. ( normalScale ? normalScale.result : 'vec2( 1.0 )' ) + ');'
  150. );
  151. }
  152. // optimization for now
  153. output.push( 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * (1.0 - metalnessFactor)' ) + ';' );
  154. output.push(
  155. // accumulation
  156. 'material.specularRoughness = clamp( roughnessFactor, DEFAULT_SPECULAR_COEFFICIENT, 1.0 );' // disney's remapping of [ 0, 1 ] roughness to [ 0.001, 1 ]
  157. );
  158. if (clearCoat) {
  159. output.push(
  160. clearCoat.code,
  161. 'material.clearCoat = saturate( ' + clearCoat.result + ' );'
  162. );
  163. } else if (useClearCoat) {
  164. output.push( 'material.clearCoat = 0.0;' );
  165. }
  166. if (clearCoatRoughness) {
  167. output.push(
  168. clearCoatRoughness.code,
  169. 'material.clearCoatRoughness = clamp( ' + clearCoatRoughness.result + ', DEFAULT_SPECULAR_COEFFICIENT, 1.0 );'
  170. );
  171. } else if (useClearCoat) {
  172. output.push( 'material.clearCoatRoughness = 0.0;' );
  173. }
  174. if ( reflectivity ) {
  175. output.push(
  176. reflectivity.code,
  177. 'material.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( ' + reflectivity.result + ' ) ), diffuseColor, metalnessFactor );'
  178. );
  179. } else {
  180. output.push(
  181. 'material.specularColor = mix( vec3( DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor, metalnessFactor );'
  182. );
  183. }
  184. output.push(
  185. THREE.ShaderChunk[ "lights_template" ]
  186. );
  187. if ( light ) {
  188. output.push(
  189. light.code,
  190. "reflectedLight.directDiffuse = " + light.result + ";"
  191. );
  192. // apply color
  193. output.push(
  194. "diffuseColor *= 1.0 - metalnessFactor;",
  195. "reflectedLight.directDiffuse *= diffuseColor;",
  196. "reflectedLight.indirectDiffuse *= diffuseColor;"
  197. );
  198. }
  199. if ( ao ) {
  200. output.push(
  201. ao.code,
  202. "reflectedLight.indirectDiffuse *= " + ao.result + ";",
  203. "float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );",
  204. "reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, " + ao.result + ", material.specularRoughness );"
  205. );
  206. }
  207. if ( ambient ) {
  208. output.push(
  209. ambient.code,
  210. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  211. );
  212. }
  213. if ( shadow ) {
  214. output.push(
  215. shadow.code,
  216. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  217. "reflectedLight.directSpecular *= " + shadow.result + ";"
  218. );
  219. }
  220. if ( emissive ) {
  221. output.push(
  222. emissive.code,
  223. "reflectedLight.directDiffuse += " + emissive.result + ";"
  224. );
  225. }
  226. if ( environment ) {
  227. output.push( environment.code );
  228. if (clearCoatEnv) {
  229. output.push(
  230. clearCoatEnv.code,
  231. "vec3 clearCoatRadiance = " + clearCoatEnv.result + ";"
  232. );
  233. } else {
  234. output.push("vec3 clearCoatRadiance = vec3( 0.0 );");
  235. }
  236. output.push( "RE_IndirectSpecular(" + environment.result + ", clearCoatRadiance, geometry, material, reflectedLight );" );
  237. }
  238. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );
  239. if ( alpha ) {
  240. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  241. } else {
  242. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  243. }
  244. output.push(
  245. THREE.ShaderChunk[ "premultiplied_alpha_fragment" ],
  246. THREE.ShaderChunk[ "tonemapping_fragment" ],
  247. THREE.ShaderChunk[ "encodings_fragment" ],
  248. THREE.ShaderChunk[ "fog_fragment" ]
  249. );
  250. code = output.join( "\n" );
  251. }
  252. return code;
  253. };