StandardNode.js 9.8 KB

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