depthRenderer.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import { Nullable } from "../types";
  2. import { Color4 } from "../Maths/math.color";
  3. import { Mesh } from "../Meshes/mesh";
  4. import { SubMesh } from "../Meshes/subMesh";
  5. import { VertexBuffer } from "../Meshes/buffer";
  6. import { SmartArray } from "../Misc/smartArray";
  7. import { Scene } from "../scene";
  8. import { Texture } from "../Materials/Textures/texture";
  9. import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
  10. import { Effect } from "../Materials/effect";
  11. import { MaterialHelper } from "../Materials/materialHelper";
  12. import { Camera } from "../Cameras/camera";
  13. import { Constants } from "../Engines/constants";
  14. import "../Shaders/depth.fragment";
  15. import "../Shaders/depth.vertex";
  16. import { _DevTools } from '../Misc/devTools';
  17. /**
  18. * This represents a depth renderer in Babylon.
  19. * A depth renderer will render to it's depth map every frame which can be displayed or used in post processing
  20. */
  21. export class DepthRenderer {
  22. private _scene: Scene;
  23. private _depthMap: RenderTargetTexture;
  24. private _effect: Effect;
  25. private readonly _storeNonLinearDepth: boolean;
  26. private readonly _clearColor: Color4;
  27. /** Get if the depth renderer is using packed depth or not */
  28. public readonly isPacked: boolean;
  29. private _cachedDefines: string;
  30. private _camera: Nullable<Camera>;
  31. /** Enable or disable the depth renderer. When disabled, the depth texture is not updated */
  32. public enabled = true;
  33. /**
  34. * Specifies that the depth renderer will only be used within
  35. * the camera it is created for.
  36. * This can help forcing its rendering during the camera processing.
  37. */
  38. public useOnlyInActiveCamera: boolean = false;
  39. /** @hidden */
  40. public static _SceneComponentInitialization: (scene: Scene) => void = (_) => {
  41. throw _DevTools.WarnImport("DepthRendererSceneComponent");
  42. }
  43. /**
  44. * Instantiates a depth renderer
  45. * @param scene The scene the renderer belongs to
  46. * @param type The texture type of the depth map (default: Engine.TEXTURETYPE_FLOAT)
  47. * @param camera The camera to be used to render the depth map (default: scene's active camera)
  48. * @param storeNonLinearDepth Defines whether the depth is stored linearly like in Babylon Shadows or directly like glFragCoord.z
  49. */
  50. constructor(scene: Scene, type: number = Constants.TEXTURETYPE_FLOAT, camera: Nullable<Camera> = null, storeNonLinearDepth = false) {
  51. this._scene = scene;
  52. this._storeNonLinearDepth = storeNonLinearDepth;
  53. this.isPacked = type === Constants.TEXTURETYPE_UNSIGNED_BYTE;
  54. if (this.isPacked) {
  55. this._clearColor = new Color4(1.0, 1.0, 1.0, 1.0);
  56. }
  57. else {
  58. this._clearColor = new Color4(1.0, 0.0, 0.0, 1.0);
  59. }
  60. DepthRenderer._SceneComponentInitialization(this._scene);
  61. this._camera = camera;
  62. var engine = scene.getEngine();
  63. // Render target
  64. var format = (this.isPacked || !engine._features.supportExtendedTextureFormats) ? Constants.TEXTUREFORMAT_RGBA : Constants.TEXTUREFORMAT_R;
  65. this._depthMap = new RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this._scene, false, true, type,
  66. false, undefined, undefined, undefined, undefined,
  67. format);
  68. this._depthMap.wrapU = Texture.CLAMP_ADDRESSMODE;
  69. this._depthMap.wrapV = Texture.CLAMP_ADDRESSMODE;
  70. this._depthMap.refreshRate = 1;
  71. this._depthMap.renderParticles = false;
  72. this._depthMap.renderList = null;
  73. // Camera to get depth map from to support multiple concurrent cameras
  74. this._depthMap.activeCamera = this._camera;
  75. this._depthMap.ignoreCameraViewport = true;
  76. this._depthMap.useCameraPostProcesses = false;
  77. // set default depth value to 1.0 (far away)
  78. this._depthMap.onClearObservable.add((engine) => {
  79. engine.clear(this._clearColor, true, true, true);
  80. });
  81. this._depthMap.onBeforeBindObservable.add(() => {
  82. engine._debugPushGroup("depth renderer", 1);
  83. });
  84. this._depthMap.onAfterUnbindObservable.add(() => {
  85. engine._debugPopGroup(1);
  86. });
  87. // Custom render function
  88. var renderSubMesh = (subMesh: SubMesh): void => {
  89. var renderingMesh = subMesh.getRenderingMesh();
  90. var effectiveMesh = subMesh.getEffectiveMesh();
  91. var scene = this._scene;
  92. var engine = scene.getEngine();
  93. let material = subMesh.getMaterial();
  94. effectiveMesh._internalAbstractMeshDataInfo._isActiveIntermediate = false;
  95. if (!material || effectiveMesh.infiniteDistance || material.disableDepthWrite || subMesh.verticesCount === 0 || subMesh._renderId === scene.getRenderId()) {
  96. return;
  97. }
  98. // Culling and reverse (right handed system)
  99. engine.setState(material.backFaceCulling, 0, false, scene.useRightHandedSystem);
  100. // Managing instances
  101. var batch = renderingMesh._getInstancesRenderList(subMesh._id, !!subMesh.getReplacementMesh());
  102. if (batch.mustReturn) {
  103. return;
  104. }
  105. var hardwareInstancedRendering = engine.getCaps().instancedArrays && (batch.visibleInstances[subMesh._id] !== null && batch.visibleInstances[subMesh._id] !== undefined || renderingMesh.hasThinInstances);
  106. var camera = this._camera || scene.activeCamera;
  107. if (this.isReady(subMesh, hardwareInstancedRendering) && camera) {
  108. subMesh._renderId = scene.getRenderId();
  109. engine.enableEffect(this._effect);
  110. renderingMesh._bind(subMesh, this._effect, material.fillMode);
  111. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  112. this._effect.setFloat2("depthValues", camera.minZ, camera.minZ + camera.maxZ);
  113. // Alpha test
  114. if (material && material.needAlphaTesting()) {
  115. var alphaTexture = material.getAlphaTestTexture();
  116. if (alphaTexture) {
  117. this._effect.setTexture("diffuseSampler", alphaTexture);
  118. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  119. }
  120. }
  121. // Bones
  122. if (renderingMesh.useBones && renderingMesh.computeBonesUsingShaders && renderingMesh.skeleton) {
  123. this._effect.setMatrices("mBones", renderingMesh.skeleton.getTransformMatrices(renderingMesh));
  124. }
  125. // Morph targets
  126. MaterialHelper.BindMorphTargetParameters(renderingMesh, this._effect);
  127. if (renderingMesh.morphTargetManager && renderingMesh.morphTargetManager.isUsingTextureForTargets) {
  128. renderingMesh.morphTargetManager._bind(this._effect);
  129. }
  130. // Draw
  131. renderingMesh._processRendering(effectiveMesh, subMesh, this._effect, material.fillMode, batch, hardwareInstancedRendering,
  132. (isInstance, world) => this._effect.setMatrix("world", world));
  133. }
  134. };
  135. this._depthMap.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void => {
  136. var index;
  137. if (depthOnlySubMeshes.length) {
  138. engine.setColorWrite(false);
  139. for (index = 0; index < depthOnlySubMeshes.length; index++) {
  140. renderSubMesh(depthOnlySubMeshes.data[index]);
  141. }
  142. engine.setColorWrite(true);
  143. }
  144. for (index = 0; index < opaqueSubMeshes.length; index++) {
  145. renderSubMesh(opaqueSubMeshes.data[index]);
  146. }
  147. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  148. renderSubMesh(alphaTestSubMeshes.data[index]);
  149. }
  150. };
  151. }
  152. /**
  153. * Creates the depth rendering effect and checks if the effect is ready.
  154. * @param subMesh The submesh to be used to render the depth map of
  155. * @param useInstances If multiple world instances should be used
  156. * @returns if the depth renderer is ready to render the depth map
  157. */
  158. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  159. var material: any = subMesh.getMaterial();
  160. if (material.disableDepthWrite) {
  161. return false;
  162. }
  163. var defines = [];
  164. var attribs = [VertexBuffer.PositionKind];
  165. var mesh = subMesh.getMesh();
  166. // Alpha test
  167. if (material && material.needAlphaTesting() && material.getAlphaTestTexture()) {
  168. defines.push("#define ALPHATEST");
  169. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  170. attribs.push(VertexBuffer.UVKind);
  171. defines.push("#define UV1");
  172. }
  173. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  174. attribs.push(VertexBuffer.UV2Kind);
  175. defines.push("#define UV2");
  176. }
  177. }
  178. // Bones
  179. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  180. attribs.push(VertexBuffer.MatricesIndicesKind);
  181. attribs.push(VertexBuffer.MatricesWeightsKind);
  182. if (mesh.numBoneInfluencers > 4) {
  183. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  184. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  185. }
  186. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  187. defines.push("#define BonesPerMesh " + (mesh.skeleton ? mesh.skeleton.bones.length + 1 : 0));
  188. } else {
  189. defines.push("#define NUM_BONE_INFLUENCERS 0");
  190. }
  191. // Morph targets
  192. const morphTargetManager = (mesh as Mesh).morphTargetManager;
  193. let numMorphInfluencers = 0;
  194. if (morphTargetManager) {
  195. if (morphTargetManager.numInfluencers > 0) {
  196. numMorphInfluencers = morphTargetManager.numInfluencers;
  197. defines.push("#define MORPHTARGETS");
  198. defines.push("#define NUM_MORPH_INFLUENCERS " + numMorphInfluencers);
  199. if (morphTargetManager.isUsingTextureForTargets) {
  200. defines.push("#define MORPHTARGETS_TEXTURE");
  201. };
  202. MaterialHelper.PrepareAttributesForMorphTargetsInfluencers(attribs, mesh, numMorphInfluencers);
  203. }
  204. }
  205. // Instances
  206. if (useInstances) {
  207. defines.push("#define INSTANCES");
  208. MaterialHelper.PushAttributesForInstances(attribs);
  209. if (subMesh.getRenderingMesh().hasThinInstances) {
  210. defines.push("#define THIN_INSTANCES");
  211. }
  212. }
  213. // None linear depth
  214. if (this._storeNonLinearDepth) {
  215. defines.push("#define NONLINEARDEPTH");
  216. }
  217. // Float Mode
  218. if (this.isPacked) {
  219. defines.push("#define PACKED");
  220. }
  221. // Get correct effect
  222. var join = defines.join("\n");
  223. if (this._cachedDefines !== join) {
  224. this._cachedDefines = join;
  225. this._effect = this._scene.getEngine().createEffect("depth",
  226. attribs,
  227. ["world", "mBones", "viewProjection", "diffuseMatrix", "depthValues", "morphTargetInfluences", "morphTargetTextureInfo"],
  228. ["diffuseSampler", "morphTargets"], join,
  229. undefined, undefined, undefined, { maxSimultaneousMorphTargets: numMorphInfluencers });
  230. }
  231. return this._effect.isReady();
  232. }
  233. /**
  234. * Gets the texture which the depth map will be written to.
  235. * @returns The depth map texture
  236. */
  237. public getDepthMap(): RenderTargetTexture {
  238. return this._depthMap;
  239. }
  240. /**
  241. * Disposes of the depth renderer.
  242. */
  243. public dispose(): void {
  244. this._depthMap.dispose();
  245. }
  246. }