Пример #1
0
        public static void LoadCachedFile(FileInfo file, IAssetMeta meta, Stream output)
        {
            var cached = GetCacheFile(file, meta);

            using var fs = cached.OpenRead();
            fs.CopyTo(output);
        }
Пример #2
0
        public override void LoadAndWriteToStream(FileInfo inputFile, IAssetMeta meta, Stream outputStream)
        {
            ShaderAsset.ShaderType type = ShaderAsset.ShaderType.Frag;
            if (inputFile.Name.ToLower().Contains("frag"))
            {
                type = ShaderAsset.ShaderType.Frag;
            }
            else if (inputFile.Name.ToLower().Contains("vert"))
            {
                type = ShaderAsset.ShaderType.Vertex;
            }
            else
            {
                type = meta.GetValue("shaderType", ShaderAsset.ShaderType.Frag);
            }

            using var ms = new MemoryStream();
            using var fs = inputFile.OpenRead();
            using var br = new BinaryWriter(outputStream, Encoding.UTF8, true);

            fs.CopyTo(ms);

            var data = ms.ToArray();

            br.Write((int)type);
            br.Write(data.Length);
            br.Write(data);
        }
Пример #3
0
        private static FileInfo GetCacheFile(FileInfo originalFile, IAssetMeta meta)
        {
            string hash    = AssetHasher.GetAssetHash(originalFile, meta).Substring(0, 32);
            var    newPath = Path.ChangeExtension(originalFile.Name, $".{hash}.dat");

            newPath = Path.Combine(cacheFolder, newPath);
            return(new FileInfo(newPath));
        }
Пример #4
0
        public static void SaveCachedFile(FileInfo file, IAssetMeta meta, Stream data)
        {
            EnsureCacheFolder();

            var originalPosition = data.Position;

            data.Seek(0, SeekOrigin.Begin);

            var newFile = GetCacheFile(file, meta);

            using FileStream fs = newFile.OpenWrite();
            data.CopyTo(fs);

            data.Position = originalPosition;
        }
Пример #5
0
        public static string GetAssetHash(FileInfo assetFile, IAssetMeta metaFile)
        {
            using var hash = SHA256.Create();
            using var fs   = assetFile.OpenRead();
            using var ms   = new MemoryStream();

            metaFile.WriteToStream(ms);
            ms.Seek(0, SeekOrigin.Begin);

            var fileHash = hash.ComputeHash(fs);
            var metaHash = hash.ComputeHash(ms);

            for (int i = 0; i < fileHash.Length; i++)
            {
                fileHash[i] ^= metaHash[i];
            }

            return(ByteArrayToString(fileHash));
        }
        public override void LoadAndWriteToStream(FileInfo inputFile, IAssetMeta meta, Stream outputStream)
        {
            PostProcessSteps assimpFlags = PostProcessSteps.FlipWindingOrder
                                           | PostProcessSteps.Triangulate
                                           | PostProcessSteps.PreTransformVertices
                                           | PostProcessSteps.GenerateUVCoords
                                           | PostProcessSteps.GenerateSmoothNormals
                                           | PostProcessSteps.FlipUVs
            ;

            var context = new AssimpContext();

            context.SetConfig(new FloatPropertyConfig("AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE", 80f));

            string extension = inputFile.Extension;

            using FileStream fs = inputFile.OpenRead();

            var scene = context.ImportFileFromStream(fs, assimpFlags, extension);

            // Generate vertex buffer from ASSIMP scene data
            float scale = 1.0f;

            MeshData newMesh = new MeshData();

            newMesh.subMeshes = new SubMeshData[scene.MeshCount];

            for (int m = 0; m < scene.MeshCount; m++)
            {
                Mesh     mesh     = scene.Meshes[m];
                Vertex[] vertices = new Vertex[mesh.VertexCount];
                Face[]   faces    = mesh.Faces.Where(x => x.IndexCount == 3).ToArray();            // Remove any degenerate faces
                UInt32[] indices  = new UInt32[faces.Length * 3];

                //DebugHelper.AssertThrow<OverflowException>(faces.Length * 3 <= UInt32.MaxValue);

                for (int v = 0; v < mesh.VertexCount; v++)
                {
                    Vertex vertex;

                    vertex.position = new vec3(mesh.Vertices[v].X, mesh.Vertices[v].Y, mesh.Vertices[v].Z) * scale;
                    vertex.normal   = new vec3(mesh.Normals[v].X, mesh.Normals[v].Y, mesh.Normals[v].Z);
                    if (mesh.HasTextureCoords(0))
                    {
                        vertex.uv0 = new vec2(mesh.TextureCoordinateChannels[0][v].X, mesh.TextureCoordinateChannels[0][v].Y);
                    }
                    else
                    {
                        vertex.uv0 = vec2.Zero;
                    }

                    vertices[v] = vertex;
                }

                for (int i = 0; i < faces.Length; i++)
                {
                    indices[i * 3 + 0] = (UInt16)faces[i].Indices[0];
                    indices[i * 3 + 1] = (UInt16)faces[i].Indices[1];
                    indices[i * 3 + 2] = (UInt16)faces[i].Indices[2];
                }

                newMesh.subMeshes[m] = new SubMeshData(vertices, indices);
            }


            //Write mesh to stream
            using BinaryWriter writer = new BinaryWriter(outputStream, Encoding.UTF8, true);
            writer.Write(newMesh.subMeshes.Length);
            foreach (SubMeshData subMesh in newMesh.subMeshes)
            {
                writer.Write(subMesh.vertices.Length);
                Span <byte> verts = new Span <Vertex>(subMesh.vertices).Cast <Vertex, byte>();
                writer.Write(verts);

                writer.Write(subMesh.indices.Length);
                Span <uint> indsU = subMesh.indices;
                var         inds  = indsU.Cast <UInt32, byte>();
                writer.Write(inds);
            }
            writer.Flush();
        }
 public override void GetDefaultMeta(IAssetMeta meta)
 {
 }
 public override void LoadAndWriteToStream(FileInfo inputFile, IAssetMeta meta, Stream outputStream)
 {
     using FileStream fileReadStream = File.OpenRead(inputFile.FullName);
     fileReadStream.CopyTo(outputStream);
 }
        public override unsafe void LoadAndWriteToStream(FileInfo inputFile, IAssetMeta meta, Stream outputStream)
        {
            bool generateMips          = meta.GetValue("generateMipMaps", true);
            CompressionFormat  format  = meta.GetValue("compressionFormat", CompressionFormat.BC3);
            CompressionQuality quality = meta.GetValue("compressionQuality", CompressionQuality.Balanced);

            using FileStream inputFs = File.OpenRead(inputFile.FullName);

            using Image <Rgba32> imageFile = Image.Load <Rgba32>(inputFs);
            bool      hasTransparency      = imageFile.GetPixelSpan().ToArray().Any(x => x.A < 255);
            BcEncoder encoder = new BcEncoder();

            encoder.OutputOptions.generateMipMaps = generateMips;
            encoder.OutputOptions.format          = format;
            encoder.OutputOptions.fileFormat      = OutputFileFormat.Ktx;
            encoder.OutputOptions.quality         = quality;

            encoder.Encode(imageFile, outputStream);


            /*
             * compressor.Process(out DDSContainer dds);
             * using BinaryWriter writer = new BinaryWriter(outputStream);
             *
             * using (dds) {
             *      KtxHeader header = new KtxHeader();
             *      byte[] id = new byte[] {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A};
             *      Unsafe.Copy(header.Identifier, ref id[0]);
             *      header.Endianness = 0x04030201;
             *      if (compressed) {
             *              header.GlType = 0; //For compressed textures
             *              header.GlTypeSize = 1; //For compressed textures
             *              header.GlFormat = 0; //For compressed textures
             *              header.GlInternalFormat = hasTransparency
             *                      ? GlInternalFormat.GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
             *                      : GlInternalFormat.GL_COMPRESSED_SRGB8_ETC2;
             *      }
             *      else {
             *              header.GlType = 0x1401; //GL_UNSIGNED_BYTE
             *              header.GlTypeSize = 1; //1 byte
             *              header.GlFormat = 0x1908; //GL_RGBA
             *      }
             *
             *
             *      header.PixelWidth = (uint)dds.MipChains[0][0].Width;
             *      header.PixelHeight = dds.Dimension == TextureDimension.One ?
             *              0 : (uint)dds.MipChains[0][0].Height;
             *      header.PixelDepth = dds.Dimension == TextureDimension.Three ?
             *              (uint)dds.MipChains[0][0].Depth : 0;
             *
             *      header.NumberOfArrayElements = 0;
             *      header.NumberOfFaces = (uint)dds.MipChains.Count;
             *      header.NumberOfMipmapLevels = (uint) dds.MipChains[0].Count;
             *      header.BytesOfKeyValueData = 0;
             *
             *      WriteStruct(writer, header);
             *      bool isCubemap = header.NumberOfFaces == 6 && header.NumberOfArrayElements == 0;
             *
             *      for (int i = 0; i < header.NumberOfMipmapLevels; i++) {
             *              uint imageSize = (uint)dds.MipChains[0][i].SizeInBytes;
             *              writer.Write(imageSize);
             *
             *              for (int j = 0; j < header.NumberOfFaces; j++) {
             *                      var face = dds.MipChains[j][i];
             *                      Span<byte> bytes = new Span<byte>(face.Data.ToPointer(), face.SizeInBytes);
             *                      writer.Write(bytes);
             *                      uint cubePadding = 0u;
             *                      if (isCubemap)
             *                      {
             *                              cubePadding = 3 - ((imageSize + 3) % 4);
             *                      }
             *
             *                      AddPadding(writer, cubePadding);
             *              }
             *
             *              uint mipPaddingBytes = 3 - ((imageSize + 3) % 4);
             *              AddPadding(writer, mipPaddingBytes);
             *      }
             *      writer.Flush();
             * }
             */
        }
 public override void GetDefaultMeta(IAssetMeta meta)
 {
     meta.SetValue("generateMipMaps", true);
     meta.SetValue("compressionFormat", CompressionFormat.BC7);
     meta.SetValue("compressionQuality", CompressionQuality.Balanced);
 }
Пример #11
0
 public abstract void LoadAndWriteToStream(FileInfo inputFile, IAssetMeta meta, Stream outputStream);
Пример #12
0
 public abstract void GetDefaultMeta(IAssetMeta meta);
Пример #13
0
 public override void GetDefaultMeta(IAssetMeta meta)
 {
     meta.SetValue("shaderType", ShaderAsset.ShaderType.Frag);
 }
Пример #14
0
        public static bool HasCachedFile(FileInfo file, IAssetMeta meta)
        {
            var cached = GetCacheFile(file, meta);

            return(cached.Exists);
        }