MeshStandardNode.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. function MeshStandardNode() {
  11. StandardNode.call( this );
  12. this.properties = {
  13. color: new Color( 0xffffff ),
  14. roughness: 0.5,
  15. metalness: 0.5,
  16. normalScale: new Vector2( 1, 1 )
  17. };
  18. this.inputs = {
  19. color: new PropertyNode( this.properties, 'color', 'c' ),
  20. roughness: new PropertyNode( this.properties, 'roughness', 'f' ),
  21. metalness: new PropertyNode( this.properties, 'metalness', 'f' ),
  22. normalScale: new PropertyNode( this.properties, 'normalScale', 'v2' )
  23. };
  24. }
  25. MeshStandardNode.prototype = Object.create( StandardNode.prototype );
  26. MeshStandardNode.prototype.constructor = MeshStandardNode;
  27. MeshStandardNode.prototype.nodeType = "MeshStandard";
  28. MeshStandardNode.prototype.build = function ( builder ) {
  29. var props = this.properties,
  30. inputs = this.inputs;
  31. if ( builder.isShader( 'fragment' ) ) {
  32. // slots
  33. // * color
  34. // * map
  35. var color = builder.findNode( props.color, inputs.color ),
  36. map = builder.resolve( props.map );
  37. this.color = map ? new OperatorNode( color, map, OperatorNode.MUL ) : color;
  38. // slots
  39. // * roughness
  40. // * roughnessMap
  41. var roughness = builder.findNode( props.roughness, inputs.roughness ),
  42. roughnessMap = builder.resolve( props.roughnessMap );
  43. this.roughness = roughnessMap ? new OperatorNode( roughness, new SwitchNode( roughnessMap, "g" ), OperatorNode.MUL ) : roughness;
  44. // slots
  45. // * metalness
  46. // * metalnessMap
  47. var metalness = builder.findNode( props.metalness, inputs.metalness ),
  48. metalnessMap = builder.resolve( props.metalnessMap );
  49. this.metalness = metalnessMap ? new OperatorNode( metalness, new SwitchNode( metalnessMap, "b" ), OperatorNode.MUL ) : metalness;
  50. // slots
  51. // * normalMap
  52. // * normalScale
  53. if ( props.normalMap ) {
  54. this.normal = new NormalMapNode( builder.resolve( props.normalMap ) );
  55. this.normal.scale = builder.findNode( props.normalScale, inputs.normalScale );
  56. } else {
  57. this.normal = undefined;
  58. }
  59. // slots
  60. // * envMap
  61. this.environment = builder.resolve( props.envMap );
  62. }
  63. // build code
  64. return StandardNode.prototype.build.call( this, builder );
  65. };
  66. MeshStandardNode.prototype.toJSON = function ( meta ) {
  67. var data = this.getJSONNode( meta );
  68. if ( ! data ) {
  69. data = this.createJSONNode( meta );
  70. console.warn( ".toJSON not implemented in", this );
  71. }
  72. return data;
  73. };
  74. export { MeshStandardNode };
粤ICP备19079148号