vectorSplitterBlock.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 expand a Vector3/4 into 4 outputs (one for each component)
  9. */
  10. export class VectorSplitterBlock extends NodeMaterialBlock {
  11. /**
  12. * Create a new VectorSplitterBlock
  13. * @param name defines the block name
  14. */
  15. public constructor(name: string) {
  16. super(name, NodeMaterialBlockTargets.Fragment);
  17. this.registerInput("xyzw", NodeMaterialBlockConnectionPointTypes.Vector4, true);
  18. this.registerInput("xyz", NodeMaterialBlockConnectionPointTypes.Vector3, true);
  19. this.registerOutput("xyz", NodeMaterialBlockConnectionPointTypes.Vector3);
  20. this.registerOutput("x", NodeMaterialBlockConnectionPointTypes.Float);
  21. this.registerOutput("y", NodeMaterialBlockConnectionPointTypes.Float);
  22. this.registerOutput("z", NodeMaterialBlockConnectionPointTypes.Float);
  23. this.registerOutput("w", NodeMaterialBlockConnectionPointTypes.Float);
  24. }
  25. /**
  26. * Gets the current class name
  27. * @returns the class name
  28. */
  29. public getClassName() {
  30. return "VectorSplitterBlock";
  31. }
  32. /**
  33. * Gets the rgba input component
  34. */
  35. public get xyzw(): NodeMaterialConnectionPoint {
  36. return this._inputs[0];
  37. }
  38. /**
  39. * Gets the rgb input component
  40. */
  41. public get xyz(): NodeMaterialConnectionPoint {
  42. return this._inputs[1];
  43. }
  44. protected _buildBlock(state: NodeMaterialBuildState) {
  45. super._buildBlock(state);
  46. let input = this.xyzw.isConnected ? this.xyzw : this.xyz;
  47. let xyzOutput = this._outputs[0];
  48. let xOutput = this._outputs[1];
  49. let yOutput = this._outputs[2];
  50. let zOutput = this._outputs[3];
  51. let wOutput = this._outputs[4];
  52. if (xyzOutput.connectedBlocks.length > 0) {
  53. state.compilationString += this._declareOutput(xyzOutput, state) + ` = ${input.associatedVariableName}.xyz;\r\n`;
  54. }
  55. if (xOutput.connectedBlocks.length > 0) {
  56. state.compilationString += this._declareOutput(xOutput, state) + ` = ${input.associatedVariableName}.x;\r\n`;
  57. }
  58. if (yOutput.connectedBlocks.length > 0) {
  59. state.compilationString += this._declareOutput(yOutput, state) + ` = ${input.associatedVariableName}.y;\r\n`;
  60. }
  61. if (zOutput.connectedBlocks.length > 0) {
  62. state.compilationString += this._declareOutput(zOutput, state) + ` = ${input.associatedVariableName}.z;\r\n`;
  63. }
  64. if (wOutput.connectedBlocks.length > 0) {
  65. state.compilationString += this._declareOutput(wOutput, state) + ` = ${input.associatedVariableName}.w;\r\n`;
  66. }
  67. return this;
  68. }
  69. }
  70. _TypeStore.RegisteredTypes["BABYLON.VectorSplitterBlock"] = VectorSplitterBlock;