rgbaSplitterBlock.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { NodeMaterialBlock, NodeMaterialBlockTargets } from '../../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../../nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialCompilationState } from '../../nodeMaterialCompilationState';
  4. /**
  5. * Block used to expand a Color4 or a Vector4 into 4 outputs (one for each component)
  6. */
  7. export class RGBASplitterBlock extends NodeMaterialBlock {
  8. /**
  9. * Create a new RGBASplitterBlock
  10. * @param name defines the block name
  11. */
  12. public constructor(name: string) {
  13. super(name, NodeMaterialBlockTargets.Fragment);
  14. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.Vector4OrColor4);
  15. this.registerOutput("r", NodeMaterialBlockConnectionPointTypes.Float);
  16. this.registerOutput("g", NodeMaterialBlockConnectionPointTypes.Float);
  17. this.registerOutput("b", NodeMaterialBlockConnectionPointTypes.Float);
  18. this.registerOutput("a", NodeMaterialBlockConnectionPointTypes.Float);
  19. }
  20. protected _buildBlock(state: NodeMaterialCompilationState) {
  21. super._buildBlock(state);
  22. let input = this._inputs[0];
  23. let rOutput = this._outputs[0];
  24. let gOutput = this._outputs[1];
  25. let bOutput = this._outputs[2];
  26. let aOutput = this._outputs[3];
  27. if (rOutput.connectedBlocks.length > 0) {
  28. state.compilationString += this._declareOutput(rOutput, state) + ` = ${input.associatedVariableName}.r;\r\n`;
  29. }
  30. if (gOutput.connectedBlocks.length > 0) {
  31. state.compilationString += this._declareOutput(gOutput, state) + ` = ${input.associatedVariableName}.g;\r\n`;
  32. }
  33. if (bOutput.connectedBlocks.length > 0) {
  34. state.compilationString += this._declareOutput(bOutput, state) + ` = ${input.associatedVariableName}.b;\r\n`;
  35. }
  36. if (aOutput.connectedBlocks.length > 0) {
  37. state.compilationString += this._declareOutput(aOutput, state) + ` = ${input.associatedVariableName}.a;\r\n`;
  38. }
  39. return this;
  40. }
  41. }