babylon.physicsViewer.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. module BABYLON.Debug {
  2. export class PhysicsViewer {
  3. protected _impostors:Array<PhysicsImpostor> = [];
  4. protected _meshes:Array<AbstractMesh> = [];
  5. protected _scene:Scene;
  6. protected _numMeshes = 0;
  7. protected _physicsEnginePlugin:IPhysicsEnginePlugin;
  8. private _renderFunction: () => void;
  9. constructor(scene:Scene){
  10. this._scene = scene || Engine.LastCreatedScene;
  11. this._physicsEnginePlugin = this._scene.getPhysicsEngine().getPhysicsPlugin();
  12. }
  13. protected _updateDebugMeshes():void{
  14. var plugin = this._physicsEnginePlugin;
  15. for (var i = 0; i < this._numMeshes; i++){
  16. if(this._impostors[i].isDisposed){
  17. this.hideImpostor(this._impostors[i--]);
  18. }else{
  19. plugin.syncMeshWithImpostor(this._meshes[i], this._impostors[i]);
  20. }
  21. }
  22. }
  23. public showImpostor(impostor:PhysicsImpostor):void{
  24. for (var i = 0; i < this._numMeshes; i++){
  25. if(this._impostors[i] == impostor){
  26. return;
  27. }
  28. }
  29. var debugMesh = this._physicsEnginePlugin.getDebugMesh(impostor, this._scene);
  30. if(debugMesh){
  31. this._impostors[this._numMeshes] = impostor;
  32. this._meshes[this._numMeshes] = debugMesh;
  33. if(this._numMeshes === 0){
  34. this._renderFunction = this._updateDebugMeshes.bind(this);
  35. this._scene.registerBeforeRender(this._renderFunction);
  36. }
  37. this._numMeshes++;
  38. }
  39. }
  40. public hideImpostor(impostor:PhysicsImpostor){
  41. var removed = false;
  42. for (var i = 0; i < this._numMeshes; i++){
  43. if(this._impostors[i] == impostor){
  44. this._scene.removeMesh(this._meshes[i]);
  45. this._meshes[i].dispose();
  46. this._numMeshes--;
  47. if(this._numMeshes > 0){
  48. this._meshes[i] = this._meshes[this._numMeshes];
  49. this._impostors[i] = this._impostors[this._numMeshes];
  50. this._meshes[this._numMeshes] = null;
  51. this._impostors[this._numMeshes] = null;
  52. }else{
  53. this._meshes[0] = null;
  54. this._impostors[0] = null;
  55. }
  56. removed = true;
  57. break;
  58. }
  59. }
  60. if(removed && this._numMeshes === 0){
  61. this._scene.unregisterBeforeRender(this._renderFunction);
  62. }
  63. }
  64. public dispose(){
  65. for (var i = 0; i < this._numMeshes; i++){
  66. this.hideImpostor(this._impostors[i]);
  67. }
  68. this._impostors.length = 0;
  69. this._scene = null;
  70. this._physicsEnginePlugin = null;
  71. }
  72. }
  73. }