@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) • 6.98 kB
JavaScript
import{Shape as e,Vec3 as t,Quaternion as s}from"cannon-es";import{Group as i,MeshBasicMaterial as r,LineBasicMaterial as a,LineSegments as o,Mesh as h,EventDispatcher as n,Float32BufferAttribute as l,BufferGeometry as p}from"three";let u=c(1,1,1);function c(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 d extends p{constructor(e){super();let t=[],s=[],i=new Map,r=0;for(let a=0;a<e.indices.length;a+=3)for(let o=0;o<3;o++){let h=e.indices[a+o],n=e.vertices[3*h],l=e.vertices[3*h+1],p=e.vertices[3*h+2];if(void 0===n||void 0===l||void 0===p){console.error("TrimeshShapeGeometry error;","Some vertcicies by indicies are undefined;",`x = ${n} = shape.verticies[${3*h}];`,`y = ${l} = 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 u=`${b(n)},${b(l)},${b(p)}`;i.has(u)?s.push(i.get(u)):(t.push(n,l,p),i.set(u,r),s.push(r),r++)}this.setIndex(s),this.setAttribute("position",new l(t,3)),this.computeVertexNormals(),this.computeBoundingBox(),this.computeBoundingSphere()}}function b(e){return 1e-10>Math.abs(e)?"0":`${e}`}let m=new t;class _ extends p{constructor(e){super();let t=[],s=[];for(let i=0;i<e.faces.length;i++){let r=e.faces[i];for(let i=0;i<r.length;i++){let a=r[i],o=r[(i+1)%r.length],h=e.vertices[a],n=e.vertices[o];s.push(h.x,h.y,h.z),m.set(h.x,h.y,h.z).normalize(),t.push(m.x,m.y,m.z),s.push(n.x,n.y,n.z),m.set(n.x,n.y,n.z).normalize(),t.push(m.x,m.y,m.z)}}let i=new l(s,3),r=new l(t,3);this.setAttribute("position",i),this.setAttribute("normal",r),this.computeBoundingBox(),this.computeBoundingSphere()}}class g extends p{constructor(e){super();let t=e.elementSize||1,s=[],i=[];for(let r=0;r<e.data.length;r++)for(let a=0;a<e.data[r].length;a++)r<e.data.length-1&&(s.push(r*t,a*t,e.data[r][a]),s.push((r+1)*t,a*t,e.data[r+1][a]),i.push(0,0,1,0,0,1)),a<e.data[r].length-1&&(s.push(r*t,a*t,e.data[r][a]),s.push(r*t,(a+1)*t,e.data[r][a+1]),i.push(0,0,1,0,0,1));this.setAttribute("position",new l(new Float32Array(s),3)),this.setAttribute("normal",new l(new Float32Array(i),3))}}class f extends n{_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,t,s=65280,o=.005){super(),this._world=t,this._graphicsObjsGroup=new i,e.add(this._graphicsObjsGroup),this._offset.value=o,this._color=s,this._material=new r({color:s,wireframe:!0,toneMapped:!1}),this._lineMaterial=new a({color:s,toneMapped:!1});let h=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=h,this._lineMaterial.onBeforeCompile=h}createObj3d(t){let s=null,{SPHERE:i,BOX:r,PLANE:a,CYLINDER:n,CONVEXPOLYHEDRON:l,TRIMESH:p,HEIGHTFIELD:u}=e.types;switch(t.type){case i:s=new o(j,this._lineMaterial);break;case r:s=new o(x,this._lineMaterial);break;case a:s=new o(v,this._lineMaterial);break;case n:case l:{let e=new _(t);s=new o(e,this._lineMaterial),this.registerComplexShapeWithObj3d(s,t,e);break}case p:{let e=new d(t);s=new h(e,this._material),this.registerComplexShapeWithObj3d(s,t,e);break}case u:{let e=new g(t);s=new o(e,this._lineMaterial),this.registerComplexShapeWithObj3d(s,t,e);break}default:s=new h}return s.userData.shapeType=t.type,this._graphicsObjsGroup.add(s),s}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.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],O),t.position.vadd(O,O),t.quaternion.mult(t.shapeOrientations[s],w),a.position.copy(O),a.quaternion.copy(w),r?(y.init.body=t,y.init.obj3d=a,y.init.shape=i,this.dispatchEvent(y.init)):(y.update.body=t,y.update.obj3d=a,y.update.shape=i,this.dispatchEvent(y.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()}}let y={init:{type:"init"},update:{type:"update"}},O=new t,w=new s,j=new class extends p{constructor(e=1,t=64){super();let s=[],i=[],r=1/t*Math.PI*2,a=0;for(let o=0;o<t;o++){let t=a,o=t+r,h=Math.cos(t)*e,n=Math.sin(t)*e,l=Math.cos(o)*e,p=Math.sin(o)*e;s.push(h,n,0,l,p,0),i.push(h,n,0,l,p,0),s.push(h,0,n,l,0,p),i.push(h,0,n,l,0,p),s.push(0,h,n,0,l,p),i.push(0,h,n,0,l,p),a+=r}this.setAttribute("position",new l(s,3)),this.setAttribute("normal",new l(i,3)),this.computeBoundingBox(),this.computeBoundingSphere()}}(1),x=new class extends p{constructor(e=1,t=1,s=1){super();let i=new l(c(e,t,s),3),r=new l(u,3);this.setAttribute("position",i),this.setAttribute("normal",r),this.computeBoundingBox(),this.computeBoundingSphere()}}(1,1,1),v=new class extends p{constructor(e=10,t=10,s=0){super();let i=e/t,r=e/2,a=[],o=[];for(let e=0,h=-r;e<=t;e++,h+=i)a.push(-r,h,s,r,h,s),a.push(h,-r,s,h,r,s),o.push(0,0,1,0,0,1,0,0,1,0,0,1);this.setAttribute("position",new l(a,3)),this.setAttribute("normal",new l(o,3))}}(100,20);export{f as CannonEsDebuggerPro};
//# sourceMappingURL=module.js.map