babylon.boundingBoxRenderer.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_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. this.renderList.push(boundingInfo.boundingBox);
  93. }
  94. }
  95. }
  96. private _activeMesh(sourceMesh: AbstractMesh, mesh: AbstractMesh): void {
  97. if (sourceMesh.showBoundingBox || this.scene.forceShowBoundingBoxes) {
  98. let boundingInfo = sourceMesh.getBoundingInfo();
  99. this.renderList.push(boundingInfo.boundingBox);
  100. }
  101. }
  102. private _prepareRessources(): void {
  103. if (this._colorShader) {
  104. return;
  105. }
  106. this._colorShader = new ShaderMaterial("colorShader", this.scene, "color",
  107. {
  108. attributes: [VertexBuffer.PositionKind],
  109. uniforms: ["world", "viewProjection", "color"]
  110. });
  111. var engine = this.scene.getEngine();
  112. var boxdata = VertexData.CreateBox({ size: 1.0 });
  113. this._vertexBuffers[VertexBuffer.PositionKind] = new VertexBuffer(engine, <FloatArray>boxdata.positions, VertexBuffer.PositionKind, false);
  114. this._createIndexBuffer();
  115. }
  116. private _createIndexBuffer(): void {
  117. var engine = this.scene.getEngine();
  118. 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]);
  119. }
  120. /**
  121. * Rebuilds the elements related to this component in case of
  122. * context lost for instance.
  123. */
  124. public rebuild(): void {
  125. let vb = this._vertexBuffers[VertexBuffer.PositionKind];
  126. if (vb) {
  127. vb._rebuild();
  128. }
  129. this._createIndexBuffer();
  130. }
  131. public reset(): void {
  132. this.renderList.reset();
  133. }
  134. public render(): void {
  135. if (this.renderList.length === 0) {
  136. return;
  137. }
  138. this._prepareRessources();
  139. if (!this._colorShader.isReady()) {
  140. return;
  141. }
  142. var engine = this.scene.getEngine();
  143. engine.setDepthWrite(false);
  144. this._colorShader._preBind();
  145. for (var boundingBoxIndex = 0; boundingBoxIndex < this.renderList.length; boundingBoxIndex++) {
  146. var boundingBox = this.renderList.data[boundingBoxIndex];
  147. var min = boundingBox.minimum;
  148. var max = boundingBox.maximum;
  149. var diff = max.subtract(min);
  150. var median = min.add(diff.scale(0.5));
  151. var worldMatrix = Matrix.Scaling(diff.x, diff.y, diff.z)
  152. .multiply(Matrix.Translation(median.x, median.y, median.z))
  153. .multiply(boundingBox.getWorldMatrix());
  154. // VBOs
  155. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, <Effect>this._colorShader.getEffect());
  156. if (this.showBackLines) {
  157. // Back
  158. engine.setDepthFunctionToGreaterOrEqual();
  159. this.scene.resetCachedMaterial();
  160. this._colorShader.setColor4("color", this.backColor.toColor4());
  161. this._colorShader.bind(worldMatrix);
  162. // Draw order
  163. engine.drawElementsType(Material.LineListDrawMode, 0, 24);
  164. }
  165. // Front
  166. engine.setDepthFunctionToLess();
  167. this.scene.resetCachedMaterial();
  168. this._colorShader.setColor4("color", this.frontColor.toColor4());
  169. this._colorShader.bind(worldMatrix);
  170. // Draw order
  171. engine.drawElementsType(Material.LineListDrawMode, 0, 24);
  172. }
  173. this._colorShader.unbind();
  174. engine.setDepthFunctionToLessOrEqual();
  175. engine.setDepthWrite(true);
  176. }
  177. public renderOcclusionBoundingBox(mesh: AbstractMesh): void {
  178. this._prepareRessources();
  179. if (!this._colorShader.isReady() || !mesh._boundingInfo) {
  180. return;
  181. }
  182. var engine = this.scene.getEngine();
  183. engine.setDepthWrite(false);
  184. engine.setColorWrite(false);
  185. this._colorShader._preBind();
  186. var boundingBox = mesh._boundingInfo.boundingBox;
  187. var min = boundingBox.minimum;
  188. var max = boundingBox.maximum;
  189. var diff = max.subtract(min);
  190. var median = min.add(diff.scale(0.5));
  191. var worldMatrix = Matrix.Scaling(diff.x, diff.y, diff.z)
  192. .multiply(Matrix.Translation(median.x, median.y, median.z))
  193. .multiply(boundingBox.getWorldMatrix());
  194. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, <Effect>this._colorShader.getEffect());
  195. engine.setDepthFunctionToLess();
  196. this.scene.resetCachedMaterial();
  197. this._colorShader.bind(worldMatrix);
  198. engine.drawElementsType(Material.LineListDrawMode, 0, 24);
  199. this._colorShader.unbind();
  200. engine.setDepthFunctionToLessOrEqual();
  201. engine.setDepthWrite(true);
  202. engine.setColorWrite(true);
  203. }
  204. public dispose(): void {
  205. if (!this._colorShader) {
  206. return;
  207. }
  208. this.renderList.dispose();
  209. this._colorShader.dispose();
  210. var buffer = this._vertexBuffers[VertexBuffer.PositionKind];
  211. if (buffer) {
  212. buffer.dispose();
  213. this._vertexBuffers[VertexBuffer.PositionKind] = null;
  214. }
  215. this.scene.getEngine()._releaseBuffer(this._indexBuffer);
  216. }
  217. }
  218. }