@vladkrutenyuk/cannon-es-debugger-pro
Version:
This is improved pro debugger for 'cannon-es' with 'three' to visualize all bodies and its shapes of physics world.
3 lines (2 loc) • 7.42 kB
JavaScript
var e=require("cannon-es"),t=require("three");Object.defineProperty(module.exports,"CannonEsDebuggerPro",{get:function(){return c},set:void 0,enumerable:!0,configurable:!0});class s extends t.BufferGeometry{constructor(e=1,s=1,a=1){super();let o=r(e,s,a),n=new t.Float32BufferAttribute(o,3),h=new t.Float32BufferAttribute(i,3);this.setAttribute("position",n),this.setAttribute("normal",h),this.computeBoundingBox(),this.computeBoundingSphere()}}const i=r(1,1,1);function r(e,t,s){let i=.5*e,r=.5*t,a=.5*s;return[-i,-r,a,i,-r,a,i,-r,a,i,-r,-a,i,-r,-a,-i,-r,-a,-i,-r,-a,-i,-r,a,-i,r,a,i,r,a,i,r,a,i,r,-a,i,r,-a,-i,r,-a,-i,r,-a,-i,r,a,i,-r,a,i,r,a,-i,-r,-a,-i,r,-a,i,-r,-a,i,r,-a,-i,-r,a,-i,r,a]}class a extends t.BufferGeometry{constructor(e){super();let s=[],i=[],r=new Map,a=0;for(let t=0;t<e.indices.length;t+=3)for(let n=0;n<3;n++){let h=e.indices[t+n],l=e.vertices[3*h],u=e.vertices[3*h+1],p=e.vertices[3*h+2];if(void 0===l||void 0===u||void 0===p){console.error("TrimeshShapeGeometry error;","Some vertcicies by indicies are undefined;",`x = ${l} = shape.verticies[${3*h}];`,`y = ${u} = shape.verticies[${3*h+1}];`,`z = ${p} = shape.verticies[${3*h+2}];`,"Please check and revise geometry you use for CANNON.Trimesh;",`Body id = ${e.body?.id}, shape id = ${e.id};`);continue}let c=`${o(l)},${o(u)},${o(p)}`;r.has(c)?i.push(r.get(c)):(s.push(l,u,p),r.set(c,a),i.push(a),a++)}this.setIndex(i),this.setAttribute("position",new t.Float32BufferAttribute(s,3)),this.computeVertexNormals(),this.computeBoundingBox(),this.computeBoundingSphere()}}function o(e){return 1e-10>Math.abs(e)?"0":`${e}`}const n=new e.Vec3;class h extends t.BufferGeometry{constructor(e){super();let s=[],i=[];for(let t=0;t<e.faces.length;t++){let r=e.faces[t];for(let t=0;t<r.length;t++){let a=r[t],o=r[(t+1)%r.length],h=e.vertices[a],l=e.vertices[o];i.push(h.x,h.y,h.z),n.set(h.x,h.y,h.z).normalize(),s.push(n.x,n.y,n.z),i.push(l.x,l.y,l.z),n.set(l.x,l.y,l.z).normalize(),s.push(n.x,n.y,n.z)}}let r=new t.Float32BufferAttribute(i,3),a=new t.Float32BufferAttribute(s,3);this.setAttribute("position",r),this.setAttribute("normal",a),this.computeBoundingBox(),this.computeBoundingSphere()}}class l extends t.BufferGeometry{constructor(e){super();let s=e.elementSize||1,i=[],r=[];for(let t=0;t<e.data.length;t++)for(let a=0;a<e.data[t].length;a++)t<e.data.length-1&&(i.push(t*s,a*s,e.data[t][a]),i.push((t+1)*s,a*s,e.data[t+1][a]),r.push(0,0,1,0,0,1)),a<e.data[t].length-1&&(i.push(t*s,a*s,e.data[t][a]),i.push(t*s,(a+1)*s,e.data[t][a+1]),r.push(0,0,1,0,0,1));this.setAttribute("position",new t.Float32BufferAttribute(new Float32Array(i),3)),this.setAttribute("normal",new t.Float32BufferAttribute(new Float32Array(r),3))}}class u extends t.BufferGeometry{constructor(e=10,s=10,i=0){super();let r=e/s,a=e/2,o=[],n=[];for(let e=0,t=-a;e<=s;e++,t+=r)o.push(-a,t,i,a,t,i),o.push(t,-a,i,t,a,i),n.push(0,0,1,0,0,1,0,0,1,0,0,1);this.setAttribute("position",new t.Float32BufferAttribute(o,3)),this.setAttribute("normal",new t.Float32BufferAttribute(n,3))}}class p extends t.BufferGeometry{constructor(e=1,s=64){super();let i=[],r=[],a=1/s*Math.PI*2,o=0;for(let t=0;t<s;t++){let t=o,s=t+a,n=Math.cos(t)*e,h=Math.sin(t)*e,l=Math.cos(s)*e,u=Math.sin(s)*e;i.push(n,h,0,l,u,0),r.push(n,h,0,l,u,0),i.push(n,0,h,l,0,u),r.push(n,0,h,l,0,u),i.push(0,n,h,0,l,u),r.push(0,n,h,0,l,u),o+=a}this.setAttribute("position",new t.Float32BufferAttribute(i,3)),this.setAttribute("normal",new t.Float32BufferAttribute(r,3)),this.computeBoundingBox(),this.computeBoundingSphere()}}class c extends t.EventDispatcher{_material;_lineMaterial;_world;_graphicsObjsGroup;_geometries=new Map;_graphicObjs=[];_isVisible=!0;get isVisible(){return this._isVisible}_isDestroyed=!1;get isDestroyed(){return this._isDestroyed}_color;get color(){return this._color}_offset={value:0};get offset(){return this._offset.value}constructor(e,s,i=65280,r=.005){super(),this._world=s,this._graphicsObjsGroup=new t.Group,e.add(this._graphicsObjsGroup),this._offset.value=r,this._color=i,this._material=new t.MeshBasicMaterial({color:i,wireframe:!0,toneMapped:!1}),this._lineMaterial=new t.LineBasicMaterial({color:i,toneMapped:!1});let a=e=>{e.uniforms._offset=this._offset,e.vertexShader=e.vertexShader.replace("#include <common>","#include <common>\nuniform float _offset;").replace("#include <begin_vertex>","#include <begin_vertex>\ntransformed += normalize(normal) * _offset;")};this._material.onBeforeCompile=a,this._lineMaterial.onBeforeCompile=a}createObj3d(s){let i=null,{SPHERE:r,BOX:o,PLANE:n,CYLINDER:u,CONVEXPOLYHEDRON:p,TRIMESH:c,HEIGHTFIELD:d}=e.Shape.types;switch(s.type){case r:i=new t.LineSegments(m,this._lineMaterial);break;case o:i=new t.LineSegments(g,this._lineMaterial);break;case n:i=new t.LineSegments(_,this._lineMaterial);break;case u:case p:{let e=new h(s);i=new t.LineSegments(e,this._lineMaterial),this.registerComplexShapeWithObj3d(i,s,e);break}case c:{let e=new a(s);i=new t.Mesh(e,this._material),this.registerComplexShapeWithObj3d(i,s,e);break}case d:{let e=new l(s);i=new t.LineSegments(e,this._lineMaterial),this.registerComplexShapeWithObj3d(i,s,e);break}default:i=new t.Mesh}return i.userData.shapeType=s.type,this._graphicsObjsGroup.add(i),i}registerComplexShapeWithObj3d(e,t,s){t.geometryId=s.id,this._geometries.set(s.id,s),e.userData.isComplexShapeObj3d=!0}rescaleObj3d(t,s){let{SPHERE:i,BOX:r,TRIMESH:a}=e.Shape.types;switch(s.type){case i:t.scale.setScalar(s.radius);break;case r:t.scale.copy(s.halfExtents).multiplyScalar(2);break;case a:t.scale.copy(s.scale)}}typeMatch(e,t){return!!e&&(e.userData.isComplexShapeObj3d?e.userData.shapeType===t.type&&e.geometry.id===t.geometryId:e.userData.shapeType===t.type)}updateObj3d(e,t){let s=this._graphicObjs[e],i=!1;return this.typeMatch(s,t)||(s&&this.removeObj3d(s),this._graphicObjs[e]=s=this.createObj3d(t),i=!0),this.rescaleObj3d(s,t),i}removeObj3d(e){this._graphicsObjsGroup.remove(e),e.userData.isComplexShapeObj3d&&(this._geometries.delete(e.geometry.id),e.geometry.dispose())}update(){if(this._isDestroyed)return;let e=0;for(let t of this._world.bodies)for(let s=0;s!==t.shapes.length;s++){let i=t.shapes[s],r=this.updateObj3d(e,i),a=this._graphicObjs[e];a&&(t.quaternion.vmult(t.shapeOffsets[s],f),t.position.vadd(f,f),t.quaternion.mult(t.shapeOrientations[s],b),a.position.copy(f),a.quaternion.copy(b),r?(d.init.body=t,d.init.obj3d=a,d.init.shape=i,this.dispatchEvent(d.init)):(d.update.body=t,d.update.obj3d=a,d.update.shape=i,this.dispatchEvent(d.update))),e++}for(let t=e;t<this._graphicObjs.length;t++){let e=this._graphicObjs[t];e&&this.removeObj3d(e)}this._graphicObjs.length=e}setColor(e){return this._material.color.set(e),this._lineMaterial.color.set(e),this._material.needsUpdate=!0,this._lineMaterial.needsUpdate=!0,this}setVisible(e){return this._isVisible=e,this._graphicsObjsGroup.visible=e,this}setOffset(e){return this._offset.value=e,this}clear(){for(this._graphicObjs=[];this._graphicsObjsGroup.children.length>0;)this._graphicsObjsGroup.remove(this._graphicsObjsGroup.children[0]);this._geometries.forEach(e=>{e.dispose()}),this._geometries.clear()}destroy(){this._isDestroyed=!0;let e=this._graphicsObjsGroup.parent;e?.remove(this._graphicsObjsGroup),this.clear(),this._material.dispose(),this._lineMaterial.dispose()}}const d={init:{type:"init"},update:{type:"update"}},f=new e.Vec3,b=new e.Quaternion,m=new p(1),g=new s(1,1,1),_=new u(100,20);
//# sourceMappingURL=index.js.map