remapBlock.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. import { Vector2 } from '../../../Maths/math.vector';
  8. import { Scene } from '../../../scene';
  9. /**
  10. * Block used to remap a float from a range to a new one
  11. */
  12. export class RemapBlock extends NodeMaterialBlock {
  13. /**
  14. * Gets or sets the source range
  15. */
  16. public sourceRange = new Vector2(-1, 1);
  17. /**
  18. * Gets or sets the target range
  19. */
  20. public targetRange = new Vector2(0, 1);
  21. /**
  22. * Creates a new RemapBlock
  23. * @param name defines the block name
  24. */
  25. public constructor(name: string) {
  26. super(name, NodeMaterialBlockTargets.Neutral);
  27. this.registerInput("input", NodeMaterialBlockConnectionPointTypes.AutoDetect);
  28. this.registerOutput("output", NodeMaterialBlockConnectionPointTypes.BasedOnInput);
  29. this._outputs[0]._typeConnectionSource = this._inputs[0];
  30. }
  31. /**
  32. * Gets the current class name
  33. * @returns the class name
  34. */
  35. public getClassName() {
  36. return "RemapBlock";
  37. }
  38. /**
  39. * Gets the input component
  40. */
  41. public get input(): NodeMaterialConnectionPoint {
  42. return this._inputs[0];
  43. }
  44. /**
  45. * Gets the output component
  46. */
  47. public get output(): NodeMaterialConnectionPoint {
  48. return this._outputs[0];
  49. }
  50. protected _buildBlock(state: NodeMaterialBuildState) {
  51. super._buildBlock(state);
  52. let output = this._outputs[0];
  53. state.compilationString += this._declareOutput(output, state) + ` = ${this._writeFloat(this.targetRange.x)} + (${this._inputs[0].associatedVariableName} - ${this._writeFloat(this.sourceRange.x)}) * (${this._writeFloat(this.targetRange.y)} - ${this._writeFloat(this.targetRange.x)}) / (${this._writeFloat(this.sourceRange.y)} - ${this._writeFloat(this.sourceRange.x)});\r\n`;
  54. return this;
  55. }
  56. protected _dumpPropertiesCode() {
  57. var codeString = `${this._codeVariableName}.sourceRange = new BABYLON.Vector2(${this.sourceRange.x}, ${this.sourceRange.y});\r\n`;
  58. codeString += `${this._codeVariableName}.targetRange = new BABYLON.Vector2(${this.targetRange.x}, ${this.targetRange.y});\r\n`;
  59. return codeString;
  60. }
  61. public serialize(): any {
  62. let serializationObject = super.serialize();
  63. serializationObject.sourceRange = this.sourceRange.asArray();
  64. serializationObject.targetRange = this.targetRange.asArray();
  65. return serializationObject;
  66. }
  67. public _deserialize(serializationObject: any, scene: Scene, rootUrl: string) {
  68. super._deserialize(serializationObject, scene, rootUrl);
  69. this.sourceRange = Vector2.FromArray(serializationObject.sourceRange);
  70. this.targetRange = Vector2.FromArray(serializationObject.targetRange);
  71. }
  72. }
  73. _TypeStore.RegisteredTypes["BABYLON.RemapBlock"] = RemapBlock;