waveBlock.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Wave block
  10. */
  11. export enum WaveBlockKind {
  12. /** SawTooth */
  13. SawTooth,
  14. /** Square */
  15. Square,
  16. /** Triangle */
  17. Triangle
  18. }
  19. /**
  20. * Block used to apply wave operation to floats
  21. */
  22. export class WaveBlock extends NodeMaterialBlock {
  23. /**
  24. * Gets or sets the kibnd of wave to be applied by the block
  25. */
  26. public kind = WaveBlockKind.SawTooth;
  27. /**
  28. * Creates a new WaveBlock
  29. * @param name defines the block name
  30. */
  31. public constructor(name: string) {
  32. super(name, NodeMaterialBlockTargets.Neutral);
  33. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  34. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
  35. this._outputs[0]._typeConnectionSource = this._inputs[0];
  36. this._inputs[0].excludedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Matrix);
  37. }
  38. /**
  39. * Gets the current class name
  40. * @returns the class name
  41. */
  42. public getClassName() {
  43. return "WaveBlock";
  44. }
  45. /**
  46. * Gets the input component
  47. */
  48. public get input(): NodeMaterialConnectionPoint {
  49. return this._inputs[0];
  50. }
  51. /**
  52. * Gets the output component
  53. */
  54. public get output(): NodeMaterialConnectionPoint {
  55. return this._outputs[0];
  56. }
  57. protected _buildBlock(state: NodeMaterialBuildState) {
  58. super._buildBlock(state);
  59. let output = this._outputs[0];
  60. switch (this.kind) {
  61. case WaveBlockKind.SawTooth: {
  62. state.compilationString += this._declareOutput(output, state) + ` = ${this.input.associatedVariableName} - floor(0.5 + ${this.input.associatedVariableName});\r\n`;
  63. break;
  64. }
  65. case WaveBlockKind.Square: {
  66. state.compilationString += this._declareOutput(output, state) + ` = 1.0 - 2.0 * round(fract(${this.input.associatedVariableName}));\r\n`;
  67. break;
  68. }
  69. case WaveBlockKind.Triangle: {
  70. state.compilationString += this._declareOutput(output, state) + ` = 2.0 * abs(2.0 * (${this.input.associatedVariableName} - floor(0.5 + ${this.input.associatedVariableName}))) - 1.0;\r\n`;
  71. break;
  72. }
  73. }
  74. return this;
  75. }
  76. public serialize(): any {
  77. let serializationObject = super.serialize();
  78. serializationObject.kind = this.kind;
  79. return serializationObject;
  80. }
  81. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  82. super._deserialize(serializationObject, scene, rootUrl);
  83. this.kind = serializationObject.kind;
  84. }
  85. }
  86. _TypeStore.RegisteredTypes["BABYLON.WaveBlock"] = WaveBlock;