babylon.depthRenderer.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var DepthRenderer = (function () {
  4. function DepthRenderer(scene, type) {
  5. if (typeof type === "undefined") { type = BABYLON.Engine.TEXTURETYPE_FLOAT; }
  6. var _this = this;
  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. } else {
  53. if (batch.renderSelf[subMesh._id]) {
  54. _this._effect.setMatrix("world", mesh.getWorldMatrix());
  55. // Draw
  56. mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
  57. }
  58. if (batch.visibleInstances[subMesh._id]) {
  59. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
  60. var instance = batch.visibleInstances[subMesh._id][instanceIndex];
  61. _this._effect.setMatrix("world", instance.getWorldMatrix());
  62. // Draw
  63. mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
  64. }
  65. }
  66. }
  67. }
  68. };
  69. this._depthMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes) {
  70. var index;
  71. for (index = 0; index < opaqueSubMeshes.length; index++) {
  72. renderSubMesh(opaqueSubMeshes.data[index]);
  73. }
  74. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  75. renderSubMesh(alphaTestSubMeshes.data[index]);
  76. }
  77. };
  78. }
  79. DepthRenderer.prototype.isReady = function (subMesh, useInstances) {
  80. var defines = [];
  81. var attribs = [BABYLON.VertexBuffer.PositionKind];
  82. var mesh = subMesh.getMesh();
  83. var scene = mesh.getScene();
  84. var material = subMesh.getMaterial();
  85. // Alpha test
  86. if (material && material.needAlphaTesting()) {
  87. defines.push("#define ALPHATEST");
  88. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  89. attribs.push(BABYLON.VertexBuffer.UVKind);
  90. defines.push("#define UV1");
  91. }
  92. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  93. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  94. defines.push("#define UV2");
  95. }
  96. }
  97. // Bones
  98. if (mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  99. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  100. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  101. defines.push("#define BONES");
  102. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  103. }
  104. // Instances
  105. if (useInstances) {
  106. defines.push("#define INSTANCES");
  107. attribs.push("world0");
  108. attribs.push("world1");
  109. attribs.push("world2");
  110. attribs.push("world3");
  111. }
  112. // Get correct effect
  113. var join = defines.join("\n");
  114. if (this._cachedDefines !== join) {
  115. this._cachedDefines = join;
  116. this._effect = this._scene.getEngine().createEffect("depth", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "far"], ["diffuseSampler"], join);
  117. }
  118. return this._effect.isReady();
  119. };
  120. DepthRenderer.prototype.getDepthMap = function () {
  121. return this._depthMap;
  122. };
  123. // Methods
  124. DepthRenderer.prototype.dispose = function () {
  125. this._depthMap.dispose();
  126. };
  127. return DepthRenderer;
  128. })();
  129. BABYLON.DepthRenderer = DepthRenderer;
  130. })(BABYLON || (BABYLON = {}));
  131. //# sourceMappingURL=babylon.depthRenderer.js.map