pbrSpecularGlossinessMaterial.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { serialize, SerializationHelper, serializeAsColor3, expandToProperty, serializeAsTexture } from "../../Misc/decorators";
  2. import { Scene } from "../../scene";
  3. import { Color3 } from "../../Maths/math.color";
  4. import { BaseTexture } from "../../Materials/Textures/baseTexture";
  5. import { PBRBaseSimpleMaterial } from "./pbrBaseSimpleMaterial";
  6. import { _TypeStore } from '../../Misc/typeStore';
  7. /**
  8. * The PBR material of BJS following the specular glossiness convention.
  9. *
  10. * This fits to the PBR convention in the GLTF definition:
  11. * https://github.com/KhronosGroup/glTF/tree/2.0/extensions/Khronos/KHR_materials_pbrSpecularGlossiness
  12. */
  13. export class PBRSpecularGlossinessMaterial extends PBRBaseSimpleMaterial {
  14. /**
  15. * Specifies the diffuse color of the material.
  16. */
  17. @serializeAsColor3("diffuse")
  18. @expandToProperty("_markAllSubMeshesAsTexturesDirty", "_albedoColor")
  19. public diffuseColor: Color3;
  20. /**
  21. * Specifies the diffuse texture of the material. This can also contains the opcity value in its alpha
  22. * channel.
  23. */
  24. @serializeAsTexture()
  25. @expandToProperty("_markAllSubMeshesAsTexturesDirty", "_albedoTexture")
  26. public diffuseTexture: BaseTexture;
  27. /**
  28. * Specifies the specular color of the material. This indicates how reflective is the material (none to mirror).
  29. */
  30. @serializeAsColor3("specular")
  31. @expandToProperty("_markAllSubMeshesAsTexturesDirty", "_reflectivityColor")
  32. public specularColor: Color3;
  33. /**
  34. * Specifies the glossiness of the material. This indicates "how sharp is the reflection".
  35. */
  36. @serialize()
  37. @expandToProperty("_markAllSubMeshesAsTexturesDirty", "_microSurface")
  38. public glossiness: number;
  39. /**
  40. * Specifies both the specular color RGB and the glossiness A of the material per pixels.
  41. */
  42. @serializeAsTexture()
  43. @expandToProperty("_markAllSubMeshesAsTexturesDirty", "_reflectivityTexture")
  44. public specularGlossinessTexture: BaseTexture;
  45. /**
  46. * Instantiates a new PBRSpecularGlossinessMaterial instance.
  47. *
  48. * @param name The material name
  49. * @param scene The scene the material will be use in.
  50. */
  51. constructor(name: string, scene: Scene) {
  52. super(name, scene);
  53. this._useMicroSurfaceFromReflectivityMapAlpha = true;
  54. }
  55. /**
  56. * Return the currrent class name of the material.
  57. */
  58. public getClassName(): string {
  59. return "PBRSpecularGlossinessMaterial";
  60. }
  61. /**
  62. * Makes a duplicate of the current material.
  63. * @param name - name to use for the new material.
  64. */
  65. public clone(name: string): PBRSpecularGlossinessMaterial {
  66. var clone = SerializationHelper.Clone(() => new PBRSpecularGlossinessMaterial(name, this.getScene()), this);
  67. clone.id = name;
  68. clone.name = name;
  69. this.clearCoat.copyTo(clone.clearCoat);
  70. this.anisotropy.copyTo(clone.anisotropy);
  71. this.brdf.copyTo(clone.brdf);
  72. this.sheen.copyTo(clone.sheen);
  73. this.subSurface.copyTo(clone.subSurface);
  74. return clone;
  75. }
  76. /**
  77. * Serialize the material to a parsable JSON object.
  78. */
  79. public serialize(): any {
  80. var serializationObject = SerializationHelper.Serialize(this);
  81. serializationObject.customType = "BABYLON.PBRSpecularGlossinessMaterial";
  82. serializationObject.clearCoat = this.clearCoat.serialize();
  83. serializationObject.anisotropy = this.anisotropy.serialize();
  84. serializationObject.brdf = this.brdf.serialize();
  85. serializationObject.sheen = this.sheen.serialize();
  86. serializationObject.subSurface = this.subSurface.serialize();
  87. return serializationObject;
  88. }
  89. /**
  90. * Parses a JSON object correponding to the serialize function.
  91. */
  92. public static Parse(source: any, scene: Scene, rootUrl: string): PBRSpecularGlossinessMaterial {
  93. const material = SerializationHelper.Parse(() => new PBRSpecularGlossinessMaterial(source.name, scene), source, scene, rootUrl);
  94. if (source.clearCoat) {
  95. material.clearCoat.parse(source.clearCoat, scene, rootUrl);
  96. }
  97. if (source.anisotropy) {
  98. material.anisotropy.parse(source.anisotropy, scene, rootUrl);
  99. }
  100. if (source.brdf) {
  101. material.brdf.parse(source.brdf, scene, rootUrl);
  102. }
  103. if (source.sheen) {
  104. material.sheen.parse(source.sheen, scene, rootUrl);
  105. }
  106. if (source.subSurface) {
  107. material.subSurface.parse(source.subSurface, scene, rootUrl);
  108. }
  109. return material;
  110. }
  111. }
  112. _TypeStore.RegisteredTypes["BABYLON.PBRSpecularGlossinessMaterial"] = PBRSpecularGlossinessMaterial;