MeshStandardNode.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import {
  5. Color,
  6. Vector2
  7. } from '../../../../../build/three.module.js';
  8. import { StandardNode } from './StandardNode.js';
  9. import { PropertyNode } from '../../inputs/PropertyNode.js';
  10. import { OperatorNode } from '../../math/OperatorNode.js';
  11. import { SwitchNode } from '../../utils/SwitchNode.js';
  12. import { NormalMapNode } from '../../misc/NormalMapNode.js';
  13. function MeshStandardNode() {
  14. StandardNode.call( this );
  15. this.properties = {
  16. color: new Color( 0xffffff ),
  17. roughness: 0.5,
  18. metalness: 0.5,
  19. normalScale: new Vector2( 1, 1 )
  20. };
  21. this.inputs = {
  22. color: new PropertyNode( this.properties, 'color', 'c' ),
  23. roughness: new PropertyNode( this.properties, 'roughness', 'f' ),
  24. metalness: new PropertyNode( this.properties, 'metalness', 'f' ),
  25. normalScale: new PropertyNode( this.properties, 'normalScale', 'v2' )
  26. };
  27. }
  28. MeshStandardNode.prototype = Object.create( StandardNode.prototype );
  29. MeshStandardNode.prototype.constructor = MeshStandardNode;
  30. MeshStandardNode.prototype.nodeType = "MeshStandard";
  31. MeshStandardNode.prototype.build = function ( builder ) {
  32. var props = this.properties,
  33. inputs = this.inputs;
  34. if ( builder.isShader( 'fragment' ) ) {
  35. // slots
  36. // * color
  37. // * map
  38. var color = builder.findNode( props.color, inputs.color ),
  39. map = builder.resolve( props.map );
  40. this.color = map ? new OperatorNode( color, map, OperatorNode.MUL ) : color;
  41. // slots
  42. // * roughness
  43. // * roughnessMap
  44. var roughness = builder.findNode( props.roughness, inputs.roughness ),
  45. roughnessMap = builder.resolve( props.roughnessMap );
  46. this.roughness = roughnessMap ? new OperatorNode( roughness, new SwitchNode( roughnessMap, "g" ), OperatorNode.MUL ) : roughness;
  47. // slots
  48. // * metalness
  49. // * metalnessMap
  50. var metalness = builder.findNode( props.metalness, inputs.metalness ),
  51. metalnessMap = builder.resolve( props.metalnessMap );
  52. this.metalness = metalnessMap ? new OperatorNode( metalness, new SwitchNode( metalnessMap, "b" ), OperatorNode.MUL ) : metalness;
  53. // slots
  54. // * normalMap
  55. // * normalScale
  56. if ( props.normalMap ) {
  57. this.normal = new NormalMapNode( builder.resolve( props.normalMap ) );
  58. this.normal.scale = builder.findNode( props.normalScale, inputs.normalScale );
  59. } else {
  60. this.normal = undefined;
  61. }
  62. // slots
  63. // * envMap
  64. this.environment = builder.resolve( props.envMap );
  65. }
  66. // build code
  67. return StandardNode.prototype.build.call( this, builder );
  68. };
  69. MeshStandardNode.prototype.toJSON = function ( meta ) {
  70. var data = this.getJSONNode( meta );
  71. if ( ! data ) {
  72. data = this.createJSONNode( meta );
  73. console.warn( ".toJSON not implemented in", this );
  74. }
  75. return data;
  76. };
  77. export { MeshStandardNode };