forked from tparisi/Unity-glTF-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSceneToGlTFWiz.cs
1159 lines (1025 loc) · 34.7 KB
/
SceneToGlTFWiz.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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
GlamExport
- Unity3D Scriptable Wizard to export Hierarchy or Project objects as glTF
****************************************************************************/
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text;
using System.Reflection;
public class SceneToGlTFWiz : EditorWindow
{
// static public List<GlTF_Accessor> Accessors;
// static public List<GlTF_BufferView> BufferViews;
static public GlTF_Writer writer;
const string KEY_PATH = "GlTFPath";
const string KEY_FILE = "GlTFFile";
static public string path = "?";
static XmlDocument xdoc;
static string savedPath;
static string savedFile;
static Preset preset = new Preset();
public static UnityEngine.TextAsset presetAsset;
public interface RTCCallback
{
double[] GetCenter(Transform transform);
}
public interface RotationCallback
{
Matrix4x4 GetBoundsRotationMatrix(Transform transform);
Matrix4x4 GetNodeRotationMatrix(Transform transform);
}
public static MonoScript rtcScript;
public static MonoScript rotScript;
public static bool unpackTexture = false;
public static bool copyShaders = false;
[MenuItem ("File/Export/glTF")]
static void CreateWizard()
{
savedPath = EditorPrefs.GetString (KEY_PATH, "/");
savedFile = EditorPrefs.GetString (KEY_FILE, "test.gltf");
path = savedPath + "/"+ savedFile;
// ScriptableWizard.DisplayWizard("Export Selected Stuff to glTF", typeof(SceneToGlTFWiz), "Export");
SceneToGlTFWiz window = (SceneToGlTFWiz)EditorWindow.GetWindow (typeof (SceneToGlTFWiz));
window.Show();
}
void OnEnable()
{
savedPath = EditorPrefs.GetString (KEY_PATH, "/");
savedFile = EditorPrefs.GetString (KEY_FILE, "test.gltf");
}
void OnWizardUpdate ()
{
// Texture[] txs = Selection.GetFiltered(Texture, SelectionMode.Assets);
// Debug.Log("found "+txs.Length);
}
void OnWizardCreate() // Create (Export) button has been hit (NOT wizard has been created!)
{
/*
Object[] deps = EditorUtility.CollectDependencies (trs);
foreach (Object o in deps)
{
Debug.Log("obj "+o.name+" "+o.GetType());
}
*/
var ext = GlTF_Writer.binary ? (GlTF_Writer.b3dm ? "b3dm" : "glb") : "gltf";
path = EditorUtility.SaveFilePanel("Save glTF file as", savedPath, savedFile, ext);
if (path.Length != 0)
{
Transform[] trs = Selection.GetTransforms (SelectionMode.Deep);
Transform root = null;
// find root, the one with no parent
for (var i = 0; i < trs.Length; ++i)
{
if (trs[i].parent == null)
{
root = trs[i];
break;
}
}
Export(path, trs, root);
}
}
void OnGUI ()
{
GUILayout.Label("Export Options");
GlTF_Writer.binary = GUILayout.Toggle(GlTF_Writer.binary, "Binary GlTF");
if (GlTF_Writer.binary)
{
GlTF_Writer.b3dm = GUILayout.Toggle(GlTF_Writer.b3dm, "B3dm");
}
else
{
GlTF_Writer.b3dm = false;
}
copyShaders = GUILayout.Toggle(copyShaders, "Copy shaders");
presetAsset = EditorGUILayout.ObjectField("Preset file", presetAsset, typeof(UnityEngine.TextAsset), false) as UnityEngine.TextAsset;
rtcScript = EditorGUILayout.ObjectField("Cesium RTC Callback", rtcScript, typeof(MonoScript), false) as MonoScript;
rotScript = EditorGUILayout.ObjectField("Rotation Callback", rotScript, typeof(MonoScript), false) as MonoScript;
if (rtcScript != null)
{
var name = typeof(RTCCallback).FullName;
var ci = rtcScript.GetClass().GetInterface(name);
if (ci == null)
{
rtcScript = null;
}
}
if (rotScript != null)
{
var name = typeof(RotationCallback).FullName;
var ci = rotScript.GetClass().GetInterface(name);
if (ci == null)
{
rotScript = null;
}
}
if (GUILayout.Button("Export"))
{
OnWizardCreate();
}
}
public static BoundsDouble Export(string path, Transform[] trs, Transform root)
{
double minHeight = 0, maxHeight = 0;
return Export(path, trs, root, out minHeight, out maxHeight);
}
public static BoundsDouble Export(string path, Transform[] trs, Transform root, out double minHeight, out double maxHeight)
{
minHeight = 0;
maxHeight = 0;
writer = new GlTF_Writer();
writer.Init ();
if (presetAsset != null)
{
string psPath = AssetDatabase.GetAssetPath(presetAsset);
if (psPath != null)
{
psPath = psPath.Remove(0, "Assets".Length);
psPath = Application.dataPath + psPath;
preset.Load(psPath);
}
}
savedPath = Path.GetDirectoryName(path);
savedFile = Path.GetFileNameWithoutExtension(path);
EditorPrefs.SetString(KEY_PATH, savedPath);
EditorPrefs.SetString(KEY_FILE, savedFile);
Debug.Log ("attempting to save to "+path);
writer.OpenFiles (path);
if (rtcScript != null && root != null)
{
var instance = Activator.CreateInstance(rtcScript.GetClass());
var rtc = instance as RTCCallback;
if (rtc != null)
{
writer.RTCCenter = rtc.GetCenter(root);
}
}
RotationCallback rotCallback = null;;
if (rotScript != null)
{
var instance = Activator.CreateInstance(rotScript.GetClass());
rotCallback = instance as RotationCallback;
}
if (unpackTexture) {
// prepass, for texture unpacker
TextureUnpacker.Reset();
foreach (Transform tr in trs)
{
TextureUnpacker.CheckPackedTexture(tr, preset);
}
TextureUnpacker.Build();
}
BoundsDouble bb = new BoundsDouble();
List<GlTF_Skin> skins = new List<GlTF_Skin>();
// first, collect objects in the scene, add to lists
foreach (Transform tr in trs)
{
if (tr.GetComponent<Camera>() != null)
{
if (tr.GetComponent<Camera>().orthographic)
{
GlTF_Orthographic cam;
cam = new GlTF_Orthographic();
cam.type = "orthographic";
cam.zfar = tr.GetComponent<Camera>().farClipPlane;
cam.znear = tr.GetComponent<Camera>().nearClipPlane;
cam.name = tr.name;
//cam.orthographic.xmag = tr.camera.
GlTF_Writer.cameras.Add(cam);
}
else
{
GlTF_Perspective cam;
cam = new GlTF_Perspective();
cam.type = "perspective";
cam.zfar = tr.GetComponent<Camera>().farClipPlane;
cam.znear = tr.GetComponent<Camera>().nearClipPlane;
cam.aspect_ratio = tr.GetComponent<Camera>().aspect;
cam.yfov = tr.GetComponent<Camera>().fieldOfView;
cam.name = tr.name;
GlTF_Writer.cameras.Add(cam);
}
}
if (tr.GetComponent<Light>() != null)
{
switch (tr.GetComponent<Light>().type)
{
case LightType.Point:
GlTF_PointLight pl = new GlTF_PointLight();
pl.color = new GlTF_ColorRGB (tr.GetComponent<Light>().color);
pl.name = tr.name;
GlTF_Writer.lights.Add (pl);
break;
case LightType.Spot:
GlTF_SpotLight sl = new GlTF_SpotLight();
sl.color = new GlTF_ColorRGB (tr.GetComponent<Light>().color);
sl.name = tr.name;
GlTF_Writer.lights.Add (sl);
break;
case LightType.Directional:
GlTF_DirectionalLight dl = new GlTF_DirectionalLight();
dl.color = new GlTF_ColorRGB (tr.GetComponent<Light>().color);
dl.name = tr.name;
GlTF_Writer.lights.Add (dl);
break;
case LightType.Area:
GlTF_AmbientLight al = new GlTF_AmbientLight();
al.color = new GlTF_ColorRGB (tr.GetComponent<Light>().color);
al.name = tr.name;
GlTF_Writer.lights.Add (al);
break;
}
}
Mesh m = GetMesh(tr);
if (m != null)
{
GlTF_Mesh mesh = new GlTF_Mesh();
mesh.name = GlTF_Mesh.GetNameFromObject(m);
GlTF_Accessor positionAccessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "position"), GlTF_Accessor.Type.VEC3, GlTF_Accessor.ComponentType.FLOAT);
positionAccessor.bufferView = GlTF_Writer.vec3BufferView;
GlTF_Writer.accessors.Add (positionAccessor);
GlTF_Accessor normalAccessor = null;
if (m.normals.Length > 0)
{
normalAccessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "normal"), GlTF_Accessor.Type.VEC3, GlTF_Accessor.ComponentType.FLOAT);
normalAccessor.bufferView = GlTF_Writer.vec3BufferView;
GlTF_Writer.accessors.Add (normalAccessor);
}
GlTF_Accessor uv0Accessor = null;
if (m.uv.Length > 0) {
uv0Accessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "uv0"), GlTF_Accessor.Type.VEC2, GlTF_Accessor.ComponentType.FLOAT);
uv0Accessor.bufferView = GlTF_Writer.vec2BufferView;
GlTF_Writer.accessors.Add (uv0Accessor);
}
GlTF_Accessor uv1Accessor = null;
if (m.uv2.Length > 0) {
uv1Accessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "uv1"), GlTF_Accessor.Type.VEC2, GlTF_Accessor.ComponentType.FLOAT);
uv1Accessor.bufferView = GlTF_Writer.vec2BufferView;
GlTF_Writer.accessors.Add (uv1Accessor);
}
GlTF_Accessor uv2Accessor = null;
if (m.uv3.Length > 0) {
uv2Accessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "uv2"), GlTF_Accessor.Type.VEC2, GlTF_Accessor.ComponentType.FLOAT);
uv2Accessor.bufferView = GlTF_Writer.vec2BufferView;
GlTF_Writer.accessors.Add (uv2Accessor);
}
GlTF_Accessor uv3Accessor = null;
if (m.uv4.Length > 0) {
uv3Accessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "uv3"), GlTF_Accessor.Type.VEC2, GlTF_Accessor.ComponentType.FLOAT);
uv3Accessor.bufferView = GlTF_Writer.vec2BufferView;
GlTF_Writer.accessors.Add (uv3Accessor);
}
GlTF_Accessor boneIndexAccessor = null;
GlTF_Accessor boneWeightAccessor = null;
if (m.boneWeights.Length > 0) {
boneIndexAccessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "joint"), GlTF_Accessor.Type.VEC4, GlTF_Accessor.ComponentType.FLOAT);
boneIndexAccessor.bufferView = GlTF_Writer.vec4BufferView;
GlTF_Writer.accessors.Add (boneIndexAccessor);
boneWeightAccessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "weight"), GlTF_Accessor.Type.VEC4, GlTF_Accessor.ComponentType.FLOAT);
boneWeightAccessor.bufferView = GlTF_Writer.vec4BufferView;
GlTF_Writer.accessors.Add (boneWeightAccessor);
}
var smCount = m.subMeshCount;
for (var i = 0; i < smCount; ++i)
{
GlTF_Primitive primitive = new GlTF_Primitive();
primitive.name = GlTF_Primitive.GetNameFromObject(m, i);
primitive.index = i;
GlTF_Attributes attributes = new GlTF_Attributes();
attributes.positionAccessor = positionAccessor;
attributes.normalAccessor = normalAccessor;
attributes.texCoord0Accessor = uv0Accessor;
attributes.texCoord1Accessor = uv1Accessor;
attributes.texCoord2Accessor = uv2Accessor;
attributes.texCoord3Accessor = uv3Accessor;
attributes.boneIndexAccessor = boneIndexAccessor;
attributes.boneWeightAccessor = boneWeightAccessor;
primitive.attributes = attributes;
GlTF_Accessor indexAccessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "indices_" + i), GlTF_Accessor.Type.SCALAR, GlTF_Accessor.ComponentType.USHORT);
indexAccessor.bufferView = GlTF_Writer.ushortBufferView;
GlTF_Writer.accessors.Add (indexAccessor);
primitive.indices = indexAccessor;
var mr = GetRenderer(tr);
var sm = mr.sharedMaterials;
if (i < sm.Length && sm[i] != null) {
var mat = sm[i];
var matName = GlTF_Material.GetNameFromObject(mat);
primitive.materialName = matName;
if (!GlTF_Writer.materials.ContainsKey (matName))
{
GlTF_Material material = new GlTF_Material();
material.name = matName;
GlTF_Writer.materials.Add (material.name, material);
//technique
var s = mat.shader;
var techName = GlTF_Technique.GetNameFromObject(s);
material.instanceTechniqueName = techName;
bool anim = boneIndexAccessor != null && boneWeightAccessor != null && m.bindposes.Length > 0;
if (!GlTF_Writer.techniques.ContainsKey(techName))
{
GlTF_Technique tech = new GlTF_Technique();
tech.name = techName;
GlTF_Technique.States ts = null;
if (preset.techniqueStates.ContainsKey(s.name))
{
ts = preset.techniqueStates[s.name];
}
else if (preset.techniqueStates.ContainsKey("*"))
{
ts = preset.techniqueStates["*"];
}
tech.states = ts;
GlTF_Technique.Parameter tParam = new GlTF_Technique.Parameter();
tParam.name = "position";
tParam.type = GlTF_Technique.Type.FLOAT_VEC3;
tParam.semantic = GlTF_Technique.Semantic.POSITION;
tech.parameters.Add(tParam);
GlTF_Technique.Attribute tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_position";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
if (normalAccessor != null)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = "normal";
tParam.type = GlTF_Technique.Type.FLOAT_VEC3;
tParam.semantic = GlTF_Technique.Semantic.NORMAL;
tech.parameters.Add(tParam);
tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_normal";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
}
if (uv0Accessor != null)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = "texcoord0";
tParam.type = GlTF_Technique.Type.FLOAT_VEC2;
tParam.semantic = GlTF_Technique.Semantic.TEXCOORD_0;
tech.parameters.Add(tParam);
tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_texcoord0";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
}
if (uv1Accessor != null)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = "texcoord1";
tParam.type = GlTF_Technique.Type.FLOAT_VEC2;
tParam.semantic = GlTF_Technique.Semantic.TEXCOORD_1;
tech.parameters.Add(tParam);
tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_texcoord1";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
}
if (uv2Accessor != null)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = "texcoord2";
tParam.type = GlTF_Technique.Type.FLOAT_VEC2;
tParam.semantic = GlTF_Technique.Semantic.TEXCOORD_2;
tech.parameters.Add(tParam);
tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_texcoord2";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
}
if (uv3Accessor != null)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = "texcoord3";
tParam.type = GlTF_Technique.Type.FLOAT_VEC2;
tParam.semantic = GlTF_Technique.Semantic.TEXCOORD_3;
tech.parameters.Add(tParam);
tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_texcoord3";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
}
if (boneIndexAccessor != null)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = "joint";
tParam.type = GlTF_Technique.Type.FLOAT_VEC4;
tParam.semantic = GlTF_Technique.Semantic.JOINT;
tech.parameters.Add(tParam);
tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_joint";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
}
if (boneWeightAccessor != null)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = "weight";
tParam.type = GlTF_Technique.Type.FLOAT_VEC4;
tParam.semantic = GlTF_Technique.Semantic.WEIGHT;
tech.parameters.Add(tParam);
tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_weight";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
}
tech.AddDefaultUniforms(writer.RTCCenter != null);
GlTF_Writer.techniques.Add (techName, tech);
GlTF_Technique.Uniform tUni;
if (m.bindposes.Length > 0)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = "jointMat";
tParam.type = GlTF_Technique.Type.FLOAT_MAT4;
tParam.semantic = GlTF_Technique.Semantic.JOINTMATRIX;
tParam.count = m.bindposes.Length;
tech.parameters.Add(tParam);
tUni = new GlTF_Technique.Uniform();
tUni.name = "u_jointMat";
tUni.param = tParam.name;
tech.uniforms.Add(tUni);
}
int spCount = ShaderUtil.GetPropertyCount(s);
for (var j = 0; j < spCount; ++j)
{
var pName = ShaderUtil.GetPropertyName(s, j);
var pType = ShaderUtil.GetPropertyType(s, j);
// Debug.Log(pName + " " + pType);
if (pType == ShaderUtil.ShaderPropertyType.Color)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = pName;
tParam.type = GlTF_Technique.Type.FLOAT_VEC4;
tech.parameters.Add(tParam);
tUni = new GlTF_Technique.Uniform();
tUni.name = pName;
tUni.param = tParam.name;
tech.uniforms.Add(tUni);
}
else if (pType == ShaderUtil.ShaderPropertyType.Vector)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = pName;
tParam.type = GlTF_Technique.Type.FLOAT_VEC4;
tech.parameters.Add(tParam);
tUni = new GlTF_Technique.Uniform();
tUni.name = pName;
tUni.param = tParam.name;
tech.uniforms.Add(tUni);
}
else if (pType == ShaderUtil.ShaderPropertyType.Float ||
pType == ShaderUtil.ShaderPropertyType.Range)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = pName;
tParam.type = GlTF_Technique.Type.FLOAT;
tech.parameters.Add(tParam);
tUni = new GlTF_Technique.Uniform();
tUni.name = pName;
tUni.param = tParam.name;
tech.uniforms.Add(tUni);
}
else if (pType == ShaderUtil.ShaderPropertyType.TexEnv)
{
var td = ShaderUtil.GetTexDim(s, j);
if (td == UnityEngine.Rendering.TextureDimension.Tex2D)
{
tParam = new GlTF_Technique.Parameter();
tParam.name = pName;
tParam.type = GlTF_Technique.Type.SAMPLER_2D;
tech.parameters.Add(tParam);
tUni = new GlTF_Technique.Uniform();
tUni.name = pName;
tUni.param = tParam.name;
tech.uniforms.Add(tUni);
}
}
}
// create program
GlTF_Program program = new GlTF_Program();
program.name = GlTF_Program.GetNameFromObject(s);
tech.program = program.name;
foreach (var attr in tech.attributes)
{
program.attributes.Add(attr.name);
}
GlTF_Writer.programs.Add(program);
// shader
GlTF_Shader vs = new GlTF_Shader();
vs.name = GlTF_Shader.GetNameFromObject(s, GlTF_Shader.Type.Vertex);
program.vertexShader = vs.name;
vs.type = GlTF_Shader.Type.Vertex;
vs.uri = preset.GetVertexShader(s.name, anim);
GlTF_Writer.shaders.Add(vs);
GlTF_Shader fs = new GlTF_Shader();
fs.name = GlTF_Shader.GetNameFromObject(s, GlTF_Shader.Type.Fragment);
program.fragmentShader = fs.name;
fs.type = GlTF_Shader.Type.Fragment;
fs.uri = preset.GetFragmentShader(s.name);
GlTF_Writer.shaders.Add(fs);
}
else
{
if (anim)
{
// check if previous technique creation contains anim
var tech = GlTF_Writer.techniques[techName];
bool jointFound = false;
bool weightFound = false;
bool jointMatFound = false;
foreach (var param in tech.parameters)
{
if (param.name == "joint")
{
jointFound = true;
}
else if (param.name == "weight")
{
weightFound = true;
}
else if (param.name == "jointMat")
{
jointMatFound = true;
}
}
var prog = GlTF_Writer.programs.Find(p => p.name == tech.program);
Action<GlTF_Technique.Attribute> injectAttr = (tAttr) => {
if (prog != null && prog.attributes.Find(attr => attr == tAttr.name) == null)
{
prog.attributes.Add(tAttr.name);
}
};
if (!jointFound)
{
var tParam = new GlTF_Technique.Parameter();
tParam.name = "joint";
tParam.type = GlTF_Technique.Type.FLOAT_VEC4;
tParam.semantic = GlTF_Technique.Semantic.JOINT;
tech.parameters.Add(tParam);
var tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_joint";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
injectAttr(tAttr);
}
if (!weightFound)
{
var tParam = new GlTF_Technique.Parameter();
tParam.name = "weight";
tParam.type = GlTF_Technique.Type.FLOAT_VEC4;
tParam.semantic = GlTF_Technique.Semantic.WEIGHT;
tech.parameters.Add(tParam);
var tAttr = new GlTF_Technique.Attribute();
tAttr.name = "a_weight";
tAttr.param = tParam.name;
tech.attributes.Add(tAttr);
injectAttr(tAttr);
}
if (!jointMatFound)
{
var tParam = new GlTF_Technique.Parameter();
tParam.name = "jointMat";
tParam.type = GlTF_Technique.Type.FLOAT_MAT4;
tParam.semantic = GlTF_Technique.Semantic.JOINTMATRIX;
tParam.count = m.bindposes.Length;
tech.parameters.Add(tParam);
var tUni = new GlTF_Technique.Uniform();
tUni.name = "u_jointMat";
tUni.param = tParam.name;
tech.uniforms.Add(tUni);
}
//check shader type
var vs = GlTF_Writer.shaders.Find(sh => sh.name == prog.vertexShader);
if (vs != null)
{
vs.uri = preset.GetVertexShader(s.name, anim);
}
}
}
int spCount2 = ShaderUtil.GetPropertyCount(s);
for (var j = 0; j < spCount2; ++j)
{
var pName = ShaderUtil.GetPropertyName(s, j);
var pType = ShaderUtil.GetPropertyType(s, j);
if (pType == ShaderUtil.ShaderPropertyType.Color)
{
var matCol = new GlTF_Material.ColorValue();
matCol.name = pName;
matCol.color = mat.GetColor(pName);
material.values.Add(matCol);
}
else if (pType == ShaderUtil.ShaderPropertyType.Vector)
{
var matVec = new GlTF_Material.VectorValue();
matVec.name = pName;
matVec.vector = mat.GetVector(pName);
material.values.Add(matVec);
}
else if (pType == ShaderUtil.ShaderPropertyType.Float ||
pType == ShaderUtil.ShaderPropertyType.Range)
{
var matFloat = new GlTF_Material.FloatValue();
matFloat.name = pName;
matFloat.value = mat.GetFloat(pName);
material.values.Add(matFloat);
}
else if (pType == ShaderUtil.ShaderPropertyType.TexEnv)
{
var td = ShaderUtil.GetTexDim(s, j);
if (td == UnityEngine.Rendering.TextureDimension.Tex2D)
{
var t = mat.GetTexture(pName);
var val = new GlTF_Material.StringValue();
val.name = pName;
string texName = null;
if (t != null) {
texName = GlTF_Texture.GetNameFromObject(t);
val.value = texName;
} else {
val.value = "";
}
material.values.Add(val);
if (t != null)
{
if (!GlTF_Writer.textures.ContainsKey (texName))
{
var texPath = ExportTexture(t, savedPath);
GlTF_Image img = new GlTF_Image();
img.name = GlTF_Image.GetNameFromObject(t);
img.uri = texPath;
GlTF_Writer.images.Add(img);
GlTF_Sampler sampler;
var samplerName = GlTF_Sampler.GetNameFromObject(t);
if (GlTF_Writer.samplers.ContainsKey(samplerName))
{
sampler = GlTF_Writer.samplers[samplerName];
}
else
{
sampler = new GlTF_Sampler(t);
sampler.name = samplerName;
GlTF_Writer.samplers[samplerName] = sampler;
}
GlTF_Texture texture = new GlTF_Texture ();
texture.name = texName;
texture.source = img.name;
texture.samplerName = samplerName;
GlTF_Writer.textures.Add (texName, texture);
}
}
}
}
}
}
}
mesh.primitives.Add (primitive);
}
mesh.Populate (m);
GlTF_Writer.meshes.Add (mesh);
if (unpackTexture) {
TextureUnpacker.ProcessMesh(mesh);
}
// calculate bounding box transform
if (root != null)
{
Matrix4x4 brot = Matrix4x4.identity;
if (rotCallback != null)
{
brot = rotCallback.GetBoundsRotationMatrix(root);
}
var pos = tr.position - root.position; // relative to parent
var objMat = Matrix4x4.TRS(pos, tr.rotation, tr.lossyScale);
//read vertices
var ms = positionAccessor.bufferView.memoryStream;
var offset = (int)positionAccessor.byteOffset;
var len = positionAccessor.count;
var buffer = new byte[len * 12];
var mspos = ms.Position;
ms.Position = offset;
ms.Read(buffer, 0, buffer.Length);
minHeight = double.MaxValue;
maxHeight = double.MinValue;
double[] c = writer.RTCCenter;
double[] minPos = new double[3];
minPos[0] = double.MaxValue;
minPos[1] = double.MaxValue;
minPos[2] = double.MaxValue;
double[] maxPos = new double[3];
maxPos[0] = double.MinValue;
maxPos[1] = double.MinValue;
maxPos[2] = double.MinValue;
for (int j = 0; j < len; ++j)
{
var x = System.BitConverter.ToSingle(buffer, j * 12);
var y = System.BitConverter.ToSingle(buffer, j * 12 + 4);
var z = System.BitConverter.ToSingle(buffer, j * 12 + 8);
// local rotation
var lx = objMat.m00 * x + objMat.m01 * y + objMat.m02 * z;
var ly = objMat.m10 * x + objMat.m11 * y + objMat.m12 * z;
var lz = objMat.m20 * x + objMat.m21 * y + objMat.m22 * z;
minHeight = Math.Min(minHeight, ly);
maxHeight = Math.Max(maxHeight, ly);
// to world
double wx = brot.m00 * lx + brot.m01 * ly + brot.m02 * lz;
double wy = brot.m10 * lx + brot.m11 * ly + brot.m12 * lz;
double wz = brot.m20 * lx + brot.m21 * ly + brot.m22 * lz;
// local translation to world
double tx = brot.m00 * pos.x + brot.m01 * pos.y + brot.m02 * pos.z;
double ty = brot.m10 * pos.x + brot.m11 * pos.y + brot.m12 * pos.z;
double tz = brot.m20 * pos.x + brot.m21 * pos.y + brot.m22 * pos.z;
wx += tx;
wy += ty;
wz += tz;
if (c != null)
{
wx += c[0];
wy += c[1];
wz += c[2];
}
minPos[0] = Math.Min(minPos[0], wx);
minPos[1] = Math.Min(minPos[1], wy);
minPos[2] = Math.Min(minPos[2], wz);
maxPos[0] = Math.Max(maxPos[0], wx);
maxPos[1] = Math.Max(maxPos[1], wy);
maxPos[2] = Math.Max(maxPos[2], wz);
}
ms.Position = mspos;
BoundsDouble tbb = new BoundsDouble();
tbb.Encapsulate(new BoundsDouble(minPos, maxPos));
bb.Encapsulate(tbb);
}
}
Animation a = tr.GetComponent<Animation>();
// Animator a = tr.GetComponent<Animator>();
if (a != null)
{
AnimationClip[] clips = AnimationUtility.GetAnimationClips(tr.gameObject);
int nClips = clips.Length;
// int nClips = a.GetClipCount();
for (int i = 0; i < nClips; i++)
{
GlTF_Animation anim = new GlTF_Animation();
anim.Populate (clips[i], tr);
GlTF_Writer.animations.Add (anim);
}
}
// next, build hierarchy of nodes
GlTF_Node node = new GlTF_Node();
var smr = tr.GetComponent<SkinnedMeshRenderer>();
if (smr != null)
{
List<Transform> skeletons = new List<Transform>();
var skin = new GlTF_Skin();
skin.Populate(smr, skeletons);
GlTF_Writer.skins.Add(skin);
node.skinName = skin.name;
skins.Add(skin);
foreach (var st in skeletons) {
node.skeletonNames.Add(GlTF_Node.GetNameFromObject(st));
}
}
Matrix4x4 rotMat = Matrix4x4.identity;
if (root != null && rotCallback != null)
{
rotMat = rotCallback.GetNodeRotationMatrix(root);
}
if (tr == root)
{
Matrix4x4 mat = Matrix4x4.identity;
mat.m22 = -1; // flip z axis
if (rotMat != Matrix4x4.identity)
{
mat = rotMat;
}
// do not use global position if rtc is defined
Vector3 pos = Vector3.zero;
// if (writer.RTCCenter == null)
// {
// pos = tr.localPosition;
// }
mat = mat * Matrix4x4.TRS(pos, tr.localRotation, tr.localScale);
node.matrix = new GlTF_Matrix(mat);
}
else
{
node.hasParent = true;
// do not set if has animation, will be modified by skin transforms
if (smr == null)
{
if (tr.localPosition != Vector3.zero)
node.translation = new GlTF_Translation (tr.localPosition);
if (tr.localScale != Vector3.one)
node.scale = new GlTF_Scale (tr.localScale);
if (tr.localRotation != Quaternion.identity)
node.rotation = new GlTF_Rotation (tr.localRotation);
}
}
node.name = GlTF_Node.GetNameFromObject(tr);
if (tr.GetComponent<Camera>() != null)
{
node.cameraName = tr.name;
}
else if (tr.GetComponent<Light>() != null)
node.lightName = tr.name;
else if (m != null)
{
node.meshNames.Add (GlTF_Mesh.GetNameFromObject(m));
}
foreach (Transform t in tr.transform) {
var found = false;
foreach (var check in trs) {
if (t == check) {
found = true;
break;
}
}
if (found)
{
node.childrenNames.Add (GlTF_Node.GetNameFromObject(t));
}
}
GlTF_Writer.nodes.Add (node);
}
// set joint name property on skinned node
if (skins.Count > 0) {
foreach (var skin in skins) {
foreach (var boneName in skin.boneNames) {
foreach (var node in GlTF_Writer.nodes) {
if (node.name == boneName) {
node.jointName = boneName;
}
}
}
}
}
if (copyShaders && preset.shaderDir != null) {
var sd = Path.Combine(Application.dataPath, preset.shaderDir);
foreach (var shader in GlTF_Writer.shaders) {
var srcPath = Path.Combine(sd, shader.uri);
if (File.Exists(srcPath)) {
var dstPath = Path.Combine(savedPath, shader.uri);
File.Copy(srcPath, dstPath, true);
}
}
}
// third, add meshes etc to byte stream, keeping track of buffer offsets
writer.Write ();
writer.CloseFiles();
return bb;
}
static string toGlTFname(string name)
{
// remove spaces and illegal chars, replace with underscores
string correctString = name.Replace(" ", "_");
// make sure it doesn't start with a number
return correctString;
}