-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathMJImport.cs
908 lines (759 loc) · 37.2 KB
/
MJImport.cs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/*
Original work Copyright 2019 Roboti LLC
Modified work Copyright 2019 Panasonic Beta, a division of Panasonic Corporation of North America
Redistribution and use of this file (hereafter "Software") in source and
binary forms, with or without modification, are permitted provided that
the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
using System.IO;
using System.Xml;
using System;
public class MJImport : MonoBehaviour
{
// script options
private string modelFile = null;
private string fileName = "";
public bool recomputeUV = false;
public bool recomputeNormal = false;
public bool importTexture = true;
public bool enableRemote = false;
public string tcpAddress = "127.0.0.1";
public int tcpPort = 1050;
public bool enable_rendering = false;
private readonly bool noVSync = true;
// Unity element arrays
Texture2D[] textures;
SortedDictionary<int, Material> materials; // (mujoco material id) -> Material
GameObject[] objects;
GameObject root = null;
SortedDictionary<string, string> assetMap;
SortedDictionary<string, Vector3> scaleMap;
SortedDictionary<string, byte> segmentIdMap;
SortedDictionary<string, byte> meshSegmentMap;
Matrix4x4 importSwap;
Vector3 eulerRotation = new Vector3(0,0,0);
Vector3 mirror = new Vector3(1, 1, 1);
Material default_material = null;
void OnGUI()
{
/*
if (GUI.Button(new Rect(10, 90, 100, 30), "Import"))
{
RunImport();
}
*/
}
// present GUI, get options, run importer
void Start()
{
default_material = Resources.Load<Material>("Materials/Default");
//TODO: take IP arguments (beinding address? port number? from command line
// tcpAddress = EditorGUILayout.TextField("TCP Address", tcpAddress);
// tcpPort = EditorGUILayout.IntField("TCP Port", tcpPort);
// noVSync = EditorGUILayout.Toggle("Disable V-Sync", noVSync);
// For Furniture Assembly Environment: change the path of StreamingAssets
modelFile = Application.streamingAssetsPath + "/default.xml";
transform.localScale.Set(transform.localScale.x, transform.localScale.x, transform.localScale.x);
assetMap = new SortedDictionary<string, string>();
scaleMap = new SortedDictionary<string, Vector3>();
materials = new SortedDictionary<int, Material>();
meshSegmentMap = new SortedDictionary<string, byte>();
DoorGym.Configuration.Initialize();
Resources.Load("Packages/com.unity.postprocessing/PostProcessing/PostProcessResources.asset");
RunImport();
}
// convert transform from plugin to GameObject
static unsafe void SetTransform(GameObject obj, MJP.TTransform transform)
{
Quaternion q = new Quaternion(0, 0, 0, 1);
q.SetLookRotation(
new Vector3(transform.yaxis[0], -transform.yaxis[2], transform.yaxis[1]),
new Vector3(-transform.zaxis[0], transform.zaxis[2], -transform.zaxis[1])
);
obj.transform.localPosition = new Vector3(-transform.position[0], transform.position[2], -transform.position[1]);
obj.transform.localRotation = q;
obj.transform.localScale = new Vector3(transform.scale[0], transform.scale[2], transform.scale[1]);
}
// convert transform from plugin to Camera
static unsafe void SetCamera(Camera cam, MJP.TTransform transform)
{
Quaternion q = new Quaternion(0, 0, 0, 1);
q.SetLookRotation(
new Vector3(transform.zaxis[0], -transform.zaxis[2], transform.zaxis[1]),
new Vector3(-transform.yaxis[0], transform.yaxis[2], -transform.yaxis[1])
);
cam.transform.localPosition = new Vector3(-transform.position[0], transform.position[2], -transform.position[1]);
cam.transform.localRotation = q;
}
// add multiple camera
private unsafe void AddCameras(int ncamera) {
if (enable_rendering == false)
{
Destroy(GameObject.Find("PreviewCamera"));
}
else
{
Destroy(GameObject.Find("DummyCamera"));
}
for (int i = -1; i < ncamera; i++) {
// add camera under root
GameObject camobj = new GameObject("camera" + i);
camobj.layer = LayerMask.NameToLayer("PostProcessing");
camobj.transform.parent = root.transform;
Camera thecamera = camobj.AddComponent<Camera>();
// For Furniture Assembly Environment: remove SITE from the culling mask
thecamera.cullingMask = 1 + (1 << 1) + (1 << 2) + (1 << 4) + (1 << 8);
thecamera.backgroundColor = new Color(1f, 1f, 1f);
thecamera.clearFlags = CameraClearFlags.SolidColor;
Shader segshader = Shader.Find("Unlit/SegmentationColor");
SegmentationShader shadersub = camobj.AddComponent<SegmentationShader>();
DepthShader shaderdepth = camobj.AddComponent<DepthShader>();
// set field of view, near, far
MJP.TCamera cam;
MJP.GetCamera(i, &cam);
thecamera.fieldOfView = cam.fov;
// For Furniture Assembly Environment: set znear and zfar independent to model extent.
thecamera.nearClipPlane = 0.01f;
thecamera.farClipPlane = 10f;
//thecamera.enabled = false;
//camobj.SetActive(false);
// set transform
MJP.TTransform transform;
MJP.GetCameraState(i, &transform);
SetCamera(thecamera, transform);
}
}
// import materials
private unsafe void ImportMaterials(int nmaterial)
{
if (default_material == null)
{
Debug.Log("Skipped assigning a material because material is missing");
}
else
{
materials.Add(-1, new Material(default_material));
}
// process materials
for ( int i=0; i<nmaterial; i++ )
{
// get material name
StringBuilder name = new StringBuilder(100);
MJP.GetElementName(MJP.TElement.MATERIAL, i, name, 100);
string matname = name.ToString();
Material matt = Resources.Load<Material>("Materials/" + matname);
if ( matt == null )
{
Debug.Log("No material for " + matname + " in Resources");
if (default_material == null)
{
Debug.Log("Skipped assigning a material because material is missing");
}
else
{
matt = new Material(default_material);
}
}
if (matt != null)
{
materials.Add(i, matt);
}
}
}
private void BuildAssetDatabase(string file)
{
scaleMap.Clear();
assetMap.Clear();
meshSegmentMap.Clear();
materials.Clear();
Stack<byte> segments = new Stack<byte>();
segments.Push(0);
string cwd = Path.GetDirectoryName(file);
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Async = true;
using (XmlReader reader = XmlReader.Create(file, settings))
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
switch(reader.Name)
{
// Look up the segmentation id by comparing body names to the segmentation id list.
// We keep track of these ids in a stack, so that we can inherit segmentation from
// parent objects without writing the segmentation term over and over again in the XML
case "body":
if (reader.HasAttributes)
{
while (reader.MoveToNextAttribute())
{
if (reader.Name == "name")
{
byte segment = 0;
int first_underscore = reader.Value.IndexOf('_');
string segname = first_underscore >= 0 ? reader.Value.Substring(0, first_underscore).ToLower() : reader.Value.ToLower();
//Check if this object's name is in the segmentation list
if( DoorGym.Configuration.SegmentIDMap.TryGetValue(segname, out segment) )
{
segments.Push(segment);
}
else // Inherit segmentation from the parent if we aren't in the segmentation list
{
segments.Push(segments.Peek());
}
break;
}
}
reader.MoveToElement();
}
break;
// 'geom' tags is always under 'body', so we can assign the segmentation id by just checking the top of the stack
case "geom":
if (reader.HasAttributes)
{
while (reader.MoveToNextAttribute())
{
if (reader.Name == "name" || reader.Name == "mesh")
{
meshSegmentMap[reader.Value] = segments.Peek();
break;
}
}
reader.MoveToElement();
}
break;
// Build a list of meshes here so we can load the mesh from an alternate source or a Unity asset, if needed.
// Initally added for testing, currently not used, left it anyway, enjoy.
case "mesh":
if (reader.HasAttributes)
{
string meshPath = null;
string meshName = null;
Vector3 scale = new Vector3();
while (reader.MoveToNextAttribute())
{
switch (reader.Name)
{
case "file":
meshPath = reader.Value;
break;
case "name":
if(meshName == null) meshName = reader.Value; //"mesh" gets priority
break;
case "mesh":
meshName = reader.Value;
break;
case "scale":
string[] words = reader.Value.Split();
scale.Set(System.Convert.ToSingle(words[0]), System.Convert.ToSingle(words[1]), System.Convert.ToSingle(words[2]));
break;
}
}
if (meshPath != null && meshName != null)
{
assetMap.Add(meshName, meshPath);
}
if (scale.sqrMagnitude != 0 && meshName != null)
{
scaleMap.Add(meshName, scale);
}
reader.MoveToElement();
}
break;
case "include":
if (reader.HasAttributes)
{
while (reader.MoveToNextAttribute())
{
if (reader.Name == "file")
{
BuildAssetDatabase(Path.Combine(cwd, reader.Value));
break;
}
}
reader.MoveToElement();
}
//
break;
}
break;
case XmlNodeType.Text:
case XmlNodeType.EndElement:
default:
// Pop the segmentation ID off the stack once we leave the 'body' tag
if (reader.Name == "body")
{
segments.Pop();
}
break;
}
}
}
}
}
// import renderable objects
private unsafe void ImportObjects(int nobject)
{
string cwd = Path.GetDirectoryName(modelFile);
// make primitives
PrimitiveType[] ptypes = {
PrimitiveType.Plane,
PrimitiveType.Sphere,
PrimitiveType.Cylinder,
PrimitiveType.Cube
};
GameObject[] primitives = new GameObject[4];
for( int i=0; i<4; i++)
primitives[i] = GameObject.CreatePrimitive(ptypes[i]);
// allocate array
objects = new GameObject[nobject];
// process objects
for( int i=0; i<nobject; i++ )
{
// get object name
StringBuilder name = new StringBuilder(100);
MJP.GetObjectName(i, name, 100);
// create new GameObject, place under root
objects[i] = new GameObject(name.ToString());
MeshFilter filt = objects[i].AddComponent<MeshFilter>();
MeshRenderer rend = objects[i].AddComponent<MeshRenderer>();
InstancedColor colors = objects[i].AddComponent<InstancedColor>();
objects[i].transform.parent = root.transform;
// get MuJoCo object descriptor
MJP.TObject obj;
MJP.GetObject(i, &obj);
// For Furniture Assembly Environment: do not visualize site
if (obj.category == (int)MJP.TCategory.SITE && !objects[i].name.Contains("conn")) {
objects[i].layer = 9;
// Debug.Log("siting " + objects[i].name);
}
if (objects[i].name.StartsWith("noviz", StringComparison.Ordinal)) {
objects[i].layer = 10;
// Debug.Log("novizing " + objects[i].name);
}
if (objects[i].name.EndsWith("collision", StringComparison.Ordinal)) {
objects[i].layer = 10;
// Debug.Log("novizing " + objects[i].name);
}
if (objects[i].name.StartsWith("FLOOR", StringComparison.Ordinal)) {
objects[i].layer = 10;
// Debug.Log("novizing " + objects[i].name);
}
Debug.Log(objects[i].name + ", " + objects[i].layer);
// set mesh
switch ( (MJP.TGeom)obj.geomtype )
{
case MJP.TGeom.PLANE:
filt.sharedMesh = primitives[0].GetComponent<MeshFilter>().sharedMesh;
break;
case MJP.TGeom.SPHERE:
filt.sharedMesh = primitives[1].GetComponent<MeshFilter>().sharedMesh;
break;
case MJP.TGeom.CYLINDER:
filt.sharedMesh = primitives[2].GetComponent<MeshFilter>().sharedMesh;
break;
case MJP.TGeom.BOX:
filt.sharedMesh = primitives[3].GetComponent<MeshFilter>().sharedMesh;
break;
case MJP.TGeom.HFIELD:
int nrow = obj.hfield_nrow;
int ncol = obj.hfield_ncol;
int r, c;
// allocate
Vector3[] hfvertices = new Vector3[nrow*ncol + 4*nrow+4*ncol];
Vector2[] hfuv = new Vector2[nrow*ncol + 4*nrow+4*ncol];
int[] hffaces0 = new int[3*2*(nrow-1)*(ncol-1)];
int[] hffaces1 = new int[3*(4*(nrow-1)+4*(ncol-1))];
// vertices and uv: surface
for( r=0; r<nrow; r++ )
for( c=0; c<ncol; c++ )
{
int k = r*ncol+c;
float wc = c / (float)(ncol-1);
float wr = r / (float)(nrow-1);
hfvertices[k].Set(-(wc-0.5f), obj.hfield_data[k], -(wr-0.5f));
hfuv[k].Set(wc, wr);
}
// vertices and uv: front and back
for( r=0; r<nrow; r+=(nrow-1) )
for( c=0; c<ncol; c++ )
{
int k = nrow*ncol + 2*((r>0?ncol:0)+c);
float wc = c / (float)(ncol-1);
float wr = r / (float)(nrow-1);
hfvertices[k].Set(-(wc-0.5f), -0.5f, -(wr-0.5f));
hfuv[k].Set(wc, 0);
hfvertices[k+1].Set(-(wc-0.5f), obj.hfield_data[r*ncol+c], -(wr-0.5f));
hfuv[k+1].Set(wc, 1);
}
// vertices and uv: left and right
for( c=0; c<ncol; c+=(ncol-1) )
for( r=0; r<nrow; r++ )
{
int k = nrow*ncol + 4*ncol + 2*((c>0?nrow:0)+r);
float wc = c / (float)(ncol-1);
float wr = r / (float)(nrow-1);
hfvertices[k].Set(-(wc-0.5f), -0.5f, -(wr-0.5f));
hfuv[k].Set(wr, 0);
hfvertices[k+1].Set(-(wc-0.5f), obj.hfield_data[r*ncol+c], -(wr-0.5f));
hfuv[k+1].Set(wr, 1);
}
// faces: surface
for( r=0; r<nrow-1; r++ )
for( c=0; c<ncol-1; c++ )
{
int f = r*(ncol-1)+c;
int k = r*ncol+c;
// first face in rectangle
hffaces0[3*2*f] = k;
hffaces0[3*2*f+2] = k+1;
hffaces0[3*2*f+1] = k+ncol+1;
// second face in rectangle
hffaces0[3*2*f+3] = k;
hffaces0[3*2*f+5] = k+ncol+1;
hffaces0[3*2*f+4] = k+ncol;
}
// faces: front and back
for( r=0; r<2; r++ )
for( c=0; c<ncol-1; c++ )
{
int f = ((r>0?(ncol-1):0)+c);
int k = nrow*ncol + 2*((r>0?ncol:0)+c);
// first face in rectangle
hffaces1[3*2*f] = k;
hffaces1[3*2*f+2] = k + (r>0 ? 1 : 3);
hffaces1[3*2*f+1] = k + (r>0 ? 3 : 1);
// second face in rectangle
hffaces1[3*2*f+3] = k;
hffaces1[3*2*f+5] = k + (r>0 ? 3 : 2);
hffaces1[3*2*f+4] = k + (r>0 ? 2 : 3);
}
// faces: left and right
for( c=0; c<2; c++ )
for( r=0; r<nrow-1; r++ )
{
int f = 2*(ncol-1) + ((c>0?(nrow-1):0)+r);
int k = nrow*ncol + 4*ncol + 2*((c>0?nrow:0)+r);
// first face in rectangle
hffaces1[3*2*f] = k;
hffaces1[3*2*f+2] = k + (c>0 ? 3 : 1);
hffaces1[3*2*f+1] = k + (c>0 ? 1 : 3);
// second face in rectangle
hffaces1[3*2*f+3] = k;
hffaces1[3*2*f+5] = k + (c>0 ? 2 : 3);
hffaces1[3*2*f+4] = k + (c>0 ? 3 : 2);
}
Debug.Log(ncol);
Debug.Log(nrow);
Debug.Log(Mathf.Min(hffaces1));
Debug.Log(Mathf.Max(hffaces1));
// create mesh with automatic normals and tangents
filt.sharedMesh = new Mesh();
filt.sharedMesh.vertices = hfvertices;
filt.sharedMesh.uv = hfuv;
filt.sharedMesh.subMeshCount = 2;
filt.sharedMesh.SetTriangles(hffaces0, 0);
filt.sharedMesh.SetTriangles(hffaces1, 1);
filt.sharedMesh.RecalculateNormals();
filt.sharedMesh.RecalculateTangents();
// set name
StringBuilder hname = new StringBuilder(100);
MJP.GetElementName(MJP.TElement.HFIELD, obj.dataid, hname, 100);
filt.sharedMesh.name = hname.ToString();
break;
case MJP.TGeom.CAPSULE:
case MJP.TGeom.MESH:
// reuse shared mesh from earlier object
if( obj.mesh_shared>=0 )
filt.sharedMesh = objects[obj.mesh_shared].GetComponent<MeshFilter>().sharedMesh;
// create new mesh
else
{
string meshName;
// set name
if ((MJP.TGeom)obj.geomtype == MJP.TGeom.CAPSULE)
meshName = "Capsule mesh";
else
{
StringBuilder mname = new StringBuilder(100);
MJP.GetElementName(MJP.TElement.MESH, obj.dataid, mname, 100);
meshName = mname.ToString();
}
{
// copy vertices, normals, uv
Vector3[] vertices = new Vector3[obj.mesh_nvertex];
Vector3[] normals = new Vector3[obj.mesh_nvertex];
Vector2[] uv = new Vector2[obj.mesh_nvertex];
for (int k = 0; k < obj.mesh_nvertex; k++)
{
vertices[k].Set(-obj.mesh_position[3 * k],
obj.mesh_position[3 * k + 2],
-obj.mesh_position[3 * k + 1]);
normals[k].Set(-obj.mesh_normal[3 * k],
obj.mesh_normal[3 * k + 2],
-obj.mesh_normal[3 * k + 1]);
uv[k].Set(obj.mesh_texcoord[2 * k],
obj.mesh_texcoord[2 * k + 1]);
}
// copy faces
int[] faces = new int[3 * obj.mesh_nface];
for (int k = 0; k < obj.mesh_nface; k++)
{
faces[3 * k] = obj.mesh_face[3 * k];
faces[3 * k + 1] = obj.mesh_face[3 * k + 2];
faces[3 * k + 2] = obj.mesh_face[3 * k + 1];
}
// number of verices can be modified by uncompressed mesh
int nvert = obj.mesh_nvertex;
// replace with uncompressed mesh when UV needs to be recomputed
// ( recomputeUV && (MJP.TGeom)obj.geomtype==MJP.TGeom.MESH )
{
// make temporary mesh
Mesh temp = new Mesh();
temp.vertices = vertices;
temp.normals = normals;
temp.triangles = faces;
// generate uncompressed UV unwrapping
/* Vector2[] UV = Unwrapping.GeneratePerTriangleUV(temp);
int N = UV.GetLength(0)/3;
if( N!=obj.mesh_nface )
throw new System.Exception("Unexpected number of faces");
nvert = 3*N;*/
int N = obj.mesh_nface;
nvert = 3 * N;
// create corresponding uncompressed vertices, normals, faces
Vector3[] Vertex = new Vector3[3*N];
Vector3[] Normal = new Vector3[3*N];
int[] Face = new int[3*N];
for( int k=0; k<N; k++ )
{
Vertex[3*k] = vertices[faces[3*k]];
Vertex[3*k+1] = vertices[faces[3*k+1]];
Vertex[3*k+2] = vertices[faces[3*k+2]];
Normal[3*k] = normals[faces[3*k]];
Normal[3*k+1] = normals[faces[3*k+1]];
Normal[3*k+2] = normals[faces[3*k+2]];
Face[3*k] = 3*k;
Face[3*k+1] = 3*k+1;
Face[3*k+2] = 3*k+2;
}
// create uncompressed mesh
filt.sharedMesh = new Mesh();
filt.sharedMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
filt.sharedMesh.vertices = Vertex;
filt.sharedMesh.normals = Normal;
filt.sharedMesh.triangles = Face;
filt.sharedMesh.name = meshName;
// filt.sharedMesh.uv = UV;
}
// otherwise create mesh directly
/* else
{
filt.sharedMesh = new Mesh();
filt.sharedMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
filt.sharedMesh.vertices = vertices;
filt.sharedMesh.normals = normals;
filt.sharedMesh.triangles = faces;
filt.sharedMesh.uv = uv;
filt.sharedMesh.name = meshName;
filt.sharedMesh.RecalculateNormals(30);
}*/
// optionally recompute normals for meshes
/* if (recomputeNormal && (MJP.TGeom)obj.geomtype == MJP.TGeom.MESH)
filt.sharedMesh.RecalculateNormals(60);*/
filt.sharedMesh.RecalculateNormals(25);
// always calculate tangents (MuJoCo does not support tangents)
filt.sharedMesh.RecalculateTangents();
}
// print error if number of vertices or faces is over 65535
/* if( obj.mesh_nface>65535 || nvert>65535 )
Debug.LogError("MESH TOO BIG: " + filt.sharedMesh.name +
", vertices " + nvert + ", faces " + obj.mesh_nface);*/
}
break;
}
//TODO: Set segmentation color with Material.SetColor("_SegColor")
// existing material
byte segmentation = 0;
// Try to get segmentation id. If nothing is found, the object will remain background (0)
if( meshSegmentMap.TryGetValue(name.ToString(), out segmentation) == false)
{
meshSegmentMap.TryGetValue(filt.sharedMesh.name, out segmentation);
}
Material base_material = null;
if( materials.TryGetValue(obj.material, out base_material) )
{
rend.sharedMaterial = base_material;
colors.Diffuse = new Color(obj.color[0], obj.color[1], obj.color[2], obj.color[3]);
colors.Segmentation = new Color32(segmentation, segmentation, segmentation, segmentation);
}
else // Missing material (shouldn't be possible?)
{
Debug.Log("Couldn't find a Material for id:" + obj.material);
}
// get MuJoCo object transform and set in Unity
MJP.TTransform transform;
int visible;
int selected;
MJP.GetObjectState(i, &transform, &visible, &selected);
SetTransform(objects[i], transform);
}
// delete primitives
for( int i=0; i<4; i++ )
Destroy(primitives[i]);
}
public unsafe void Import(string _file)
{
modelFile = _file;
RunImport();
}
// run importer
private unsafe void RunImport()
{
Resources.UnloadUnusedAssets();
BuildAssetDatabase(modelFile);
// adjust global settings
Time.fixedDeltaTime = 0.005f;
//PlayerSettings.runInBackground = true;
if( enableRemote )
{
QualitySettings.vSyncCount = (noVSync ? 0 : 1);
}
else
QualitySettings.vSyncCount = 1;
// disable active cameras
//Camera[] activecam = FindObjectsOfType<Camera>();
//foreach( Camera ac in activecam )
// ac.gameObject.SetActive(false);
fileName = Path.GetFileName(modelFile);
// initialize plugin and load model
MJP.Close();
MJP.Initialize();
MJP.LoadModel(modelFile);
// get model sizes
MJP.TSize size;
MJP.GetSize(&size);
// import materials
//if( size.nmaterial>0 )
{
// MakeDirectory("Assets", "Materials");
ImportMaterials(size.nmaterial);
}
// create root, destroy old if present
root = GameObject.Find("MuJoCo");
if( root!=null )
{
Destroy(root);
}
root = new GameObject("MuJoCo");
if( root==null )
throw new System.Exception("Could not create root MuJoCo object");
root.transform.localPosition = transform.localPosition;
root.transform.localRotation = transform.localRotation;
root.transform.localScale = transform.localScale;
// add camera to root
AddCameras(size.ncamera);
// import renderable objects under root
ImportObjects(size.nobject);
// ImportLights(size.nlight);
// attach script to root
if( enableRemote )
{
// add remote
MJRemote extsim = root.GetComponent<MJRemote>();
if(extsim == null )
extsim = root.AddComponent<MJRemote>();
extsim.root = root;
extsim.modelFile = fileName + ".mjb";
MJTCPInterface tcpif = this.gameObject.GetComponent<MJTCPInterface>();
if (tcpif == null)
tcpif = this.gameObject.AddComponent<MJTCPInterface>();
tcpif.root = root;
tcpif.tcpAddress = tcpAddress;
tcpif.tcpPort = tcpPort;
// destroy simulate if present
if( root.GetComponent<MJInternalSimulation>() )
Destroy(root.GetComponent<MJInternalSimulation>());
}
else
{
// add simulate
MJInternalSimulation sim = root.GetComponent<MJInternalSimulation>();
if( sim==null )
sim = root.AddComponent<MJInternalSimulation>();
sim.root = root;
sim.modelFile = fileName + ".mjb";
// destroy remote if present
if( root.GetComponent<MJRemote>() )
Destroy(root.GetComponent<MJRemote>());
// destroy remote if present
if (this.GetComponent<MJTCPInterface>())
Destroy(root.GetComponent<MJTCPInterface>());
}
// close plugin
// MJP.Close();
}
private unsafe void ImportLights(int nlight)
{
// allocate array
objects = new GameObject[nlight];
// process objects
for (int i = 0; i < nlight; i++)
{
// get object name
StringBuilder name = new StringBuilder(100);
MJP.GetElementName(MJP.TElement.LIGHT, i, name, 100);
if (name.Length == 0) { name.Append("Light "); name.Append(i); }
// create new GameObject, place under root
objects[i] = new GameObject(name.ToString());
Light lightComp = objects[i].AddComponent<Light>();
objects[i].transform.parent = root.transform;
// get MuJoCo object descriptor
MJP.TLight obj;
MJP.GetLight(i, &obj);
//GetLightState
// set mesh
lightComp.type = obj.directional != 0 ? LightType.Directional : LightType.Point;
lightComp.shadows = obj.castshadow == 0 ? LightShadows.None : LightShadows.Soft;
lightComp.color = new Color(obj.diffuse[0], obj.diffuse[1], obj.diffuse[2]);
lightComp.range = (float)Math.Sqrt(255 / obj.attenuation[2]);
float[] pos = { 0, 0, 0 };
float[] dir = { 0, 0, 0 };
fixed (float* ppos = pos)
fixed (float* pdir = dir)
MJP.GetLightState(i, 0, ppos, pdir);
objects[i].transform.localPosition = new Vector3(-pos[0], pos[2], -pos[1]);
objects[i].transform.rotation = Quaternion.LookRotation(new Vector3(-dir[0], dir[2], -dir[1]), Vector3.up);
}
}
void OnApplicationQuit()
{
MJP.Close();
}
}