babylon.depthRenderer.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // Custom render function
  22. var renderSubMesh = (subMesh: SubMesh): void => {
  23. var mesh = subMesh.getRenderingMesh();
  24. var scene = this._scene;
  25. var engine = scene.getEngine();
  26. // Culling
  27. engine.setState(subMesh.getMaterial().backFaceCulling);
  28. // Managing instances
  29. var batch = mesh._getInstancesRenderList(subMesh._id);
  30. if (batch.mustReturn) {
  31. return;
  32. }
  33. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
  34. if (this.isReady(subMesh, hardwareInstancedRendering)) {
  35. engine.enableEffect(this._effect);
  36. mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
  37. var material = subMesh.getMaterial();
  38. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  39. this._effect.setFloat("far", scene.activeCamera.maxZ);
  40. // Alpha test
  41. if (material && material.needAlphaTesting()) {
  42. var alphaTexture = material.getAlphaTestTexture();
  43. this._effect.setTexture("diffuseSampler", alphaTexture);
  44. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  45. }
  46. // Bones
  47. if (mesh.useBones) {
  48. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  49. }
  50. // Draw
  51. mesh._processRendering(subMesh, this._effect, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  52. (isInstance, world) => this._effect.setMatrix("world", world));
  53. }
  54. };
  55. this._depthMap.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>): void => {
  56. var index;
  57. for (index = 0; index < opaqueSubMeshes.length; index++) {
  58. renderSubMesh(opaqueSubMeshes.data[index]);
  59. }
  60. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  61. renderSubMesh(alphaTestSubMeshes.data[index]);
  62. }
  63. };
  64. }
  65. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  66. var defines = [];
  67. var attribs = [VertexBuffer.PositionKind];
  68. var mesh = subMesh.getMesh();
  69. var scene = mesh.getScene();
  70. var material = subMesh.getMaterial();
  71. // Alpha test
  72. if (material && material.needAlphaTesting()) {
  73. defines.push("#define ALPHATEST");
  74. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  75. attribs.push(VertexBuffer.UVKind);
  76. defines.push("#define UV1");
  77. }
  78. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  79. attribs.push(VertexBuffer.UV2Kind);
  80. defines.push("#define UV2");
  81. }
  82. }
  83. // Bones
  84. if (mesh.useBones) {
  85. attribs.push(VertexBuffer.MatricesIndicesKind);
  86. attribs.push(VertexBuffer.MatricesWeightsKind);
  87. defines.push("#define BONES");
  88. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  89. }
  90. // Instances
  91. if (useInstances) {
  92. defines.push("#define INSTANCES");
  93. attribs.push("world0");
  94. attribs.push("world1");
  95. attribs.push("world2");
  96. attribs.push("world3");
  97. }
  98. // Get correct effect
  99. var join = defines.join("\n");
  100. if (this._cachedDefines !== join) {
  101. this._cachedDefines = join;
  102. this._effect = this._scene.getEngine().createEffect("depth",
  103. attribs,
  104. ["world", "mBones", "viewProjection", "diffuseMatrix", "far"],
  105. ["diffuseSampler"], join);
  106. }
  107. return this._effect.isReady();
  108. }
  109. public getDepthMap(): RenderTargetTexture {
  110. return this._depthMap;
  111. }
  112. // Methods
  113. public dispose(): void {
  114. this._depthMap.dispose();
  115. }
  116. }
  117. }