| | 1 | | #if UNITY_2017_1_OR_NEWER |
| | 2 | | using GLTF; |
| | 3 | | using GLTF.Schema; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.IO; |
| | 8 | | using System.Linq; |
| | 9 | | using UnityEditor; |
| | 10 | | using UnityEditor.AssetImporters; |
| | 11 | | using UnityEngine; |
| | 12 | | using UnityGLTF.Loader; |
| | 13 | | using UnityGLTF.Cache; |
| | 14 | | using Object = UnityEngine.Object; |
| | 15 | |
|
| | 16 | | // using System.Threading.Tasks; |
| | 17 | |
|
| | 18 | | namespace UnityGLTF |
| | 19 | | { |
| | 20 | | [ScriptedImporter(1, new[] { "glb", "gltf" })] |
| | 21 | | public class GLTFImporter : ScriptedImporter |
| | 22 | | { |
| 0 | 23 | | [SerializeField] private bool _removeEmptyRootObjects = true; |
| 0 | 24 | | [SerializeField] private float _scaleFactor = 1.0f; |
| 0 | 25 | | [SerializeField] private int _maximumLod = 300; |
| 0 | 26 | | [SerializeField] private bool _readWriteEnabled = true; |
| | 27 | | [SerializeField] private bool _generateColliders = false; |
| | 28 | | [SerializeField] private bool _swapUvs = false; |
| | 29 | | [SerializeField] private GLTFImporterNormals _importNormals = GLTFImporterNormals.Import; |
| 0 | 30 | | [SerializeField] private bool _importMaterials = true; |
| | 31 | | [SerializeField] private bool _useJpgTextures = false; |
| | 32 | |
|
| 0 | 33 | | public bool _importTextures = true; |
| | 34 | |
|
| | 35 | | static int delayCallsCount = 0; |
| | 36 | |
|
| 0 | 37 | | public static bool finishedImporting { get { return delayCallsCount == 0; } } |
| | 38 | |
|
| | 39 | | public List<Material> SimplifyMaterials(Renderer[] renderers) |
| | 40 | | { |
| 0 | 41 | | Dictionary<string, Material> matByCrc = new Dictionary<string, Material>(); |
| 0 | 42 | | List<Material> materials = new List<Material>(); |
| | 43 | |
|
| 0 | 44 | | foreach (var rend in renderers) |
| | 45 | | { |
| 0 | 46 | | var matList = new List<Material>(1); |
| | 47 | |
|
| 0 | 48 | | foreach (var mat in rend.sharedMaterials) |
| | 49 | | { |
| 0 | 50 | | if (rend.sharedMaterials.Length == 0) |
| | 51 | | break; |
| 0 | 52 | | if (mat == null) |
| | 53 | | continue; |
| | 54 | |
|
| 0 | 55 | | string crc = mat.ComputeCRC() + mat.name; |
| | 56 | |
|
| 0 | 57 | | if (!matByCrc.ContainsKey(crc)) |
| | 58 | | { |
| 0 | 59 | | matByCrc.Add(crc, mat); |
| 0 | 60 | | materials.Add(mat); |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | matList.Add(matByCrc[crc]); |
| | 64 | | } |
| | 65 | |
|
| 0 | 66 | | rend.sharedMaterials = matList.ToArray(); |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | return materials; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | public override void OnImportAsset(AssetImportContext ctx) |
| | 73 | | { |
| | 74 | | string sceneName = null; |
| 0 | 75 | | GameObject gltfScene = null; |
| 0 | 76 | | UnityEngine.Mesh[] meshes = null; |
| | 77 | | try |
| | 78 | | { |
| 0 | 79 | | char ps = Path.DirectorySeparatorChar; |
| | 80 | |
|
| 0 | 81 | | string assetPath = ctx.assetPath; |
| | 82 | |
|
| 0 | 83 | | assetPath = assetPath.Replace('/', ps); |
| 0 | 84 | | assetPath = assetPath.Replace('\\', ps); |
| | 85 | |
|
| 0 | 86 | | sceneName = Path.GetFileNameWithoutExtension(assetPath); |
| 0 | 87 | | gltfScene = CreateGLTFScene(assetPath); |
| | 88 | |
|
| | 89 | | // Remove empty roots |
| 0 | 90 | | if (_removeEmptyRootObjects) |
| | 91 | | { |
| 0 | 92 | | var t = gltfScene.transform; |
| 0 | 93 | | while ( |
| | 94 | | gltfScene.transform.childCount == 1 && |
| | 95 | | gltfScene.GetComponents<Component>().Length == 1) |
| | 96 | | { |
| 0 | 97 | | var parent = gltfScene; |
| 0 | 98 | | gltfScene = gltfScene.transform.GetChild(0).gameObject; |
| 0 | 99 | | t = gltfScene.transform; |
| 0 | 100 | | t.parent = null; // To keep transform information in the new parent |
| 0 | 101 | | Object.DestroyImmediate(parent); // Get rid of the parent |
| | 102 | | } |
| | 103 | | } |
| | 104 | |
|
| | 105 | | // Ensure there are no hide flags present (will cause problems when saving) |
| 0 | 106 | | gltfScene.hideFlags &= ~(HideFlags.HideAndDontSave); |
| 0 | 107 | | foreach (Transform child in gltfScene.transform) |
| | 108 | | { |
| 0 | 109 | | child.gameObject.hideFlags &= ~(HideFlags.HideAndDontSave); |
| | 110 | | } |
| | 111 | |
|
| | 112 | | // Zero position |
| 0 | 113 | | gltfScene.transform.position = Vector3.zero; |
| | 114 | |
|
| 0 | 115 | | Animation animation = gltfScene.GetComponentInChildren<Animation>(); |
| 0 | 116 | | HashSet<AnimationClip> animationClips = new HashSet<AnimationClip>(); |
| | 117 | |
|
| 0 | 118 | | if (animation != null) |
| | 119 | | { |
| 0 | 120 | | foreach (AnimationState animationState in animation) |
| | 121 | | { |
| 0 | 122 | | if (!animationClips.Contains(animationState.clip)) |
| | 123 | | { |
| 0 | 124 | | animationClips.Add(animationState.clip); |
| | 125 | | } |
| | 126 | | } |
| | 127 | | } |
| | 128 | |
|
| | 129 | | // Get meshes |
| 0 | 130 | | var meshNames = new List<string>(); |
| 0 | 131 | | var meshHash = new HashSet<UnityEngine.Mesh>(); |
| 0 | 132 | | var meshFilters = gltfScene.GetComponentsInChildren<MeshFilter>(); |
| 0 | 133 | | var vertexBuffer = new List<Vector3>(); |
| 0 | 134 | | meshes = meshFilters.Where(mf => mf.sharedMesh != null) |
| | 135 | | .Select(mf => |
| | 136 | | { |
| 0 | 137 | | var mesh = mf.sharedMesh; |
| | 138 | |
|
| 0 | 139 | | vertexBuffer.Clear(); |
| 0 | 140 | | mesh.GetVertices(vertexBuffer); |
| 0 | 141 | | for (var i = 0; i < vertexBuffer.Count; ++i) |
| | 142 | | { |
| 0 | 143 | | vertexBuffer[i] *= _scaleFactor; |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | mesh.SetVertices(vertexBuffer); |
| 0 | 147 | | if (_swapUvs) |
| | 148 | | { |
| 0 | 149 | | var uv = mesh.uv; |
| 0 | 150 | | var uv2 = mesh.uv2; |
| 0 | 151 | | mesh.uv = uv2; |
| 0 | 152 | | mesh.uv2 = uv2; |
| | 153 | | } |
| | 154 | |
|
| 0 | 155 | | if (_importNormals == GLTFImporterNormals.None) |
| | 156 | | { |
| 0 | 157 | | mesh.normals = new Vector3[0]; |
| | 158 | | } |
| | 159 | |
|
| 0 | 160 | | if (_importNormals == GLTFImporterNormals.Calculate) |
| | 161 | | { |
| 0 | 162 | | mesh.RecalculateNormals(); |
| | 163 | | } |
| | 164 | |
|
| 0 | 165 | | mesh.UploadMeshData(!_readWriteEnabled); |
| | 166 | |
|
| 0 | 167 | | if (_generateColliders) |
| | 168 | | { |
| 0 | 169 | | var collider = mf.gameObject.AddComponent<MeshCollider>(); |
| 0 | 170 | | collider.sharedMesh = mesh; |
| | 171 | | } |
| | 172 | |
|
| 0 | 173 | | if (meshHash.Add(mesh)) |
| | 174 | | { |
| 0 | 175 | | var meshName = string.IsNullOrEmpty(mesh.name) ? mf.gameObject.name : mesh.n |
| 0 | 176 | | mesh.name = ObjectNames.GetUniqueName(meshNames.ToArray(), meshName); |
| 0 | 177 | | meshNames.Add(mesh.name); |
| | 178 | | } |
| | 179 | |
|
| 0 | 180 | | return mesh; |
| | 181 | | }) |
| | 182 | | .ToArray(); |
| | 183 | |
|
| 0 | 184 | | var renderers = gltfScene.GetComponentsInChildren<Renderer>(); |
| | 185 | |
|
| 0 | 186 | | if (animationClips.Count > 0) |
| | 187 | | { |
| 0 | 188 | | var folderName = Path.GetDirectoryName(ctx.assetPath); |
| 0 | 189 | | var animationsRoot = string.Concat(folderName, "/", "Animations/"); |
| 0 | 190 | | Directory.CreateDirectory(animationsRoot); |
| 0 | 191 | | foreach (AnimationClip clip in animationClips) |
| | 192 | | { |
| 0 | 193 | | string fileName = clip.name; |
| 0 | 194 | | foreach (char c in System.IO.Path.GetInvalidFileNameChars()) |
| | 195 | | { |
| 0 | 196 | | fileName = fileName.Replace(c, '_'); |
| | 197 | | } |
| | 198 | |
|
| 0 | 199 | | AssetDatabase.CreateAsset(clip, animationsRoot + fileName + ".anim"); |
| 0 | 200 | | var importer = AssetImporter.GetAtPath(animationsRoot + fileName + ".anim"); |
| | 201 | | } |
| | 202 | | } |
| | 203 | |
|
| 0 | 204 | | if (_importMaterials) |
| | 205 | | { |
| 0 | 206 | | var materials = SimplifyMaterials(renderers); |
| | 207 | | // Get materials |
| 0 | 208 | | List<string> materialNames = new List<string>(); |
| | 209 | |
|
| 0 | 210 | | foreach (var mat in materials) |
| | 211 | | { |
| 0 | 212 | | var matName = string.IsNullOrEmpty(mat.name) ? mat.shader.name : mat.name; |
| 0 | 213 | | if (matName == mat.shader.name) |
| | 214 | | { |
| 0 | 215 | | matName = matName.Substring(Mathf.Min(matName.LastIndexOf("/") + 1, matName.Length - 1)); |
| | 216 | | } |
| | 217 | |
|
| | 218 | | // Ensure name is unique |
| 0 | 219 | | matName = ObjectNames.NicifyVariableName(matName); |
| 0 | 220 | | matName = ObjectNames.GetUniqueName(materialNames.ToArray(), matName); |
| | 221 | |
|
| 0 | 222 | | mat.name = matName; |
| 0 | 223 | | materialNames.Add(matName); |
| | 224 | | } |
| | 225 | |
|
| 0 | 226 | | List<Texture2D> textures = new List<Texture2D>(); |
| 0 | 227 | | var texMaterialMap = new Dictionary<Texture2D, List<TexMaterialMap>>(); |
| | 228 | |
|
| 0 | 229 | | if (_importTextures) |
| | 230 | | { |
| | 231 | | // Get textures |
| 0 | 232 | | var textureNames = new List<string>(); |
| 0 | 233 | | var textureHash = new HashSet<Texture2D>(); |
| | 234 | |
|
| 0 | 235 | | textures = materials.SelectMany(mat => |
| | 236 | | { |
| 0 | 237 | | var shader = mat.shader; |
| 0 | 238 | | if (!shader) |
| | 239 | | { |
| 0 | 240 | | return Enumerable.Empty<Texture2D>(); |
| | 241 | | } |
| | 242 | |
|
| 0 | 243 | | var matTextures = new List<Texture2D>(); |
| | 244 | |
|
| 0 | 245 | | for (var i = 0; i < ShaderUtil.GetPropertyCount(shader); ++i) |
| | 246 | | { |
| 0 | 247 | | if (ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderProper |
| | 248 | | { |
| 0 | 249 | | var propertyName = ShaderUtil.GetPropertyName(shader, i); |
| 0 | 250 | | var tex = mat.GetTexture(propertyName) as Texture2D; |
| | 251 | |
|
| 0 | 252 | | if (!tex) |
| | 253 | | continue; |
| | 254 | |
|
| 0 | 255 | | if (textureHash.Add(tex)) |
| | 256 | | { |
| 0 | 257 | | var texName = tex.name; |
| | 258 | |
|
| 0 | 259 | | if (string.IsNullOrEmpty(texName)) |
| | 260 | | { |
| 0 | 261 | | if (propertyName.StartsWith("_")) |
| | 262 | | { |
| 0 | 263 | | texName = propertyName.Substring(Mathf.Min(1, proper |
| | 264 | | } |
| | 265 | | } |
| | 266 | |
|
| | 267 | | // Ensure name is unique |
| 0 | 268 | | texName = ObjectNames.NicifyVariableName(texName); |
| 0 | 269 | | texName = ObjectNames.GetUniqueName(textureNames.ToArray(), |
| | 270 | |
|
| 0 | 271 | | tex.name = texName; |
| 0 | 272 | | textureNames.Add(texName); |
| 0 | 273 | | matTextures.Add(tex); |
| | 274 | | } |
| | 275 | |
|
| | 276 | | List<TexMaterialMap> materialMaps; |
| | 277 | |
|
| 0 | 278 | | if (!texMaterialMap.TryGetValue(tex, out materialMaps)) |
| | 279 | | { |
| 0 | 280 | | materialMaps = new List<TexMaterialMap>(); |
| 0 | 281 | | texMaterialMap.Add(tex, materialMaps); |
| | 282 | | } |
| | 283 | |
|
| 0 | 284 | | materialMaps.Add(new TexMaterialMap(mat, propertyName, propertyN |
| | 285 | | } |
| | 286 | | } |
| | 287 | |
|
| 0 | 288 | | return matTextures; |
| | 289 | | }) |
| | 290 | | .ToList(); |
| | 291 | |
|
| 0 | 292 | | var folderName = Path.GetDirectoryName(ctx.assetPath); |
| | 293 | |
|
| | 294 | | // Save textures as separate assets and rewrite refs |
| | 295 | | // TODO: Support for other texture types |
| 0 | 296 | | if (textures.Count > 0) |
| | 297 | | { |
| 0 | 298 | | var texturesRoot = string.Concat(folderName, "/", "Textures/"); |
| | 299 | |
|
| 0 | 300 | | if (!Directory.Exists(texturesRoot)) |
| 0 | 301 | | Directory.CreateDirectory(texturesRoot); |
| | 302 | |
|
| 0 | 303 | | Texture2D[] cachedTextures = PersistentAssetCache.ImageCacheByUri.Values.Select((x) => { ret |
| | 304 | |
|
| 0 | 305 | | foreach (var tex in textures) |
| | 306 | | { |
| 0 | 307 | | var ext = _useJpgTextures ? ".jpg" : ".png"; |
| 0 | 308 | | var texPath = string.Concat(texturesRoot, tex.name, ext); |
| 0 | 309 | | var absolutePath = Application.dataPath + "/../" + texPath; |
| | 310 | |
|
| 0 | 311 | | if (File.Exists(absolutePath) || cachedTextures.Contains(tex)) |
| | 312 | | continue; |
| | 313 | |
|
| 0 | 314 | | File.WriteAllBytes(texPath, _useJpgTextures ? tex.EncodeToJPG() : tex.EncodeToPNG()); |
| 0 | 315 | | AssetDatabase.ImportAsset(texPath, ImportAssetOptions.ForceSynchronousImport | ImportAss |
| | 316 | | } |
| | 317 | |
|
| 0 | 318 | | AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUp |
| | 319 | | } |
| | 320 | | } |
| | 321 | |
|
| | 322 | |
|
| 0 | 323 | | List<Material> materialCopies = new List<Material>(materials); |
| | 324 | | // Save materials as separate assets and rewrite refs |
| 0 | 325 | | if (materials.Count > 0) |
| | 326 | | { |
| 0 | 327 | | var folderName = Path.GetDirectoryName(ctx.assetPath); |
| 0 | 328 | | var materialRoot = string.Concat(folderName, "/", "Materials/"); |
| 0 | 329 | | Directory.CreateDirectory(materialRoot); |
| | 330 | |
|
| 0 | 331 | | for (var matIndex = 0; matIndex < materials.Count; matIndex++) |
| | 332 | | { |
| 0 | 333 | | var mat = materials[matIndex]; |
| 0 | 334 | | var materialPath = string.Concat(materialRoot, mat.name, ".mat"); |
| | 335 | |
|
| 0 | 336 | | CopyOrNew(mat, materialPath, m => |
| | 337 | | { |
| 0 | 338 | | materialCopies[matIndex] = m; |
| | 339 | |
|
| 0 | 340 | | foreach (var r in renderers) |
| | 341 | | { |
| 0 | 342 | | var sharedMaterials = r.sharedMaterials; |
| 0 | 343 | | for (var i = 0; i < sharedMaterials.Length; ++i) |
| | 344 | | { |
| 0 | 345 | | var sharedMaterial = sharedMaterials[i]; |
| 0 | 346 | | if (sharedMaterial.name == mat.name) |
| | 347 | | { |
| 0 | 348 | | sharedMaterials[i] = m; |
| 0 | 349 | | EditorUtility.SetDirty(m); |
| | 350 | | } |
| | 351 | | } |
| | 352 | |
|
| 0 | 353 | | sharedMaterials = sharedMaterials.Where(sm => sm).ToArray(); |
| 0 | 354 | | r.sharedMaterials = sharedMaterials; |
| | 355 | | } |
| 0 | 356 | | }); |
| | 357 | | } |
| | 358 | |
|
| | 359 | | // Fix textures |
| | 360 | | // HACK: This needs to be a delayed call. |
| | 361 | | // Unity needs a frame to kick off the texture import so we can rewrite the ref |
| 0 | 362 | | if (textures.Count > 0) |
| | 363 | | { |
| 0 | 364 | | delayCallsCount++; |
| 0 | 365 | | EditorApplication.delayCall += () => |
| | 366 | | { |
| 0 | 367 | | Texture2D[] cachedTextures = PersistentAssetCache.ImageCacheByUri.Values.Select((x) => { |
| | 368 | |
|
| 0 | 369 | | delayCallsCount--; |
| | 370 | |
|
| 0 | 371 | | for (var i = 0; i < textures.Count; ++i) |
| | 372 | | { |
| 0 | 373 | | var tex = textures[i]; |
| 0 | 374 | | var materialMaps = texMaterialMap[tex]; |
| 0 | 375 | | bool isExternal = cachedTextures.Contains(tex); |
| | 376 | |
|
| 0 | 377 | | var texturesRoot = string.Concat(folderName, "/", "Textures/"); |
| 0 | 378 | | var ext = _useJpgTextures ? ".jpg" : ".png"; |
| 0 | 379 | | var texPath = string.Concat(texturesRoot, tex.name, ext); |
| | 380 | |
|
| 0 | 381 | | var importedTex = AssetDatabase.LoadAssetAtPath<Texture2D>(texPath); |
| 0 | 382 | | var importer = (TextureImporter) TextureImporter.GetAtPath(texPath); |
| | 383 | |
|
| 0 | 384 | | if (importer != null) |
| | 385 | | { |
| 0 | 386 | | importer.isReadable = false; |
| 0 | 387 | | var isNormalMap = true; |
| | 388 | |
|
| 0 | 389 | | for (var matIndex = 0; matIndex < materials.Count; matIndex++) |
| | 390 | | { |
| 0 | 391 | | var originalMaterial = materials[matIndex]; |
| | 392 | |
|
| 0 | 393 | | foreach (var materialMap in materialMaps) |
| | 394 | | { |
| 0 | 395 | | if (materialMap.Material == originalMaterial) |
| | 396 | | { |
| | 397 | | //NOTE(Brian): Only set as normal map if is exclusively |
| | 398 | | // used for that. |
| | 399 | | // We don't want DXTnm in color textures. |
| 0 | 400 | | if (!materialMap.IsNormalMap) |
| 0 | 401 | | isNormalMap = false; |
| | 402 | |
|
| 0 | 403 | | materialCopies[matIndex].SetTexture(materialMap.Property, importedTe |
| | 404 | | } |
| | 405 | | } |
| | 406 | | } |
| | 407 | |
|
| 0 | 408 | | if (isExternal) |
| 0 | 409 | | isNormalMap = false; |
| | 410 | |
|
| 0 | 411 | | if (isNormalMap) |
| | 412 | | { |
| | 413 | | // Try to auto-detect normal maps |
| 0 | 414 | | importer.textureType = TextureImporterType.NormalMap; |
| 0 | 415 | | } |
| 0 | 416 | | else if (importer.textureType == TextureImporterType.Sprite) |
| | 417 | | { |
| | 418 | | // Force disable sprite mode, even for 2D projects |
| 0 | 419 | | importer.textureType = TextureImporterType.Default; |
| | 420 | | } |
| | 421 | |
|
| 0 | 422 | | importer.crunchedCompression = true; |
| 0 | 423 | | importer.compressionQuality = 100; |
| 0 | 424 | | importer.textureCompression = TextureImporterCompression.CompressedHQ; |
| 0 | 425 | | importer.SaveAndReimport(); |
| 0 | 426 | | } |
| | 427 | | else |
| | 428 | | { |
| 0 | 429 | | Debug.LogWarning(string.Format("GLTFImporter: Unable to import texture at path: |
| | 430 | | } |
| | 431 | |
|
| 0 | 432 | | if (delayCallsCount == 0) |
| | 433 | | { |
| 0 | 434 | | AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport | ImportAssetOpt |
| 0 | 435 | | AssetDatabase.SaveAssets(); |
| | 436 | | } |
| | 437 | | } |
| 0 | 438 | | }; |
| | 439 | | } |
| | 440 | |
|
| 0 | 441 | | AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate |
| 0 | 442 | | AssetDatabase.SaveAssets(); |
| 0 | 443 | | } |
| | 444 | | else |
| | 445 | | { |
| 0 | 446 | | var temp = GameObject.CreatePrimitive(PrimitiveType.Plane); |
| 0 | 447 | | temp.SetActive(false); |
| 0 | 448 | | var defaultMat = new[] { temp.GetComponent<Renderer>().sharedMaterial }; |
| 0 | 449 | | DestroyImmediate(temp); |
| | 450 | |
|
| 0 | 451 | | foreach (var rend in renderers) |
| | 452 | | { |
| 0 | 453 | | rend.sharedMaterials = defaultMat; |
| | 454 | | } |
| | 455 | | } |
| | 456 | |
|
| 0 | 457 | | var rootObject = gltfScene.GetComponentInChildren<InstantiatedGLTFObject>(); |
| | 458 | |
|
| 0 | 459 | | if (rootObject != null) |
| 0 | 460 | | DestroyImmediate(rootObject); |
| | 461 | | } |
| 0 | 462 | | } |
| 0 | 463 | | catch (Exception e) |
| | 464 | | { |
| 0 | 465 | | if (gltfScene) |
| | 466 | | { |
| 0 | 467 | | DestroyImmediate(gltfScene); |
| | 468 | | } |
| | 469 | |
|
| 0 | 470 | | throw new Exception(e.Message + "\n" + e.StackTrace, e); |
| | 471 | | } |
| | 472 | |
|
| | 473 | | // Set main asset |
| 0 | 474 | | ctx.AddObjectToAsset("main asset", gltfScene); |
| | 475 | |
|
| | 476 | | // Add meshes |
| 0 | 477 | | foreach (var mesh in meshes) |
| | 478 | | { |
| | 479 | | try |
| | 480 | | { |
| 0 | 481 | | ctx.AddObjectToAsset("mesh " + mesh.name, mesh); |
| 0 | 482 | | } |
| | 483 | | catch (System.InvalidOperationException e) |
| | 484 | | { |
| 0 | 485 | | Debug.LogWarning(e.ToString(), mesh); |
| 0 | 486 | | } |
| | 487 | | } |
| | 488 | |
|
| 0 | 489 | | ctx.SetMainObject(gltfScene); |
| 0 | 490 | | } |
| | 491 | |
|
| | 492 | | public static event System.Action<GLTFRoot> OnGLTFRootIsConstructed; |
| | 493 | | public static event System.Action<GLTFSceneImporter> OnGLTFWillLoad; |
| | 494 | |
|
| | 495 | | private GameObject CreateGLTFScene(string projectFilePath) |
| | 496 | | { |
| 0 | 497 | | ILoader fileLoader = new FileLoader(Path.GetDirectoryName(projectFilePath)); |
| 0 | 498 | | using (var stream = File.OpenRead(projectFilePath)) |
| | 499 | | { |
| | 500 | | GLTFRoot gLTFRoot; |
| 0 | 501 | | GLTFParser.ParseJson(stream, out gLTFRoot); |
| | 502 | |
|
| 0 | 503 | | OnGLTFRootIsConstructed?.Invoke(gLTFRoot); |
| | 504 | |
|
| 0 | 505 | | var loader = new GLTFSceneImporter(Path.GetFullPath(projectFilePath), gLTFRoot, fileLoader, null, stream |
| 0 | 506 | | GLTFSceneImporter.budgetPerFrameInMilliseconds = float.MaxValue; |
| 0 | 507 | | loader.addImagesToPersistentCaching = false; // Since we control the PersistentAssetCache during AB Conv |
| 0 | 508 | | loader.addMaterialsToPersistentCaching = false; |
| 0 | 509 | | loader.initialVisibility = true; |
| 0 | 510 | | loader.useMaterialTransition = false; |
| 0 | 511 | | loader.maximumLod = _maximumLod; |
| 0 | 512 | | loader.isMultithreaded = true; |
| | 513 | |
|
| 0 | 514 | | OnGLTFWillLoad?.Invoke(loader); |
| | 515 | |
|
| | 516 | | // HACK: Force the coroutine to run synchronously in the editor |
| 0 | 517 | | var stack = new Stack<IEnumerator>(); |
| 0 | 518 | | stack.Push(loader.LoadScene()); |
| | 519 | |
|
| 0 | 520 | | while (stack.Count > 0) |
| | 521 | | { |
| 0 | 522 | | var enumerator = stack.Pop(); |
| | 523 | |
|
| | 524 | | try |
| | 525 | | { |
| 0 | 526 | | if (enumerator.MoveNext()) |
| | 527 | | { |
| 0 | 528 | | stack.Push(enumerator); |
| 0 | 529 | | var subEnumerator = enumerator.Current as IEnumerator; |
| 0 | 530 | | if (subEnumerator != null) |
| | 531 | | { |
| 0 | 532 | | stack.Push(subEnumerator); |
| | 533 | | } |
| | 534 | | } |
| 0 | 535 | | } |
| 0 | 536 | | catch (Exception e) |
| | 537 | | { |
| 0 | 538 | | Debug.Log("GLTFImporter - CreateGLTFScene Failed: " + e.Message + "\n" + e.StackTrace); |
| 0 | 539 | | } |
| | 540 | | } |
| | 541 | |
|
| 0 | 542 | | return loader.lastLoadedScene; |
| | 543 | | } |
| 0 | 544 | | } |
| | 545 | |
|
| | 546 | | private void CopyOrNew<T>(T asset, string assetPath, Action<T> replaceReferences) where T : Object |
| | 547 | | { |
| 0 | 548 | | var existingAsset = AssetDatabase.LoadAssetAtPath<T>(assetPath); |
| 0 | 549 | | if (existingAsset) |
| | 550 | | { |
| 0 | 551 | | EditorUtility.CopySerialized(asset, existingAsset); |
| 0 | 552 | | replaceReferences(existingAsset); |
| 0 | 553 | | return; |
| | 554 | | } |
| | 555 | |
|
| 0 | 556 | | AssetDatabase.CreateAsset(asset, assetPath); |
| 0 | 557 | | } |
| | 558 | |
|
| | 559 | | private class TexMaterialMap |
| | 560 | | { |
| 0 | 561 | | public UnityEngine.Material Material { get; set; } |
| 0 | 562 | | public string Property { get; set; } |
| 0 | 563 | | public bool IsNormalMap { get; set; } |
| | 564 | |
|
| 0 | 565 | | public TexMaterialMap(UnityEngine.Material material, string property, bool isNormalMap) |
| | 566 | | { |
| 0 | 567 | | Material = material; |
| 0 | 568 | | Property = property; |
| 0 | 569 | | IsNormalMap = isNormalMap; |
| 0 | 570 | | } |
| | 571 | | } |
| | 572 | | } |
| | 573 | | } |
| | 574 | | #endif |