-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.js
More file actions
34 lines (34 loc) · 2.25 KB
/
Copy pathview.js
File metadata and controls
34 lines (34 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import {Group,Mesh2,PhysicalMaterial,PlaneGeometry,SphereGeometry,CapsuleGeometry,CylinderGeometry,BoxGeometry,BufferGeometry,Float32BufferAttribute,Matrix4,Color} from '@kite3d/engine'
// MuJoCo geom types: plane=0, hfield=1, sphere=2, capsule=3, ellipsoid=4, cylinder=5, box=6, mesh=7.
export class ModelView {
constructor(description){
this.root=new Group();this.root.name='MuJoCo model view';this.root.rotation.x=-Math.PI/2
this.meshes=[];this.matrix=new Matrix4()
try{
for(const g of description.geometries){
const s=g.size;let geometry
if(g.type===0)geometry=new PlaneGeometry(2*Math.min(s[0]||3,1.4),2*Math.min(s[1]||3,1.4))
else if(g.type===2)geometry=new SphereGeometry(s[0],24,16)
else if(g.type===3){geometry=new CapsuleGeometry(s[0],2*s[1],8,16);geometry.rotateX(Math.PI/2)}
else if(g.type===4){geometry=new SphereGeometry(1,24,16);geometry.scale(...s)}
else if(g.type===5){geometry=new CylinderGeometry(s[0],s[0],2*s[1],24);geometry.rotateX(Math.PI/2)}
else if(g.type===6)geometry=new BoxGeometry(2*s[0],2*s[1],2*s[2])
else if(g.type===7){geometry=new BufferGeometry();geometry.setAttribute('position',new Float32BufferAttribute(g.vertices,3));geometry.setIndex(Array.from(g.faces));geometry.computeVertexNormals()}
else throw Error(`MuJoCo visual geom type ${g.type} is not supported (${g.name})`)
const material=new PhysicalMaterial({color:new Color().setRGB(...g.rgba.slice(0,3)),roughness:.65,metalness:0,transparent:g.rgba[3]<1,opacity:g.rgba[3],side:2})
const mesh=new Mesh2(geometry,material);mesh.name=g.name;mesh.userData.mujocoGeomId=g.id
this.meshes[g.id]=mesh;this.root.add(mesh)
}
}catch(error){this.dispose();throw error}
}
update(frame){
const p=frame.geomPositions,r=frame.geomMatrices
for(let i=0;i<this.meshes.length;i++){
const mesh=this.meshes[i],b=i*9
mesh.position.fromArray(p,i*3)
this.matrix.set(r[b],r[b+1],r[b+2],0,r[b+3],r[b+4],r[b+5],0,r[b+6],r[b+7],r[b+8],0,0,0,0,1)
mesh.quaternion.setFromRotationMatrix(this.matrix);mesh.updateMatrix()
}
}
dispose(){this.root.removeFromParent();for(const mesh of this.meshes){mesh.geometry.dispose();mesh.material.dispose()}this.meshes=[];this.root.clear()}
}