colorMergerBlock.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { NodeMaterialBlock } from '../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
  4. import { NodeMaterialBlockTargets } from '../nodeMaterialBlockTargets';
  5. import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
  6. import { _TypeStore } from '../../../Misc/typeStore';
  7. /**
  8. * Block used to create a Color3/4 out of individual inputs (one for each component)
  9. */
  10. export class ColorMergerBlock extends NodeMaterialBlock {
  11. /**
  12. * Create a new ColorMergerBlock
  13. * @param name defines the block name
  14. */
  15. public constructor(name: string) {
  16. super(name, NodeMaterialBlockTargets.Fragment);
  17. this.registerInput("r", NodeMaterialBlockConnectionPointTypes.Float);
  18. this.registerInput("g", NodeMaterialBlockConnectionPointTypes.Float);
  19. this.registerInput("b", NodeMaterialBlockConnectionPointTypes.Float);
  20. this.registerInput("a", NodeMaterialBlockConnectionPointTypes.Float, true);
  21. this.registerOutput("rgba", NodeMaterialBlockConnectionPointTypes.Color4);
  22. this.registerOutput("rgb", NodeMaterialBlockConnectionPointTypes.Color3);
  23. }
  24. /**
  25. * Gets the current class name
  26. * @returns the class name
  27. */
  28. public getClassName() {
  29. return "ColorMergerBlock";
  30. }
  31. /**
  32. * Gets the r component (input)
  33. */
  34. public get r(): NodeMaterialConnectionPoint {
  35. return this._inputs[0];
  36. }
  37. /**
  38. * Gets the g component (input)
  39. */
  40. public get g(): NodeMaterialConnectionPoint {
  41. return this._inputs[1];
  42. }
  43. /**
  44. * Gets the b component (input)
  45. */
  46. public get b(): NodeMaterialConnectionPoint {
  47. return this._inputs[2];
  48. }
  49. /**
  50. * Gets the a component (input)
  51. */
  52. public get a(): NodeMaterialConnectionPoint {
  53. return this._inputs[3];
  54. }
  55. /**
  56. * Gets the rgba component (output)
  57. */
  58. public get rgba(): NodeMaterialConnectionPoint {
  59. return this._outputs[0];
  60. }
  61. /**
  62. * Gets the rgb component (output)
  63. */
  64. public get rgb(): NodeMaterialConnectionPoint {
  65. return this._outputs[1];
  66. }
  67. protected _buildBlock(state: NodeMaterialBuildState) {
  68. super._buildBlock(state);
  69. let rInput = this.r;
  70. let gInput = this.g;
  71. let bInput = this.b;
  72. let aInput = this.a;
  73. let color4Output = this._outputs[0];
  74. let color3Output = this._outputs[1];
  75. if (color4Output.endpoints.length) {
  76. state.compilationString += this._declareOutput(color4Output, state) + ` = vec4(${this._writeVariable(rInput)}, ${this._writeVariable(gInput)}, ${this._writeVariable(bInput)}, ${aInput.isConnected ? this._writeVariable(aInput) : "0.0"});\r\n`;
  77. } else if (color3Output.endpoints.length) {
  78. state.compilationString += this._declareOutput(color3Output, state) + ` = vec3(${this._writeVariable(rInput)}, ${this._writeVariable(gInput)}, ${this._writeVariable(bInput)});\r\n`;
  79. }
  80. return this;
  81. }
  82. }
  83. _TypeStore.RegisteredTypes["BABYLON.ColorMergerBlock"] = ColorMergerBlock;