babylon.boundingBoxRenderer.js 3.3 KB

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