rotate2dBlock.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import { NodeMaterial } from '../nodeMaterial';
  8. import { InputBlock } from './Input/inputBlock';
  9. /**
  10. * Block used to rotate a 2d vector by a given angle
  11. */
  12. export class Rotate2dBlock extends NodeMaterialBlock {
  13. /**
  14. * Creates a new Rotate2dBlock
  15. * @param name defines the block name
  16. */
  17. public constructor(name: string) {
  18. super(name, NodeMaterialBlockTargets.Neutral);
  19. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.Vector2);
  20. this.registerInput("angle", NodeMaterialBlockConnectionPointTypes.Float);
  21. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Vector2);
  22. }
  23. /**
  24. * Gets the current class name
  25. * @returns the class name
  26. */
  27. public getClassName() {
  28. return "Rotate2dBlock";
  29. }
  30. /**
  31. * Gets the input vector
  32. */
  33. public get input(): NodeMaterialConnectionPoint {
  34. return this._inputs[0];
  35. }
  36. /**
  37. * Gets the input angle
  38. */
  39. public get angle(): NodeMaterialConnectionPoint {
  40. return this._inputs[1];
  41. }
  42. /**
  43. * Gets the output component
  44. */
  45. public get output(): NodeMaterialConnectionPoint {
  46. return this._outputs[0];
  47. }
  48. public autoConfigure(material: NodeMaterial) {
  49. if (!this.angle.isConnected) {
  50. let angleInput = new InputBlock("angle");
  51. angleInput.value = 0;
  52. angleInput.output.connectTo(this.angle);
  53. }
  54. }
  55. protected _buildBlock(state: NodeMaterialBuildState) {
  56. super._buildBlock(state);
  57. let output = this._outputs[0];
  58. let angle = this.angle;
  59. let input = this.input;
  60. state.compilationString += this._declareOutput(output, state) + ` = vec2(cos(${angle.associatedVariableName}) * ${input.associatedVariableName}.x - sin(${angle.associatedVariableName}) * ${input.associatedVariableName}.y, sin(${angle.associatedVariableName}) * ${input.associatedVariableName}.x + cos(${angle.associatedVariableName}) * ${input.associatedVariableName}.y);\r\n`;
  61. return this;
  62. }
  63. }
  64. _TypeStore.RegisteredTypes["BABYLON.Rotate2dBlock"] = Rotate2dBlock;