colorMergerBlock.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { NodeMaterialBlock } from '../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../Enums/nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
  4. import { NodeMaterialBlockTargets } from '../Enums/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.Neutral);
  17. this.registerInput("rgb ", NodeMaterialBlockConnectionPointTypes.Color3, true);
  18. this.registerInput("r", NodeMaterialBlockConnectionPointTypes.Float, true);
  19. this.registerInput("g", NodeMaterialBlockConnectionPointTypes.Float, true);
  20. this.registerInput("b", NodeMaterialBlockConnectionPointTypes.Float, true);
  21. this.registerInput("a", NodeMaterialBlockConnectionPointTypes.Float, true);
  22. this.registerOutput("rgba", NodeMaterialBlockConnectionPointTypes.Color4);
  23. this.registerOutput("rgb", NodeMaterialBlockConnectionPointTypes.Color3);
  24. }
  25. /**
  26. * Gets the current class name
  27. * @returns the class name
  28. */
  29. public getClassName() {
  30. return "ColorMergerBlock";
  31. }
  32. /**
  33. * Gets the rgb component (input)
  34. */
  35. public get rgbIn(): NodeMaterialConnectionPoint {
  36. return this._inputs[0];
  37. }
  38. /**
  39. * Gets the r component (input)
  40. */
  41. public get r(): NodeMaterialConnectionPoint {
  42. return this._inputs[1];
  43. }
  44. /**
  45. * Gets the g component (input)
  46. */
  47. public get g(): NodeMaterialConnectionPoint {
  48. return this._inputs[2];
  49. }
  50. /**
  51. * Gets the b component (input)
  52. */
  53. public get b(): NodeMaterialConnectionPoint {
  54. return this._inputs[3];
  55. }
  56. /**
  57. * Gets the a component (input)
  58. */
  59. public get a(): NodeMaterialConnectionPoint {
  60. return this._inputs[4];
  61. }
  62. /**
  63. * Gets the rgba component (output)
  64. */
  65. public get rgba(): NodeMaterialConnectionPoint {
  66. return this._outputs[0];
  67. }
  68. /**
  69. * Gets the rgb component (output)
  70. */
  71. public get rgbOut(): NodeMaterialConnectionPoint {
  72. return this._outputs[1];
  73. }
  74. /**
  75. * Gets the rgb component (output)
  76. * @deprecated Please use rgbOut instead.
  77. */
  78. public get rgb(): NodeMaterialConnectionPoint {
  79. return this.rgbOut;
  80. }
  81. protected _buildBlock(state: NodeMaterialBuildState) {
  82. super._buildBlock(state);
  83. let rInput = this.r;
  84. let gInput = this.g;
  85. let bInput = this.b;
  86. let aInput = this.a;
  87. let rgbInput = this.rgbIn;
  88. let color4Output = this._outputs[0];
  89. let color3Output = this._outputs[1];
  90. if (rgbInput.isConnected) {
  91. if (color4Output.hasEndpoints) {
  92. state.compilationString += this._declareOutput(color4Output, state) + ` = vec4(${rgbInput.associatedVariableName}, ${aInput.isConnected ? this._writeVariable(aInput) : "0.0"});\r\n`;
  93. } else if (color3Output.hasEndpoints) {
  94. state.compilationString += this._declareOutput(color3Output, state) + ` = ${rgbInput.associatedVariableName};\r\n`;
  95. }
  96. } else {
  97. if (color4Output.hasEndpoints) {
  98. state.compilationString += this._declareOutput(color4Output, state) + ` = vec4(${rInput.isConnected ? this._writeVariable(rInput) : "0.0"}, ${gInput.isConnected ? this._writeVariable(gInput) : "0.0"}, ${bInput.isConnected ? this._writeVariable(bInput) : "0.0"}, ${aInput.isConnected ? this._writeVariable(aInput) : "0.0"});\r\n`;
  99. } else if (color3Output.hasEndpoints) {
  100. state.compilationString += this._declareOutput(color3Output, state) + ` = vec3(${rInput.isConnected ? this._writeVariable(rInput) : "0.0"}, ${gInput.isConnected ? this._writeVariable(gInput) : "0.0"}, ${bInput.isConnected ? this._writeVariable(bInput) : "0.0"});\r\n`;
  101. }
  102. }
  103. return this;
  104. }
  105. }
  106. _TypeStore.RegisteredTypes["BABYLON.ColorMergerBlock"] = ColorMergerBlock;