29
29
30
30
namespace TiltBrush
31
31
{
32
+
32
33
public class Model
33
34
{
35
+
34
36
public struct Location
35
37
{
36
38
public enum Type
@@ -59,7 +61,6 @@ public static Location File(string relativePath)
59
61
path = relativePath . Substring ( 0 , lastIndex ) ;
60
62
fragment = relativePath . Substring ( lastIndex + 1 ) ;
61
63
}
62
-
63
64
return new Location
64
65
{
65
66
type = Type . LocalFile ,
@@ -87,15 +88,13 @@ public string AbsolutePath
87
88
{
88
89
return null ;
89
90
}
90
-
91
91
switch ( type )
92
92
{
93
93
case Type . LocalFile :
94
94
return Path . Combine ( App . ModelLibraryPath ( ) , path ) . Replace ( "\\ " , "/" ) ;
95
95
case Type . IcosaAssetId :
96
96
return path . Replace ( "\\ " , "/" ) ;
97
97
}
98
-
99
98
return null ;
100
99
}
101
100
}
@@ -104,11 +103,7 @@ public string RelativePath
104
103
{
105
104
get
106
105
{
107
- if ( type == Type . LocalFile )
108
- {
109
- return path ;
110
- }
111
-
106
+ if ( type == Type . LocalFile ) { return path ; }
112
107
throw new Exception ( "Invalid relative path request" ) ;
113
108
}
114
109
}
@@ -119,19 +114,12 @@ public string AssetId
119
114
{
120
115
get
121
116
{
122
- if ( type == Type . IcosaAssetId )
123
- {
124
- return id ;
125
- }
126
-
117
+ if ( type == Type . IcosaAssetId ) { return id ; }
127
118
throw new Exception ( "Invalid Icosa asset id request" ) ;
128
119
}
129
120
}
130
121
131
- public Type GetLocationType ( )
132
- {
133
- return type ;
134
- }
122
+ public Type GetLocationType ( ) { return type ; }
135
123
136
124
public override int GetHashCode ( )
137
125
{
@@ -149,7 +137,6 @@ public override string ToString()
149
137
{
150
138
str = $ "{ type } :{ path } ";
151
139
}
152
-
153
140
return str ;
154
141
}
155
142
@@ -159,7 +146,6 @@ public override bool Equals(object obj)
159
146
{
160
147
return false ;
161
148
}
162
-
163
149
return this == ( Location ) obj ;
164
150
}
165
151
@@ -212,10 +198,8 @@ public LoadError(string message, string detail = null)
212
198
this . message = message ;
213
199
this . detail = detail ;
214
200
}
215
-
216
201
public readonly string message ; // Human-readable short message
217
-
218
- public readonly string detail ; // Maybe non-human-readable details
202
+ public readonly string detail ; // Maybe non-human-readable details
219
203
// maybe? public bool transient; // true if we know for sure that this error is transient
220
204
}
221
205
@@ -225,7 +209,6 @@ public LoadError(string message, string detail = null)
225
209
226
210
/// m_LoadError != null implies m_Valid == false
227
211
private LoadError ? m_LoadError ;
228
-
229
212
public LoadError ? Error => m_LoadError ;
230
213
231
214
// How many widgets are using this model?
@@ -267,11 +250,7 @@ public IExportableMaterial GetExportableMaterial(Material material)
267
250
return m_ImportMaterialCollector . GetExportableMaterial ( material ) ;
268
251
}
269
252
270
-
271
- public Model ( Location location )
272
- {
273
- m_Location = location ;
274
- }
253
+ public Model ( Location location ) { m_Location = location ; }
275
254
276
255
public Location GetLocation ( )
277
256
{
@@ -309,7 +288,11 @@ abstract class ModelBuilder
309
288
/// It's unclear if the intent is that the user should continue calling TryEndAsyncLoad
310
289
/// until it returns true, or if they should stop calling TryEndAsyncLoad. etc. Probably
311
290
/// we should remove this.
312
- public bool IsValid { get ; protected set ; }
291
+ public bool IsValid
292
+ {
293
+ get ;
294
+ protected set ;
295
+ }
313
296
314
297
public ModelBuilder ( string localPath )
315
298
{
@@ -334,15 +317,13 @@ public void CancelAsyncLoad()
334
317
if ( m_root != null )
335
318
{
336
319
foreach ( var mesh in m_root . GetComponentsInChildren < MeshFilter > ( )
337
- . Select ( x => x . sharedMesh ) )
320
+ . Select ( x => x . sharedMesh ) )
338
321
{
339
322
UObject . Destroy ( mesh ) ;
340
323
}
341
-
342
324
UObject . Destroy ( m_root ) ;
343
325
m_root = null ;
344
326
}
345
-
346
327
m_stateReader . Close ( ) ;
347
328
}
348
329
@@ -352,7 +333,7 @@ public void CancelAsyncLoad()
352
333
/// ImportMaterialCollector - non-null upon successful completion.
353
334
/// Raises an exception on unsuccessful completion.
354
335
public bool TryEndAsyncLoad ( out GameObject root ,
355
- out ImportMaterialCollector importMaterialCollector )
336
+ out ImportMaterialCollector importMaterialCollector )
356
337
{
357
338
// Three things happen in this function.
358
339
// 1: It waits to try and get the result of reading the model on a background thread
@@ -364,10 +345,7 @@ public bool TryEndAsyncLoad(out GameObject root,
364
345
if ( m_meshEnumerator == null )
365
346
{
366
347
IDisposable state ;
367
- if ( ! m_stateReader . TryGetResult ( out state ) )
368
- {
369
- return false ;
370
- }
348
+ if ( ! m_stateReader . TryGetResult ( out state ) ) { return false ; }
371
349
372
350
IEnumerable < Null > enumerable ;
373
351
m_root = DoUnityThreadWork ( state , out enumerable , out m_ImportMaterialCollector ) ;
@@ -380,15 +358,13 @@ public bool TryEndAsyncLoad(out GameObject root,
380
358
{
381
359
return false ;
382
360
}
383
-
384
361
m_ImportMaterialCollector = new ImportMaterialCollector (
385
362
Path . GetDirectoryName ( m_localPath ) ,
386
363
uniqueSeed : m_localPath
387
364
) ;
388
365
m_meshEnumerator = enumerable . GetEnumerator ( ) ;
389
366
m_root . SetActive ( false ) ;
390
367
}
391
-
392
368
// Yield until the limiter unblocks.
393
369
// Multiple calls to TryGetResult above are harmless.
394
370
if ( sm_Limiter . IsBlocked ( ) )
@@ -410,7 +386,6 @@ public bool TryEndAsyncLoad(out GameObject root,
410
386
stopwatch . Stop ( ) ;
411
387
return true ;
412
388
}
413
-
414
389
if ( stopwatch . ElapsedTicks > numTicks )
415
390
{
416
391
stopwatch . Stop ( ) ;
@@ -461,14 +436,13 @@ protected override IDisposable DoBackgroundThreadWork()
461
436
{
462
437
return ImportGltf . BeginImport ( m_localPath , loader , options ) ;
463
438
}
464
-
465
439
return NewGltfImporter . BeginImport ( m_localPath ) ;
466
440
}
467
441
468
442
protected override GameObject DoUnityThreadWork ( IDisposable state__ ,
469
- out IEnumerable < Null > meshEnumerable ,
470
- out ImportMaterialCollector
471
- importMaterialCollector )
443
+ out IEnumerable < Null > meshEnumerable ,
444
+ out ImportMaterialCollector
445
+ importMaterialCollector )
472
446
{
473
447
GameObject rootObject = null ;
474
448
if ( m_fromIcosa )
@@ -492,7 +466,6 @@ out ImportMaterialCollector
492
466
importMaterialCollector = ( ImportMaterialCollector ) result . materialCollector ;
493
467
}
494
468
}
495
-
496
469
IsValid = rootObject != null ;
497
470
meshEnumerable = null ;
498
471
importMaterialCollector = null ;
@@ -511,11 +484,9 @@ out ImportMaterialCollector
511
484
// EndImport doesn't try to use the loadImages functionality of UriLoader anyway.
512
485
// It knows it's on the main thread, so chooses to use Unity's fast loading.
513
486
rootObject = state . root ;
514
- importMaterialCollector =
515
- new ImportMaterialCollector ( assetLocation , uniqueSeed : m_localPath ) ;
487
+ importMaterialCollector = new ImportMaterialCollector ( assetLocation , uniqueSeed : m_localPath ) ;
516
488
}
517
489
}
518
-
519
490
IsValid = rootObject != null ;
520
491
return rootObject ;
521
492
}
@@ -533,6 +504,7 @@ GameObject LoadUsd(List<string> warnings)
533
504
534
505
GameObject LoadPly ( List < string > warningsOut )
535
506
{
507
+
536
508
try
537
509
{
538
510
var reader = new PlyReader ( m_Location . AbsolutePath ) ;
@@ -549,6 +521,7 @@ GameObject LoadPly(List<string> warningsOut)
549
521
Debug . LogException ( ex ) ;
550
522
return null ;
551
523
}
524
+
552
525
}
553
526
554
527
GameObject LoadSvg ( List < string > warningsOut , out SVGParser . SceneInfo sceneInfo )
@@ -689,7 +662,6 @@ public bool TryLoadModel()
689
662
{
690
663
return false ;
691
664
}
692
-
693
665
isValid = m_builder . IsValid ;
694
666
}
695
667
catch ( ObjectDisposedException ex )
@@ -732,11 +704,12 @@ public async Task LoadModelAsync()
732
704
{
733
705
Task t = StartCreatePrefab ( null ) ;
734
706
await t ;
735
- }
736
707
708
+ }
737
709
public void LoadModel ( )
738
710
{
739
711
StartCreatePrefab ( null ) ;
712
+
740
713
}
741
714
742
715
/// Either synchronously load a GameObject hierarchy and convert it to a "prefab"
@@ -776,7 +749,7 @@ private async Task StartCreatePrefab(GameObject go)
776
749
EndCreatePrefab ( go , warnings ) ;
777
750
}
778
751
else if ( m_Location . GetLocationType ( ) == Location . Type . IcosaAssetId ||
779
- ext == ".gltf2" || ext == ".gltf" || ext == ".glb" )
752
+ ext == ".gltf2" || ext == ".gltf" || ext == ".glb" )
780
753
{
781
754
// If we pulled this from Icosa, it's going to be a gltf file.
782
755
Task t = LoadGltf ( warnings ) ;
@@ -806,6 +779,7 @@ private async Task StartCreatePrefab(GameObject go)
806
779
m_LoadError = new LoadError ( "Unknown format" , ext ) ;
807
780
}
808
781
}
782
+
809
783
}
810
784
811
785
public void CalcBoundsGltf ( GameObject go )
@@ -827,7 +801,6 @@ public void CalcBoundsGltf(GameObject go)
827
801
b . Encapsulate ( bounds ) ;
828
802
}
829
803
}
830
-
831
804
m_MeshBounds = b ;
832
805
if ( first )
833
806
{
@@ -857,16 +830,15 @@ private void CalcBoundsNonGltf(GameObject go)
857
830
{
858
831
b . Encapsulate ( bc . bounds ) ;
859
832
}
860
-
861
833
UnityEngine . Object . Destroy ( bc ) ;
862
834
}
863
-
864
835
m_MeshBounds = b ;
865
836
if ( first )
866
837
{
867
838
// There was no geometry
868
839
Debug . LogErrorFormat ( "No usable geometry in model. LoadModel({0})" , go . name ) ;
869
840
}
841
+
870
842
}
871
843
872
844
public void EndCreatePrefab ( GameObject go , List < string > warnings )
@@ -885,7 +857,6 @@ public void EndCreatePrefab(GameObject go, List<string> warnings)
885
857
{
886
858
UnityEngine . Object . Destroy ( m_ModelParent . gameObject ) ;
887
859
}
888
-
889
860
m_ModelParent = go . transform ;
890
861
891
862
#if DEVELOPMENT_BUILD || UNITY_EDITOR
@@ -928,27 +899,24 @@ void SetUniqueNameForNode(Transform node)
928
899
}
929
900
}
930
901
931
-
932
902
public void UnloadModel ( )
933
903
{
934
904
if ( m_builder != null )
935
905
{
936
906
m_builder . CancelAsyncLoad ( ) ;
937
907
m_builder = null ;
938
908
}
939
-
940
909
m_Valid = false ;
941
910
m_LoadError = null ;
942
911
if ( m_ModelParent != null )
943
912
{
944
913
// Procedurally created meshes need to be explicitly destroyed - you can't just destroy
945
914
// the MeshFilter that references them.
946
915
foreach ( var mesh in m_ModelParent . GetComponentsInChildren < MeshFilter > ( )
947
- . Select ( x => x . sharedMesh ) )
916
+ . Select ( x => x . sharedMesh ) )
948
917
{
949
918
UObject . Destroy ( mesh ) ;
950
919
}
951
-
952
920
UObject . Destroy ( m_ModelParent . gameObject ) ;
953
921
m_ModelParent = null ;
954
922
}
@@ -976,7 +944,6 @@ public IEnumerator LoadFullyCoroutine(string reason)
976
944
{
977
945
yield return null ;
978
946
}
979
-
980
947
break ;
981
948
default :
982
949
m_LoadError = new LoadError ( $ "Unknown load type { type } ") ;
@@ -1001,7 +968,7 @@ private void DisplayWarnings(List<string> warnings)
1001
968
public bool IsCached ( )
1002
969
{
1003
970
return m_Location . GetLocationType ( ) == Location . Type . IcosaAssetId &&
1004
- Directory . Exists ( m_Location . AbsolutePath ) ;
971
+ Directory . Exists ( m_Location . AbsolutePath ) ;
1005
972
}
1006
973
1007
974
public void RefreshCache ( )
@@ -1019,7 +986,6 @@ public MeshFilter[] GetMeshes()
1019
986
{
1020
987
throw new InvalidOperationException ( ) ;
1021
988
}
1022
-
1023
989
return m_ModelParent . GetComponent < ObjModelScript > ( ) . m_MeshChildren ;
1024
990
}
1025
991
@@ -1032,7 +998,6 @@ public string GetExportName()
1032
998
case Model . Location . Type . IcosaAssetId :
1033
999
return AssetId ;
1034
1000
}
1035
-
1036
1001
return "Unknown" ;
1037
1002
}
1038
1003
@@ -1061,4 +1026,4 @@ public void EnsureCollectorExists()
1061
1026
}
1062
1027
}
1063
1028
}
1064
- } // namespace TiltBrush;
1029
+ } // namespace TiltBrush;
0 commit comments