MeshStandardNode.js 2.5 KB

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