babylon.depthRenderer.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var DepthRenderer = (function () {
  4. function DepthRenderer(scene, type) {
  5. var _this = this;
  6. if (type === void 0) { type = BABYLON.Engine.TEXTURETYPE_FLOAT; }
  7. this._viewMatrix = BABYLON.Matrix.Zero();
  8. this._projectionMatrix = BABYLON.Matrix.Zero();
  9. this._transformMatrix = BABYLON.Matrix.Zero();
  10. this._worldViewProjection = BABYLON.Matrix.Zero();
  11. this._scene = scene;
  12. var engine = scene.getEngine();
  13. // Render target
  14. this._depthMap = new BABYLON.RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this._scene, false, true, type);
  15. this._depthMap.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  16. this._depthMap.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  17. this._depthMap.refreshRate = 1;
  18. this._depthMap.renderParticles = false;
  19. this._depthMap.renderList = null;
  20. // Custom render function
  21. var renderSubMesh = function (subMesh) {
  22. var mesh = subMesh.getRenderingMesh();
  23. var scene = _this._scene;
  24. var engine = scene.getEngine();
  25. // Culling
  26. engine.setState(subMesh.getMaterial().backFaceCulling);
  27. // Managing instances
  28. var batch = mesh._getInstancesRenderList(subMesh._id);
  29. if (batch.mustReturn) {
  30. return;
  31. }
  32. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
  33. if (_this.isReady(subMesh, hardwareInstancedRendering)) {
  34. engine.enableEffect(_this._effect);
  35. mesh._bind(subMesh, _this._effect, BABYLON.Material.TriangleFillMode);
  36. var material = subMesh.getMaterial();
  37. _this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  38. _this._effect.setFloat("far", scene.activeCamera.maxZ);
  39. // Alpha test
  40. if (material && material.needAlphaTesting()) {
  41. var alphaTexture = material.getAlphaTestTexture();
  42. _this._effect.setTexture("diffuseSampler", alphaTexture);
  43. _this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  44. }
  45. // Bones
  46. var useBones = mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
  47. if (useBones) {
  48. _this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  49. }
  50. if (hardwareInstancedRendering) {
  51. mesh._renderWithInstances(subMesh, BABYLON.Material.TriangleFillMode, batch, _this._effect, engine);
  52. }
  53. else {
  54. if (batch.renderSelf[subMesh._id]) {
  55. _this._effect.setMatrix("world", mesh.getWorldMatrix());
  56. // Draw
  57. mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
  58. }
  59. if (batch.visibleInstances[subMesh._id]) {
  60. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
  61. var instance = batch.visibleInstances[subMesh._id][instanceIndex];
  62. _this._effect.setMatrix("world", instance.getWorldMatrix());
  63. // Draw
  64. mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
  65. }
  66. }
  67. }
  68. }
  69. };
  70. this._depthMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes) {
  71. var index;
  72. for (index = 0; index < opaqueSubMeshes.length; index++) {
  73. renderSubMesh(opaqueSubMeshes.data[index]);
  74. }
  75. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  76. renderSubMesh(alphaTestSubMeshes.data[index]);
  77. }
  78. };
  79. }
  80. DepthRenderer.prototype.isReady = function (subMesh, useInstances) {
  81. var defines = [];
  82. var attribs = [BABYLON.VertexBuffer.PositionKind];
  83. var mesh = subMesh.getMesh();
  84. var scene = mesh.getScene();
  85. var material = subMesh.getMaterial();
  86. // Alpha test
  87. if (material && material.needAlphaTesting()) {
  88. defines.push("#define ALPHATEST");
  89. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  90. attribs.push(BABYLON.VertexBuffer.UVKind);
  91. defines.push("#define UV1");
  92. }
  93. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  94. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  95. defines.push("#define UV2");
  96. }
  97. }
  98. // Bones
  99. if (mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  100. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  101. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  102. defines.push("#define BONES");
  103. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  104. }
  105. // Instances
  106. if (useInstances) {
  107. defines.push("#define INSTANCES");
  108. attribs.push("world0");
  109. attribs.push("world1");
  110. attribs.push("world2");
  111. attribs.push("world3");
  112. }
  113. // Get correct effect
  114. var join = defines.join("\n");
  115. if (this._cachedDefines !== join) {
  116. this._cachedDefines = join;
  117. this._effect = this._scene.getEngine().createEffect("depth", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "far"], ["diffuseSampler"], join);
  118. }
  119. return this._effect.isReady();
  120. };
  121. DepthRenderer.prototype.getDepthMap = function () {
  122. return this._depthMap;
  123. };
  124. // Methods
  125. DepthRenderer.prototype.dispose = function () {
  126. this._depthMap.dispose();
  127. };
  128. return DepthRenderer;
  129. })();
  130. BABYLON.DepthRenderer = DepthRenderer;
  131. })(BABYLON || (BABYLON = {}));
  132. //# sourceMappingURL=babylon.depthRenderer.js.map