babylon.geometryBufferRenderer.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. }
  88. return this._effect.isReady();
  89. }
  90. public getGBuffer(): MultiRenderTarget {
  91. return this._multiRenderTarget;
  92. }
  93. // Methods
  94. public dispose(): void {
  95. this.getGBuffer().dispose();
  96. }
  97. private _createRenderTargets(): void {
  98. var engine = this._scene.getEngine();
  99. var count = this._enablePosition ? 3 : 2;
  100. this._multiRenderTarget = new MultiRenderTarget("gBuffer", { width: engine.getRenderWidth() * this._ratio, height: engine.getRenderHeight() * this._ratio }, count, this._scene, { generateMipMaps : false, generateDepthTexture: true });
  101. if (!this.isSupported) {
  102. return null;
  103. }
  104. this._multiRenderTarget.wrapU = Texture.CLAMP_ADDRESSMODE;
  105. this._multiRenderTarget.wrapV = Texture.CLAMP_ADDRESSMODE;
  106. this._multiRenderTarget.refreshRate = 1;
  107. this._multiRenderTarget.renderParticles = false;
  108. this._multiRenderTarget.renderList = null;
  109. // set default depth value to 1.0 (far away)
  110. this._multiRenderTarget.onClearObservable.add((engine: Engine) => {
  111. engine.clear(new Color4(0.0, 0.0, 0.0, 1.0), true, true, true);
  112. });
  113. // Custom render function
  114. var renderSubMesh = (subMesh: SubMesh): void => {
  115. var mesh = subMesh.getRenderingMesh();
  116. var scene = this._scene;
  117. var engine = scene.getEngine();
  118. // Culling
  119. engine.setState(subMesh.getMaterial().backFaceCulling);
  120. // Managing instances
  121. var batch = mesh._getInstancesRenderList(subMesh._id);
  122. if (batch.mustReturn) {
  123. return;
  124. }
  125. var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null);
  126. if (this.isReady(subMesh, hardwareInstancedRendering)) {
  127. engine.enableEffect(this._effect);
  128. mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
  129. var material = subMesh.getMaterial();
  130. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  131. this._effect.setMatrix("view", scene.getViewMatrix());
  132. // Alpha test
  133. if (material && material.needAlphaTesting()) {
  134. var alphaTexture = material.getAlphaTestTexture();
  135. this._effect.setTexture("diffuseSampler", alphaTexture);
  136. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  137. }
  138. // Bones
  139. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  140. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  141. }
  142. // Draw
  143. mesh._processRendering(subMesh, this._effect, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  144. (isInstance, world) => this._effect.setMatrix("world", world));
  145. }
  146. };
  147. this._multiRenderTarget.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>): void => {
  148. var index;
  149. for (index = 0; index < opaqueSubMeshes.length; index++) {
  150. renderSubMesh(opaqueSubMeshes.data[index]);
  151. }
  152. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  153. renderSubMesh(alphaTestSubMeshes.data[index]);
  154. }
  155. };
  156. }
  157. }
  158. }