@@ -37,7 +37,6 @@ public static ZOSimDocumentRoot Import(XmlDocument xmlDocument, string workingDi
3737 XmlNode [ ] xmlLinks = robot . GetChildrenByName ( "link" ) ;
3838 Dictionary < string , Tuple < XmlNode , GameObject > > goLinks = new Dictionary < string , Tuple < XmlNode , GameObject > > ( ) ;
3939
40- // find root link. which is a link without an explicit parent
4140
4241 // create links
4342 foreach ( XmlNode xmlLink in xmlLinks ) {
@@ -92,7 +91,7 @@ public static ZOSimDocumentRoot Import(XmlDocument xmlDocument, string workingDi
9291 if ( xmlMesh != null ) {
9392 string meshFileName = xmlMesh . Attributes [ "filename" ] . Value ;
9493 string meshFilePath = Path . Combine ( workingDirectory , meshFileName ) ;
95- visualGeo = ZOImportOBJ . Import ( meshFilePath ) ;
94+ visualGeo = ZOImportOBJ . Import ( meshFilePath , splitByMaterial : true ) ;
9695 if ( xmlMesh . Attributes [ "scale" ] != null ) {
9796 Vector3 scale = xmlMesh . Attributes [ "scale" ] . Value . FromURDFStringToVector3 ( ) . Ros2UnityScale ( ) ;
9897 visualGeo . transform . localScale = new Vector3 ( visualGeo . transform . localScale . x * scale . x , visualGeo . transform . localScale . y * scale . y , visualGeo . transform . localScale . z * scale . z ) ;
@@ -123,11 +122,98 @@ public static ZOSimDocumentRoot Import(XmlDocument xmlDocument, string workingDi
123122
124123 }
125124
126- // get the origin
127- // XmlNode origin =
125+ // process the collisions
126+ XmlNode [ ] xmlCollisions = xmlLink . GetChildrenByName ( "collision" ) ;
127+
128+ foreach ( XmlNode xmlCollision in xmlCollisions ) {
129+
130+
131+ XmlNode [ ] xmlGeometries = xmlCollision . GetChildrenByName ( "geometry" ) ;
132+
133+ foreach ( XmlNode xmlGeom in xmlGeometries ) {
134+
135+ GameObject collisionGeo = null ;
136+
137+ XmlNode xmlBox = xmlGeom . GetChildByName ( "box" ) ;
138+ if ( xmlBox != null ) {
139+ collisionGeo = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
140+ Vector3 size = xmlBox . Attributes [ "size" ] . Value . FromURDFStringToVector3 ( ) ;
141+
142+ collisionGeo . transform . localScale = size . Ros2UnityScale ( ) ;
143+ }
144+
145+ XmlNode xmlCylinder = xmlGeom . GetChildByName ( "cylinder" ) ;
146+ if ( xmlCylinder != null ) {
147+ collisionGeo = GameObject . CreatePrimitive ( PrimitiveType . Cylinder ) ;
148+ float radius = float . Parse ( xmlCylinder . Attributes [ "radius" ] . Value ) ;
149+ float length = float . Parse ( xmlCylinder . Attributes [ "length" ] . Value ) ;
150+ collisionGeo . transform . localScale = new Vector3 ( radius * 2.0f , length * 0.5f , radius * 2.0f ) ;
151+ }
152+
153+ XmlNode xmlSphere = xmlGeom . GetChildByName ( "sphere" ) ;
154+ if ( xmlSphere != null ) {
155+ collisionGeo = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
156+ float radius = float . Parse ( xmlSphere . Attributes [ "radius" ] . Value ) ;
157+ collisionGeo . transform . localScale = new Vector3 ( radius * 2.0f , radius * 2.0f , radius * 2.0f ) ;
158+ }
159+
160+ XmlNode xmlMesh = xmlGeom . GetChildByName ( "mesh" ) ;
161+ if ( xmlMesh != null ) {
162+ string meshFileName = xmlMesh . Attributes [ "filename" ] . Value ;
163+ string meshFilePath = Path . Combine ( workingDirectory , meshFileName ) ;
164+ collisionGeo = ZOImportOBJ . Import ( meshFilePath , splitByMaterial : false ) ;
165+
166+ MeshFilter [ ] meshFilters = collisionGeo . GetComponentsInChildren < MeshFilter > ( ) ;
167+ foreach ( MeshFilter meshFilter in meshFilters ) {
168+ MeshCollider meshCollider = collisionGeo . AddComponent < MeshCollider > ( ) ;
169+
170+ // add mesh collider
171+ meshCollider . sharedMesh = meshFilter . sharedMesh ;
172+ meshCollider . convex = true ;
173+
174+ // remove mesh renderer and mesh filter
175+ GameObject . DestroyImmediate ( collisionGeo . GetComponent < MeshRenderer > ( ) ) ;
176+ GameObject . DestroyImmediate ( collisionGeo . GetComponent < MeshFilter > ( ) ) ;
177+
178+ }
179+
180+
181+ if ( xmlMesh . Attributes [ "scale" ] != null ) {
182+ Vector3 scale = xmlMesh . Attributes [ "scale" ] . Value . FromURDFStringToVector3 ( ) . Ros2UnityScale ( ) ;
183+ collisionGeo . transform . localScale = new Vector3 ( collisionGeo . transform . localScale . x * scale . x , collisionGeo . transform . localScale . y * scale . y , collisionGeo . transform . localScale . z * scale . z ) ;
184+ }
185+
186+ collisionGeo . transform . localRotation = Quaternion . Euler ( 270 , 90 , 0 ) ;
187+
188+
189+
190+
191+ }
192+ if ( collisionGeo != null ) {
193+ // set parent
194+ collisionGeo . transform . SetParent ( goCollisionsEmpty . transform ) ;
195+ if ( xmlCollision . Attributes [ "name" ] != null ) {
196+ collisionGeo . name = xmlCollision . Attributes [ "name" ] . Value ;
197+ }
198+
199+ // set transform
200+ XmlNode xmlOrigin = xmlCollision . GetChildByName ( "origin" ) ;
201+ if ( xmlOrigin != null ) {
202+ Tuple < Vector3 , Quaternion > transform = OriginXMLToUnity ( xmlOrigin ) ;
203+ collisionGeo . transform . localPosition += transform . Item1 ;
204+ collisionGeo . transform . localRotation *= transform . Item2 ;
205+
206+ }
207+
208+ }
209+
210+ }
211+
212+ }
128213
129214 }
130215
216+
131217 // create hierarchy from joints
132218 XmlNode [ ] xmlJoints = robot . GetChildrenByName ( "joint" ) ;
133219 foreach ( var goLink in goLinks ) {
0 commit comments