depthRenderer.ts 11 KB

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