babylon.depthRenderer.ts 6.5 KB

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