colorSplitterBlock.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 expand a Color3/4 into 4 outputs (one for each component)
  9. */
  10. export class ColorSplitterBlock extends NodeMaterialBlock {
  11. /**
  12. * Create a new ColorSplitterBlock
  13. * @param name defines the block name
  14. */
  15. public constructor(name: string) {
  16. super(name, NodeMaterialBlockTargets.Neutral);
  17. this.registerInput("rgba", NodeMaterialBlockConnectionPointTypes.Color4, true);
  18. this.registerInput("rgb ", NodeMaterialBlockConnectionPointTypes.Color3, true);
  19. this.registerOutput("rgb", NodeMaterialBlockConnectionPointTypes.Color3);
  20. this.registerOutput("r", NodeMaterialBlockConnectionPointTypes.Float);
  21. this.registerOutput("g", NodeMaterialBlockConnectionPointTypes.Float);
  22. this.registerOutput("b", NodeMaterialBlockConnectionPointTypes.Float);
  23. this.registerOutput("a", NodeMaterialBlockConnectionPointTypes.Float);
  24. this.inputsAreExclusive = true;
  25. }
  26. /**
  27. * Gets the current class name
  28. * @returns the class name
  29. */
  30. public getClassName() {
  31. return "ColorSplitterBlock";
  32. }
  33. /**
  34. * Gets the rgba component (input)
  35. */
  36. public get rgba(): NodeMaterialConnectionPoint {
  37. return this._inputs[0];
  38. }
  39. /**
  40. * Gets the rgb component (input)
  41. */
  42. public get rgbIn(): NodeMaterialConnectionPoint {
  43. return this._inputs[1];
  44. }
  45. /**
  46. * Gets the rgb component (output)
  47. */
  48. public get rgbOut(): NodeMaterialConnectionPoint {
  49. return this._outputs[0];
  50. }
  51. /**
  52. * Gets the r component (output)
  53. */
  54. public get r(): NodeMaterialConnectionPoint {
  55. return this._outputs[1];
  56. }
  57. /**
  58. * Gets the g component (output)
  59. */
  60. public get g(): NodeMaterialConnectionPoint {
  61. return this._outputs[2];
  62. }
  63. /**
  64. * Gets the b component (output)
  65. */
  66. public get b(): NodeMaterialConnectionPoint {
  67. return this._outputs[3];
  68. }
  69. /**
  70. * Gets the a component (output)
  71. */
  72. public get a(): NodeMaterialConnectionPoint {
  73. return this._outputs[4];
  74. }
  75. protected _inputRename(name: string) {
  76. if (name === "rgb ") {
  77. return "rgbIn";
  78. }
  79. return name;
  80. }
  81. protected _outputRename(name: string) {
  82. if (name === "rgb") {
  83. return "rgbOut";
  84. }
  85. return name;
  86. }
  87. protected _buildBlock(state: NodeMaterialBuildState) {
  88. super._buildBlock(state);
  89. let input = this.rgba.isConnected ? this.rgba : this.rgbIn;
  90. if (!input.isConnected) {
  91. return;
  92. }
  93. let rgbOutput = this._outputs[0];
  94. let rOutput = this._outputs[1];
  95. let gOutput = this._outputs[2];
  96. let bOutput = this._outputs[3];
  97. let aOutput = this._outputs[4];
  98. if (rgbOutput.hasEndpoints) {
  99. state.compilationString += this._declareOutput(rgbOutput, state) + ` = ${input.associatedVariableName}.rgb;\r\n`;
  100. }
  101. if (rOutput.hasEndpoints) {
  102. state.compilationString += this._declareOutput(rOutput, state) + ` = ${input.associatedVariableName}.r;\r\n`;
  103. }
  104. if (gOutput.hasEndpoints) {
  105. state.compilationString += this._declareOutput(gOutput, state) + ` = ${input.associatedVariableName}.g;\r\n`;
  106. }
  107. if (bOutput.hasEndpoints) {
  108. state.compilationString += this._declareOutput(bOutput, state) + ` = ${input.associatedVariableName}.b;\r\n`;
  109. }
  110. if (aOutput.hasEndpoints) {
  111. state.compilationString += this._declareOutput(aOutput, state) + ` = ${input.associatedVariableName}.a;\r\n`;
  112. }
  113. return this;
  114. }
  115. }
  116. _TypeStore.RegisteredTypes["BABYLON.ColorSplitterBlock"] = ColorSplitterBlock;