babylon.boundingBoxRenderer.js 3.6 KB

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