babylon.boundingBoxRenderer.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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)
  45. .multiply(BABYLON.Matrix.Translation(median.x, median.y, median.z))
  46. .multiply(boundingBox.getWorldMatrix());
  47. // VBOs
  48. engine.bindBuffers(this._vb.getBuffer(), this._ib, [3], 3 * 4, this._colorShader.getEffect());
  49. if (this.showBackLines) {
  50. // Back
  51. engine.setDepthFunctionToGreaterOrEqual();
  52. this._scene.resetCachedMaterial();
  53. this._colorShader.setColor4("color", this.backColor.toColor4());
  54. this._colorShader.bind(worldMatrix);
  55. // Draw order
  56. engine.draw(false, 0, 24);
  57. }
  58. // Front
  59. engine.setDepthFunctionToLess();
  60. this._scene.resetCachedMaterial();
  61. this._colorShader.setColor4("color", this.frontColor.toColor4());
  62. this._colorShader.bind(worldMatrix);
  63. // Draw order
  64. engine.draw(false, 0, 24);
  65. }
  66. this._colorShader.unbind();
  67. engine.setDepthFunctionToLessOrEqual();
  68. engine.setDepthWrite(true);
  69. };
  70. BoundingBoxRenderer.prototype.dispose = function () {
  71. if (!this._colorShader) {
  72. return;
  73. }
  74. this._colorShader.dispose();
  75. this._vb.dispose();
  76. this._scene.getEngine()._releaseBuffer(this._ib);
  77. };
  78. return BoundingBoxRenderer;
  79. })();
  80. BABYLON.BoundingBoxRenderer = BoundingBoxRenderer;
  81. })(BABYLON || (BABYLON = {}));
  82. //# sourceMappingURL=babylon.boundingBoxRenderer.js.map