babylon.boundingBoxRenderer.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(null, boxdata.positions, BABYLON.VertexBuffer.PositionKind, false, engine);
  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 mesh = this.renderList.data[boundingBoxIndex];
  31. var boundingBox = mesh.getBoundingInfo().boundingBox;
  32. var min = boundingBox.minimum;
  33. var max = boundingBox.maximum;
  34. var diff = max.subtract(min);
  35. var median = min.add(diff.scale(0.5));
  36. var worldMatrix = BABYLON.Matrix.Scaling(diff.x, diff.y, diff.z).multiply(BABYLON.Matrix.Translation(median.x, median.y, median.z)).multiply(mesh.getWorldMatrix());
  37. // VBOs
  38. engine.bindBuffers(this._vb.getBuffer(), this._ib, [3], 3 * 4, this._colorShader.getEffect());
  39. if (this.showBackLines) {
  40. // Back
  41. engine.setDepthFunctionToGreaterOrEqual();
  42. this._colorShader.setColor3("color", this.backColor);
  43. this._colorShader.bind(worldMatrix, mesh);
  44. // Draw order
  45. engine.draw(false, 0, 24);
  46. }
  47. // Front
  48. engine.setDepthFunctionToLess();
  49. this._colorShader.setColor3("color", this.frontColor);
  50. this._colorShader.bind(worldMatrix, mesh);
  51. // Draw order
  52. engine.draw(false, 0, 24);
  53. }
  54. this._colorShader.unbind();
  55. engine.setDepthFunctionToLessOrEqual();
  56. engine.setDepthWrite(true);
  57. };
  58. BoundingBoxRenderer.prototype.dispose = function () {
  59. this._colorShader.dispose();
  60. this._vb.dispose();
  61. this._scene.getEngine()._releaseBuffer(this._ib);
  62. };
  63. return BoundingBoxRenderer;
  64. })();
  65. BABYLON.BoundingBoxRenderer = BoundingBoxRenderer;
  66. })(BABYLON || (BABYLON = {}));
  67. //# sourceMappingURL=babylon.boundingBoxRenderer.js.map