fresnelParameters.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { DeepCopier } from "../Misc/deepCopier.js";
  2. import { Color3 } from "../Maths/math.color.js";
  3. import { Engine } from "../Engines/engine.js";
  4. import { SerializationHelper } from "../Misc/decorators.serialization.js";
  5. /**
  6. * This represents all the required information to add a fresnel effect on a material:
  7. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/fresnelParameters
  8. */
  9. export class FresnelParameters {
  10. /**
  11. * Define if the fresnel effect is enable or not.
  12. */
  13. get isEnabled() {
  14. return this._isEnabled;
  15. }
  16. set isEnabled(value) {
  17. if (this._isEnabled === value) {
  18. return;
  19. }
  20. this._isEnabled = value;
  21. Engine.MarkAllMaterialsAsDirty(4 | 16);
  22. }
  23. /**
  24. * Creates a new FresnelParameters object.
  25. *
  26. * @param options provide your own settings to optionally to override defaults
  27. */
  28. constructor(options = {}) {
  29. this._isEnabled = true;
  30. this.bias = options.bias === undefined ? 0 : options.bias;
  31. this.power = options.power === undefined ? 1 : options.power;
  32. this.leftColor = options.leftColor || Color3.White();
  33. this.rightColor = options.rightColor || Color3.Black();
  34. if (options.isEnabled === false) {
  35. this.isEnabled = false;
  36. }
  37. }
  38. /**
  39. * Clones the current fresnel and its values
  40. * @returns a clone fresnel configuration
  41. */
  42. clone() {
  43. const newFresnelParameters = new FresnelParameters();
  44. DeepCopier.DeepCopy(this, newFresnelParameters);
  45. return newFresnelParameters;
  46. }
  47. /**
  48. * Determines equality between FresnelParameters objects
  49. * @param otherFresnelParameters defines the second operand
  50. * @returns true if the power, bias, leftColor, rightColor and isEnabled values are equal to the given ones
  51. */
  52. equals(otherFresnelParameters) {
  53. return (otherFresnelParameters &&
  54. this.bias === otherFresnelParameters.bias &&
  55. this.power === otherFresnelParameters.power &&
  56. this.leftColor.equals(otherFresnelParameters.leftColor) &&
  57. this.rightColor.equals(otherFresnelParameters.rightColor) &&
  58. this.isEnabled === otherFresnelParameters.isEnabled);
  59. }
  60. /**
  61. * Serializes the current fresnel parameters to a JSON representation.
  62. * @returns the JSON serialization
  63. */
  64. serialize() {
  65. return {
  66. isEnabled: this.isEnabled,
  67. leftColor: this.leftColor.asArray(),
  68. rightColor: this.rightColor.asArray(),
  69. bias: this.bias,
  70. power: this.power,
  71. };
  72. }
  73. /**
  74. * Parse a JSON object and deserialize it to a new Fresnel parameter object.
  75. * @param parsedFresnelParameters Define the JSON representation
  76. * @returns the parsed parameters
  77. */
  78. static Parse(parsedFresnelParameters) {
  79. return new FresnelParameters({
  80. isEnabled: parsedFresnelParameters.isEnabled,
  81. leftColor: Color3.FromArray(parsedFresnelParameters.leftColor),
  82. rightColor: Color3.FromArray(parsedFresnelParameters.rightColor),
  83. bias: parsedFresnelParameters.bias,
  84. power: parsedFresnelParameters.power || 1.0,
  85. });
  86. }
  87. }
  88. // References the dependencies.
  89. SerializationHelper._FresnelParametersParser = FresnelParameters.Parse;
  90. //# sourceMappingURL=fresnelParameters.js.map