StandardNode.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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( 'ALPHATEST', '0.0' );
  17. material.requestAttrib.light = true;
  18. if ( builder.isShader( 'vertex' ) ) {
  19. var transform = this.transform ? this.transform.verifyAndBuildCode( builder, 'v3', 'transform' ) : undefined;
  20. material.mergeUniform( THREE.UniformsUtils.merge( [
  21. THREE.UniformsLib[ "fog" ],
  22. THREE.UniformsLib[ "lights" ]
  23. ] ) );
  24. material.addVertexPars( [
  25. "varying vec3 vViewPosition;",
  26. "#ifndef FLAT_SHADED",
  27. " varying vec3 vNormal;",
  28. "#endif",
  29. THREE.ShaderChunk[ "common" ],
  30. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  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[ "lights_phong_vertex" ],
  61. THREE.ShaderChunk[ "shadowmap_vertex" ]
  62. );
  63. code = output.join( "\n" );
  64. }
  65. else {
  66. // autoblur textures for PBR Material effect
  67. var requires = {
  68. bias : new THREE.RoughnessToBlinnExponentNode()
  69. };
  70. // verify all nodes to reuse generate codes
  71. this.color.verify( builder );
  72. this.roughness.verify( builder );
  73. this.metalness.verify( builder );
  74. if ( this.alpha ) this.alpha.verify( builder );
  75. if ( this.light ) this.light.verify( builder, 'light' );
  76. if ( this.ao ) this.ao.verify( builder );
  77. if ( this.ambient ) this.ambient.verify( builder );
  78. if ( this.shadow ) this.shadow.verify( builder );
  79. if ( this.emissive ) this.emissive.verify( builder );
  80. if ( this.normal ) this.normal.verify( builder );
  81. if ( this.normalScale && this.normal ) this.normalScale.verify( builder );
  82. if ( this.environment ) this.environment.verify( builder, 'env', requires ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
  83. // build code
  84. var color = this.color.buildCode( builder, 'c' );
  85. var roughness = this.roughness.buildCode( builder, 'fv1' );
  86. var metalness = this.metalness.buildCode( builder, 'fv1' );
  87. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  88. var light = this.light ? this.light.buildCode( builder, 'v3', 'light' ) : undefined;
  89. var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
  90. var ambient = this.ambient ? this.ambient.buildCode( builder, 'c' ) : undefined;
  91. var shadow = this.shadow ? this.shadow.buildCode( builder, 'c' ) : undefined;
  92. var emissive = this.emissive ? this.emissive.buildCode( builder, 'c' ) : 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 environment = this.environment ? this.environment.buildCode( builder, 'c', 'env', requires ) : undefined;
  96. material.requestAttrib.transparent = alpha != undefined;
  97. material.addFragmentPars( [
  98. "varying vec3 vViewPosition;",
  99. "#ifndef FLAT_SHADED",
  100. " varying vec3 vNormal;",
  101. "#endif",
  102. THREE.ShaderChunk[ "common" ],
  103. THREE.ShaderChunk[ "fog_pars_fragment" ],
  104. THREE.ShaderChunk[ "bsdfs" ],
  105. THREE.ShaderChunk[ "lights_pars" ],
  106. THREE.ShaderChunk[ "lights_standard_pars_fragment" ],
  107. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  108. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  109. ].join( "\n" ) );
  110. var output = [
  111. // prevent undeclared normal
  112. THREE.ShaderChunk[ "normal_fragment" ],
  113. // prevent undeclared material
  114. " StandardMaterial material;",
  115. " material.diffuseColor = vec3( 1.0 );",
  116. color.code,
  117. " vec3 diffuseColor = " + color.result + ";",
  118. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  119. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  120. roughness.code,
  121. " float roughnessFactor = " + roughness.result + ";",
  122. metalness.code,
  123. " float metalnessFactor = " + metalness.result + ";"
  124. ];
  125. if ( alpha ) {
  126. output.push(
  127. alpha.code,
  128. 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
  129. );
  130. }
  131. if ( normal ) {
  132. builder.include( 'perturbNormal2Arb' );
  133. output.push( normal.code );
  134. if ( normalScale ) output.push( normalScale.code );
  135. output.push(
  136. 'normal = perturbNormal2Arb(-vViewPosition,normal,' +
  137. normal.result + ',' +
  138. new THREE.UVNode().build( builder, 'v2' ) + ',' +
  139. ( normalScale ? normalScale.result : 'vec2( 1.0 )' ) + ');'
  140. );
  141. }
  142. // optimization for now
  143. output.push( 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * (1.0 - metalnessFactor)' ) + ';' );
  144. output.push(
  145. // accumulation
  146. 'material.specularRoughness = clamp( roughnessFactor, 0.001, 1.0 );', // disney's remapping of [ 0, 1 ] roughness to [ 0.001, 1 ]
  147. 'material.specularColor = mix( vec3( 0.04 ), diffuseColor, metalnessFactor );',
  148. THREE.ShaderChunk[ "lights_template" ]
  149. );
  150. if ( light ) {
  151. output.push(
  152. light.code,
  153. "reflectedLight.directDiffuse = " + light.result + ";"
  154. );
  155. // apply color
  156. output.push(
  157. "diffuseColor *= 1.0 - metalnessFactor;",
  158. "reflectedLight.directDiffuse *= diffuseColor;",
  159. "reflectedLight.indirectDiffuse *= diffuseColor;"
  160. );
  161. }
  162. if ( ao ) {
  163. output.push(
  164. ao.code,
  165. "reflectedLight.indirectDiffuse *= " + ao.result + ";"
  166. );
  167. }
  168. if ( ambient ) {
  169. output.push(
  170. ambient.code,
  171. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  172. );
  173. }
  174. if ( shadow ) {
  175. output.push(
  176. shadow.code,
  177. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  178. "reflectedLight.directSpecular *= " + shadow.result + ";"
  179. );
  180. }
  181. if ( emissive ) {
  182. output.push(
  183. emissive.code,
  184. "reflectedLight.directDiffuse += " + emissive.result + ";"
  185. );
  186. }
  187. if ( environment ) {
  188. output.push(
  189. environment.code,
  190. "RE_IndirectSpecular(" + environment.result + ", geometry, material, reflectedLight );"
  191. );
  192. }
  193. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );
  194. output.push(
  195. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  196. THREE.ShaderChunk[ "fog_fragment" ]
  197. );
  198. if ( alpha ) {
  199. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  200. }
  201. else {
  202. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  203. }
  204. code = output.join( "\n" );
  205. }
  206. return code;
  207. };