depthRenderer.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. * Specifiess 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.webGLVersion === 1) ? 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. // Custom render function
  82. var renderSubMesh = (subMesh: SubMesh): void => {
  83. var renderingMesh = subMesh.getRenderingMesh();
  84. var effectiveMesh = subMesh.getEffectiveMesh();
  85. var scene = this._scene;
  86. var engine = scene.getEngine();
  87. let material = subMesh.getMaterial();
  88. effectiveMesh._internalAbstractMeshDataInfo._isActiveIntermediate = false;
  89. if (!material) {
  90. return;
  91. }
  92. // Culling and reverse (right handed system)
  93. engine.setState(material.backFaceCulling, 0, false, scene.useRightHandedSystem);
  94. // Managing instances
  95. var batch = renderingMesh._getInstancesRenderList(subMesh._id, !!subMesh.getReplacementMesh());
  96. if (batch.mustReturn) {
  97. return;
  98. }
  99. var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null || renderingMesh.hasThinInstances);
  100. var camera = this._camera || scene.activeCamera;
  101. if (this.isReady(subMesh, hardwareInstancedRendering) && camera) {
  102. engine.enableEffect(this._effect);
  103. renderingMesh._bind(subMesh, this._effect, material.fillMode);
  104. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  105. this._effect.setFloat2("depthValues", camera.minZ, camera.minZ + camera.maxZ);
  106. // Alpha test
  107. if (material && material.needAlphaTesting()) {
  108. var alphaTexture = material.getAlphaTestTexture();
  109. if (alphaTexture) {
  110. this._effect.setTexture("diffuseSampler", alphaTexture);
  111. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  112. }
  113. }
  114. // Bones
  115. if (renderingMesh.useBones && renderingMesh.computeBonesUsingShaders && renderingMesh.skeleton) {
  116. this._effect.setMatrices("mBones", renderingMesh.skeleton.getTransformMatrices(renderingMesh));
  117. }
  118. // Morph targets
  119. MaterialHelper.BindMorphTargetParameters(renderingMesh, this._effect);
  120. // Draw
  121. renderingMesh._processRendering(effectiveMesh, subMesh, this._effect, material.fillMode, batch, hardwareInstancedRendering,
  122. (isInstance, world) => this._effect.setMatrix("world", world));
  123. }
  124. };
  125. this._depthMap.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void => {
  126. var index;
  127. if (depthOnlySubMeshes.length) {
  128. engine.setColorWrite(false);
  129. for (index = 0; index < depthOnlySubMeshes.length; index++) {
  130. renderSubMesh(depthOnlySubMeshes.data[index]);
  131. }
  132. engine.setColorWrite(true);
  133. }
  134. for (index = 0; index < opaqueSubMeshes.length; index++) {
  135. renderSubMesh(opaqueSubMeshes.data[index]);
  136. }
  137. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  138. renderSubMesh(alphaTestSubMeshes.data[index]);
  139. }
  140. };
  141. }
  142. /**
  143. * Creates the depth rendering effect and checks if the effect is ready.
  144. * @param subMesh The submesh to be used to render the depth map of
  145. * @param useInstances If multiple world instances should be used
  146. * @returns if the depth renderer is ready to render the depth map
  147. */
  148. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  149. var material: any = subMesh.getMaterial();
  150. if (material.disableDepthWrite) {
  151. return false;
  152. }
  153. var defines = [];
  154. var attribs = [VertexBuffer.PositionKind];
  155. var mesh = subMesh.getMesh();
  156. // Alpha test
  157. if (material && material.needAlphaTesting() && material.getAlphaTestTexture()) {
  158. defines.push("#define ALPHATEST");
  159. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  160. attribs.push(VertexBuffer.UVKind);
  161. defines.push("#define UV1");
  162. }
  163. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  164. attribs.push(VertexBuffer.UV2Kind);
  165. defines.push("#define UV2");
  166. }
  167. }
  168. // Bones
  169. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  170. attribs.push(VertexBuffer.MatricesIndicesKind);
  171. attribs.push(VertexBuffer.MatricesWeightsKind);
  172. if (mesh.numBoneInfluencers > 4) {
  173. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  174. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  175. }
  176. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  177. defines.push("#define BonesPerMesh " + (mesh.skeleton ? mesh.skeleton.bones.length + 1 : 0));
  178. } else {
  179. defines.push("#define NUM_BONE_INFLUENCERS 0");
  180. }
  181. // Morph targets
  182. const morphTargetManager = (mesh as Mesh).morphTargetManager;
  183. let numMorphInfluencers = 0;
  184. if (morphTargetManager) {
  185. if (morphTargetManager.numInfluencers > 0) {
  186. numMorphInfluencers = morphTargetManager.numInfluencers;
  187. defines.push("#define MORPHTARGETS");
  188. defines.push("#define NUM_MORPH_INFLUENCERS " + numMorphInfluencers);
  189. MaterialHelper.PrepareAttributesForMorphTargetsInfluencers(attribs, mesh, numMorphInfluencers);
  190. }
  191. }
  192. // Instances
  193. if (useInstances) {
  194. defines.push("#define INSTANCES");
  195. MaterialHelper.PushAttributesForInstances(attribs);
  196. if (subMesh.getRenderingMesh().hasThinInstances) {
  197. defines.push("#define THIN_INSTANCES");
  198. }
  199. }
  200. // None linear depth
  201. if (this._storeNonLinearDepth) {
  202. defines.push("#define NONLINEARDEPTH");
  203. }
  204. // Float Mode
  205. if (this.isPacked) {
  206. defines.push("#define PACKED");
  207. }
  208. // Get correct effect
  209. var join = defines.join("\n");
  210. if (this._cachedDefines !== join) {
  211. this._cachedDefines = join;
  212. this._effect = this._scene.getEngine().createEffect("depth",
  213. attribs,
  214. ["world", "mBones", "viewProjection", "diffuseMatrix", "depthValues", "morphTargetInfluences"],
  215. ["diffuseSampler"], join,
  216. undefined, undefined, undefined, { maxSimultaneousMorphTargets: numMorphInfluencers });
  217. }
  218. return this._effect.isReady();
  219. }
  220. /**
  221. * Gets the texture which the depth map will be written to.
  222. * @returns The depth map texture
  223. */
  224. public getDepthMap(): RenderTargetTexture {
  225. return this._depthMap;
  226. }
  227. /**
  228. * Disposes of the depth renderer.
  229. */
  230. public dispose(): void {
  231. this._depthMap.dispose();
  232. }
  233. }