GLTFTexture.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using Newtonsoft.Json;
  6. using UnityEngine;
  7. using UnityEngine.Scripting;
  8. namespace Siccity.GLTFUtility {
  9. // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#texture
  10. [Preserve] public class GLTFTexture {
  11. public int? sampler;
  12. public int? source;
  13. public string name;
  14. public class ImportResult {
  15. private GLTFImage.ImportResult image;
  16. private Texture2D cache;
  17. /// <summary> Constructor </summary>
  18. public ImportResult(GLTFImage.ImportResult image) {
  19. this.image = image;
  20. }
  21. /// <summary> Create or return cached texture </summary>
  22. public IEnumerator GetTextureCached(bool linear, Action<Texture2D> onFinish, Action<float> onProgress = null) {
  23. if (cache == null) {
  24. IEnumerator en = image.CreateTextureAsync(linear, x => cache = x, onProgress);
  25. while (en.MoveNext()) { yield return null; };
  26. }
  27. onFinish(cache);
  28. }
  29. }
  30. public ImportResult Import(GLTFImage.ImportResult[] images) {
  31. if (source.HasValue) {
  32. ImportResult result = new ImportResult(images[source.Value]);
  33. return result;
  34. }
  35. return null;
  36. }
  37. public class ImportTask : Importer.ImportTask<ImportResult[]> {
  38. public ImportTask(List<GLTFTexture> textures, GLTFImage.ImportTask imageTask) : base(imageTask) {
  39. task = new Task(() => {
  40. if (textures == null) return;
  41. Result = new ImportResult[textures.Count];
  42. for (int i = 0; i < Result.Length; i++) {
  43. Result[i] = textures[i].Import(imageTask.Result);
  44. }
  45. });
  46. }
  47. }
  48. }
  49. }