babylon.depthRenderer.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // set default depth value to 1.0 (far away)
  21. this._depthMap.onClear = function (engine) {
  22. engine.clear(new BABYLON.Color4(1.0, 1.0, 1.0, 1.0), true, true);
  23. };
  24. // Custom render function
  25. var renderSubMesh = function (subMesh) {
  26. var mesh = subMesh.getRenderingMesh();
  27. var scene = _this._scene;
  28. var engine = scene.getEngine();
  29. // Culling
  30. engine.setState(subMesh.getMaterial().backFaceCulling);
  31. // Managing instances
  32. var batch = mesh._getInstancesRenderList(subMesh._id);
  33. if (batch.mustReturn) {
  34. return;
  35. }
  36. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
  37. if (_this.isReady(subMesh, hardwareInstancedRendering)) {
  38. engine.enableEffect(_this._effect);
  39. mesh._bind(subMesh, _this._effect, BABYLON.Material.TriangleFillMode);
  40. var material = subMesh.getMaterial();
  41. _this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  42. _this._effect.setFloat("far", scene.activeCamera.maxZ);
  43. // Alpha test
  44. if (material && material.needAlphaTesting()) {
  45. var alphaTexture = material.getAlphaTestTexture();
  46. _this._effect.setTexture("diffuseSampler", alphaTexture);
  47. _this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  48. }
  49. // Bones
  50. if (mesh.useBones) {
  51. _this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  52. }
  53. // Draw
  54. mesh._processRendering(subMesh, _this._effect, BABYLON.Material.TriangleFillMode, batch, hardwareInstancedRendering, function (isInstance, world) { return _this._effect.setMatrix("world", world); });
  55. }
  56. };
  57. this._depthMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes) {
  58. var index;
  59. for (index = 0; index < opaqueSubMeshes.length; index++) {
  60. renderSubMesh(opaqueSubMeshes.data[index]);
  61. }
  62. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  63. renderSubMesh(alphaTestSubMeshes.data[index]);
  64. }
  65. };
  66. }
  67. DepthRenderer.prototype.isReady = function (subMesh, useInstances) {
  68. var defines = [];
  69. var attribs = [BABYLON.VertexBuffer.PositionKind];
  70. var mesh = subMesh.getMesh();
  71. var scene = mesh.getScene();
  72. var material = subMesh.getMaterial();
  73. // Alpha test
  74. if (material && material.needAlphaTesting()) {
  75. defines.push("#define ALPHATEST");
  76. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  77. attribs.push(BABYLON.VertexBuffer.UVKind);
  78. defines.push("#define UV1");
  79. }
  80. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  81. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  82. defines.push("#define UV2");
  83. }
  84. }
  85. // Bones
  86. if (mesh.useBones) {
  87. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  88. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  89. defines.push("#define BONES");
  90. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  91. }
  92. // Instances
  93. if (useInstances) {
  94. defines.push("#define INSTANCES");
  95. attribs.push("world0");
  96. attribs.push("world1");
  97. attribs.push("world2");
  98. attribs.push("world3");
  99. }
  100. // Get correct effect
  101. var join = defines.join("\n");
  102. if (this._cachedDefines !== join) {
  103. this._cachedDefines = join;
  104. this._effect = this._scene.getEngine().createEffect("depth", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "far"], ["diffuseSampler"], join);
  105. }
  106. return this._effect.isReady();
  107. };
  108. DepthRenderer.prototype.getDepthMap = function () {
  109. return this._depthMap;
  110. };
  111. // Methods
  112. DepthRenderer.prototype.dispose = function () {
  113. this._depthMap.dispose();
  114. };
  115. return DepthRenderer;
  116. })();
  117. BABYLON.DepthRenderer = DepthRenderer;
  118. })(BABYLON || (BABYLON = {}));
  119. //# sourceMappingURL=babylon.depthRenderer.js.map