remapBlock.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 { Vector2 } from '../../../Maths/math.vector';
  8. import { Scene } from '../../../scene';
  9. import { editableInPropertyPage, PropertyTypeForEdition } from "../nodeMaterialDecorator";
  10. /**
  11. * Block used to remap a float from a range to a new one
  12. */
  13. export class RemapBlock extends NodeMaterialBlock {
  14. /**
  15. * Gets or sets the source range
  16. */
  17. @editableInPropertyPage("From", PropertyTypeForEdition.Vector2)
  18. public sourceRange = new Vector2(-1, 1);
  19. /**
  20. * Gets or sets the target range
  21. */
  22. @editableInPropertyPage("To", PropertyTypeForEdition.Vector2)
  23. public targetRange = new Vector2(0, 1);
  24. /**
  25. * Creates a new RemapBlock
  26. * @param name defines the block name
  27. */
  28. public constructor(name: string) {
  29. super(name, NodeMaterialBlockTargets.Neutral);
  30. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  31. this.registerInput("sourceMin", NodeMaterialBlockConnectionPointTypes.Float, true);
  32. this.registerInput("sourceMax", NodeMaterialBlockConnectionPointTypes.Float, true);
  33. this.registerInput("targetMin", NodeMaterialBlockConnectionPointTypes.Float, true);
  34. this.registerInput("targetMax", NodeMaterialBlockConnectionPointTypes.Float, true);
  35. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
  36. this._outputs[0]._typeConnectionSource = this._inputs[0];
  37. }
  38. /**
  39. * Gets the current class name
  40. * @returns the class name
  41. */
  42. public getClassName() {
  43. return "RemapBlock";
  44. }
  45. /**
  46. * Gets the input component
  47. */
  48. public get input(): NodeMaterialConnectionPoint {
  49. return this._inputs[0];
  50. }
  51. /**
  52. * Gets the source min input component
  53. */
  54. public get sourceMin(): NodeMaterialConnectionPoint {
  55. return this._inputs[1];
  56. }
  57. /**
  58. * Gets the source max input component
  59. */
  60. public get sourceMax(): NodeMaterialConnectionPoint {
  61. return this._inputs[2];
  62. }
  63. /**
  64. * Gets the target min input component
  65. */
  66. public get targetMin(): NodeMaterialConnectionPoint {
  67. return this._inputs[3];
  68. }
  69. /**
  70. * Gets the target max input component
  71. */
  72. public get targetMax(): NodeMaterialConnectionPoint {
  73. return this._inputs[4];
  74. }
  75. /**
  76. * Gets the output component
  77. */
  78. public get output(): NodeMaterialConnectionPoint {
  79. return this._outputs[0];
  80. }
  81. protected _buildBlock(state: NodeMaterialBuildState) {
  82. super._buildBlock(state);
  83. let output = this._outputs[0];
  84. let sourceMin = this.sourceMin.isConnected ? this.sourceMin.associatedVariableName : this._writeFloat(this.sourceRange.x);
  85. let sourceMax = this.sourceMax.isConnected ? this.sourceMax.associatedVariableName : this._writeFloat(this.sourceRange.y);
  86. let targetMin = this.targetMin.isConnected ? this.targetMin.associatedVariableName : this._writeFloat(this.targetRange.x);
  87. let targetMax = this.targetMax.isConnected ? this.targetMax.associatedVariableName : this._writeFloat(this.targetRange.y);
  88. state.compilationString += this._declareOutput(output, state) + ` = ${targetMin} + (${this._inputs[0].associatedVariableName} - ${sourceMin}) * (${targetMax} - ${targetMin}) / (${sourceMax} - ${sourceMin});\r\n`;
  89. return this;
  90. }
  91. protected _dumpPropertiesCode() {
  92. var codeString = `${this._codeVariableName}.sourceRange = new BABYLON.Vector2(${this.sourceRange.x}, ${this.sourceRange.y});\r\n`;
  93. codeString += `${this._codeVariableName}.targetRange = new BABYLON.Vector2(${this.targetRange.x}, ${this.targetRange.y});\r\n`;
  94. return codeString;
  95. }
  96. public serialize(): any {
  97. let serializationObject = super.serialize();
  98. serializationObject.sourceRange = this.sourceRange.asArray();
  99. serializationObject.targetRange = this.targetRange.asArray();
  100. return serializationObject;
  101. }
  102. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  103. super._deserialize(serializationObject, scene, rootUrl);
  104. this.sourceRange = Vector2.FromArray(serializationObject.sourceRange);
  105. this.targetRange = Vector2.FromArray(serializationObject.targetRange);
  106. }
  107. }
  108. _TypeStore.RegisteredTypes["BABYLON.RemapBlock"] = RemapBlock;