babylon.boundingBoxRenderer.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. module BABYLON {
  2. export class BoundingBoxRenderer {
  3. public frontColor = new Color3(1, 1, 1);
  4. public backColor = new Color3(0.1, 0.1, 0.1);
  5. public showBackLines = true;
  6. public renderList = new SmartArray<BoundingBox>(32);
  7. private _scene: Scene;
  8. private _colorShader: ShaderMaterial;
  9. private _vertexBuffer: VertexBuffer;
  10. private _vertexBuffers: { [key: string]: IVertexBuffer } = {};
  11. private _indexBuffer: WebGLBuffer;
  12. constructor(scene: Scene) {
  13. this._scene = scene;
  14. }
  15. private _prepareRessources(): void {
  16. if (this._colorShader) {
  17. return;
  18. }
  19. this._colorShader = new ShaderMaterial("colorShader", this._scene, "color",
  20. {
  21. attributes: [VertexBuffer.PositionKind],
  22. uniforms: ["worldViewProjection", "color"]
  23. });
  24. var engine = this._scene.getEngine();
  25. var boxdata = VertexData.CreateBox(1.0);
  26. this._vertexBuffer = new VertexBuffer(engine, boxdata.positions, VertexBuffer.PositionKind, false);
  27. this._vertexBuffers[VertexBuffer.PositionKind] = this._vertexBuffer;
  28. 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]);
  29. }
  30. public reset(): void {
  31. this.renderList.reset();
  32. }
  33. public render(): void {
  34. if (this.renderList.length === 0) {
  35. return;
  36. }
  37. this._prepareRessources();
  38. if (!this._colorShader.isReady()) {
  39. return;
  40. }
  41. var engine = this._scene.getEngine();
  42. engine.setDepthWrite(false);
  43. this._colorShader._preBind();
  44. for (var boundingBoxIndex = 0; boundingBoxIndex < this.renderList.length; boundingBoxIndex++) {
  45. var boundingBox = this.renderList.data[boundingBoxIndex];
  46. var min = boundingBox.minimum;
  47. var max = boundingBox.maximum;
  48. var diff = max.subtract(min);
  49. var median = min.add(diff.scale(0.5));
  50. var worldMatrix = Matrix.Scaling(diff.x, diff.y, diff.z)
  51. .multiply(Matrix.Translation(median.x, median.y, median.z))
  52. .multiply(boundingBox.getWorldMatrix());
  53. // VBOs
  54. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, this._colorShader.getEffect());
  55. if (this.showBackLines) {
  56. // Back
  57. engine.setDepthFunctionToGreaterOrEqual();
  58. this._scene.resetCachedMaterial();
  59. this._colorShader.setColor4("color", this.backColor.toColor4());
  60. this._colorShader.bind(worldMatrix);
  61. // Draw order
  62. engine.draw(false, 0, 24);
  63. }
  64. // Front
  65. engine.setDepthFunctionToLess();
  66. this._scene.resetCachedMaterial();
  67. this._colorShader.setColor4("color", this.frontColor.toColor4());
  68. this._colorShader.bind(worldMatrix);
  69. // Draw order
  70. engine.draw(false, 0, 24);
  71. }
  72. this._colorShader.unbind();
  73. engine.setDepthFunctionToLessOrEqual();
  74. engine.setDepthWrite(true);
  75. }
  76. public dispose(): void {
  77. if (!this._colorShader) {
  78. return;
  79. }
  80. this._colorShader.dispose();
  81. this._vertexBuffer.dispose();
  82. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  83. }
  84. }
  85. }