vectorMergerBlock.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. protected _buildBlock(state: NodeMaterialBuildState) {
  89. super._buildBlock(state);
  90. let xInput = this.x;
  91. let yInput = this.y;
  92. let zInput = this.z;
  93. let wInput = this.w;
  94. let xyInput = this.xyIn;
  95. let xyzInput = this.xyzIn;
  96. let v4Output = this._outputs[0];
  97. let v3Output = this._outputs[1];
  98. let v2Output = this._outputs[2];
  99. if (xyInput.isConnected) {
  100. if (v4Output.hasEndpoints) {
  101. 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`;
  102. } else if (v3Output.hasEndpoints) {
  103. state.compilationString += this._declareOutput(v3Output, state) + ` = vec3(${xyInput.associatedVariableName}, ${zInput.isConnected ? this._writeVariable(zInput) : "0.0"});\r\n`;
  104. } else if (v2Output.hasEndpoints) {
  105. state.compilationString += this._declareOutput(v2Output, state) + ` = ${xyInput.associatedVariableName};\r\n`;
  106. }
  107. } else if (xyzInput.isConnected) {
  108. if (v4Output.hasEndpoints) {
  109. state.compilationString += this._declareOutput(v4Output, state) + ` = vec4(${xyzInput.associatedVariableName}, ${wInput.isConnected ? this._writeVariable(wInput) : "0.0"});\r\n`;
  110. } else if (v3Output.hasEndpoints) {
  111. state.compilationString += this._declareOutput(v3Output, state) + ` = ${xyzInput.associatedVariableName};\r\n`;
  112. } else if (v2Output.hasEndpoints) {
  113. state.compilationString += this._declareOutput(v2Output, state) + ` = ${xyzInput.associatedVariableName}.xy;\r\n`;
  114. }
  115. } else {
  116. if (v4Output.hasEndpoints) {
  117. 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`;
  118. } else if (v3Output.hasEndpoints) {
  119. 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`;
  120. } else if (v2Output.hasEndpoints) {
  121. state.compilationString += this._declareOutput(v2Output, state) + ` = vec2(${xInput.isConnected ? this._writeVariable(xInput) : "0.0"}, ${yInput.isConnected ? this._writeVariable(yInput) : "0.0"});\r\n`;
  122. }
  123. }
  124. return this;
  125. }
  126. }
  127. _TypeStore.RegisteredTypes["BABYLON.VectorMergerBlock"] = VectorMergerBlock;