effectFallbacks.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * EffectFallbacks can be used to add fallbacks (properties to disable) to certain properties when desired to improve performance.
  3. * (Eg. Start at high quality with reflection and fog, if fps is low, remove reflection, if still low remove fog)
  4. */
  5. export class EffectFallbacks {
  6. constructor() {
  7. this._defines = {};
  8. this._currentRank = 32;
  9. this._maxRank = -1;
  10. this._mesh = null;
  11. }
  12. /**
  13. * Removes the fallback from the bound mesh.
  14. */
  15. unBindMesh() {
  16. this._mesh = null;
  17. }
  18. /**
  19. * Adds a fallback on the specified property.
  20. * @param rank The rank of the fallback (Lower ranks will be fallbacked to first)
  21. * @param define The name of the define in the shader
  22. */
  23. addFallback(rank, define) {
  24. if (!this._defines[rank]) {
  25. if (rank < this._currentRank) {
  26. this._currentRank = rank;
  27. }
  28. if (rank > this._maxRank) {
  29. this._maxRank = rank;
  30. }
  31. this._defines[rank] = new Array();
  32. }
  33. this._defines[rank].push(define);
  34. }
  35. /**
  36. * Sets the mesh to use CPU skinning when needing to fallback.
  37. * @param rank The rank of the fallback (Lower ranks will be fallbacked to first)
  38. * @param mesh The mesh to use the fallbacks.
  39. */
  40. addCPUSkinningFallback(rank, mesh) {
  41. this._mesh = mesh;
  42. if (rank < this._currentRank) {
  43. this._currentRank = rank;
  44. }
  45. if (rank > this._maxRank) {
  46. this._maxRank = rank;
  47. }
  48. }
  49. /**
  50. * Checks to see if more fallbacks are still available.
  51. */
  52. get hasMoreFallbacks() {
  53. return this._currentRank <= this._maxRank;
  54. }
  55. /**
  56. * Removes the defines that should be removed when falling back.
  57. * @param currentDefines defines the current define statements for the shader.
  58. * @param effect defines the current effect we try to compile
  59. * @returns The resulting defines with defines of the current rank removed.
  60. */
  61. reduce(currentDefines, effect) {
  62. // First we try to switch to CPU skinning
  63. if (this._mesh && this._mesh.computeBonesUsingShaders && this._mesh.numBoneInfluencers > 0) {
  64. this._mesh.computeBonesUsingShaders = false;
  65. currentDefines = currentDefines.replace("#define NUM_BONE_INFLUENCERS " + this._mesh.numBoneInfluencers, "#define NUM_BONE_INFLUENCERS 0");
  66. effect._bonesComputationForcedToCPU = true;
  67. const scene = this._mesh.getScene();
  68. for (let index = 0; index < scene.meshes.length; index++) {
  69. const otherMesh = scene.meshes[index];
  70. if (!otherMesh.material) {
  71. if (!this._mesh.material && otherMesh.computeBonesUsingShaders && otherMesh.numBoneInfluencers > 0) {
  72. otherMesh.computeBonesUsingShaders = false;
  73. }
  74. continue;
  75. }
  76. if (!otherMesh.computeBonesUsingShaders || otherMesh.numBoneInfluencers === 0) {
  77. continue;
  78. }
  79. if (otherMesh.material.getEffect() === effect) {
  80. otherMesh.computeBonesUsingShaders = false;
  81. }
  82. else if (otherMesh.subMeshes) {
  83. for (const subMesh of otherMesh.subMeshes) {
  84. const subMeshEffect = subMesh.effect;
  85. if (subMeshEffect === effect) {
  86. otherMesh.computeBonesUsingShaders = false;
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. else {
  94. const currentFallbacks = this._defines[this._currentRank];
  95. if (currentFallbacks) {
  96. for (let index = 0; index < currentFallbacks.length; index++) {
  97. currentDefines = currentDefines.replace("#define " + currentFallbacks[index], "");
  98. }
  99. }
  100. this._currentRank++;
  101. }
  102. return currentDefines;
  103. }
  104. }
  105. //# sourceMappingURL=effectFallbacks.js.map