subSurfaceSceneComponent.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { Scene } from "../scene.js";
  2. import { SceneComponentConstants } from "../sceneComponent.js";
  3. import { SubSurfaceConfiguration } from "./subSurfaceConfiguration.js";
  4. import { AbstractScene } from "../abstractScene.js";
  5. import { Color3 } from "../Maths/math.color.js";
  6. // Adds the parser to the scene parsers.
  7. AbstractScene.AddParser(SceneComponentConstants.NAME_SUBSURFACE, (parsedData, scene) => {
  8. // Diffusion profiles
  9. if (parsedData.ssDiffusionProfileColors !== undefined && parsedData.ssDiffusionProfileColors !== null) {
  10. scene.enableSubSurfaceForPrePass();
  11. if (scene.subSurfaceConfiguration) {
  12. for (let index = 0, cache = parsedData.ssDiffusionProfileColors.length; index < cache; index++) {
  13. const color = parsedData.ssDiffusionProfileColors[index];
  14. scene.subSurfaceConfiguration.addDiffusionProfile(new Color3(color.r, color.g, color.b));
  15. }
  16. }
  17. }
  18. });
  19. Object.defineProperty(Scene.prototype, "subSurfaceConfiguration", {
  20. get: function () {
  21. return this._subSurfaceConfiguration;
  22. },
  23. set: function (value) {
  24. if (value) {
  25. if (this.enablePrePassRenderer()) {
  26. this._subSurfaceConfiguration = value;
  27. }
  28. }
  29. },
  30. enumerable: true,
  31. configurable: true,
  32. });
  33. Scene.prototype.enableSubSurfaceForPrePass = function () {
  34. if (this._subSurfaceConfiguration) {
  35. return this._subSurfaceConfiguration;
  36. }
  37. const prePassRenderer = this.enablePrePassRenderer();
  38. if (prePassRenderer) {
  39. this._subSurfaceConfiguration = new SubSurfaceConfiguration(this);
  40. prePassRenderer.addEffectConfiguration(this._subSurfaceConfiguration);
  41. return this._subSurfaceConfiguration;
  42. }
  43. return null;
  44. };
  45. Scene.prototype.disableSubSurfaceForPrePass = function () {
  46. if (!this._subSurfaceConfiguration) {
  47. return;
  48. }
  49. this._subSurfaceConfiguration.dispose();
  50. this._subSurfaceConfiguration = null;
  51. };
  52. /**
  53. * Defines the Geometry Buffer scene component responsible to manage a G-Buffer useful
  54. * in several rendering techniques.
  55. */
  56. export class SubSurfaceSceneComponent {
  57. /**
  58. * Creates a new instance of the component for the given scene
  59. * @param scene Defines the scene to register the component in
  60. */
  61. constructor(scene) {
  62. /**
  63. * The component name helpful to identify the component in the list of scene components.
  64. */
  65. this.name = SceneComponentConstants.NAME_PREPASSRENDERER;
  66. this.scene = scene;
  67. }
  68. /**
  69. * Registers the component in a given scene
  70. */
  71. register() { }
  72. /**
  73. * Serializes the component data to the specified json object
  74. * @param serializationObject The object to serialize to
  75. */
  76. serialize(serializationObject) {
  77. if (!this.scene.subSurfaceConfiguration) {
  78. return;
  79. }
  80. const ssDiffusionProfileColors = this.scene.subSurfaceConfiguration.ssDiffusionProfileColors;
  81. serializationObject.ssDiffusionProfileColors = [];
  82. for (let i = 0; i < ssDiffusionProfileColors.length; i++) {
  83. serializationObject.ssDiffusionProfileColors.push({
  84. r: ssDiffusionProfileColors[i].r,
  85. g: ssDiffusionProfileColors[i].g,
  86. b: ssDiffusionProfileColors[i].b,
  87. });
  88. }
  89. }
  90. /**
  91. * Adds all the elements from the container to the scene
  92. */
  93. addFromContainer() {
  94. // Nothing to do
  95. }
  96. /**
  97. * Removes all the elements in the container from the scene
  98. */
  99. removeFromContainer() {
  100. // Make sure nothing will be serialized
  101. if (!this.scene.prePassRenderer) {
  102. return;
  103. }
  104. if (this.scene.subSurfaceConfiguration) {
  105. this.scene.subSurfaceConfiguration.clearAllDiffusionProfiles();
  106. }
  107. }
  108. /**
  109. * Rebuilds the elements related to this component in case of
  110. * context lost for instance.
  111. */
  112. rebuild() {
  113. // Nothing to do for this component
  114. }
  115. /**
  116. * Disposes the component and the associated resources
  117. */
  118. dispose() {
  119. // Nothing to do for this component
  120. }
  121. }
  122. SubSurfaceConfiguration._SceneComponentInitialization = (scene) => {
  123. // Register the G Buffer component to the scene.
  124. let component = scene._getComponent(SceneComponentConstants.NAME_SUBSURFACE);
  125. if (!component) {
  126. component = new SubSurfaceSceneComponent(scene);
  127. scene._addComponent(component);
  128. }
  129. };
  130. //# sourceMappingURL=subSurfaceSceneComponent.js.map