colorSplitterBlock.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Fragment);
  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. }
  25. /**
  26. * Gets the current class name
  27. * @returns the class name
  28. */
  29. public getClassName() {
  30. return "ColorSplitterBlock";
  31. }
  32. /**
  33. * Gets the rgba component (input)
  34. */
  35. public get rgba(): NodeMaterialConnectionPoint {
  36. return this._inputs[0];
  37. }
  38. /**
  39. * Gets the rgb component (input)
  40. */
  41. public get rgbIn(): NodeMaterialConnectionPoint {
  42. return this._inputs[1];
  43. }
  44. /**
  45. * Gets the rgb component (output)
  46. */
  47. public get rgbOut(): NodeMaterialConnectionPoint {
  48. return this._outputs[0];
  49. }
  50. /**
  51. * Gets the r component (output)
  52. */
  53. public get r(): NodeMaterialConnectionPoint {
  54. return this._outputs[1];
  55. }
  56. /**
  57. * Gets the g component (output)
  58. */
  59. public get g(): NodeMaterialConnectionPoint {
  60. return this._outputs[2];
  61. }
  62. /**
  63. * Gets the b component (output)
  64. */
  65. public get b(): NodeMaterialConnectionPoint {
  66. return this._outputs[3];
  67. }
  68. /**
  69. * Gets the a component (output)
  70. */
  71. public get a(): NodeMaterialConnectionPoint {
  72. return this._outputs[4];
  73. }
  74. protected _inputRename(name: string) {
  75. if (name === "rgb ") {
  76. return "rgbIn";
  77. }
  78. return name;
  79. }
  80. protected _outputRename(name: string) {
  81. if (name === "rgb") {
  82. return "rgbOut";
  83. }
  84. return name;
  85. }
  86. protected _buildBlock(state: NodeMaterialBuildState) {
  87. super._buildBlock(state);
  88. let input = this.rgba.isConnected ? this.rgba : this.rgbIn;
  89. if (!input.isConnected) {
  90. return;
  91. }
  92. let rgbOutput = this._outputs[0];
  93. let rOutput = this._outputs[1];
  94. let gOutput = this._outputs[2];
  95. let bOutput = this._outputs[3];
  96. let aOutput = this._outputs[4];
  97. if (rgbOutput.hasEndpoints) {
  98. state.compilationString += this._declareOutput(rgbOutput, state) + ` = ${input.associatedVariableName}.rgb;\r\n`;
  99. }
  100. if (rOutput.hasEndpoints) {
  101. state.compilationString += this._declareOutput(rOutput, state) + ` = ${input.associatedVariableName}.r;\r\n`;
  102. }
  103. if (gOutput.hasEndpoints) {
  104. state.compilationString += this._declareOutput(gOutput, state) + ` = ${input.associatedVariableName}.g;\r\n`;
  105. }
  106. if (bOutput.hasEndpoints) {
  107. state.compilationString += this._declareOutput(bOutput, state) + ` = ${input.associatedVariableName}.b;\r\n`;
  108. }
  109. if (aOutput.hasEndpoints) {
  110. state.compilationString += this._declareOutput(aOutput, state) + ` = ${input.associatedVariableName}.a;\r\n`;
  111. }
  112. return this;
  113. }
  114. }
  115. _TypeStore.RegisteredTypes["BABYLON.ColorSplitterBlock"] = ColorSplitterBlock;