public Texture2D(AssetCabinet file) : this(file, 0, UnityClassID.Texture2D, UnityClassID.Texture2D) { file.ReplaceSubfile(-1, this, null); file.Parser.Textures.Add(this); m_TextureSettings = new GLTextureSettings(); }
public void CopyTo(GLTextureSettings dest) { dest.m_FilterMode = m_FilterMode; dest.m_MipBias = m_MipBias; dest.m_Aniso = m_Aniso; dest.m_WrapMode = m_WrapMode; }
public void LoadFrom(Stream stream) { BinaryReader reader = new BinaryReader(stream); m_Name = reader.ReadNameA4(); m_Width = reader.ReadInt32(); m_Height = reader.ReadInt32(); m_CompleteImageSize = reader.ReadInt32(); m_TextureFormat = (TextureFormat)reader.ReadInt32(); m_MipMap = reader.ReadBoolean(); m_isReadable = reader.ReadBoolean(); m_ReadAllowed = reader.ReadBoolean(); reader.ReadByte(); m_ImageCount = reader.ReadInt32(); m_TextureDimension = reader.ReadInt32(); m_TextureSettings = new GLTextureSettings(stream); m_LightmapFormat = reader.ReadInt32(); m_ColorSpace = reader.ReadInt32(); int size = reader.ReadInt32(); image_data = reader.ReadBytes(size); reader.ReadBytes(4 - size & 3); }
public void LoadFrom(ImportedTexture tex) { m_Name = Path.GetFileNameWithoutExtension(tex.Name); Device dev = new Device(new Direct3D(), 0, DeviceType.Hardware, new IntPtr(), CreateFlags.SoftwareVertexProcessing, new PresentParameters()); ImageInformation imageInfo; using (Texture renderTexture = Texture.FromMemory(dev, tex.Data, 0, 0, -1, Usage.None, Format.Unknown, Pool.Managed, Filter.Default, Filter.Default, 0, out imageInfo)) { m_Width = imageInfo.Width; m_Height = imageInfo.Height; TextureFormat tf; if (TextureFormat.TryParse(imageInfo.Format.ToString(), true, out tf)) { m_TextureFormat = tf; } else if (imageInfo.Format == Format.R8G8B8) { m_TextureFormat = TextureFormat.RGB24; } else if (imageInfo.Format == Format.A8R8G8B8) { m_TextureFormat = TextureFormat.ARGB32; } else { throw new Exception("Unknown format " + imageInfo.Format); } m_MipMap = imageInfo.MipLevels > 1; m_ReadAllowed = true; m_ImageCount = 1; m_TextureDimension = 2; m_TextureSettings = new GLTextureSettings(); m_ColorSpace = 1; int bytesPerPixel = 0, originY = 0; using (BinaryReader reader = new BinaryReader(new MemoryStream(tex.Data))) { switch (m_TextureFormat) { case TextureFormat.DXT1: case TextureFormat.DXT5: reader.BaseStream.Position = 0x80; m_CompleteImageSize = (int)(reader.BaseStream.Length - reader.BaseStream.Position); break; case TextureFormat.RGB24: case TextureFormat.ARGB32: reader.BaseStream.Position = 0x0A; originY = reader.ReadInt16(); reader.BaseStream.Position = 0x12; bytesPerPixel = m_TextureFormat == TextureFormat.RGB24 ? 3 : 4; m_CompleteImageSize = m_Width * m_Height * bytesPerPixel; break; } image_data = reader.ReadBytes(m_CompleteImageSize); } switch (m_TextureFormat) { case TextureFormat.RGB24: for (int i = 0, j = 2; j < m_CompleteImageSize; i += 3, j += 3) { byte b = image_data[j]; image_data[j] = image_data[i]; image_data[i] = b; } break; case TextureFormat.ARGB32: for (int i = 0, j = 3, k = 1, l = 2; j < m_CompleteImageSize; i += 4, j += 4, k += 4, l += 4) { byte b = image_data[j]; image_data[j] = image_data[i]; image_data[i] = b; b = image_data[l]; image_data[l] = image_data[k]; image_data[k] = b; } break; } if (bytesPerPixel > 0 && originY > 0) { for (int srcIdx = 0, dstIdx = (originY - 1) * m_Width * bytesPerPixel; srcIdx < dstIdx; srcIdx += m_Width * bytesPerPixel, dstIdx -= m_Width * bytesPerPixel) { for (int i = 0; i < m_Width * bytesPerPixel; i++) { byte b = image_data[srcIdx + i]; image_data[srcIdx + i] = image_data[dstIdx + i]; image_data[dstIdx + i] = b; } } } } dev.Direct3D.Dispose(); dev.Dispose(); }
public void LoadFrom(ImportedTexture tex) { TextureFormat destFormat = (TextureFormat)0; Match nameWithFormat = Regex.Match(tex.Name, @"(.+)-([^-]+)(\..+)", RegexOptions.CultureInvariant); if (nameWithFormat.Success) { m_Name = nameWithFormat.Groups[1].Value; TextureFormat.TryParse(nameWithFormat.Groups[2].Value, true, out destFormat); } else { m_Name = Path.GetFileNameWithoutExtension(tex.Name); } Device dev = null; try { if (Gui.Renderer != null) { dev = Gui.Renderer.Device; } else { IntPtr handle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; if (handle.ToInt64() == 0) { handle = FindWindowByCaption(IntPtr.Zero, Console.Title); } dev = new Device(new Direct3D(), 0, DeviceType.Hardware, handle, CreateFlags.SoftwareVertexProcessing, new PresentParameters()); } ImageInformation imageInfo; using (Texture renderTexture = Texture.FromMemory(dev, tex.Data, 0, 0, -1, Usage.None, Format.Unknown, Pool.Managed, Filter.Default, Filter.Default, 0, out imageInfo)) { m_Width = imageInfo.Width; m_Height = imageInfo.Height; TextureFormat tf; if (TextureFormat.TryParse(imageInfo.Format.ToString(), true, out tf)) { m_TextureFormat = tf; } else if (imageInfo.Format == Format.R8G8B8) { m_TextureFormat = TextureFormat.RGB24; } else if (imageInfo.Format == Format.A8R8G8B8) { m_TextureFormat = destFormat != (TextureFormat)0 ? destFormat : TextureFormat.ARGB32; } else { throw new Exception("Unknown format " + imageInfo.Format); } m_MipMap = imageInfo.MipLevels > 1; m_ReadAllowed = true; m_ImageCount = 1; m_TextureDimension = 2; m_TextureSettings = new GLTextureSettings(); m_ColorSpace = 1; int bytesPerPixel = 0, originY = 0; using (BinaryReader reader = new BinaryReader(new MemoryStream(tex.Data))) { switch (m_TextureFormat) { case TextureFormat.DXT1: case TextureFormat.DXT5: reader.BaseStream.Position = 0x80; m_CompleteImageSize = (int)(reader.BaseStream.Length - reader.BaseStream.Position); break; case TextureFormat.RGB24: case TextureFormat.ARGB32: case TextureFormat.RGBA32: reader.BaseStream.Position = 0x0A; originY = reader.ReadInt16(); reader.BaseStream.Position = 0x12; bytesPerPixel = m_TextureFormat == TextureFormat.RGB24 ? 3 : 4; m_CompleteImageSize = m_Width * m_Height * bytesPerPixel; break; } image_data = reader.ReadBytes(m_CompleteImageSize); } switch (m_TextureFormat) { case TextureFormat.RGB24: for (int i = 0, j = 2; j < m_CompleteImageSize; i += 3, j += 3) { byte b = image_data[j]; image_data[j] = image_data[i]; image_data[i] = b; } break; case TextureFormat.ARGB32: for (int i = 0, j = 3, k = 1, l = 2; j < m_CompleteImageSize; i += 4, j += 4, k += 4, l += 4) { byte b = image_data[j]; image_data[j] = image_data[i]; image_data[i] = b; b = image_data[l]; image_data[l] = image_data[k]; image_data[k] = b; } break; } if (bytesPerPixel > 0 && originY > 0) { for (int srcIdx = 0, dstIdx = (originY - 1) * m_Width * bytesPerPixel; srcIdx < dstIdx; srcIdx += m_Width * bytesPerPixel, dstIdx -= m_Width * bytesPerPixel) { for (int i = 0; i < m_Width * bytesPerPixel; i++) { byte b = image_data[srcIdx + i]; image_data[srcIdx + i] = image_data[dstIdx + i]; image_data[dstIdx + i] = b; } } } } } catch (Exception e) { Utility.ReportException(e); } finally { if (Gui.Renderer == null && dev != null) { dev.Direct3D.Dispose(); dev.Dispose(); } } }