babylon.depthRenderer.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.onClear = (engine: Engine) => {
  23. engine.clear(new Color4(1.0, 1.0, 1.0, 1.0), 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 !== null) && (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.setFloat("far", 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) {
  52. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  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 defines = [];
  71. var attribs = [VertexBuffer.PositionKind];
  72. var mesh = subMesh.getMesh();
  73. var scene = mesh.getScene();
  74. var material = subMesh.getMaterial();
  75. // Alpha test
  76. if (material && material.needAlphaTesting()) {
  77. defines.push("#define ALPHATEST");
  78. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  79. attribs.push(VertexBuffer.UVKind);
  80. defines.push("#define UV1");
  81. }
  82. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  83. attribs.push(VertexBuffer.UV2Kind);
  84. defines.push("#define UV2");
  85. }
  86. }
  87. // Bones
  88. if (mesh.useBones) {
  89. attribs.push(VertexBuffer.MatricesIndicesKind);
  90. attribs.push(VertexBuffer.MatricesWeightsKind);
  91. defines.push("#define BONES");
  92. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  93. }
  94. // Instances
  95. if (useInstances) {
  96. defines.push("#define INSTANCES");
  97. attribs.push("world0");
  98. attribs.push("world1");
  99. attribs.push("world2");
  100. attribs.push("world3");
  101. }
  102. // Get correct effect
  103. var join = defines.join("\n");
  104. if (this._cachedDefines !== join) {
  105. this._cachedDefines = join;
  106. this._effect = this._scene.getEngine().createEffect("depth",
  107. attribs,
  108. ["world", "mBones", "viewProjection", "diffuseMatrix", "far"],
  109. ["diffuseSampler"], join);
  110. }
  111. return this._effect.isReady();
  112. }
  113. public getDepthMap(): RenderTargetTexture {
  114. return this._depthMap;
  115. }
  116. // Methods
  117. public dispose(): void {
  118. this._depthMap.dispose();
  119. }
  120. }
  121. }