|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Silk.NET.Assimp; |
| 5 | +using Silk.NET.OpenGL; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Numerics; |
| 10 | +using AssimpMesh = Silk.NET.Assimp.Mesh; |
| 11 | + |
| 12 | +namespace Tutorial |
| 13 | +{ |
| 14 | + public class Model : IDisposable |
| 15 | + { |
| 16 | + public Model(GL gl, string path, bool gamma = false) |
| 17 | + { |
| 18 | + var assimp = Silk.NET.Assimp.Assimp.GetApi(); |
| 19 | + _assimp = assimp; |
| 20 | + _gl = gl; |
| 21 | + LoadModel(path); |
| 22 | + } |
| 23 | + |
| 24 | + private readonly GL _gl; |
| 25 | + private Assimp _assimp; |
| 26 | + private List<Texture> _texturesLoaded = new List<Texture>(); |
| 27 | + public string Directory { get; protected set; } = string.Empty; |
| 28 | + public List<Mesh> Meshes { get; protected set; } = new List<Mesh>(); |
| 29 | + |
| 30 | + private unsafe void LoadModel(string path) |
| 31 | + { |
| 32 | + var scene = _assimp.ImportFile(path, (uint)PostProcessSteps.Triangulate); |
| 33 | + |
| 34 | + if (scene == null || scene->MFlags == Silk.NET.Assimp.Assimp.SceneFlagsIncomplete || scene->MRootNode == null) |
| 35 | + { |
| 36 | + var error = _assimp.GetErrorStringS(); |
| 37 | + throw new Exception(error); |
| 38 | + } |
| 39 | + |
| 40 | + Directory = path; |
| 41 | + |
| 42 | + ProcessNode(scene->MRootNode, scene); |
| 43 | + } |
| 44 | + |
| 45 | + private unsafe void ProcessNode(Node* node, Scene* scene) |
| 46 | + { |
| 47 | + for (var i = 0; i < node->MNumMeshes; i++) |
| 48 | + { |
| 49 | + var mesh = scene->MMeshes[node->MMeshes[i]]; |
| 50 | + Meshes.Add(ProcessMesh(mesh, scene)); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + for (var i = 0; i < node->MNumChildren; i++) |
| 55 | + { |
| 56 | + ProcessNode(node->MChildren[i], scene); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private unsafe Mesh ProcessMesh(AssimpMesh* mesh, Scene* scene) |
| 61 | + { |
| 62 | + // data to fill |
| 63 | + List<Vertex> vertices = new List<Vertex>(); |
| 64 | + List<uint> indices = new List<uint>(); |
| 65 | + List<Texture> textures = new List<Texture>(); |
| 66 | + |
| 67 | + // walk through each of the mesh's vertices |
| 68 | + for (uint i = 0; i < mesh->MNumVertices; i++) |
| 69 | + { |
| 70 | + Vertex vertex = new Vertex(); |
| 71 | + vertex.BoneIds = new int[Vertex.MAX_BONE_INFLUENCE]; |
| 72 | + vertex.Weights = new float[Vertex.MAX_BONE_INFLUENCE]; |
| 73 | + Vector3 vector = new Vector3(); // we declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to glm's vec3 class so we transfer the data to this placeholder glm::vec3 first. |
| 74 | + // positions |
| 75 | + vector.X = mesh->MVertices[i].X; |
| 76 | + vector.Y = mesh->MVertices[i].Y; |
| 77 | + vector.Z = mesh->MVertices[i].Z; |
| 78 | + vertex.Position = vector; |
| 79 | + // normals |
| 80 | + |
| 81 | + if (mesh->MNormals != null) |
| 82 | + { |
| 83 | + vector.X = mesh->MNormals[i].X; |
| 84 | + vector.Y = mesh->MNormals[i].Y; |
| 85 | + vector.Z = mesh->MNormals[i].Z; |
| 86 | + vertex.Normal = vector; |
| 87 | + } |
| 88 | + // texture coordinates |
| 89 | + if (mesh->MTextureCoords[0] != null) // does the mesh contain texture coordinates? |
| 90 | + { |
| 91 | + Vector2 vec = new Vector2(); |
| 92 | + // a vertex can contain up to 8 different texture coordinates. We thus make the assumption that we won't |
| 93 | + // use models where a vertex can have multiple texture coordinates so we always take the first set (0). |
| 94 | + vec.X = mesh->MTextureCoords[0][i].X; |
| 95 | + vec.Y = mesh->MTextureCoords[0][i].Y; |
| 96 | + vertex.TexCoords = vec; |
| 97 | + // tangent |
| 98 | + if (mesh->MTangents != null) |
| 99 | + { |
| 100 | + vector.X = mesh->MTangents[i].X; |
| 101 | + vector.Y = mesh->MTangents[i].Y; |
| 102 | + vector.Z = mesh->MTangents[i].Z; |
| 103 | + vertex.Tangent = vector; |
| 104 | + } |
| 105 | + // bitangent |
| 106 | + if (mesh->MBitangents != null) |
| 107 | + { |
| 108 | + vector.X = mesh->MBitangents[i].X; |
| 109 | + vector.Y = mesh->MBitangents[i].Y; |
| 110 | + vector.Z = mesh->MBitangents[i].Z; |
| 111 | + vertex.Bitangent = vector; |
| 112 | + } |
| 113 | + } |
| 114 | + else |
| 115 | + vertex.TexCoords = new Vector2(0.0f, 0.0f); |
| 116 | + |
| 117 | + vertices.Add(vertex); |
| 118 | + } |
| 119 | + // now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices. |
| 120 | + for (uint i = 0; i < mesh->MNumFaces; i++) |
| 121 | + { |
| 122 | + Face face = mesh->MFaces[i]; |
| 123 | + // retrieve all indices of the face and store them in the indices vector |
| 124 | + for (uint j = 0; j < face.MNumIndices; j++) |
| 125 | + indices.Add(face.MIndices[j]); |
| 126 | + } |
| 127 | + // process materials |
| 128 | + Material* material = scene->MMaterials[mesh->MMaterialIndex]; |
| 129 | + // we assume a convention for sampler names in the shaders. Each diffuse texture should be named |
| 130 | + // as 'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER. |
| 131 | + // Same applies to other texture as the following list summarizes: |
| 132 | + // diffuse: texture_diffuseN |
| 133 | + // specular: texture_specularN |
| 134 | + // normal: texture_normalN |
| 135 | + |
| 136 | + // 1. diffuse maps |
| 137 | + var diffuseMaps = LoadMaterialTextures(material, TextureType.Diffuse, "texture_diffuse"); |
| 138 | + if (diffuseMaps.Any()) |
| 139 | + textures.AddRange(diffuseMaps); |
| 140 | + // 2. specular maps |
| 141 | + var specularMaps = LoadMaterialTextures(material, TextureType.Specular, "texture_specular"); |
| 142 | + if (specularMaps.Any()) |
| 143 | + textures.AddRange(specularMaps); |
| 144 | + // 3. normal maps |
| 145 | + var normalMaps = LoadMaterialTextures(material, TextureType.Height, "texture_normal"); |
| 146 | + if (normalMaps.Any()) |
| 147 | + textures.AddRange(normalMaps); |
| 148 | + // 4. height maps |
| 149 | + var heightMaps = LoadMaterialTextures(material, TextureType.Ambient, "texture_height"); |
| 150 | + if (heightMaps.Any()) |
| 151 | + textures.AddRange(heightMaps); |
| 152 | + |
| 153 | + // return a mesh object created from the extracted mesh data |
| 154 | + var result = new Mesh(_gl, BuildVertices(vertices), BuildIndices(indices), textures); |
| 155 | + return result; |
| 156 | + } |
| 157 | + |
| 158 | + private unsafe List<Texture> LoadMaterialTextures(Material* mat, TextureType type, string typeName) |
| 159 | + { |
| 160 | + var textureCount = _assimp.GetMaterialTextureCount(mat, type); |
| 161 | + List<Texture> textures = new List<Texture>(); |
| 162 | + for (uint i = 0; i < textureCount; i++) |
| 163 | + { |
| 164 | + AssimpString path; |
| 165 | + _assimp.GetMaterialTexture(mat, type, i, &path, null, null, null, null, null, null); |
| 166 | + bool skip = false; |
| 167 | + for (int j = 0; j < _texturesLoaded.Count; j++) |
| 168 | + { |
| 169 | + if (_texturesLoaded[j].Path == path) |
| 170 | + { |
| 171 | + textures.Add(_texturesLoaded[j]); |
| 172 | + skip = true; |
| 173 | + break; |
| 174 | + } |
| 175 | + } |
| 176 | + if (!skip) |
| 177 | + { |
| 178 | + var texture = new Texture(_gl, Directory, type); |
| 179 | + texture.Path = path; |
| 180 | + textures.Add(texture); |
| 181 | + _texturesLoaded.Add(texture); |
| 182 | + } |
| 183 | + } |
| 184 | + return textures; |
| 185 | + } |
| 186 | + |
| 187 | + private float[] BuildVertices(List<Vertex> vertexCollection) |
| 188 | + { |
| 189 | + var vertices = new List<float>(); |
| 190 | + |
| 191 | + foreach (var vertex in vertexCollection) |
| 192 | + { |
| 193 | + vertices.Add(vertex.Position.X); |
| 194 | + vertices.Add(vertex.Position.Y); |
| 195 | + vertices.Add(vertex.Position.Z); |
| 196 | + vertices.Add(vertex.TexCoords.X); |
| 197 | + vertices.Add(vertex.TexCoords.Y); |
| 198 | + } |
| 199 | + |
| 200 | + return vertices.ToArray(); |
| 201 | + } |
| 202 | + |
| 203 | + private uint[] BuildIndices(List<uint> indices) |
| 204 | + { |
| 205 | + return indices.ToArray(); |
| 206 | + } |
| 207 | + |
| 208 | + public void Dispose() |
| 209 | + { |
| 210 | + foreach (var mesh in Meshes) |
| 211 | + { |
| 212 | + mesh.Dispose(); |
| 213 | + } |
| 214 | + |
| 215 | + _texturesLoaded = null; |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments