public static void LoadSource(this RMesh rmesh, string filename) { AssimpContext context = new AssimpContext(); context.SetConfig(new Assimp.Configs.FBXImportAllMaterialsConfig(true)); context.SetConfig(new Assimp.Configs.FBXImportAllGeometryLayersConfig(true)); context.SetConfig(new Assimp.Configs.MultithreadingConfig(2)); context.SetConfig(new Assimp.Configs.FBXStrictModeConfig(false)); if (!context.IsImportFormatSupported(Path.GetExtension(filename))) { throw new ReactorException("Attempted to load a model that Assimp doesn't know how to load"); } LogStream log = new LogStream((msg, user) => { RLog.Info(msg.Remove(msg.Length - 2)); }); log.Attach(); int platform = (int)Environment.OSVersion.Platform; Scene scene = context.ImportFile(filename, PostProcessSteps.FindInvalidData | PostProcessSteps.GenerateSmoothNormals | PostProcessSteps.Triangulate | PostProcessSteps.GenerateUVCoords | PostProcessSteps.CalculateTangentSpace | PostProcessSteps.PreTransformVertices); if (scene.HasMeshes) { foreach (Mesh mesh in scene.Meshes) { if (!mesh.HasVertices) { continue; } RMeshPart rmeshpart = RMeshPart.Create <RMeshPart> (); RVertexData [] data = new RVertexData [mesh.VertexCount]; List <int> indicesList = new List <int> (); if (mesh.HasFaces) { foreach (Face face in mesh.Faces) { indicesList.AddRange(face.Indices.ToArray()); foreach (int index in face.Indices) { Vector3D p = mesh.Vertices [index]; data [index].Position = new Vector3(p.X, p.Y, p.Z); if (mesh.HasTextureCoords(0)) { Vector3D t = mesh.TextureCoordinateChannels [0] [index]; data [index].TexCoord = new Vector2(t.X, -t.Y); } if (mesh.HasNormals) { Vector3D n = mesh.Normals [index]; data [index].Normal = new Vector3(n.X, n.Y, n.Z); } if (mesh.HasTangentBasis) { Vector3D b = mesh.BiTangents [index]; Vector3D t = mesh.Tangents [index]; data [index].Bitangent = new Vector3(b.X, b.Y, b.Z); data [index].Tangent = new Vector3(t.X, t.Y, t.Z); } } } } RVertexBuffer vbuffer = new RVertexBuffer(typeof(RVertexData), mesh.VertexCount, RBufferUsage.WriteOnly, true); RIndexBuffer ibuffer = new RIndexBuffer(typeof(int), indicesList.Count, RBufferUsage.WriteOnly); ibuffer.SetData(indicesList.ToArray()); vbuffer.SetData <RVertexData> (data); #if WINDOWS var separator = "\\"; #else var separator = "/"; #endif rmeshpart.VertexBuffer = vbuffer; rmeshpart.IndexBuffer = ibuffer; RMaterial material = new RMaterial(rmesh.Name + ":Material"); if (scene.HasMaterials) { Material mat = scene.Materials[mesh.MaterialIndex]; material.Shininess = mat.Shininess; material.SetColor(RMaterialColor.DIFFUSE, new RColor(mat.ColorDiffuse.R, mat.ColorDiffuse.G, mat.ColorDiffuse.B, mat.ColorDiffuse.A)); if (mat.HasTextureDiffuse) { var texFileName = Path.GetFileName(mat.TextureDiffuse.FilePath.Replace("\\", separator).Replace("/", separator)); RTexture2D tex = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower()); tex.Bind(); tex.GenerateMipmaps(); tex.SetTextureMagFilter(RTextureMagFilter.Linear); tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear); tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat); tex.Unbind(); material.SetTexture(RTextureLayer.DIFFUSE, tex); } if (mat.HasTextureNormal) { var texFileName = Path.GetFileName(mat.TextureNormal.FilePath.Replace("\\", separator).Replace("/", separator)); RTexture2D tex = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower()); tex.Bind(); tex.GenerateMipmaps(); tex.SetTextureMagFilter(RTextureMagFilter.Linear); tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear); tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat); tex.Unbind(); material.SetTexture(RTextureLayer.NORMAL, tex); } if (mat.HasTextureAmbient) { var texFileName = Path.GetFileName(mat.TextureAmbient.FilePath.Replace("\\", separator).Replace("/", separator)); RTexture2D tex = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower()); tex.SetTextureMagFilter(RTextureMagFilter.Linear); tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear); tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat); material.SetTexture(RTextureLayer.AMBIENT, tex); } if (mat.HasTextureHeight) { var texFileName = Path.GetFileName(mat.TextureHeight.FilePath.Replace("\\", separator).Replace("/", separator)); RTexture2D tex = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower()); tex.SetTextureMagFilter(RTextureMagFilter.Linear); tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear); tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat); material.SetTexture(RTextureLayer.HEIGHT, tex); } if (mat.HasTextureEmissive) { var texFileName = Path.GetFileName(mat.TextureEmissive.FilePath.Replace("\\", separator).Replace("/", separator)); RTexture2D tex = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower()); tex.SetTextureMagFilter(RTextureMagFilter.Linear); tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear); tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat); material.SetTexture(RTextureLayer.GLOW, tex); } if (mat.HasTextureSpecular) { var texFileName = Path.GetFileName(mat.TextureSpecular.FilePath.Replace("\\", separator).Replace("/", separator)); RTexture2D tex = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower()); tex.SetTextureMagFilter(RTextureMagFilter.Linear); tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear); tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat); material.SetTexture(RTextureLayer.SPECULAR, tex); } } rmeshpart.Material = material; rmesh.Parts.Add(rmeshpart); } //return rmesh; } //return null; if (rmesh.Parts.Count == 0) { throw new ReactorException("Attempted to load a model when Assimp couldn't find any verticies!"); } context.Dispose(); }
public void CreateBox(Vector3 Center, Vector3 Size, bool FlipNormals) { RVertexData[] vertices = new RVertexData[36]; // Calculate the position of the vertices on the top face. Vector3 topLeftFront = Position + new Vector3(-1.0f, 1.0f, -1.0f) * Size; Vector3 topLeftBack = Position + new Vector3(-1.0f, 1.0f, 1.0f) * Size; Vector3 topRightFront = Position + new Vector3(1.0f, 1.0f, -1.0f) * Size; Vector3 topRightBack = Position + new Vector3(1.0f, 1.0f, 1.0f) * Size; // Calculate the position of the vertices on the bottom face. Vector3 btmLeftFront = Position + new Vector3(-1.0f, -1.0f, -1.0f) * Size; Vector3 btmLeftBack = Position + new Vector3(-1.0f, -1.0f, 1.0f) * Size; Vector3 btmRightFront = Position + new Vector3(1.0f, -1.0f, -1.0f) * Size; Vector3 btmRightBack = Position + new Vector3(1.0f, -1.0f, 1.0f) * Size; // Normal vectors for each face (needed for lighting / display) Vector3 normalFront = new Vector3(0.0f, 0.0f, -1.0f) * Size; Vector3 normalBack = new Vector3(0.0f, 0.0f, 1.0f) * Size; Vector3 normalTop = new Vector3(0.0f, 1.0f, 0.0f) * Size; Vector3 normalBottom = new Vector3(0.0f, -1.0f, 0.0f) * Size; Vector3 normalLeft = new Vector3(1.0f, 0.0f, 0.0f) * Size; Vector3 normalRight = new Vector3(-1.0f, 0.0f, 0.0f) * Size; // UV texture coordinates Vector2 textureTopLeft = new Vector2(1.0f * Size.X, 0.0f * Size.Y); Vector2 textureTopRight = new Vector2(0.0f * Size.X, 0.0f * Size.Y); Vector2 textureBottomLeft = new Vector2(1.0f * Size.X, 1.0f * Size.Y); Vector2 textureBottomRight = new Vector2(0.0f * Size.X, 1.0f * Size.Y); // Add the vertices for the FRONT face. vertices[0] = new RVertexData(topLeftFront, normalFront, Vector3.Zero, Vector3.Zero, textureTopLeft); vertices[1] = new RVertexData(btmLeftFront, normalFront, Vector3.Zero, Vector3.Zero, textureBottomLeft); vertices[2] = new RVertexData(topRightFront, normalFront, Vector3.Zero, Vector3.Zero, textureTopRight); vertices[3] = new RVertexData(btmLeftFront, normalFront, Vector3.Zero, Vector3.Zero, textureBottomLeft); vertices[4] = new RVertexData(btmRightFront, normalFront, Vector3.Zero, Vector3.Zero, textureBottomRight); vertices[5] = new RVertexData(topRightFront, normalFront, Vector3.Zero, Vector3.Zero, textureTopRight); // Add the vertices for the BACK face. vertices[6] = new RVertexData(topLeftBack, normalBack, Vector3.Zero, Vector3.Zero, textureTopRight); vertices[7] = new RVertexData(topRightBack, normalBack, Vector3.Zero, Vector3.Zero, textureTopLeft); vertices[8] = new RVertexData(btmLeftBack, normalBack, Vector3.Zero, Vector3.Zero, textureBottomRight); vertices[9] = new RVertexData(btmLeftBack, normalBack, Vector3.Zero, Vector3.Zero, textureBottomRight); vertices[10] = new RVertexData(topRightBack, normalBack, Vector3.Zero, Vector3.Zero, textureTopLeft); vertices[11] = new RVertexData(btmRightBack, normalBack, Vector3.Zero, Vector3.Zero, textureBottomLeft); // Add the vertices for the TOP face. vertices[12] = new RVertexData(topLeftFront, normalTop, Vector3.Zero, Vector3.Zero, textureBottomLeft); vertices[13] = new RVertexData(topRightBack, normalTop, Vector3.Zero, Vector3.Zero, textureTopRight); vertices[14] = new RVertexData(topLeftBack, normalTop, Vector3.Zero, Vector3.Zero, textureTopLeft); vertices[15] = new RVertexData(topLeftFront, normalTop, Vector3.Zero, Vector3.Zero, textureBottomLeft); vertices[16] = new RVertexData(topRightFront, normalTop, Vector3.Zero, Vector3.Zero, textureBottomRight); vertices[17] = new RVertexData(topRightBack, normalTop, Vector3.Zero, Vector3.Zero, textureTopRight); // Add the vertices for the BOTTOM face. vertices[18] = new RVertexData(btmLeftFront, normalBottom, Vector3.Zero, Vector3.Zero, textureTopLeft); vertices[19] = new RVertexData(btmLeftBack, normalBottom, Vector3.Zero, Vector3.Zero, textureBottomLeft); vertices[20] = new RVertexData(btmRightBack, normalBottom, Vector3.Zero, Vector3.Zero, textureBottomRight); vertices[21] = new RVertexData(btmLeftFront, normalBottom, Vector3.Zero, Vector3.Zero, textureTopLeft); vertices[22] = new RVertexData(btmRightBack, normalBottom, Vector3.Zero, Vector3.Zero, textureBottomRight); vertices[23] = new RVertexData(btmRightFront, normalBottom, Vector3.Zero, Vector3.Zero, textureTopRight); // Add the vertices for the LEFT face. vertices[24] = new RVertexData(topLeftFront, normalLeft, Vector3.Zero, Vector3.Zero, textureTopRight); vertices[25] = new RVertexData(btmLeftBack, normalLeft, Vector3.Zero, Vector3.Zero, textureBottomLeft); vertices[26] = new RVertexData(btmLeftFront, normalLeft, Vector3.Zero, Vector3.Zero, textureBottomRight); vertices[27] = new RVertexData(topLeftBack, normalLeft, Vector3.Zero, Vector3.Zero, textureTopLeft); vertices[28] = new RVertexData(btmLeftBack, normalLeft, Vector3.Zero, Vector3.Zero, textureBottomLeft); vertices[29] = new RVertexData(topLeftFront, normalLeft, Vector3.Zero, Vector3.Zero, textureTopRight); // Add the vertices for the RIGHT face. vertices[30] = new RVertexData(topRightFront, normalRight, Vector3.Zero, Vector3.Zero, textureTopLeft); vertices[31] = new RVertexData(btmRightFront, normalRight, Vector3.Zero, Vector3.Zero, textureBottomLeft); vertices[32] = new RVertexData(btmRightBack, normalRight, Vector3.Zero, Vector3.Zero, textureBottomRight); vertices[33] = new RVertexData(topRightBack, normalRight, Vector3.Zero, Vector3.Zero, textureTopRight); vertices[34] = new RVertexData(topRightFront, normalRight, Vector3.Zero, Vector3.Zero, textureTopLeft); vertices[35] = new RVertexData(btmRightBack, normalRight, Vector3.Zero, Vector3.Zero, textureBottomRight); if (FlipNormals) { for (int i = 0; i < 36; i++) { vertices[i].Normal *= -1.0f; } } VertexBuffer = new RVertexBuffer(typeof(RVertexData), vertices.Length, RBufferUsage.WriteOnly); VertexBuffer.SetData <RVertexData>(vertices); vertices = null; vertCount = 36; this.Position = Center; }