trigonometryBlock.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /** Log */
  31. Log,
  32. /** Tangent */
  33. Tan,
  34. /** Arc tangent */
  35. ArcTan,
  36. /** Arc cosinus */
  37. ArcCos,
  38. /** Arc sinus */
  39. ArcSin,
  40. /** Fraction */
  41. Fract,
  42. /** Sign */
  43. Sign,
  44. /** To radians (from degrees) */
  45. Radians,
  46. /** To degrees (from radians) */
  47. Degrees
  48. }
  49. /**
  50. * Block used to apply trigonometry operation to floats
  51. */
  52. export class TrigonometryBlock extends NodeMaterialBlock {
  53. /**
  54. * Gets or sets the operation applied by the block
  55. */
  56. public operation = TrigonometryBlockOperations.Cos;
  57. /**
  58. * Creates a new TrigonometryBlock
  59. * @param name defines the block name
  60. */
  61. public constructor(name: string) {
  62. super(name, NodeMaterialBlockTargets.Neutral);
  63. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  64. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
  65. this._outputs[0]._typeConnectionSource = this._inputs[0];
  66. }
  67. /**
  68. * Gets the current class name
  69. * @returns the class name
  70. */
  71. public getClassName() {
  72. return "TrigonometryBlock";
  73. }
  74. /**
  75. * Gets the input component
  76. */
  77. public get input(): NodeMaterialConnectionPoint {
  78. return this._inputs[0];
  79. }
  80. /**
  81. * Gets the output component
  82. */
  83. public get output(): NodeMaterialConnectionPoint {
  84. return this._outputs[0];
  85. }
  86. protected _buildBlock(state: NodeMaterialBuildState) {
  87. super._buildBlock(state);
  88. let output = this._outputs[0];
  89. let operation = "";
  90. switch (this.operation) {
  91. case TrigonometryBlockOperations.Cos: {
  92. operation = "cos";
  93. break;
  94. }
  95. case TrigonometryBlockOperations.Sin: {
  96. operation = "sin";
  97. break;
  98. }
  99. case TrigonometryBlockOperations.Abs: {
  100. operation = "abs";
  101. break;
  102. }
  103. case TrigonometryBlockOperations.Exp: {
  104. operation = "exp";
  105. break;
  106. }
  107. case TrigonometryBlockOperations.Exp2: {
  108. operation = "exp2";
  109. break;
  110. }
  111. case TrigonometryBlockOperations.Round: {
  112. operation = "round";
  113. break;
  114. }
  115. case TrigonometryBlockOperations.Floor: {
  116. operation = "floor";
  117. break;
  118. }
  119. case TrigonometryBlockOperations.Ceiling: {
  120. operation = "ceil";
  121. break;
  122. }
  123. case TrigonometryBlockOperations.Sqrt: {
  124. operation = "sqrt";
  125. break;
  126. }
  127. case TrigonometryBlockOperations.Log: {
  128. operation = "log";
  129. break;
  130. }
  131. case TrigonometryBlockOperations.Tan: {
  132. operation = "tan";
  133. break;
  134. }
  135. case TrigonometryBlockOperations.ArcTan: {
  136. operation = "atan";
  137. break;
  138. }
  139. case TrigonometryBlockOperations.ArcCos: {
  140. operation = "acos";
  141. break;
  142. }
  143. case TrigonometryBlockOperations.ArcSin: {
  144. operation = "asin";
  145. break;
  146. }
  147. case TrigonometryBlockOperations.Fract: {
  148. operation = "fract";
  149. break;
  150. }
  151. case TrigonometryBlockOperations.Sign: {
  152. operation = "sign";
  153. break;
  154. }
  155. case TrigonometryBlockOperations.Radians: {
  156. operation = "radians";
  157. break;
  158. }
  159. case TrigonometryBlockOperations.Degrees: {
  160. operation = "degrees";
  161. break;
  162. }
  163. }
  164. state.compilationString += this._declareOutput(output, state) + ` = ${operation}(${this.input.associatedVariableName});\r\n`;
  165. return this;
  166. }
  167. public serialize(): any {
  168. let serializationObject = super.serialize();
  169. serializationObject.operation = this.operation;
  170. return serializationObject;
  171. }
  172. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  173. super._deserialize(serializationObject, scene, rootUrl);
  174. this.operation = serializationObject.operation;
  175. }
  176. protected _dumpPropertiesCode() {
  177. var codeString = `${this._codeVariableName}.operation = BABYLON.TrigonometryBlockOperations.${TrigonometryBlockOperations[this.operation]};\r\n`;
  178. return codeString;
  179. }
  180. }
  181. _TypeStore.RegisteredTypes["BABYLON.TrigonometryBlock"] = TrigonometryBlock;