vector4TransformBlock.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /**
  7. * Block used to transform a vector4 with a matrix
  8. */
  9. export class Vector4TransformBlock extends NodeMaterialBlock {
  10. /**
  11. * Defines the value to use to complement Vector3 to transform it to a Vector4
  12. */
  13. public complementW = 1;
  14. /**
  15. * Creates a new Vector4TransformBlock
  16. * @param name defines the block name
  17. */
  18. public constructor(name: string) {
  19. super(name, NodeMaterialBlockTargets.Vertex);
  20. this.registerInput("vector", NodeMaterialBlockConnectionPointTypes.Vector3OrVector4);
  21. this.registerInput("transform", NodeMaterialBlockConnectionPointTypes.Matrix);
  22. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Vector4);
  23. }
  24. /**
  25. * Gets the current class name
  26. * @returns the class name
  27. */
  28. public getClassName() {
  29. return "Vector4TransformBlock";
  30. }
  31. /**
  32. * Gets the vector input
  33. */
  34. public get vector(): NodeMaterialConnectionPoint {
  35. return this._inputs[0];
  36. }
  37. /**
  38. * Gets the output component
  39. */
  40. public get output(): NodeMaterialConnectionPoint {
  41. return this._outputs[0];
  42. }
  43. /**
  44. * Gets the matrix transform input
  45. */
  46. public get transform(): NodeMaterialConnectionPoint {
  47. return this._inputs[1];
  48. }
  49. protected _buildBlock(state: NodeMaterialBuildState) {
  50. super._buildBlock(state);
  51. let output = this._outputs[0];
  52. let vector = this.vector;
  53. let transform = this.transform;
  54. if (vector.connectedPoint && vector.connectedPoint!.type === NodeMaterialBlockConnectionPointTypes.Vector3) {
  55. state.compilationString += this._declareOutput(output, state) + ` = ${transform.associatedVariableName} * vec4(${vector.associatedVariableName}, ${this._writeFloat(this.complementW)});\r\n`;
  56. } else {
  57. state.compilationString += this._declareOutput(output, state) + ` = ${transform.associatedVariableName} * ${vector.associatedVariableName};\r\n`;
  58. }
  59. return this;
  60. }
  61. }