StandardNode.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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.nodeType = "Standard";
  13. THREE.StandardNode.prototype.build = function ( builder ) {
  14. var material = builder.material;
  15. var code;
  16. material.define( this.clearCoat || this.clearCoatRoughness ? 'PHYSICAL' : 'STANDARD' );
  17. material.define( 'ALPHATEST', '0.0' );
  18. material.requires.lights = 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 <encodings_pars_fragment>", // encoding functions
  33. "#include <fog_pars_vertex>",
  34. "#include <morphtarget_pars_vertex>",
  35. "#include <skinning_pars_vertex>",
  36. "#include <shadowmap_pars_vertex>",
  37. "#include <logdepthbuf_pars_vertex>"
  38. ].join( "\n" ) );
  39. var output = [
  40. "#include <beginnormal_vertex>",
  41. "#include <morphnormal_vertex>",
  42. "#include <skinbase_vertex>",
  43. "#include <skinnormal_vertex>",
  44. "#include <defaultnormal_vertex>",
  45. "#include <logdepthbuf_pars_vertex>",
  46. "#include <logdepthbuf_pars_vertex>",
  47. "#include <logdepthbuf_pars_vertex>",
  48. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  49. " vNormal = normalize( transformedNormal );",
  50. "#endif",
  51. "#include <begin_vertex>"
  52. ];
  53. if ( transform ) {
  54. output.push(
  55. transform.code,
  56. transform.result ? "transformed = " + transform.result + ";" : ''
  57. );
  58. }
  59. output.push(
  60. "#include <morphtarget_vertex>",
  61. "#include <skinning_vertex>",
  62. "#include <project_vertex>",
  63. "#include <fog_vertex>",
  64. "#include <logdepthbuf_vertex>",
  65. " vViewPosition = - mvPosition.xyz;",
  66. "#include <worldpos_vertex>",
  67. "#include <shadowmap_vertex>"
  68. );
  69. code = output.join( "\n" );
  70. } else {
  71. // blur textures for PBR effect
  72. var requiresEnvironment = {
  73. bias: THREE.RoughnessToBlinnExponentNode,
  74. gamma: true
  75. };
  76. var requiresGamma = {
  77. gamma: true
  78. };
  79. var useClearCoat = ! material.isDefined( 'STANDARD' );
  80. // parse all nodes to reuse generate codes
  81. this.color.parse( builder, { slot: 'color', requires: requiresGamma } );
  82. this.roughness.parse( builder );
  83. this.metalness.parse( builder );
  84. if ( this.alpha ) this.alpha.parse( builder );
  85. if ( this.normal ) this.normal.parse( builder );
  86. if ( this.normalScale && this.normal ) this.normalScale.parse( builder );
  87. if ( this.clearCoat ) this.clearCoat.parse( builder );
  88. if ( this.clearCoatRoughness ) this.clearCoatRoughness.parse( builder );
  89. if ( this.reflectivity ) this.reflectivity.parse( builder );
  90. if ( this.light ) this.light.parse( builder, { cache: 'light' } );
  91. if ( this.ao ) this.ao.parse( builder );
  92. if ( this.ambient ) this.ambient.parse( builder );
  93. if ( this.shadow ) this.shadow.parse( builder );
  94. if ( this.emissive ) this.emissive.parse( builder, { slot: 'emissive' } );
  95. if ( this.environment ) this.environment.parse( builder, { cache: 'env', requires: requiresEnvironment, slot: 'environment' } ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
  96. // build code
  97. var color = this.color.buildCode( builder, 'c', { slot: 'color', requires: requiresGamma } );
  98. var roughness = this.roughness.buildCode( builder, 'fv1' );
  99. var metalness = this.metalness.buildCode( builder, 'fv1' );
  100. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  101. var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
  102. var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
  103. var clearCoat = this.clearCoat ? this.clearCoat.buildCode( builder, 'fv1' ) : undefined;
  104. var clearCoatRoughness = this.clearCoatRoughness ? this.clearCoatRoughness.buildCode( builder, 'fv1' ) : undefined;
  105. var reflectivity = this.reflectivity ? this.reflectivity.buildCode( builder, 'fv1' ) : undefined;
  106. var light = this.light ? this.light.buildCode( builder, 'v3', { cache: 'light' } ) : undefined;
  107. var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
  108. var ambient = this.ambient ? this.ambient.buildCode( builder, 'c' ) : undefined;
  109. var shadow = this.shadow ? this.shadow.buildCode( builder, 'c' ) : undefined;
  110. var emissive = this.emissive ? this.emissive.buildCode( builder, 'c', { slot: 'emissive' } ) : undefined;
  111. var environment = this.environment ? this.environment.buildCode( builder, 'c', { cache: 'env', requires: requiresEnvironment, slot: 'environment' } ) : undefined;
  112. var clearCoatEnv = useClearCoat && environment ? this.environment.buildCode( builder, 'c', { cache: 'clearCoat', requires: requiresEnvironment, slot: 'environment' } ) : undefined;
  113. material.requires.transparent = alpha != undefined;
  114. material.addFragmentPars( [
  115. "varying vec3 vViewPosition;",
  116. "#ifndef FLAT_SHADED",
  117. " varying vec3 vNormal;",
  118. "#endif",
  119. "#include <common>",
  120. "#include <fog_pars_fragment>",
  121. "#include <bsdfs>",
  122. "#include <lights_pars_begin>",
  123. "#include <lights_physical_pars_fragment>",
  124. "#include <shadowmap_pars_fragment>",
  125. "#include <logdepthbuf_pars_fragment>",
  126. "#include <logdepthbuf_vertex>"
  127. ].join( "\n" ) );
  128. var output = [
  129. // prevent undeclared normal
  130. " #include <normal_fragment_begin>",
  131. // prevent undeclared material
  132. " PhysicalMaterial material;",
  133. " material.diffuseColor = vec3( 1.0 );",
  134. color.code,
  135. " vec3 diffuseColor = " + color.result + ";",
  136. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  137. "#include <logdepthbuf_fragment>",
  138. roughness.code,
  139. " float roughnessFactor = " + roughness.result + ";",
  140. metalness.code,
  141. " float metalnessFactor = " + metalness.result + ";"
  142. ];
  143. if ( alpha ) {
  144. output.push(
  145. alpha.code,
  146. 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
  147. );
  148. }
  149. if ( normal ) {
  150. output.push(
  151. normal.code,
  152. 'normal = ' + normal.result + ';'
  153. );
  154. }
  155. // optimization for now
  156. output.push(
  157. 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * (1.0 - metalnessFactor)' ) + ';',
  158. 'material.specularRoughness = clamp( roughnessFactor, 0.04, 1.0 );'
  159. );
  160. if ( clearCoat ) {
  161. output.push(
  162. clearCoat.code,
  163. 'material.clearCoat = saturate( ' + clearCoat.result + ' );'
  164. );
  165. } else if ( useClearCoat ) {
  166. output.push( 'material.clearCoat = 0.0;' );
  167. }
  168. if ( clearCoatRoughness ) {
  169. output.push(
  170. clearCoatRoughness.code,
  171. 'material.clearCoatRoughness = clamp( ' + clearCoatRoughness.result + ', 0.04, 1.0 );'
  172. );
  173. } else if ( useClearCoat ) {
  174. output.push( 'material.clearCoatRoughness = 0.0;' );
  175. }
  176. if ( reflectivity ) {
  177. output.push(
  178. reflectivity.code,
  179. 'material.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( ' + reflectivity.result + ' ) ), diffuseColor, metalnessFactor );'
  180. );
  181. } else {
  182. output.push(
  183. 'material.specularColor = mix( vec3( DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor, metalnessFactor );'
  184. );
  185. }
  186. output.push(
  187. "#include <lights_fragment_begin>"
  188. );
  189. if ( light ) {
  190. output.push(
  191. light.code,
  192. "reflectedLight.directDiffuse = " + light.result + ";"
  193. );
  194. // apply color
  195. output.push(
  196. "diffuseColor *= 1.0 - metalnessFactor;",
  197. "reflectedLight.directDiffuse *= diffuseColor;",
  198. "reflectedLight.indirectDiffuse *= diffuseColor;"
  199. );
  200. }
  201. if ( ao ) {
  202. output.push(
  203. ao.code,
  204. "reflectedLight.indirectDiffuse *= " + ao.result + ";",
  205. "float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );",
  206. "reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, " + ao.result + ", material.specularRoughness );"
  207. );
  208. }
  209. if ( ambient ) {
  210. output.push(
  211. ambient.code,
  212. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  213. );
  214. }
  215. if ( shadow ) {
  216. output.push(
  217. shadow.code,
  218. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  219. "reflectedLight.directSpecular *= " + shadow.result + ";"
  220. );
  221. }
  222. if ( emissive ) {
  223. output.push(
  224. emissive.code,
  225. "reflectedLight.directDiffuse += " + emissive.result + ";"
  226. );
  227. }
  228. if ( environment ) {
  229. output.push( environment.code );
  230. if ( clearCoatEnv ) {
  231. output.push(
  232. clearCoatEnv.code,
  233. "clearCoatRadiance += " + clearCoatEnv.result + ";"
  234. );
  235. }
  236. output.push( "radiance += " + environment.result + ";" );
  237. }
  238. output.push(
  239. "#include <lights_fragment_end>"
  240. );
  241. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );
  242. if ( alpha ) {
  243. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  244. } else {
  245. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  246. }
  247. output.push(
  248. "#include <premultiplied_alpha_fragment>",
  249. "#include <tonemapping_fragment>",
  250. "#include <encodings_fragment>",
  251. "#include <fog_fragment>",
  252. "#include <premultiplied_alpha_fragment>",
  253. "#include <dithering_fragment>"
  254. );
  255. code = output.join( "\n" );
  256. }
  257. return code;
  258. };
  259. THREE.StandardNode.prototype.copy = function ( source ) {
  260. THREE.GLNode.prototype.copy.call( this, source );
  261. // vertex
  262. if ( source.transform ) this.transform = source.transform;
  263. // fragment
  264. this.color = source.color;
  265. this.roughness = source.roughness;
  266. this.metalness = source.metalness;
  267. if ( source.alpha ) this.alpha = source.alpha;
  268. if ( source.normal ) this.normal = source.normal;
  269. if ( source.clearCoat ) this.clearCoat = source.clearCoat;
  270. if ( source.clearCoatRoughness ) this.clearCoatRoughness = source.clearCoatRoughness;
  271. if ( source.reflectivity ) this.reflectivity = source.reflectivity;
  272. if ( source.light ) this.light = source.light;
  273. if ( source.shadow ) this.shadow = source.shadow;
  274. if ( source.ao ) this.ao = source.ao;
  275. if ( source.emissive ) this.emissive = source.emissive;
  276. if ( source.ambient ) this.ambient = source.ambient;
  277. if ( source.environment ) this.environment = source.environment;
  278. };
  279. THREE.StandardNode.prototype.toJSON = function ( meta ) {
  280. var data = this.getJSONNode( meta );
  281. if ( ! data ) {
  282. data = this.createJSONNode( meta );
  283. // vertex
  284. if ( this.transform ) data.transform = this.transform.toJSON( meta ).uuid;
  285. // fragment
  286. data.color = this.color.toJSON( meta ).uuid;
  287. data.roughness = this.roughness.toJSON( meta ).uuid;
  288. data.metalness = this.metalness.toJSON( meta ).uuid;
  289. if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
  290. if ( this.normal ) data.normal = this.normal.toJSON( meta ).uuid;
  291. if ( this.clearCoat ) data.clearCoat = this.clearCoat.toJSON( meta ).uuid;
  292. if ( this.clearCoatRoughness ) data.clearCoatRoughness = this.clearCoatRoughness.toJSON( meta ).uuid;
  293. if ( this.reflectivity ) data.reflectivity = this.reflectivity.toJSON( meta ).uuid;
  294. if ( this.light ) data.light = this.light.toJSON( meta ).uuid;
  295. if ( this.shadow ) data.shadow = this.shadow.toJSON( meta ).uuid;
  296. if ( this.ao ) data.ao = this.ao.toJSON( meta ).uuid;
  297. if ( this.emissive ) data.emissive = this.emissive.toJSON( meta ).uuid;
  298. if ( this.ambient ) data.ambient = this.ambient.toJSON( meta ).uuid;
  299. if ( this.environment ) data.environment = this.environment.toJSON( meta ).uuid;
  300. }
  301. return data;
  302. };
粤ICP备19079148号