StandardNode.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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( 'STANDARD' );
  16. material.define( 'PHYSICAL' );
  17. material.define( 'ALPHATEST', '0.0' );
  18. material.requestAttribs.light = true;
  19. if ( builder.isShader( 'vertex' ) ) {
  20. var transform = this.transform ? this.transform.parseAndBuildCode( builder, 'v3', { cache : 'transform' } ) : undefined;
  21. material.mergeUniform( Object.assign( {},
  22. THREE.UniformsLib[ "fog" ],
  23. THREE.UniformsLib[ "ambient" ],
  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. }
  65. else {
  66. // blur textures for PBR effect
  67. var requires = {
  68. bias : new THREE.RoughnessToBlinnExponentNode(),
  69. offsetU : 0,
  70. offsetV : 0
  71. };
  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.light ) this.light.parse( builder, { cache : 'light' } );
  78. if ( this.ao ) this.ao.parse( builder );
  79. if ( this.ambient ) this.ambient.parse( builder );
  80. if ( this.shadow ) this.shadow.parse( builder );
  81. if ( this.emissive ) this.emissive.parse( builder, { slot : 'emissive' } );
  82. if ( this.normal ) this.normal.parse( builder );
  83. if ( this.normalScale && this.normal ) this.normalScale.parse( builder );
  84. if ( this.environment ) this.environment.parse( builder, { cache : 'env', requires : requires, slot : 'environment' } ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
  85. // build code
  86. var color = this.color.buildCode( builder, 'c', { slot : 'color' } );
  87. var roughness = this.roughness.buildCode( builder, 'fv1' );
  88. var metalness = this.metalness.buildCode( builder, 'fv1' );
  89. var reflectivity = this.reflectivity ? this.reflectivity.buildCode( builder, 'fv1' ) : undefined;
  90. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  91. var light = this.light ? this.light.buildCode( builder, 'v3', { cache : 'light' } ) : undefined;
  92. var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
  93. var ambient = this.ambient ? this.ambient.buildCode( builder, 'c' ) : undefined;
  94. var shadow = this.shadow ? this.shadow.buildCode( builder, 'c' ) : undefined;
  95. var emissive = this.emissive ? this.emissive.buildCode( builder, 'c', { slot : 'emissive' } ) : undefined;
  96. var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
  97. var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
  98. var environment = this.environment ? this.environment.buildCode( builder, 'c', { cache : 'env', requires : requires, slot : 'environment' } ) : undefined;
  99. material.requestAttribs.transparent = alpha != undefined;
  100. material.addFragmentPars( [
  101. "varying vec3 vViewPosition;",
  102. "#ifndef FLAT_SHADED",
  103. " varying vec3 vNormal;",
  104. "#endif",
  105. THREE.ShaderChunk[ "common" ],
  106. THREE.ShaderChunk[ "fog_pars_fragment" ],
  107. THREE.ShaderChunk[ "bsdfs" ],
  108. THREE.ShaderChunk[ "lights_pars" ],
  109. THREE.ShaderChunk[ "lights_physical_pars_fragment" ],
  110. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  111. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ]
  112. ].join( "\n" ) );
  113. var output = [
  114. // prevent undeclared normal
  115. THREE.ShaderChunk[ "normal_flip" ],
  116. THREE.ShaderChunk[ "normal_fragment" ],
  117. // prevent undeclared material
  118. " PhysicalMaterial material;",
  119. " material.diffuseColor = vec3( 1.0 );",
  120. color.code,
  121. " vec3 diffuseColor = " + color.result + ";",
  122. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  123. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  124. roughness.code,
  125. " float roughnessFactor = " + roughness.result + ";",
  126. metalness.code,
  127. " float metalnessFactor = " + metalness.result + ";"
  128. ];
  129. if ( alpha ) {
  130. output.push(
  131. alpha.code,
  132. 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
  133. );
  134. }
  135. if ( normal ) {
  136. builder.include( 'perturbNormal2Arb' );
  137. output.push( normal.code );
  138. if ( normalScale ) output.push( normalScale.code );
  139. output.push(
  140. 'normal = perturbNormal2Arb(-vViewPosition,normal,' +
  141. normal.result + ',' +
  142. new THREE.UVNode().build( builder, 'v2' ) + ',' +
  143. ( normalScale ? normalScale.result : 'vec2( 1.0 )' ) + ');'
  144. );
  145. }
  146. // optimization for now
  147. output.push( 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * (1.0 - metalnessFactor)' ) + ';' );
  148. output.push(
  149. // accumulation
  150. 'material.specularRoughness = clamp( roughnessFactor, 0.04, 1.0 );' // disney's remapping of [ 0, 1 ] roughness to [ 0.001, 1 ]
  151. );
  152. if ( reflectivity ) {
  153. output.push(
  154. 'material.specularColor = mix( vec3( 0.16 * pow2( ' + reflectivity.builder( builder, 'fv1' ) + ' ) ), diffuseColor, metalnessFactor );'
  155. );
  156. }
  157. else {
  158. output.push(
  159. 'material.specularColor = mix( vec3( 0.04 ), diffuseColor, metalnessFactor );'
  160. );
  161. }
  162. output.push(
  163. THREE.ShaderChunk[ "lights_template" ]
  164. );
  165. if ( light ) {
  166. output.push(
  167. light.code,
  168. "reflectedLight.directDiffuse = " + light.result + ";"
  169. );
  170. // apply color
  171. output.push(
  172. "diffuseColor *= 1.0 - metalnessFactor;",
  173. "reflectedLight.directDiffuse *= diffuseColor;",
  174. "reflectedLight.indirectDiffuse *= diffuseColor;"
  175. );
  176. }
  177. if ( ao ) {
  178. output.push(
  179. ao.code,
  180. "reflectedLight.indirectDiffuse *= " + ao.result + ";",
  181. "float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );",
  182. "reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, " + ao.result + ", material.specularRoughness );"
  183. );
  184. }
  185. if ( ambient ) {
  186. output.push(
  187. ambient.code,
  188. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  189. );
  190. }
  191. if ( shadow ) {
  192. output.push(
  193. shadow.code,
  194. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  195. "reflectedLight.directSpecular *= " + shadow.result + ";"
  196. );
  197. }
  198. if ( emissive ) {
  199. output.push(
  200. emissive.code,
  201. "reflectedLight.directDiffuse += " + emissive.result + ";"
  202. );
  203. }
  204. if ( environment ) {
  205. output.push(
  206. environment.code,
  207. "RE_IndirectSpecular_Physical(" + environment.result + ", vec3( 0.0 ), geometry, material, reflectedLight );"
  208. );
  209. }
  210. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );
  211. if ( alpha ) {
  212. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  213. }
  214. else {
  215. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  216. }
  217. output.push(
  218. THREE.ShaderChunk[ "premultiplied_alpha_fragment" ],
  219. THREE.ShaderChunk[ "tonemapping_fragment" ],
  220. THREE.ShaderChunk[ "encodings_fragment" ],
  221. THREE.ShaderChunk[ "fog_fragment" ]
  222. );
  223. code = output.join( "\n" );
  224. }
  225. return code;
  226. };