babylon.boundingBoxRenderer.js 3.0 KB

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