vectorSplitterBlock.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.registerInput("xy ", NodeMaterialBlockConnectionPointTypes.Vector2, true);
  20. this.registerOutput("xyz", NodeMaterialBlockConnectionPointTypes.Vector3);
  21. this.registerOutput("xy", NodeMaterialBlockConnectionPointTypes.Vector2);
  22. this.registerOutput("x", NodeMaterialBlockConnectionPointTypes.Float);
  23. this.registerOutput("y", NodeMaterialBlockConnectionPointTypes.Float);
  24. this.registerOutput("z", NodeMaterialBlockConnectionPointTypes.Float);
  25. this.registerOutput("w", NodeMaterialBlockConnectionPointTypes.Float);
  26. }
  27. /**
  28. * Gets the current class name
  29. * @returns the class name
  30. */
  31. public getClassName() {
  32. return "VectorSplitterBlock";
  33. }
  34. /**
  35. * Gets the xyzw component (input)
  36. */
  37. public get xyzw(): NodeMaterialConnectionPoint {
  38. return this._inputs[0];
  39. }
  40. /**
  41. * Gets the xyz component (input)
  42. */
  43. public get xyzIn(): NodeMaterialConnectionPoint {
  44. return this._inputs[1];
  45. }
  46. /**
  47. * Gets the xy component (input)
  48. */
  49. public get xyIn(): NodeMaterialConnectionPoint {
  50. return this._inputs[2];
  51. }
  52. /**
  53. * Gets the xyz component (output)
  54. */
  55. public get xyzOut(): NodeMaterialConnectionPoint {
  56. return this._outputs[0];
  57. }
  58. /**
  59. * Gets the xy component (output)
  60. */
  61. public get xyOut(): NodeMaterialConnectionPoint {
  62. return this._outputs[1];
  63. }
  64. /**
  65. * Gets the x component (output)
  66. */
  67. public get x(): NodeMaterialConnectionPoint {
  68. return this._outputs[2];
  69. }
  70. /**
  71. * Gets the y component (output)
  72. */
  73. public get y(): NodeMaterialConnectionPoint {
  74. return this._outputs[3];
  75. }
  76. /**
  77. * Gets the z component (output)
  78. */
  79. public get z(): NodeMaterialConnectionPoint {
  80. return this._outputs[4];
  81. }
  82. /**
  83. * Gets the w component (output)
  84. */
  85. public get w(): NodeMaterialConnectionPoint {
  86. return this._outputs[5];
  87. }
  88. protected _buildBlock(state: NodeMaterialBuildState) {
  89. super._buildBlock(state);
  90. let input = this.xyzw.isConnected ? this.xyzw : this.xyzIn.isConnected ? this.xyzIn : this.xyIn;
  91. let xyzOutput = this._outputs[0];
  92. let xyOutput = this._outputs[1];
  93. let xOutput = this._outputs[2];
  94. let yOutput = this._outputs[3];
  95. let zOutput = this._outputs[4];
  96. let wOutput = this._outputs[5];
  97. if (xyzOutput.hasEndpoints) {
  98. if (input === this.xyIn) {
  99. state.compilationString += this._declareOutput(xyzOutput, state) + ` = vec3(${input.associatedVariableName}, 0.0);\r\n`;
  100. } else {
  101. state.compilationString += this._declareOutput(xyzOutput, state) + ` = ${input.associatedVariableName}.xyz;\r\n`;
  102. }
  103. }
  104. if (xyOutput.hasEndpoints) {
  105. state.compilationString += this._declareOutput(xyOutput, state) + ` = ${input.associatedVariableName}.xy;\r\n`;
  106. }
  107. if (xOutput.hasEndpoints) {
  108. state.compilationString += this._declareOutput(xOutput, state) + ` = ${input.associatedVariableName}.x;\r\n`;
  109. }
  110. if (yOutput.hasEndpoints) {
  111. state.compilationString += this._declareOutput(yOutput, state) + ` = ${input.associatedVariableName}.y;\r\n`;
  112. }
  113. if (zOutput.hasEndpoints) {
  114. state.compilationString += this._declareOutput(zOutput, state) + ` = ${input.associatedVariableName}.z;\r\n`;
  115. }
  116. if (wOutput.hasEndpoints) {
  117. state.compilationString += this._declareOutput(wOutput, state) + ` = ${input.associatedVariableName}.w;\r\n`;
  118. }
  119. return this;
  120. }
  121. }
  122. _TypeStore.RegisteredTypes["BABYLON.VectorSplitterBlock"] = VectorSplitterBlock;