vector2TransformBlock.ts 2.3 KB

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