babylon.boundingBoxRenderer.ts 3.6 KB

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