babylon.depthRenderer.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. if (mesh.useBones) {
  47. _this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  48. }
  49. // Draw
  50. mesh._processRendering(subMesh, _this._effect, BABYLON.Material.TriangleFillMode, batch, hardwareInstancedRendering, function (isInstance, world) { return _this._effect.setMatrix("world", world); });
  51. }
  52. };
  53. this._depthMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes) {
  54. var index;
  55. for (index = 0; index < opaqueSubMeshes.length; index++) {
  56. renderSubMesh(opaqueSubMeshes.data[index]);
  57. }
  58. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  59. renderSubMesh(alphaTestSubMeshes.data[index]);
  60. }
  61. };
  62. }
  63. DepthRenderer.prototype.isReady = function (subMesh, useInstances) {
  64. var defines = [];
  65. var attribs = [BABYLON.VertexBuffer.PositionKind];
  66. var mesh = subMesh.getMesh();
  67. var scene = mesh.getScene();
  68. var material = subMesh.getMaterial();
  69. // Alpha test
  70. if (material && material.needAlphaTesting()) {
  71. defines.push("#define ALPHATEST");
  72. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  73. attribs.push(BABYLON.VertexBuffer.UVKind);
  74. defines.push("#define UV1");
  75. }
  76. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  77. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  78. defines.push("#define UV2");
  79. }
  80. }
  81. // Bones
  82. if (mesh.useBones) {
  83. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  84. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  85. defines.push("#define BONES");
  86. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  87. }
  88. // Instances
  89. if (useInstances) {
  90. defines.push("#define INSTANCES");
  91. attribs.push("world0");
  92. attribs.push("world1");
  93. attribs.push("world2");
  94. attribs.push("world3");
  95. }
  96. // Get correct effect
  97. var join = defines.join("\n");
  98. if (this._cachedDefines !== join) {
  99. this._cachedDefines = join;
  100. this._effect = this._scene.getEngine().createEffect("depth", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "far"], ["diffuseSampler"], join);
  101. }
  102. return this._effect.isReady();
  103. };
  104. DepthRenderer.prototype.getDepthMap = function () {
  105. return this._depthMap;
  106. };
  107. // Methods
  108. DepthRenderer.prototype.dispose = function () {
  109. this._depthMap.dispose();
  110. };
  111. return DepthRenderer;
  112. })();
  113. BABYLON.DepthRenderer = DepthRenderer;
  114. })(BABYLON || (BABYLON = {}));
  115. //# sourceMappingURL=babylon.depthRenderer.js.map