vectorSplitterBlock.ts 5.4 KB

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