materialPluginBase.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { __decorate } from "../tslib.es6.js";
  2. import { serialize } from "../Misc/decorators.js";
  3. import { MaterialPluginManager } from "./materialPluginManager.js";
  4. import { SerializationHelper } from "../Misc/decorators.serialization.js";
  5. /**
  6. * Base class for material plugins.
  7. * @since 5.0
  8. */
  9. export class MaterialPluginBase {
  10. _enable(enable) {
  11. if (enable) {
  12. this._pluginManager._activatePlugin(this);
  13. }
  14. }
  15. /**
  16. * Creates a new material plugin
  17. * @param material parent material of the plugin
  18. * @param name name of the plugin
  19. * @param priority priority of the plugin
  20. * @param defines list of defines used by the plugin. The value of the property is the default value for this property
  21. * @param addToPluginList true to add the plugin to the list of plugins managed by the material plugin manager of the material (default: true)
  22. * @param enable true to enable the plugin (it is handy if the plugin does not handle properties to switch its current activation)
  23. * @param resolveIncludes Indicates that any #include directive in the plugin code must be replaced by the corresponding code (default: false)
  24. */
  25. constructor(material, name, priority, defines, addToPluginList = true, enable = false, resolveIncludes = false) {
  26. /**
  27. * Defines the priority of the plugin. Lower numbers run first.
  28. */
  29. this.priority = 500;
  30. /**
  31. * Indicates that any #include directive in the plugin code must be replaced by the corresponding code.
  32. */
  33. this.resolveIncludes = false;
  34. /**
  35. * Indicates that this plugin should be notified for the extra events (HasRenderTargetTextures / FillRenderTargetTextures / HardBindForSubMesh)
  36. */
  37. this.registerForExtraEvents = false;
  38. this._material = material;
  39. this.name = name;
  40. this.priority = priority;
  41. this.resolveIncludes = resolveIncludes;
  42. if (!material.pluginManager) {
  43. material.pluginManager = new MaterialPluginManager(material);
  44. material.onDisposeObservable.add(() => {
  45. material.pluginManager = undefined;
  46. });
  47. }
  48. this._pluginDefineNames = defines;
  49. this._pluginManager = material.pluginManager;
  50. if (addToPluginList) {
  51. this._pluginManager._addPlugin(this);
  52. }
  53. if (enable) {
  54. this._enable(true);
  55. }
  56. this.markAllDefinesAsDirty = material._dirtyCallbacks[63];
  57. }
  58. /**
  59. * Gets the current class name useful for serialization or dynamic coding.
  60. * @returns The class name.
  61. */
  62. getClassName() {
  63. return "MaterialPluginBase";
  64. }
  65. /**
  66. * Specifies that the submesh is ready to be used.
  67. * @param defines the list of "defines" to update.
  68. * @param scene defines the scene the material belongs to.
  69. * @param engine the engine this scene belongs to.
  70. * @param subMesh the submesh to check for readiness
  71. * @returns - boolean indicating that the submesh is ready or not.
  72. */
  73. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  74. isReadyForSubMesh(defines, scene, engine, subMesh) {
  75. return true;
  76. }
  77. /**
  78. * Binds the material data (this function is called even if mustRebind() returns false)
  79. * @param uniformBuffer defines the Uniform buffer to fill in.
  80. * @param scene defines the scene the material belongs to.
  81. * @param engine defines the engine the material belongs to.
  82. * @param subMesh the submesh to bind data for
  83. */
  84. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  85. hardBindForSubMesh(uniformBuffer, scene, engine, subMesh) { }
  86. /**
  87. * Binds the material data.
  88. * @param uniformBuffer defines the Uniform buffer to fill in.
  89. * @param scene defines the scene the material belongs to.
  90. * @param engine the engine this scene belongs to.
  91. * @param subMesh the submesh to bind data for
  92. */
  93. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  94. bindForSubMesh(uniformBuffer, scene, engine, subMesh) { }
  95. /**
  96. * Disposes the resources of the material.
  97. * @param forceDisposeTextures - Forces the disposal of all textures.
  98. */
  99. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  100. dispose(forceDisposeTextures) { }
  101. /**
  102. * Returns a list of custom shader code fragments to customize the shader.
  103. * @param shaderType "vertex" or "fragment"
  104. * @returns null if no code to be added, or a list of pointName =\> code.
  105. * Note that `pointName` can also be a regular expression if it starts with a `!`.
  106. * In that case, the string found by the regular expression (if any) will be
  107. * replaced by the code provided.
  108. */
  109. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  110. getCustomCode(shaderType) {
  111. return null;
  112. }
  113. /**
  114. * Collects all defines.
  115. * @param defines The object to append to.
  116. */
  117. collectDefines(defines) {
  118. if (!this._pluginDefineNames) {
  119. return;
  120. }
  121. for (const key of Object.keys(this._pluginDefineNames)) {
  122. if (key[0] === "_") {
  123. continue;
  124. }
  125. const type = typeof this._pluginDefineNames[key];
  126. defines[key] = {
  127. type: type === "number" ? "number" : type === "string" ? "string" : type === "boolean" ? "boolean" : "object",
  128. default: this._pluginDefineNames[key],
  129. };
  130. }
  131. }
  132. /**
  133. * Sets the defines for the next rendering. Called before PrepareDefinesForAttributes is called.
  134. * @param defines the list of "defines" to update.
  135. * @param scene defines the scene to the material belongs to.
  136. * @param mesh the mesh being rendered
  137. */
  138. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  139. prepareDefinesBeforeAttributes(defines, scene, mesh) { }
  140. /**
  141. * Sets the defines for the next rendering
  142. * @param defines the list of "defines" to update.
  143. * @param scene defines the scene to the material belongs to.
  144. * @param mesh the mesh being rendered
  145. */
  146. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  147. prepareDefines(defines, scene, mesh) { }
  148. /**
  149. * Checks to see if a texture is used in the material.
  150. * @param texture - Base texture to use.
  151. * @returns - Boolean specifying if a texture is used in the material.
  152. */
  153. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  154. hasTexture(texture) {
  155. return false;
  156. }
  157. /**
  158. * Gets a boolean indicating that current material needs to register RTT
  159. * @returns true if this uses a render target otherwise false.
  160. */
  161. hasRenderTargetTextures() {
  162. return false;
  163. }
  164. /**
  165. * Fills the list of render target textures.
  166. * @param renderTargets the list of render targets to update
  167. */
  168. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  169. fillRenderTargetTextures(renderTargets) { }
  170. /**
  171. * Returns an array of the actively used textures.
  172. * @param activeTextures Array of BaseTextures
  173. */
  174. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  175. getActiveTextures(activeTextures) { }
  176. /**
  177. * Returns the animatable textures.
  178. * @param animatables Array of animatable textures.
  179. */
  180. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  181. getAnimatables(animatables) { }
  182. /**
  183. * Add fallbacks to the effect fallbacks list.
  184. * @param defines defines the Base texture to use.
  185. * @param fallbacks defines the current fallback list.
  186. * @param currentRank defines the current fallback rank.
  187. * @returns the new fallback rank.
  188. */
  189. addFallbacks(defines, fallbacks, currentRank) {
  190. return currentRank;
  191. }
  192. /**
  193. * Gets the samplers used by the plugin.
  194. * @param samplers list that the sampler names should be added to.
  195. */
  196. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  197. getSamplers(samplers) { }
  198. /**
  199. * Gets the attributes used by the plugin.
  200. * @param attributes list that the attribute names should be added to.
  201. * @param scene the scene that the material belongs to.
  202. * @param mesh the mesh being rendered.
  203. */
  204. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  205. getAttributes(attributes, scene, mesh) { }
  206. /**
  207. * Gets the uniform buffers names added by the plugin.
  208. * @param ubos list that the ubo names should be added to.
  209. */
  210. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  211. getUniformBuffersNames(ubos) { }
  212. /**
  213. * Gets the description of the uniforms to add to the ubo (if engine supports ubos) or to inject directly in the vertex/fragment shaders (if engine does not support ubos)
  214. * @returns the description of the uniforms
  215. */
  216. getUniforms() {
  217. return {};
  218. }
  219. /**
  220. * Makes a duplicate of the current configuration into another one.
  221. * @param plugin define the config where to copy the info
  222. */
  223. copyTo(plugin) {
  224. SerializationHelper.Clone(() => plugin, this);
  225. }
  226. /**
  227. * Serializes this plugin configuration.
  228. * @returns - An object with the serialized config.
  229. */
  230. serialize() {
  231. return SerializationHelper.Serialize(this);
  232. }
  233. /**
  234. * Parses a plugin configuration from a serialized object.
  235. * @param source - Serialized object.
  236. * @param scene Defines the scene we are parsing for
  237. * @param rootUrl Defines the rootUrl to load from
  238. */
  239. parse(source, scene, rootUrl) {
  240. SerializationHelper.Parse(() => this, source, scene, rootUrl);
  241. }
  242. }
  243. __decorate([
  244. serialize()
  245. ], MaterialPluginBase.prototype, "name", void 0);
  246. __decorate([
  247. serialize()
  248. ], MaterialPluginBase.prototype, "priority", void 0);
  249. __decorate([
  250. serialize()
  251. ], MaterialPluginBase.prototype, "resolveIncludes", void 0);
  252. __decorate([
  253. serialize()
  254. ], MaterialPluginBase.prototype, "registerForExtraEvents", void 0);
  255. //# sourceMappingURL=materialPluginBase.js.map