trigonometryBlock.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { NodeMaterialBlock } from '../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
  4. import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
  5. import { NodeMaterialBlockTargets } from '../nodeMaterialBlockTargets';
  6. import { _TypeStore } from '../../../Misc/typeStore';
  7. /**
  8. * Operations supported by the Trigonometry block
  9. */
  10. export enum TrigonometryBlockOperations {
  11. /** Cos */
  12. Cos,
  13. /** Sin */
  14. Sin,
  15. /** Abs */
  16. Abs,
  17. /** Exp */
  18. Exp,
  19. /** Exp2 */
  20. Exp2
  21. }
  22. /**
  23. * Block used to apply trigonometry operation to floats
  24. */
  25. export class TrigonometryBlock extends NodeMaterialBlock {
  26. /**
  27. * Gets or sets the operation applied by the block
  28. */
  29. public operation = TrigonometryBlockOperations.Cos;
  30. /**
  31. * Creates a new TrigonometryBlock
  32. * @param name defines the block name
  33. */
  34. public constructor(name: string) {
  35. super(name, NodeMaterialBlockTargets.Neutral);
  36. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.Float);
  37. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Float);
  38. }
  39. /**
  40. * Gets the current class name
  41. * @returns the class name
  42. */
  43. public getClassName() {
  44. return "TrigonometryBlock";
  45. }
  46. /**
  47. * Gets the input component
  48. */
  49. public get input(): NodeMaterialConnectionPoint {
  50. return this._inputs[0];
  51. }
  52. /**
  53. * Gets the output component
  54. */
  55. public get output(): NodeMaterialConnectionPoint {
  56. return this._outputs[0];
  57. }
  58. protected _buildBlock(state: NodeMaterialBuildState) {
  59. super._buildBlock(state);
  60. let output = this._outputs[0];
  61. let operation = "";
  62. switch (this.operation) {
  63. case TrigonometryBlockOperations.Cos: {
  64. operation = "cos";
  65. break;
  66. }
  67. case TrigonometryBlockOperations.Sin: {
  68. operation = "sin";
  69. break;
  70. }
  71. case TrigonometryBlockOperations.Cos: {
  72. operation = "abs";
  73. break;
  74. }
  75. case TrigonometryBlockOperations.Exp: {
  76. operation = "exp";
  77. break;
  78. }
  79. case TrigonometryBlockOperations.Exp2: {
  80. operation = "exp2";
  81. break;
  82. }
  83. }
  84. state.compilationString += this._declareOutput(output, state) + ` = ${operation}(${this.input.associatedVariableName});\r\n`;
  85. return this;
  86. }
  87. }
  88. _TypeStore.RegisteredTypes["BABYLON.TrigonometryBlock"] = TrigonometryBlock;