babylon.depthRenderer.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. module BABYLON {
  2. /**
  3. * This represents a depth renderer in Babylon.
  4. * A depth renderer will render to it's depth map every frame which can be displayed or used in post processing
  5. */
  6. export class DepthRenderer {
  7. private _scene: Scene;
  8. private _depthMap: RenderTargetTexture;
  9. private _effect: Effect;
  10. private _cachedDefines: string;
  11. private _camera:Nullable<Camera>;
  12. /**
  13. * Instantiates a depth renderer
  14. * @param scene The scene the renderer belongs to
  15. * @param type The texture type of the depth map (default: Engine.TEXTURETYPE_FLOAT)
  16. * @param camera The camera to be used to render the depth map (default: scene's active camera)
  17. */
  18. constructor(scene: Scene, type: number = Engine.TEXTURETYPE_FLOAT, camera:Nullable<Camera> = null) {
  19. this._scene = scene;
  20. this._camera = camera;
  21. var engine = scene.getEngine();
  22. // Render target
  23. this._depthMap = new RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this._scene, false, true, type);
  24. this._depthMap.wrapU = Texture.CLAMP_ADDRESSMODE;
  25. this._depthMap.wrapV = Texture.CLAMP_ADDRESSMODE;
  26. this._depthMap.refreshRate = 1;
  27. this._depthMap.renderParticles = false;
  28. this._depthMap.renderList = null;
  29. // Camera to get depth map from to support multiple concurrent cameras
  30. this._depthMap.activeCamera = this._camera;
  31. this._depthMap.ignoreCameraViewport = true;
  32. this._depthMap.useCameraPostProcesses = false;
  33. // set default depth value to 1.0 (far away)
  34. this._depthMap.onClearObservable.add((engine: Engine) => {
  35. engine.clear(new Color4(1.0, 1.0, 1.0, 1.0), true, true, true);
  36. });
  37. // Custom render function
  38. var renderSubMesh = (subMesh: SubMesh): void => {
  39. var mesh = subMesh.getRenderingMesh();
  40. var scene = this._scene;
  41. var engine = scene.getEngine();
  42. let material = subMesh.getMaterial();
  43. if (!material) {
  44. return;
  45. }
  46. // Culling and reverse (right handed system)
  47. engine.setState(material.backFaceCulling, 0, false, scene.useRightHandedSystem);
  48. // Managing instances
  49. var batch = mesh._getInstancesRenderList(subMesh._id);
  50. if (batch.mustReturn) {
  51. return;
  52. }
  53. var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null);
  54. var camera = this._camera || scene.activeCamera;
  55. if (this.isReady(subMesh, hardwareInstancedRendering) && camera) {
  56. engine.enableEffect(this._effect);
  57. mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
  58. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  59. this._effect.setFloat2("depthValues", camera.minZ, camera.minZ + camera.maxZ);
  60. // Alpha test
  61. if (material && material.needAlphaTesting()) {
  62. var alphaTexture = material.getAlphaTestTexture();
  63. if (alphaTexture) {
  64. this._effect.setTexture("diffuseSampler", alphaTexture);
  65. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  66. }
  67. }
  68. // Bones
  69. if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
  70. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  71. }
  72. // Draw
  73. mesh._processRendering(subMesh, this._effect, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  74. (isInstance, world) => this._effect.setMatrix("world", world));
  75. }
  76. };
  77. this._depthMap.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void => {
  78. var index;
  79. if (depthOnlySubMeshes.length) {
  80. engine.setColorWrite(false);
  81. for (index = 0; index < depthOnlySubMeshes.length; index++) {
  82. renderSubMesh(depthOnlySubMeshes.data[index]);
  83. }
  84. engine.setColorWrite(true);
  85. }
  86. for (index = 0; index < opaqueSubMeshes.length; index++) {
  87. renderSubMesh(opaqueSubMeshes.data[index]);
  88. }
  89. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  90. renderSubMesh(alphaTestSubMeshes.data[index]);
  91. }
  92. };
  93. }
  94. /**
  95. * Creates the depth rendering effect and checks if the effect is ready.
  96. * @param subMesh The submesh to be used to render the depth map of
  97. * @param useInstances If multiple world instances should be used
  98. * @returns if the depth renderer is ready to render the depth map
  99. */
  100. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  101. var material: any = subMesh.getMaterial();
  102. if (material.disableDepthWrite) {
  103. return false;
  104. }
  105. var defines = [];
  106. var attribs = [VertexBuffer.PositionKind];
  107. var mesh = subMesh.getMesh();
  108. // Alpha test
  109. if (material && material.needAlphaTesting()) {
  110. defines.push("#define ALPHATEST");
  111. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  112. attribs.push(VertexBuffer.UVKind);
  113. defines.push("#define UV1");
  114. }
  115. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  116. attribs.push(VertexBuffer.UV2Kind);
  117. defines.push("#define UV2");
  118. }
  119. }
  120. // Bones
  121. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  122. attribs.push(VertexBuffer.MatricesIndicesKind);
  123. attribs.push(VertexBuffer.MatricesWeightsKind);
  124. if (mesh.numBoneInfluencers > 4) {
  125. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  126. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  127. }
  128. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  129. defines.push("#define BonesPerMesh " + (mesh.skeleton ? mesh.skeleton.bones.length + 1 : 0));
  130. } else {
  131. defines.push("#define NUM_BONE_INFLUENCERS 0");
  132. }
  133. // Instances
  134. if (useInstances) {
  135. defines.push("#define INSTANCES");
  136. attribs.push("world0");
  137. attribs.push("world1");
  138. attribs.push("world2");
  139. attribs.push("world3");
  140. }
  141. // Get correct effect
  142. var join = defines.join("\n");
  143. if (this._cachedDefines !== join) {
  144. this._cachedDefines = join;
  145. this._effect = this._scene.getEngine().createEffect("depth",
  146. attribs,
  147. ["world", "mBones", "viewProjection", "diffuseMatrix", "depthValues"],
  148. ["diffuseSampler"], join);
  149. }
  150. return this._effect.isReady();
  151. }
  152. /**
  153. * Gets the texture which the depth map will be written to.
  154. * @returns The depth map texture
  155. */
  156. public getDepthMap(): RenderTargetTexture {
  157. return this._depthMap;
  158. }
  159. /**
  160. * Disposes of the depth renderer.
  161. */
  162. public dispose(): void {
  163. this._depthMap.dispose();
  164. }
  165. }
  166. }