babylon.boundingBoxRenderer.js 3.9 KB

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