babylon.boundingBoxRenderer.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. module BABYLON {
  2. export interface Scene {
  3. /** @hidden (Backing field) */
  4. _boundingBoxRenderer: BoundingBoxRenderer;
  5. /** @hidden (Backing field) */
  6. _forceShowBoundingBoxes: boolean;
  7. /**
  8. * Gets or sets a boolean indicating if all bounding boxes must be rendered
  9. */
  10. forceShowBoundingBoxes: boolean;
  11. /**
  12. * Gets the bounding box renderer associated with the scene
  13. * @returns a BoundingBoxRenderer
  14. */
  15. getBoundingBoxRenderer(): BoundingBoxRenderer;
  16. }
  17. Object.defineProperty(Scene.prototype, "forceShowBoundingBoxes", {
  18. get: function (this:Scene) {
  19. return this._forceShowBoundingBoxes || false;
  20. },
  21. set: function (this:Scene, value: boolean) {
  22. this._forceShowBoundingBoxes = value;
  23. // Lazyly creates a BB renderer if needed.
  24. if (value) {
  25. this.getBoundingBoxRenderer();
  26. }
  27. },
  28. enumerable: true,
  29. configurable: true
  30. });
  31. Scene.prototype.getBoundingBoxRenderer = function(): BoundingBoxRenderer {
  32. if (!this._boundingBoxRenderer) {
  33. this._boundingBoxRenderer = new BoundingBoxRenderer(this);
  34. }
  35. return this._boundingBoxRenderer;
  36. }
  37. export interface AbstractMesh {
  38. /** @hidden (Backing field) */
  39. _showBoundingBox: boolean;
  40. /**
  41. * Gets or sets a boolean indicating if the bounding box must be rendered as well (false by default)
  42. */
  43. showBoundingBox: boolean;
  44. }
  45. Object.defineProperty(AbstractMesh.prototype, "showBoundingBox", {
  46. get: function (this: AbstractMesh) {
  47. return this._showBoundingBox || false;
  48. },
  49. set: function (this: AbstractMesh, value: boolean) {
  50. this._showBoundingBox = value;
  51. // Lazyly creates a BB renderer if needed.
  52. if (value) {
  53. this.getScene().getBoundingBoxRenderer();
  54. }
  55. },
  56. enumerable: true,
  57. configurable: true
  58. });
  59. export class BoundingBoxRenderer implements ISceneComponent {
  60. /**
  61. * The component name helpfull to identify the component in the list of scene components.
  62. */
  63. public readonly name = SceneComponentConstants.NAME_BOUNDINGBOXRENDERER;
  64. /**
  65. * The scene the component belongs to.
  66. */
  67. public scene: Scene;
  68. public frontColor = new Color3(1, 1, 1);
  69. public backColor = new Color3(0.1, 0.1, 0.1);
  70. public showBackLines = true;
  71. public renderList = new SmartArray<BoundingBox>(32);
  72. private _colorShader: ShaderMaterial;
  73. private _vertexBuffers: { [key: string]: Nullable<VertexBuffer> } = {};
  74. private _indexBuffer: WebGLBuffer;
  75. constructor(scene: Scene) {
  76. this.scene = scene;
  77. scene._addComponent(this);
  78. }
  79. /**
  80. * Registers the component in a given scene
  81. */
  82. public register(): void {
  83. this.scene._beforeEvaluateActiveMeshStage.registerStep(SceneComponentConstants.STEP_BEFOREEVALUATEACTIVEMESH_BOUNDINGBOXRENDERER, this, this.reset);
  84. this.scene._activeMeshStage.registerStep(SceneComponentConstants.STEP_ACTIVEMESH_BOUNDINGBOXRENDERER, this, this._activeMesh);
  85. this.scene._evaluateSubMeshStage.registerStep(SceneComponentConstants.STEP_EVALUATESUBMESH_BOUNDINGBOXRENDERER, this, this._evaluateSubMesh);
  86. this.scene._afterRenderingGroupDrawStage.registerStep(SceneComponentConstants.STEP_AFTERRENDERINGGROUPDRAW_BOUNDINGBOXRENDERER, this, this.render);
  87. }
  88. private _evaluateSubMesh(mesh: AbstractMesh, subMesh: SubMesh): void {
  89. if (mesh.showSubMeshesBoundingBox) {
  90. const boundingInfo = subMesh.getBoundingInfo();
  91. if (boundingInfo !== null && boundingInfo !== undefined) {
  92. boundingInfo.boundingBox._tag = mesh.renderingGroupId;
  93. this.renderList.push(boundingInfo.boundingBox);
  94. }
  95. }
  96. }
  97. private _activeMesh(sourceMesh: AbstractMesh, mesh: AbstractMesh): void {
  98. if (sourceMesh.showBoundingBox || this.scene.forceShowBoundingBoxes) {
  99. let boundingInfo = sourceMesh.getBoundingInfo();
  100. boundingInfo.boundingBox._tag = mesh.renderingGroupId;
  101. this.renderList.push(boundingInfo.boundingBox);
  102. }
  103. }
  104. private _prepareRessources(): void {
  105. if (this._colorShader) {
  106. return;
  107. }
  108. this._colorShader = new ShaderMaterial("colorShader", this.scene, "color",
  109. {
  110. attributes: [VertexBuffer.PositionKind],
  111. uniforms: ["world", "viewProjection", "color"]
  112. });
  113. var engine = this.scene.getEngine();
  114. var boxdata = VertexData.CreateBox({ size: 1.0 });
  115. this._vertexBuffers[VertexBuffer.PositionKind] = new VertexBuffer(engine, <FloatArray>boxdata.positions, VertexBuffer.PositionKind, false);
  116. this._createIndexBuffer();
  117. }
  118. private _createIndexBuffer(): void {
  119. var engine = this.scene.getEngine();
  120. this._indexBuffer = engine.createIndexBuffer([0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 7, 1, 6, 2, 5, 3, 4]);
  121. }
  122. /**
  123. * Rebuilds the elements related to this component in case of
  124. * context lost for instance.
  125. */
  126. public rebuild(): void {
  127. let vb = this._vertexBuffers[VertexBuffer.PositionKind];
  128. if (vb) {
  129. vb._rebuild();
  130. }
  131. this._createIndexBuffer();
  132. }
  133. public reset(): void {
  134. this.renderList.reset();
  135. }
  136. /**
  137. * Render the bounding boxes of a specific rendering group
  138. * @param renderingGroupId defines the rendering group to render
  139. */
  140. public render(renderingGroupId: number): void {
  141. if (this.renderList.length === 0) {
  142. return;
  143. }
  144. this._prepareRessources();
  145. if (!this._colorShader.isReady()) {
  146. return;
  147. }
  148. var engine = this.scene.getEngine();
  149. engine.setDepthWrite(false);
  150. this._colorShader._preBind();
  151. for (var boundingBoxIndex = 0; boundingBoxIndex < this.renderList.length; boundingBoxIndex++) {
  152. var boundingBox = this.renderList.data[boundingBoxIndex];
  153. if (boundingBox._tag !== renderingGroupId) {
  154. continue;
  155. }
  156. var min = boundingBox.minimum;
  157. var max = boundingBox.maximum;
  158. var diff = max.subtract(min);
  159. var median = min.add(diff.scale(0.5));
  160. var worldMatrix = Matrix.Scaling(diff.x, diff.y, diff.z)
  161. .multiply(Matrix.Translation(median.x, median.y, median.z))
  162. .multiply(boundingBox.getWorldMatrix());
  163. // VBOs
  164. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, <Effect>this._colorShader.getEffect());
  165. if (this.showBackLines) {
  166. // Back
  167. engine.setDepthFunctionToGreaterOrEqual();
  168. this.scene.resetCachedMaterial();
  169. this._colorShader.setColor4("color", this.backColor.toColor4());
  170. this._colorShader.bind(worldMatrix);
  171. // Draw order
  172. engine.drawElementsType(Material.LineListDrawMode, 0, 24);
  173. }
  174. // Front
  175. engine.setDepthFunctionToLess();
  176. this.scene.resetCachedMaterial();
  177. this._colorShader.setColor4("color", this.frontColor.toColor4());
  178. this._colorShader.bind(worldMatrix);
  179. // Draw order
  180. engine.drawElementsType(Material.LineListDrawMode, 0, 24);
  181. }
  182. this._colorShader.unbind();
  183. engine.setDepthFunctionToLessOrEqual();
  184. engine.setDepthWrite(true);
  185. }
  186. public renderOcclusionBoundingBox(mesh: AbstractMesh): void {
  187. this._prepareRessources();
  188. if (!this._colorShader.isReady() || !mesh._boundingInfo) {
  189. return;
  190. }
  191. var engine = this.scene.getEngine();
  192. engine.setDepthWrite(false);
  193. engine.setColorWrite(false);
  194. this._colorShader._preBind();
  195. var boundingBox = mesh._boundingInfo.boundingBox;
  196. var min = boundingBox.minimum;
  197. var max = boundingBox.maximum;
  198. var diff = max.subtract(min);
  199. var median = min.add(diff.scale(0.5));
  200. var worldMatrix = Matrix.Scaling(diff.x, diff.y, diff.z)
  201. .multiply(Matrix.Translation(median.x, median.y, median.z))
  202. .multiply(boundingBox.getWorldMatrix());
  203. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, <Effect>this._colorShader.getEffect());
  204. engine.setDepthFunctionToLess();
  205. this.scene.resetCachedMaterial();
  206. this._colorShader.bind(worldMatrix);
  207. engine.drawElementsType(Material.LineListDrawMode, 0, 24);
  208. this._colorShader.unbind();
  209. engine.setDepthFunctionToLessOrEqual();
  210. engine.setDepthWrite(true);
  211. engine.setColorWrite(true);
  212. }
  213. public dispose(): void {
  214. if (!this._colorShader) {
  215. return;
  216. }
  217. this.renderList.dispose();
  218. this._colorShader.dispose();
  219. var buffer = this._vertexBuffers[VertexBuffer.PositionKind];
  220. if (buffer) {
  221. buffer.dispose();
  222. this._vertexBuffers[VertexBuffer.PositionKind] = null;
  223. }
  224. this.scene.getEngine()._releaseBuffer(this._indexBuffer);
  225. }
  226. }
  227. }