vectorMergerBlock.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 create a Vector2/3/4 out of individual inputs (one for each component)
  9. */
  10. export class VectorMergerBlock extends NodeMaterialBlock {
  11. /**
  12. * Create a new VectorMergerBlock
  13. * @param name defines the block name
  14. */
  15. public constructor(name: string) {
  16. super(name, NodeMaterialBlockTargets.Neutral);
  17. this.registerInput("xyz ", NodeMaterialBlockConnectionPointTypes.Vector3, true);
  18. this.registerInput("xy ", NodeMaterialBlockConnectionPointTypes.Vector2, true);
  19. this.registerInput("x", NodeMaterialBlockConnectionPointTypes.Float, true);
  20. this.registerInput("y", NodeMaterialBlockConnectionPointTypes.Float, true);
  21. this.registerInput("z", NodeMaterialBlockConnectionPointTypes.Float, true);
  22. this.registerInput("w", NodeMaterialBlockConnectionPointTypes.Float, true);
  23. this.registerOutput("xyzw", NodeMaterialBlockConnectionPointTypes.Vector4);
  24. this.registerOutput("xyz", NodeMaterialBlockConnectionPointTypes.Vector3);
  25. this.registerOutput("xy", NodeMaterialBlockConnectionPointTypes.Vector2);
  26. }
  27. /**
  28. * Gets the current class name
  29. * @returns the class name
  30. */
  31. public getClassName() {
  32. return "VectorMergerBlock";
  33. }
  34. /**
  35. * Gets the xyz component (input)
  36. */
  37. public get xyzIn(): NodeMaterialConnectionPoint {
  38. return this._inputs[0];
  39. }
  40. /**
  41. * Gets the xy component (input)
  42. */
  43. public get xyIn(): NodeMaterialConnectionPoint {
  44. return this._inputs[1];
  45. }
  46. /**
  47. * Gets the x component (input)
  48. */
  49. public get x(): NodeMaterialConnectionPoint {
  50. return this._inputs[2];
  51. }
  52. /**
  53. * Gets the y component (input)
  54. */
  55. public get y(): NodeMaterialConnectionPoint {
  56. return this._inputs[3];
  57. }
  58. /**
  59. * Gets the z component (input)
  60. */
  61. public get z(): NodeMaterialConnectionPoint {
  62. return this._inputs[4];
  63. }
  64. /**
  65. * Gets the w component (input)
  66. */
  67. public get w(): NodeMaterialConnectionPoint {
  68. return this._inputs[5];
  69. }
  70. /**
  71. * Gets the xyzw component (output)
  72. */
  73. public get xyzw(): NodeMaterialConnectionPoint {
  74. return this._outputs[0];
  75. }
  76. /**
  77. * Gets the xyz component (output)
  78. */
  79. public get xyzOut(): NodeMaterialConnectionPoint {
  80. return this._outputs[1];
  81. }
  82. /**
  83. * Gets the xy component (output)
  84. */
  85. public get xyOut(): NodeMaterialConnectionPoint {
  86. return this._outputs[2];
  87. }
  88. /**
  89. * Gets the xy component (output)
  90. * @deprecated Please use xyOut instead.
  91. */
  92. public get xy(): NodeMaterialConnectionPoint {
  93. return this.xyOut;
  94. }
  95. /**
  96. * Gets the xyz component (output)
  97. * @deprecated Please use xyzOut instead.
  98. */
  99. public get xyz(): NodeMaterialConnectionPoint {
  100. return this.xyzOut;
  101. }
  102. protected _buildBlock(state: NodeMaterialBuildState) {
  103. super._buildBlock(state);
  104. let xInput = this.x;
  105. let yInput = this.y;
  106. let zInput = this.z;
  107. let wInput = this.w;
  108. let xyInput = this.xyIn;
  109. let xyzInput = this.xyzIn;
  110. let v4Output = this._outputs[0];
  111. let v3Output = this._outputs[1];
  112. let v2Output = this._outputs[2];
  113. if (xyzInput.isConnected) {
  114. if (v4Output.hasEndpoints) {
  115. state.compilationString += this._declareOutput(v4Output, state) + ` = vec4(${xyzInput.associatedVariableName}, ${wInput.isConnected ? this._writeVariable(wInput) : "0.0"});\r\n`;
  116. } else if (v3Output.hasEndpoints) {
  117. state.compilationString += this._declareOutput(v3Output, state) + ` = ${xyzInput.associatedVariableName};\r\n`;
  118. } else if (v2Output.hasEndpoints) {
  119. state.compilationString += this._declareOutput(v2Output, state) + ` = ${xyzInput.associatedVariableName}.xy;\r\n`;
  120. }
  121. } else if (xyInput.isConnected) {
  122. if (v4Output.hasEndpoints) {
  123. state.compilationString += this._declareOutput(v4Output, state) + ` = vec4(${xyInput.associatedVariableName}, ${zInput.isConnected ? this._writeVariable(zInput) : "0.0"}, ${wInput.isConnected ? this._writeVariable(wInput) : "0.0"});\r\n`;
  124. } else if (v3Output.hasEndpoints) {
  125. state.compilationString += this._declareOutput(v3Output, state) + ` = vec3(${xyInput.associatedVariableName}, ${zInput.isConnected ? this._writeVariable(zInput) : "0.0"});\r\n`;
  126. } else if (v2Output.hasEndpoints) {
  127. state.compilationString += this._declareOutput(v2Output, state) + ` = ${xyInput.associatedVariableName};\r\n`;
  128. }
  129. } else {
  130. if (v4Output.hasEndpoints) {
  131. state.compilationString += this._declareOutput(v4Output, state) + ` = vec4(${xInput.isConnected ? this._writeVariable(xInput) : "0.0"}, ${yInput.isConnected ? this._writeVariable(yInput) : "0.0"}, ${zInput.isConnected ? this._writeVariable(zInput) : "0.0"}, ${wInput.isConnected ? this._writeVariable(wInput) : "0.0"});\r\n`;
  132. } else if (v3Output.hasEndpoints) {
  133. state.compilationString += this._declareOutput(v3Output, state) + ` = vec3(${xInput.isConnected ? this._writeVariable(xInput) : "0.0"}, ${yInput.isConnected ? this._writeVariable(yInput) : "0.0"}, ${zInput.isConnected ? this._writeVariable(zInput) : "0.0"});\r\n`;
  134. } else if (v2Output.hasEndpoints) {
  135. state.compilationString += this._declareOutput(v2Output, state) + ` = vec2(${xInput.isConnected ? this._writeVariable(xInput) : "0.0"}, ${yInput.isConnected ? this._writeVariable(yInput) : "0.0"});\r\n`;
  136. }
  137. }
  138. return this;
  139. }
  140. }
  141. _TypeStore.RegisteredTypes["BABYLON.VectorMergerBlock"] = VectorMergerBlock;