babylon.boundingBoxRenderer.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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._colorShader.setColor4("color", this.backColor.toColor4());
  42. this._colorShader.bind(worldMatrix);
  43. // Draw order
  44. engine.draw(false, 0, 24);
  45. }
  46. // Front
  47. engine.setDepthFunctionToLess();
  48. this._colorShader.setColor4("color", this.frontColor.toColor4());
  49. this._colorShader.bind(worldMatrix);
  50. // Draw order
  51. engine.draw(false, 0, 24);
  52. }
  53. this._colorShader.unbind();
  54. engine.setDepthFunctionToLessOrEqual();
  55. engine.setDepthWrite(true);
  56. };
  57. BoundingBoxRenderer.prototype.dispose = function () {
  58. this._colorShader.dispose();
  59. this._vb.dispose();
  60. this._scene.getEngine()._releaseBuffer(this._ib);
  61. };
  62. return BoundingBoxRenderer;
  63. })();
  64. BABYLON.BoundingBoxRenderer = BoundingBoxRenderer;
  65. })(BABYLON || (BABYLON = {}));
  66. //# sourceMappingURL=babylon.boundingBoxRenderer.js.map