StandardNode.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. material.define( 'ALPHATEST', '0.0' );
  17. material.requestAttrib.light = true;
  18. if ( builder.isShader( 'vertex' ) ) {
  19. var transform = this.transform ? this.transform.parseAndBuildCode( builder, 'v3', 'transform' ) : undefined;
  20. material.mergeUniform( THREE.UniformsUtils.merge( [
  21. THREE.UniformsLib[ "fog" ],
  22. THREE.UniformsLib[ "ambient" ],
  23. THREE.UniformsLib[ "lights" ]
  24. ] ) );
  25. material.addVertexPars( [
  26. "varying vec3 vViewPosition;",
  27. "#ifndef FLAT_SHADED",
  28. " varying vec3 vNormal;",
  29. "#endif",
  30. THREE.ShaderChunk[ "common" ],
  31. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  32. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  33. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  34. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ]
  35. ].join( "\n" ) );
  36. var output = [
  37. THREE.ShaderChunk[ "beginnormal_vertex" ],
  38. THREE.ShaderChunk[ "morphnormal_vertex" ],
  39. THREE.ShaderChunk[ "skinbase_vertex" ],
  40. THREE.ShaderChunk[ "skinnormal_vertex" ],
  41. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  42. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  43. " vNormal = normalize( transformedNormal );",
  44. "#endif",
  45. THREE.ShaderChunk[ "begin_vertex" ]
  46. ];
  47. if ( transform ) {
  48. output.push(
  49. transform.code,
  50. "transformed = " + transform.result + ";"
  51. );
  52. }
  53. output.push(
  54. THREE.ShaderChunk[ "morphtarget_vertex" ],
  55. THREE.ShaderChunk[ "skinning_vertex" ],
  56. THREE.ShaderChunk[ "project_vertex" ],
  57. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  58. " vViewPosition = - mvPosition.xyz;",
  59. THREE.ShaderChunk[ "worldpos_vertex" ],
  60. THREE.ShaderChunk[ "shadowmap_vertex" ]
  61. );
  62. code = output.join( "\n" );
  63. }
  64. else {
  65. // blur textures for PBR effect
  66. var requires = {
  67. bias : new THREE.RoughnessToBlinnExponentNode(),
  68. offsetU : 0,
  69. offsetV : 0
  70. };
  71. // parse all nodes to reuse generate codes
  72. this.color.parse( builder );
  73. this.roughness.parse( builder );
  74. this.metalness.parse( builder );
  75. if ( this.alpha ) this.alpha.parse( builder );
  76. if ( this.light ) this.light.parse( builder, 'light' );
  77. if ( this.ao ) this.ao.parse( builder );
  78. if ( this.ambient ) this.ambient.parse( builder );
  79. if ( this.shadow ) this.shadow.parse( builder );
  80. if ( this.emissive ) this.emissive.parse( builder );
  81. if ( this.normal ) this.normal.parse( builder );
  82. if ( this.normalScale && this.normal ) this.normalScale.parse( builder );
  83. if ( this.environment ) this.environment.parse( builder, 'env', requires ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
  84. // build code
  85. var color = this.color.buildCode( builder, 'c' );
  86. var roughness = this.roughness.buildCode( builder, 'fv1' );
  87. var metalness = this.metalness.buildCode( builder, 'fv1' );
  88. var reflectivity = this.reflectivity ? this.reflectivity.buildCode( builder, 'fv1' ) : undefined;
  89. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  90. var light = this.light ? this.light.buildCode( builder, 'v3', 'light' ) : undefined;
  91. var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
  92. var ambient = this.ambient ? this.ambient.buildCode( builder, 'c' ) : undefined;
  93. var shadow = this.shadow ? this.shadow.buildCode( builder, 'c' ) : undefined;
  94. var emissive = this.emissive ? this.emissive.buildCode( builder, 'c' ) : undefined;
  95. var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
  96. var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
  97. var environment = this.environment ? this.environment.buildCode( builder, 'c', 'env', requires ) : undefined;
  98. material.requestAttrib.transparent = alpha != undefined;
  99. material.addFragmentPars( [
  100. "varying vec3 vViewPosition;",
  101. "#ifndef FLAT_SHADED",
  102. " varying vec3 vNormal;",
  103. "#endif",
  104. THREE.ShaderChunk[ "common" ],
  105. THREE.ShaderChunk[ "fog_pars_fragment" ],
  106. THREE.ShaderChunk[ "bsdfs" ],
  107. THREE.ShaderChunk[ "lights_pars" ],
  108. THREE.ShaderChunk[ "lights_physical_pars_fragment" ],
  109. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  110. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  111. ].join( "\n" ) );
  112. var output = [
  113. // prevent undeclared normal
  114. THREE.ShaderChunk[ "normal_fragment" ],
  115. // prevent undeclared material
  116. " PhysicalMaterial material;",
  117. " material.diffuseColor = vec3( 1.0 );",
  118. color.code,
  119. " vec3 diffuseColor = " + color.result + ";",
  120. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  121. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  122. roughness.code,
  123. " float roughnessFactor = " + roughness.result + ";",
  124. metalness.code,
  125. " float metalnessFactor = " + metalness.result + ";"
  126. ];
  127. if ( alpha ) {
  128. output.push(
  129. alpha.code,
  130. 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
  131. );
  132. }
  133. if ( normal ) {
  134. builder.include( 'perturbNormal2Arb' );
  135. output.push( normal.code );
  136. if ( normalScale ) output.push( normalScale.code );
  137. output.push(
  138. 'normal = perturbNormal2Arb(-vViewPosition,normal,' +
  139. normal.result + ',' +
  140. new THREE.UVNode().build( builder, 'v2' ) + ',' +
  141. ( normalScale ? normalScale.result : 'vec2( 1.0 )' ) + ');'
  142. );
  143. }
  144. // optimization for now
  145. output.push( 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * (1.0 - metalnessFactor)' ) + ';' );
  146. output.push(
  147. // accumulation
  148. 'material.specularRoughness = clamp( roughnessFactor, 0.04, 1.0 );' // disney's remapping of [ 0, 1 ] roughness to [ 0.001, 1 ]
  149. );
  150. if ( reflectivity ) {
  151. output.push(
  152. 'material.specularColor = mix( vec3( 0.16 * pow2( ' + reflectivity.builder( builder, 'fv1' ) + ' ) ), diffuseColor, metalnessFactor );'
  153. );
  154. }
  155. else {
  156. output.push(
  157. 'material.specularColor = mix( vec3( 0.04 ), diffuseColor, metalnessFactor );'
  158. );
  159. }
  160. output.push(
  161. THREE.ShaderChunk[ "lights_template" ]
  162. );
  163. if ( light ) {
  164. output.push(
  165. light.code,
  166. "reflectedLight.directDiffuse = " + light.result + ";"
  167. );
  168. // apply color
  169. output.push(
  170. "diffuseColor *= 1.0 - metalnessFactor;",
  171. "reflectedLight.directDiffuse *= diffuseColor;",
  172. "reflectedLight.indirectDiffuse *= diffuseColor;"
  173. );
  174. }
  175. if ( ao ) {
  176. output.push(
  177. ao.code,
  178. "reflectedLight.indirectDiffuse *= " + ao.result + ";"
  179. );
  180. }
  181. if ( ambient ) {
  182. output.push(
  183. ambient.code,
  184. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  185. );
  186. }
  187. if ( shadow ) {
  188. output.push(
  189. shadow.code,
  190. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  191. "reflectedLight.directSpecular *= " + shadow.result + ";"
  192. );
  193. }
  194. if ( emissive ) {
  195. output.push(
  196. emissive.code,
  197. "reflectedLight.directDiffuse += " + emissive.result + ";"
  198. );
  199. }
  200. if ( environment ) {
  201. output.push(
  202. environment.code,
  203. "RE_IndirectSpecular(" + environment.result + ", geometry, material, reflectedLight );"
  204. );
  205. }
  206. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );
  207. output.push(
  208. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  209. THREE.ShaderChunk[ "fog_fragment" ]
  210. );
  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. code = output.join( "\n" );
  218. }
  219. return code;
  220. };