babylon.geometryBufferRenderer.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. module BABYLON {
  2. /**
  3. * This renderer is helpfull to fill one of the render target with a geometry buffer.
  4. */
  5. export class GeometryBufferRenderer {
  6. /**
  7. * Constant used to retrieve the position texture index in the G-Buffer textures array
  8. * using getIndex(GeometryBufferRenderer.POSITION_TEXTURE_INDEX)
  9. */
  10. public static readonly POSITION_TEXTURE_TYPE = 1;
  11. /**
  12. * Constant used to retrieve the velocity texture index in the G-Buffer textures array
  13. * using getIndex(GeometryBufferRenderer.VELOCITY_TEXTURE_INDEX)
  14. */
  15. public static readonly VELOCITY_TEXTURE_TYPE = 2;
  16. private _scene: Scene;
  17. private _multiRenderTarget: MultiRenderTarget;
  18. private _ratio: number;
  19. private _enablePosition: boolean = false;
  20. private _enableVelocity: boolean = false;
  21. private _positionIndex: number = -1;
  22. private _velocityIndex: number = -1;
  23. private _previousWorldMatrices: { [index: number]: Matrix } = { };
  24. protected _effect: Effect;
  25. protected _cachedDefines: string;
  26. /**
  27. * Set the render list (meshes to be rendered) used in the G buffer.
  28. */
  29. public set renderList(meshes: Mesh[]) {
  30. this._multiRenderTarget.renderList = meshes;
  31. }
  32. /**
  33. * Gets wether or not G buffer are supported by the running hardware.
  34. * This requires draw buffer supports
  35. */
  36. public get isSupported(): boolean {
  37. return this._multiRenderTarget.isSupported;
  38. }
  39. /**
  40. * Returns the index of the given texture type in the G-Buffer textures array
  41. * @param textureType The texture type constant. For example GeometryBufferRenderer.POSITION_TEXTURE_INDEX
  42. * @returns the index of the given texture type in the G-Buffer textures array
  43. */
  44. public getTextureIndex(textureType: number): number {
  45. switch (textureType) {
  46. case GeometryBufferRenderer.POSITION_TEXTURE_TYPE: return this._positionIndex;
  47. case GeometryBufferRenderer.VELOCITY_TEXTURE_TYPE: return this._velocityIndex;
  48. default: return -1;
  49. }
  50. }
  51. /**
  52. * Gets a boolean indicating if objects positions are enabled for the G buffer.
  53. */
  54. public get enablePosition(): boolean {
  55. return this._enablePosition;
  56. }
  57. /**
  58. * Sets whether or not objects positions are enabled for the G buffer.
  59. */
  60. public set enablePosition(enable: boolean) {
  61. this._enablePosition = enable;
  62. this.dispose();
  63. this._createRenderTargets();
  64. }
  65. /**
  66. * Gets a boolean indicating if objects velocities are enabled for the G buffer.
  67. */
  68. public get enableVelocity(): boolean {
  69. return this._enableVelocity;
  70. }
  71. /**
  72. * Sets wether or not objects velocities are enabled for the G buffer.
  73. */
  74. public set enableVelocity(enable: boolean) {
  75. this._enableVelocity = enable;
  76. this.dispose();
  77. this._createRenderTargets();
  78. }
  79. /**
  80. * Gets the scene associated with the buffer.
  81. */
  82. public get scene(): Scene {
  83. return this._scene;
  84. }
  85. /**
  86. * Gets the ratio used by the buffer during its creation.
  87. * How big is the buffer related to the main canvas.
  88. */
  89. public get ratio(): number {
  90. return this._ratio;
  91. }
  92. /**
  93. * Creates a new G Buffer for the scene
  94. * @param scene The scene the buffer belongs to
  95. * @param ratio How big is the buffer related to the main canvas.
  96. */
  97. constructor(scene: Scene, ratio: number = 1) {
  98. this._scene = scene;
  99. this._ratio = ratio;
  100. // Register the G Buffer component to the scene.
  101. let component = scene._getComponent(SceneComponentConstants.NAME_GEOMETRYBUFFERRENDERER) as GeometryBufferRendererSceneComponent;
  102. if (!component) {
  103. component = new GeometryBufferRendererSceneComponent(scene);
  104. scene._addComponent(component);
  105. }
  106. // Render target
  107. this._createRenderTargets();
  108. }
  109. /**
  110. * Checks wether everything is ready to render a submesh to the G buffer.
  111. * @param subMesh the submesh to check readiness for
  112. * @param useInstances is the mesh drawn using instance or not
  113. * @returns true if ready otherwise false
  114. */
  115. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  116. var material: any = subMesh.getMaterial();
  117. if (material && material.disableDepthWrite) {
  118. return false;
  119. }
  120. var defines = [];
  121. var attribs = [VertexBuffer.PositionKind, VertexBuffer.NormalKind];
  122. var mesh = subMesh.getMesh();
  123. // Alpha test
  124. if (material && material.needAlphaTesting()) {
  125. defines.push("#define ALPHATEST");
  126. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  127. attribs.push(VertexBuffer.UVKind);
  128. defines.push("#define UV1");
  129. }
  130. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  131. attribs.push(VertexBuffer.UV2Kind);
  132. defines.push("#define UV2");
  133. }
  134. }
  135. // Buffers
  136. if (this._enablePosition) {
  137. defines.push("#define POSITION");
  138. defines.push("#define POSITION_INDEX " + this._positionIndex);
  139. }
  140. if (this._enableVelocity) {
  141. defines.push("#define VELOCITY");
  142. defines.push("#define VELOCITY_INDEX " + this._velocityIndex);
  143. }
  144. // Bones
  145. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  146. attribs.push(VertexBuffer.MatricesIndicesKind);
  147. attribs.push(VertexBuffer.MatricesWeightsKind);
  148. if (mesh.numBoneInfluencers > 4) {
  149. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  150. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  151. }
  152. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  153. defines.push("#define BonesPerMesh " + (mesh.skeleton ? mesh.skeleton.bones.length + 1 : 0));
  154. } else {
  155. defines.push("#define NUM_BONE_INFLUENCERS 0");
  156. }
  157. // Instances
  158. if (useInstances) {
  159. defines.push("#define INSTANCES");
  160. attribs.push("world0");
  161. attribs.push("world1");
  162. attribs.push("world2");
  163. attribs.push("world3");
  164. }
  165. // Setup textures count
  166. defines.push("#define RENDER_TARGET_COUNT " + this._multiRenderTarget.textures.length);
  167. // Get correct effect
  168. var join = defines.join("\n");
  169. if (this._cachedDefines !== join) {
  170. this._cachedDefines = join;
  171. this._effect = this._scene.getEngine().createEffect("geometry",
  172. attribs,
  173. ["world", "mBones", "viewProjection", "diffuseMatrix", "view", "previousWorldViewProjection"],
  174. ["diffuseSampler"], join,
  175. undefined, undefined, undefined,
  176. { buffersCount: this._enablePosition ? 3 : 2 });
  177. }
  178. return this._effect.isReady();
  179. }
  180. /**
  181. * Gets the current underlying G Buffer.
  182. * @returns the buffer
  183. */
  184. public getGBuffer(): MultiRenderTarget {
  185. return this._multiRenderTarget;
  186. }
  187. /**
  188. * Gets the number of samples used to render the buffer (anti aliasing).
  189. */
  190. public get samples(): number {
  191. return this._multiRenderTarget.samples;
  192. }
  193. /**
  194. * Sets the number of samples used to render the buffer (anti aliasing).
  195. */
  196. public set samples(value: number) {
  197. this._multiRenderTarget.samples = value;
  198. }
  199. /**
  200. * Disposes the renderer and frees up associated resources.
  201. */
  202. public dispose(): void {
  203. this.getGBuffer().dispose();
  204. }
  205. protected _createRenderTargets(): void {
  206. var engine = this._scene.getEngine();
  207. var count = 2;
  208. if (this._enablePosition) {
  209. this._positionIndex = count;
  210. count++;
  211. }
  212. if (this._enableVelocity) {
  213. this._velocityIndex = count;
  214. count++;
  215. }
  216. this._multiRenderTarget = new MultiRenderTarget("gBuffer",
  217. { width: engine.getRenderWidth() * this._ratio, height: engine.getRenderHeight() * this._ratio }, count, this._scene,
  218. { generateMipMaps: false, generateDepthTexture: true, defaultType: Engine.TEXTURETYPE_FLOAT });
  219. if (!this.isSupported) {
  220. return;
  221. }
  222. this._multiRenderTarget.wrapU = Texture.CLAMP_ADDRESSMODE;
  223. this._multiRenderTarget.wrapV = Texture.CLAMP_ADDRESSMODE;
  224. this._multiRenderTarget.refreshRate = 1;
  225. this._multiRenderTarget.renderParticles = false;
  226. this._multiRenderTarget.renderList = null;
  227. // set default depth value to 1.0 (far away)
  228. this._multiRenderTarget.onClearObservable.add((engine: Engine) => {
  229. engine.clear(new Color4(0.0, 0.0, 0.0, 1.0), true, true, true);
  230. });
  231. // Custom render function
  232. var renderSubMesh = (subMesh: SubMesh): void => {
  233. var mesh = subMesh.getRenderingMesh();
  234. var scene = this._scene;
  235. var engine = scene.getEngine();
  236. let material = subMesh.getMaterial();
  237. if (!material) {
  238. return;
  239. }
  240. // Velocity
  241. if (!this._previousWorldMatrices[mesh.uniqueId]) {
  242. this._previousWorldMatrices[mesh.uniqueId] = Matrix.Identity();
  243. }
  244. // Culling
  245. engine.setState(material.backFaceCulling, 0, false, scene.useRightHandedSystem);
  246. // Managing instances
  247. var batch = mesh._getInstancesRenderList(subMesh._id);
  248. if (batch.mustReturn) {
  249. return;
  250. }
  251. var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null);
  252. if (this.isReady(subMesh, hardwareInstancedRendering)) {
  253. engine.enableEffect(this._effect);
  254. mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
  255. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  256. this._effect.setMatrix("view", scene.getViewMatrix());
  257. // Alpha test
  258. if (material && material.needAlphaTesting()) {
  259. var alphaTexture = material.getAlphaTestTexture();
  260. if (alphaTexture) {
  261. this._effect.setTexture("diffuseSampler", alphaTexture);
  262. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  263. }
  264. }
  265. // Bones
  266. if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
  267. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  268. }
  269. // Velocity
  270. this._effect.setMatrix("previousWorldViewProjection", this._previousWorldMatrices[mesh.uniqueId]);
  271. // Draw
  272. mesh._processRendering(subMesh, this._effect, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  273. (isInstance, world) => this._effect.setMatrix("world", world));
  274. }
  275. // Velocity
  276. this._previousWorldMatrices[mesh.uniqueId] = mesh.getWorldMatrix().multiply(this._scene.getTransformMatrix());
  277. };
  278. this._multiRenderTarget.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void => {
  279. var index;
  280. if (depthOnlySubMeshes.length) {
  281. engine.setColorWrite(false);
  282. for (index = 0; index < depthOnlySubMeshes.length; index++) {
  283. renderSubMesh(depthOnlySubMeshes.data[index]);
  284. }
  285. engine.setColorWrite(true);
  286. }
  287. for (index = 0; index < opaqueSubMeshes.length; index++) {
  288. renderSubMesh(opaqueSubMeshes.data[index]);
  289. }
  290. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  291. renderSubMesh(alphaTestSubMeshes.data[index]);
  292. }
  293. };
  294. }
  295. }
  296. }