babylon.geometryBufferRenderer.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. module BABYLON {
  2. export class GeometryBufferRenderer {
  3. private _scene: Scene;
  4. private _multiRenderTarget: MultiRenderTarget;
  5. private _effect: Effect;
  6. private _ratio: number;
  7. private _viewMatrix = Matrix.Zero();
  8. private _projectionMatrix = Matrix.Zero();
  9. private _transformMatrix = Matrix.Zero();
  10. private _worldViewProjection = Matrix.Zero();
  11. private _cachedDefines: string;
  12. private _enablePosition: boolean = false;
  13. public set renderList(meshes: Mesh[]) {
  14. this._multiRenderTarget.renderList = meshes;
  15. }
  16. public get isSupported(): boolean {
  17. return this._multiRenderTarget.isSupported;
  18. }
  19. public get enablePosition(): boolean {
  20. return this._enablePosition;
  21. }
  22. public set enablePosition(enable: boolean) {
  23. this._enablePosition = enable;
  24. this.dispose();
  25. this._createRenderTargets();
  26. }
  27. constructor(scene: Scene, ratio: number = 1) {
  28. this._scene = scene;
  29. this._ratio = ratio;
  30. // Render target
  31. this._createRenderTargets();
  32. }
  33. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  34. var material: any = subMesh.getMaterial();
  35. if (material && material.disableDepthWrite) {
  36. return false;
  37. }
  38. var defines = [];
  39. var attribs = [VertexBuffer.PositionKind, VertexBuffer.NormalKind];
  40. var mesh = subMesh.getMesh();
  41. var scene = mesh.getScene();
  42. // Alpha test
  43. if (material && material.needAlphaTesting()) {
  44. defines.push("#define ALPHATEST");
  45. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  46. attribs.push(VertexBuffer.UVKind);
  47. defines.push("#define UV1");
  48. }
  49. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  50. attribs.push(VertexBuffer.UV2Kind);
  51. defines.push("#define UV2");
  52. }
  53. }
  54. // Buffers
  55. if (this._enablePosition) {
  56. defines.push("#define POSITION");
  57. }
  58. // Bones
  59. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  60. attribs.push(VertexBuffer.MatricesIndicesKind);
  61. attribs.push(VertexBuffer.MatricesWeightsKind);
  62. if (mesh.numBoneInfluencers > 4) {
  63. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  64. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  65. }
  66. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  67. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  68. } else {
  69. defines.push("#define NUM_BONE_INFLUENCERS 0");
  70. }
  71. // Instances
  72. if (useInstances) {
  73. defines.push("#define INSTANCES");
  74. attribs.push("world0");
  75. attribs.push("world1");
  76. attribs.push("world2");
  77. attribs.push("world3");
  78. }
  79. // Get correct effect
  80. var join = defines.join("\n");
  81. if (this._cachedDefines !== join) {
  82. this._cachedDefines = join;
  83. this._effect = this._scene.getEngine().createEffect("geometry",
  84. attribs,
  85. ["world", "mBones", "viewProjection", "diffuseMatrix", "view"],
  86. ["diffuseSampler"], join,
  87. null, null, null,
  88. { buffersCount: this._enablePosition ? 3 : 2 });
  89. }
  90. return this._effect.isReady();
  91. }
  92. public getGBuffer(): MultiRenderTarget {
  93. return this._multiRenderTarget;
  94. }
  95. // Methods
  96. public dispose(): void {
  97. this.getGBuffer().dispose();
  98. }
  99. private _createRenderTargets(): void {
  100. var engine = this._scene.getEngine();
  101. var count = this._enablePosition ? 3 : 2;
  102. this._multiRenderTarget = new MultiRenderTarget("gBuffer", { width: engine.getRenderWidth() * this._ratio, height: engine.getRenderHeight() * this._ratio }, count, this._scene, { generateMipMaps : false, generateDepthTexture: true });
  103. if (!this.isSupported) {
  104. return null;
  105. }
  106. this._multiRenderTarget.wrapU = Texture.CLAMP_ADDRESSMODE;
  107. this._multiRenderTarget.wrapV = Texture.CLAMP_ADDRESSMODE;
  108. this._multiRenderTarget.refreshRate = 1;
  109. this._multiRenderTarget.renderParticles = false;
  110. this._multiRenderTarget.renderList = null;
  111. // set default depth value to 1.0 (far away)
  112. this._multiRenderTarget.onClearObservable.add((engine: Engine) => {
  113. engine.clear(new Color4(0.0, 0.0, 0.0, 1.0), true, true, true);
  114. });
  115. // Custom render function
  116. var renderSubMesh = (subMesh: SubMesh): void => {
  117. var mesh = subMesh.getRenderingMesh();
  118. var scene = this._scene;
  119. var engine = scene.getEngine();
  120. // Culling
  121. engine.setState(subMesh.getMaterial().backFaceCulling);
  122. // Managing instances
  123. var batch = mesh._getInstancesRenderList(subMesh._id);
  124. if (batch.mustReturn) {
  125. return;
  126. }
  127. var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null);
  128. if (this.isReady(subMesh, hardwareInstancedRendering)) {
  129. engine.enableEffect(this._effect);
  130. mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
  131. var material = subMesh.getMaterial();
  132. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  133. this._effect.setMatrix("view", scene.getViewMatrix());
  134. // Alpha test
  135. if (material && material.needAlphaTesting()) {
  136. var alphaTexture = material.getAlphaTestTexture();
  137. this._effect.setTexture("diffuseSampler", alphaTexture);
  138. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  139. }
  140. // Bones
  141. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  142. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  143. }
  144. // Draw
  145. mesh._processRendering(subMesh, this._effect, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  146. (isInstance, world) => this._effect.setMatrix("world", world));
  147. }
  148. };
  149. this._multiRenderTarget.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void => {
  150. var index;
  151. if (depthOnlySubMeshes.length) {
  152. engine.setColorWrite(false);
  153. for (index = 0; index < depthOnlySubMeshes.length; index++) {
  154. renderSubMesh(depthOnlySubMeshes.data[index]);
  155. }
  156. engine.setColorWrite(true);
  157. }
  158. for (index = 0; index < opaqueSubMeshes.length; index++) {
  159. renderSubMesh(opaqueSubMeshes.data[index]);
  160. }
  161. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  162. renderSubMesh(alphaTestSubMeshes.data[index]);
  163. }
  164. };
  165. }
  166. }
  167. }