trigonometryBlock.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { NodeMaterialBlock } from '../nodeMaterialBlock';
  2. import { NodeMaterialBlockConnectionPointTypes } from '../Enums/nodeMaterialBlockConnectionPointTypes';
  3. import { NodeMaterialBuildState } from '../nodeMaterialBuildState';
  4. import { NodeMaterialConnectionPoint } from '../nodeMaterialBlockConnectionPoint';
  5. import { NodeMaterialBlockTargets } from '../Enums/nodeMaterialBlockTargets';
  6. import { _TypeStore } from '../../../Misc/typeStore';
  7. import { Scene } from '../../../scene';
  8. /**
  9. * Operations supported by the Trigonometry block
  10. */
  11. export enum TrigonometryBlockOperations {
  12. /** Cos */
  13. Cos,
  14. /** Sin */
  15. Sin,
  16. /** Abs */
  17. Abs,
  18. /** Exp */
  19. Exp,
  20. /** Exp2 */
  21. Exp2,
  22. /** Round */
  23. Round,
  24. /** Floor */
  25. Floor,
  26. /** Ceiling */
  27. Ceiling,
  28. /** Square root */
  29. Sqrt,
  30. }
  31. /**
  32. * Block used to apply trigonometry operation to floats
  33. */
  34. export class TrigonometryBlock extends NodeMaterialBlock {
  35. /**
  36. * Gets or sets the operation applied by the block
  37. */
  38. public operation = TrigonometryBlockOperations.Cos;
  39. /**
  40. * Creates a new TrigonometryBlock
  41. * @param name defines the block name
  42. */
  43. public constructor(name: string) {
  44. super(name, NodeMaterialBlockTargets.Neutral);
  45. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.Float);
  46. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.Float);
  47. }
  48. /**
  49. * Gets the current class name
  50. * @returns the class name
  51. */
  52. public getClassName() {
  53. return "TrigonometryBlock";
  54. }
  55. /**
  56. * Gets the input component
  57. */
  58. public get input(): NodeMaterialConnectionPoint {
  59. return this._inputs[0];
  60. }
  61. /**
  62. * Gets the output component
  63. */
  64. public get output(): NodeMaterialConnectionPoint {
  65. return this._outputs[0];
  66. }
  67. protected _buildBlock(state: NodeMaterialBuildState) {
  68. super._buildBlock(state);
  69. let output = this._outputs[0];
  70. let operation = "";
  71. switch (this.operation) {
  72. case TrigonometryBlockOperations.Cos: {
  73. operation = "cos";
  74. break;
  75. }
  76. case TrigonometryBlockOperations.Sin: {
  77. operation = "sin";
  78. break;
  79. }
  80. case TrigonometryBlockOperations.Abs: {
  81. operation = "abs";
  82. break;
  83. }
  84. case TrigonometryBlockOperations.Exp: {
  85. operation = "exp";
  86. break;
  87. }
  88. case TrigonometryBlockOperations.Exp2: {
  89. operation = "exp2";
  90. break;
  91. }
  92. case TrigonometryBlockOperations.Round: {
  93. operation = "round";
  94. break;
  95. }
  96. case TrigonometryBlockOperations.Floor: {
  97. operation = "floor";
  98. break;
  99. }
  100. case TrigonometryBlockOperations.Ceiling: {
  101. operation = "ceil";
  102. break;
  103. }
  104. case TrigonometryBlockOperations.Sqrt: {
  105. operation = "sqrt";
  106. break;
  107. }
  108. }
  109. state.compilationString += this._declareOutput(output, state) + ` = ${operation}(${this.input.associatedVariableName});\r\n`;
  110. return this;
  111. }
  112. public serialize(): any {
  113. let serializationObject = super.serialize();
  114. serializationObject.operation = this.operation;
  115. return serializationObject;
  116. }
  117. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  118. super._deserialize(serializationObject, scene, rootUrl);
  119. this.operation = serializationObject.operation;
  120. }
  121. }
  122. _TypeStore.RegisteredTypes["BABYLON.TrigonometryBlock"] = TrigonometryBlock;