public Sampler(Sampler sampler, GLTFRoot gltfRoot) : base(sampler, gltfRoot) { if (sampler == null) { return; } MagFilter = sampler.MagFilter; MinFilter = sampler.MinFilter; WrapS = sampler.WrapS; WrapT = sampler.WrapT; }
/// <summary> /// Applies the Sampler's settings to a Unity texture. /// </summary> /// <param name="image">Texture to apply the settings to</param> /// <param name="defaultMinFilter">Fallback minification filter</param> /// <param name="defaultMagFilter">Fallback magnification filter</param> public void Apply(Texture2D image, MinFilterMode defaultMinFilter = MinFilterMode.Linear, MagFilterMode defaultMagFilter = MagFilterMode.Linear) { if (image == null) { return; } image.wrapModeU = wrapU; image.wrapModeV = wrapV; // Use the default filtering mode for textures that have no such specification in data image.filterMode = ConvertFilterMode( minFilter == MinFilterMode.None ? defaultMinFilter : minFilter, magFilter == MagFilterMode.None ? defaultMagFilter : magFilter ); }
static FilterMode ConvertFilterMode(MinFilterMode minFilterToConvert, MagFilterMode magFilterToConvert) { switch (minFilterToConvert) { case MinFilterMode.LinearMipmapLinear: return(FilterMode.Trilinear); case MinFilterMode.Nearest: case MinFilterMode.NearestMipmapNearest: case MinFilterMode.NearestMipmapLinear: // incorrect mip-map filtering in this case! return(FilterMode.Point); } switch (magFilterToConvert) { case MagFilterMode.Nearest: return(FilterMode.Point); default: return(FilterMode.Bilinear); } }
/// <summary> /// Constructs a Sampler with filter and wrap modes. /// </summary> /// <param name="filterMode">Unity texture filter mode</param> /// <param name="wrapModeU">Unity texture wrap mode (horizontal)</param> /// <param name="wrapModeV">Unity texture wrap mode (vertical)</param> public Sampler(FilterMode filterMode, TextureWrapMode wrapModeU, TextureWrapMode wrapModeV) { switch (filterMode) { case FilterMode.Point: magFilter = MagFilterMode.Nearest; minFilter = MinFilterMode.Nearest; break; case FilterMode.Bilinear: magFilter = MagFilterMode.Linear; minFilter = MinFilterMode.Linear; break; case FilterMode.Trilinear: magFilter = MagFilterMode.Linear; minFilter = MinFilterMode.LinearMipmapLinear; break; } wrapS = ConvertWrapMode(wrapModeU); wrapT = ConvertWrapMode(wrapModeV); }
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; }