vectorSplitterBlock.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. }
  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 _inputRename(name: string) {
  89. switch (name) {
  90. case "xy ":
  91. return "xyIn";
  92. case "xyz ":
  93. return "xyzIn";
  94. default:
  95. return name;
  96. }
  97. }
  98. protected _outputRename(name: string) {
  99. switch (name) {
  100. case "xy":
  101. return "xyOut";
  102. case "xyz":
  103. return "xyzOut";
  104. default:
  105. return name;
  106. }
  107. }
  108. protected _buildBlock(state: NodeMaterialBuildState) {
  109. super._buildBlock(state);
  110. let input = this.xyzw.isConnected ? this.xyzw : this.xyzIn.isConnected ? this.xyzIn : this.xyIn;
  111. let xyzOutput = this._outputs[0];
  112. let xyOutput = this._outputs[1];
  113. let xOutput = this._outputs[2];
  114. let yOutput = this._outputs[3];
  115. let zOutput = this._outputs[4];
  116. let wOutput = this._outputs[5];
  117. if (xyzOutput.hasEndpoints) {
  118. if (input === this.xyIn) {
  119. state.compilationString += this._declareOutput(xyzOutput, state) + ` = vec3(${input.associatedVariableName}, 0.0);\r\n`;
  120. } else {
  121. state.compilationString += this._declareOutput(xyzOutput, state) + ` = ${input.associatedVariableName}.xyz;\r\n`;
  122. }
  123. }
  124. if (xyOutput.hasEndpoints) {
  125. state.compilationString += this._declareOutput(xyOutput, state) + ` = ${input.associatedVariableName}.xy;\r\n`;
  126. }
  127. if (xOutput.hasEndpoints) {
  128. state.compilationString += this._declareOutput(xOutput, state) + ` = ${input.associatedVariableName}.x;\r\n`;
  129. }
  130. if (yOutput.hasEndpoints) {
  131. state.compilationString += this._declareOutput(yOutput, state) + ` = ${input.associatedVariableName}.y;\r\n`;
  132. }
  133. if (zOutput.hasEndpoints) {
  134. state.compilationString += this._declareOutput(zOutput, state) + ` = ${input.associatedVariableName}.z;\r\n`;
  135. }
  136. if (wOutput.hasEndpoints) {
  137. state.compilationString += this._declareOutput(wOutput, state) + ` = ${input.associatedVariableName}.w;\r\n`;
  138. }
  139. return this;
  140. }
  141. }
  142. _TypeStore.RegisteredTypes["BABYLON.VectorSplitterBlock"] = VectorSplitterBlock;