Beispiel #1
0
        private TextureId ExportTexture(UnityEngine.Texture textureObj)
        {
            TextureId id = GetTextureId(_root, textureObj);

            if (id != null)
            {
                return(id);
            }

            var texture = new GLTF.Schema.Texture();

            if (ExportNames)
            {
                texture.Name = textureObj.name;
            }

            texture.Source  = ExportImage(textureObj);
            texture.Sampler = ExportSampler(textureObj);

            _textures.Add(textureObj);

            id = new TextureId {
                Id   = _root.Textures.Count,
                Root = _root
            };

            _root.Textures.Add(texture);

            return(id);
        }
        protected virtual UnityEngine.Texture CreateTexture(GLTF.Schema.Texture texture)
        {
            if (_assetCache.TextureCache[texture.Source.Id] == null)
            {
                var source            = _assetCache.ImageCache[texture.Source.Id];
                var desiredFilterMode = FilterMode.Bilinear;
                var desiredWrapMode   = UnityEngine.TextureWrapMode.Repeat;

                if (texture.Sampler != null)
                {
                    var sampler = texture.Sampler.Value;
                    switch (sampler.MinFilter)
                    {
                    case MinFilterMode.Nearest:
                        desiredFilterMode = FilterMode.Point;
                        break;

                    case MinFilterMode.Linear:
                    default:
                        desiredFilterMode = FilterMode.Bilinear;
                        break;
                    }

                    switch (sampler.WrapS)
                    {
                    case GLTF.Schema.WrapMode.ClampToEdge:
                        desiredWrapMode = UnityEngine.TextureWrapMode.Clamp;
                        break;

                    case GLTF.Schema.WrapMode.Repeat:
                    default:
                        desiredWrapMode = UnityEngine.TextureWrapMode.Repeat;
                        break;
                    }
                }

                if (source.filterMode == desiredFilterMode && source.wrapMode == desiredWrapMode)
                {
                    _assetCache.TextureCache[texture.Source.Id] = source;
                }
                else
                {
                    var unityTexture = UnityEngine.Object.Instantiate(source);
                    unityTexture.filterMode = desiredFilterMode;
                    unityTexture.wrapMode   = desiredWrapMode;
                    _assetCache.TextureCache[texture.Source.Id] = unityTexture;
                }
            }

            return(_assetCache.TextureCache[texture.Source.Id]);
        }
        private void SetupTexture(GLTF.Schema.Texture def, int textureIndex)
        {
            Texture2D source = _assetManager.getOrCreateTexture(def.Source.Id, textureIndex);
            // Default values
            var desiredFilterMode = FilterMode.Bilinear;
            var desiredWrapMode   = UnityEngine.TextureWrapMode.Repeat;

            if (def.Sampler != null)
            {
                var sampler = def.Sampler.Value;
                switch (sampler.MinFilter)
                {
                case MinFilterMode.Nearest:
                    desiredFilterMode = FilterMode.Point;
                    break;

                case MinFilterMode.Linear:
                default:
                    desiredFilterMode = FilterMode.Bilinear;
                    break;
                }

                switch (sampler.WrapS)
                {
                case GLTF.Schema.WrapMode.ClampToEdge:
                    desiredWrapMode = UnityEngine.TextureWrapMode.Clamp;
                    break;

                case GLTF.Schema.WrapMode.Repeat:
                default:
                    desiredWrapMode = UnityEngine.TextureWrapMode.Repeat;
                    break;
                }
            }

            source.filterMode = desiredFilterMode;
            source.wrapMode   = desiredWrapMode;
            _assetManager.registerTexture(source);
        }
        private TextureId ExportTexture(UnityEngine.Texture textureObj)
        {
            TextureId id = GetTextureId(_root, textureObj);

            if (id != null)
            {
                return(id);
            }

            var texture = new GLTF.Schema.Texture();

            //If texture name not set give it a unique name using count
            if (textureObj.name == "")
            {
                textureObj.name = (_root.Textures.Count + 1).ToString();
            }

            if (ExportNames)
            {
                texture.Name = textureObj.name;
            }

            texture.Source  = ExportImage(textureObj);
            texture.Sampler = ExportSampler(textureObj);

            _textures.Add(textureObj);

            id = new TextureId {
                Id   = _root.Textures.Count,
                Root = _root
            };

            _root.Textures.Add(texture);

            return(id);
        }
        private TextureId GenerateTexture(Texture2D texture, ImageId imageId)
        {
            if (root.Textures == null)
            {
                root.Textures = new List<GLTF.Schema.Texture>();
            }

            if (root.Samplers == null)
            {
                root.Samplers = new List<GLTF.Schema.Sampler>();
            }

            var hasMipMap = texture.mipmapCount > 0;
            MagFilterMode magFilter = MagFilterMode.None;
            MinFilterMode minFilter = MinFilterMode.None;
            GLTF.Schema.WrapMode wrap = GLTF.Schema.WrapMode.None;

            switch (texture.filterMode)
            {
                case FilterMode.Point:
                    magFilter = MagFilterMode.Nearest;
                    if (hasMipMap)
                    {
                        minFilter = MinFilterMode.NearestMipmapNearest;
                    }
                    else
                    {
                        minFilter = MinFilterMode.Nearest;
                    }
                    break;

                case FilterMode.Bilinear:
                    magFilter = MagFilterMode.Linear;
                    if (hasMipMap)
                    {
                        minFilter = MinFilterMode.LinearMipmapNearest;
                    }
                    else
                    {
                        minFilter = MinFilterMode.Linear;
                    }
                    break;

                case FilterMode.Trilinear:
                    magFilter = MagFilterMode.Linear;
                    if (hasMipMap)
                    {
                        minFilter = MinFilterMode.Linear;
                    }
                    else
                    {
                        minFilter = MinFilterMode.LinearMipmapLinear;
                    }
                    break;
            }

            switch (texture.wrapMode)
            {
                case TextureWrapMode.Clamp:
                    wrap = GLTF.Schema.WrapMode.ClampToEdge;
                    break;
                case TextureWrapMode.Repeat:
                    wrap = GLTF.Schema.WrapMode.Repeat;
                    break;
            }

            var sampler = new Sampler
            {
                MagFilter = magFilter,
                MinFilter = minFilter,
                WrapS = wrap,
                WrapT = wrap
            };
            root.Samplers.Add(sampler);
            var samplerId = new SamplerId { Id = root.Samplers.Count - 1, Root = root };

            var gltfTexture = new GLTF.Schema.Texture { Source = imageId, Sampler = samplerId };
            root.Textures.Add(gltfTexture);

            var id = new TextureId { Id = root.Textures.Count - 1, Root = root };
            _texture2d2ID[texture] = id;

            return id;
        }
Beispiel #6
0
        protected override void Serialize(UnityEngine.Object sourceAsset)
        {
            this.texture = sourceAsset as UnityEngine.Texture2D;
            //先把原始图片导出来
            this.ExportTexture();

            var path   = PathHelper.GetTexturePath(this.texture);
            var mipmap = this.texture.mipmapCount > 1;
            //
            {
                this._root.Images.Add(new Image()
                {
                    Uri = ExportSetting.instance.GetExportPath(path)
                });
            }
            //
            {
                var filterMode = this.texture.filterMode;
                var wrapMode   = this.texture.wrapMode;

                var sampler = new Sampler();
                this._root.Samplers.Add(sampler);
                if (wrapMode == TextureWrapMode.Repeat)
                {
                    sampler.WrapS = GLTF.Schema.WrapMode.Repeat;
                    sampler.WrapT = GLTF.Schema.WrapMode.Repeat;
                }
                else
                {
                    sampler.WrapS = GLTF.Schema.WrapMode.ClampToEdge;
                    sampler.WrapT = GLTF.Schema.WrapMode.ClampToEdge;
                }
                sampler.MagFilter = filterMode == FilterMode.Point ? MagFilterMode.Nearest : MagFilterMode.Linear;
                if (!mipmap)
                {
                    sampler.MagFilter = filterMode == FilterMode.Point ? MagFilterMode.Nearest : MagFilterMode.Linear;
                }
                else if (filterMode == FilterMode.Point)
                {
                    sampler.MinFilter = MinFilterMode.NearestMipmapNearest;
                }
                else if (filterMode == FilterMode.Bilinear)
                {
                    sampler.MinFilter = MinFilterMode.LinearMipmapNearest;
                }
                else if (filterMode == FilterMode.Trilinear)
                {
                    sampler.MinFilter = MinFilterMode.LinearMipmapLinear;
                }
            }
            //
            {
                var gltfTexture = new GLTF.Schema.Texture();
                this._root.Textures.Add(gltfTexture);

                gltfTexture.Sampler    = new SamplerId();
                gltfTexture.Source     = new ImageId();
                gltfTexture.Extensions = new Dictionary <string, IExtension>()
                {
                    {
                        TextureExtension.EXTENSION_NAME,
                        new TextureExtension()
                        {
                            anisotropy = this.texture.anisoLevel,
                            format     = GetTextureFormat(),
                            levels     = mipmap ? 0 : 1
                        }
                    }
                };
            }
        }
Beispiel #7
0
 public virtual void Import(EditorImporter importer, Texture2D texture, GLTF.Schema.Texture gltfTex, Extension extension)
 {
 }
 public static void Import(string extensionName, EditorImporter importer, Texture2D texture, GLTF.Schema.Texture gltfTex, Extension extension)
 {
     if (Name2Extensions.ContainsKey(extensionName))
     {
         Name2Extensions[extensionName].Import(importer, texture, gltfTex, extension);
     }
 }
        protected override void Serialize(UnityEngine.Object sourceAsset)
        {
            this.textureArray = sourceAsset as Texture2DArrayData;
            var firstTexture = this.textureArray.textures[0];

            //先把原始图片导出来
            this.ExportTexture();

            var mipmap = firstTexture.mipmapCount > 1;
            //
            {
                var image = new Image();
                image.Uris = new List <string>();
                foreach (var tex in this.textureArray.textures)
                {
                    image.Uris.Add(ExportSetting.instance.GetExportPath(PathHelper.GetTexturePath(tex)));
                }
                this._root.Images.Add(image);
            }
            //
            {
                var filterMode = firstTexture.filterMode;
                var wrapMode   = firstTexture.wrapMode;

                var sampler = new Sampler();
                this._root.Samplers.Add(sampler);
                if (wrapMode == TextureWrapMode.Repeat)
                {
                    sampler.WrapS = GLTF.Schema.WrapMode.Repeat;
                    sampler.WrapT = GLTF.Schema.WrapMode.Repeat;
                }
                else
                {
                    sampler.WrapS = GLTF.Schema.WrapMode.ClampToEdge;
                    sampler.WrapT = GLTF.Schema.WrapMode.ClampToEdge;
                }
                sampler.MagFilter = filterMode == FilterMode.Point ? MagFilterMode.Nearest : MagFilterMode.Linear;
                if (!mipmap)
                {
                    sampler.MagFilter = filterMode == FilterMode.Point ? MagFilterMode.Nearest : MagFilterMode.Linear;
                }
                else if (filterMode == FilterMode.Point)
                {
                    sampler.MinFilter = MinFilterMode.NearestMipmapNearest;
                }
                else if (filterMode == FilterMode.Bilinear)
                {
                    sampler.MinFilter = MinFilterMode.LinearMipmapNearest;
                }
                else if (filterMode == FilterMode.Trilinear)
                {
                    sampler.MinFilter = MinFilterMode.LinearMipmapLinear;
                }
            }
            //
            {
                var gltfTexture = new GLTF.Schema.Texture();
                this._root.Textures.Add(gltfTexture);

                gltfTexture.Sampler    = new SamplerId();
                gltfTexture.Source     = new ImageId();
                gltfTexture.Extensions = new Dictionary <string, IExtension>()
                {
                    {
                        TextureExtension.EXTENSION_NAME,
                        new TextureExtension()
                        {
                            width      = firstTexture.width,
                            height     = firstTexture.height,
                            format     = GetTextureFormat(),
                            levels     = mipmap ? 0 : 1,
                            encoding   = 2,
                            faces      = 6,
                            mapping    = 1,
                            anisotropy = firstTexture.anisoLevel,
                        }
                    }
                };
            }
        }