MeshStandardNode.js 2.7 KB

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