private TextureProvider[] InitTextures(Assimp.Scene aiScene, string filename) { var textureProviders = new TextureProvider[aiScene.MaterialCount]; // Extract the directory part from the file name string directory = new FileInfo(filename).DirectoryName; // Initialize the materials for (uint i = 0; i < aiScene.MaterialCount; i++) { Assimp.Material material = aiScene.Materials[i]; if (material.GetTextureCount(Assimp.TextureType.Diffuse) > 0) { Assimp.TextureSlot slot = material.GetTexture(Assimp.TextureType.Diffuse, 0); string fullname = Path.Combine(directory, slot.FilePath); var provider = new TextureProvider(fullname); textureProviders[i] = provider; } } return(textureProviders); }
internal static Mesh3D LoadFromFileSUB(Device device, Assimp.Scene pScene, InputElement[] ieLayout, List <SceneObject> meshArr) { string sourceTextures = ConfigurationSettings.AppSettings["SourceTextures"]; int iVBufferSize = 0; int iIBufferSize = 0; foreach (SceneObject so in meshArr) { iVBufferSize += (int)so.pMesh.VertexCount; iIBufferSize += (int)so.pMesh.FaceCount * 3; } MeshInputElements10.sNormalMesh[] tVertex = new MeshInputElements10.sNormalMesh[iVBufferSize]; //short[] tIndex = new short[iIBufferSize]; uint[] tIndex = new uint[iIBufferSize]; MeshAttributeRange[] tAttibutes = new MeshAttributeRange[meshArr.Count]; MeshNode[] nodes = new MeshNode[meshArr.Count]; string[] tTextures = new string[meshArr.Count]; // Monitor global poisition in the vertex, index and attribute buffers. int iAttribute = 0; int iBVertex = 0; int iBIndex = 0; int prevVertCount = 0; foreach (SceneObject so in meshArr) { MeshNode n = new MeshNode(); n.attributeId = iAttribute; n.nodeName = so.nodeName; n.parentNodeName = so.parentNodeName; n.transform = toDXMat(so.transform); nodes[iAttribute] = n; MeshAttributeRange pAttrib = new MeshAttributeRange(); pAttrib.Id = iAttribute; pAttrib.VertexStart = iBVertex; pAttrib.FaceStart = iBIndex / 3; Assimp.Material mat = pScene.Materials[so.pMesh.MaterialIndex]; if (mat.GetTextureCount(Assimp.TextureType.Diffuse) > 0) { string inputFilePath = mat.GetTexture(Assimp.TextureType.Diffuse, 0).FilePath.Replace("\\\\", "\\"); string inputFileName = Path.GetFileName(inputFilePath); string ext = Path.GetExtension(inputFileName); string subFolder = inputFilePath.Replace(sourceTextures, "").Split('\\')[1]; string diffOutFile = subFolder + "\\" + inputFileName.Replace(ext, ".dds"); tTextures[iAttribute] = diffOutFile; } // Copy verticies. int iMeshVerts = (int)so.pMesh.VertexCount; for (int iVertex = 0; iVertex < iMeshVerts; ++iVertex) { tVertex[iVertex + iBVertex] = getVert(so, iVertex); } // Increment the vertex count by the number of verticies we just looped over. iBVertex += iMeshVerts; // Copy indicies. int iMeshFaces = (int)so.pMesh.FaceCount; for (int iFace = 0; iFace < iMeshFaces; ++iFace) { uint[] tIndices = so.pMesh.Faces[iFace].Indices; //tIndex[iBIndex++] = Convert.ToInt16(tIndices[0] + (iAttribute == 0 ? 0 : prevVertCount)); //tIndex[iBIndex++] = Convert.ToInt16(tIndices[1] + (iAttribute == 0 ? 0 : prevVertCount)); //tIndex[iBIndex++] = Convert.ToInt16(tIndices[2] + (iAttribute == 0 ? 0 : prevVertCount)); tIndex[iBIndex++] = Convert.ToUInt32(tIndices[0] + (iAttribute == 0 ? 0 : prevVertCount)); tIndex[iBIndex++] = Convert.ToUInt32(tIndices[1] + (iAttribute == 0 ? 0 : prevVertCount)); tIndex[iBIndex++] = Convert.ToUInt32(tIndices[2] + (iAttribute == 0 ? 0 : prevVertCount)); } // Increment the face count by the number of faces we just looped over. prevVertCount += iMeshVerts; pAttrib.FaceCount = iMeshFaces; pAttrib.VertexCount = iMeshVerts; tAttibutes[iAttribute] = pAttrib; iAttribute++; } Mesh3D gm = new Mesh3D(); Mesh output = new Mesh(device, ieLayout, ieLayout[0].SemanticName, iVBufferSize, iIBufferSize / 3, MeshFlags.Has32BitIndices); output.SetAttributeTable(tAttibutes); DataStream verts = new DataStream(iVBufferSize * Marshal.SizeOf(typeof(MeshInputElements10.sNormalMesh)), true, true); //DataStream indicies = new DataStream(iIBufferSize * Marshal.SizeOf(typeof(short)), true, true); DataStream indicies = new DataStream(iIBufferSize * Marshal.SizeOf(typeof(uint)), true, true); verts.WriteRange(tVertex); indicies.WriteRange(tIndex); verts.Position = 0; indicies.Position = 0; output.SetVertexData(0, verts); output.SetIndexData(indicies, iIBufferSize); output.Commit(); verts.Dispose(); indicies.Dispose(); gm.attrTable = tAttibutes; gm.meshObj = output; gm.NumAttributes = meshArr.Count; gm.materials = tTextures; gm.nodes = nodes; if (pScene.HasAnimations) { gm.animations = new List <Mesh3DAnimation>(); foreach (Assimp.Animation a in pScene.Animations) { Mesh3DAnimation anim = new Mesh3DAnimation(); anim.animName = a.Name; anim.duration = a.DurationInTicks; anim.channels = new AnimationChannel[a.NodeAnimationChannelCount]; int x = 0; foreach (Assimp.NodeAnimationChannel c in a.NodeAnimationChannels) { if (c == null) { continue; } AnimationChannel ac = new AnimationChannel(); ac.nodeName = c.NodeName; ac.animKeys = new AnimationKey[c.RotationKeyCount]; for (int i = 0; i < c.PositionKeys.Length; i++) { double time = 0; SceneObject sObj = meshArr[0]; foreach (SceneObject so in meshArr) { if (so.nodeName == c.NodeName) { sObj = so; break; } } time = c.PositionKeys[i].Time; Matrix mat = toDXMat(sObj.transform); Vector3 rotationCenter = new Vector3(mat.M41, mat.M42, mat.M43); mat.Invert(); Vector3 pos = Vector3.TransformCoordinate(toDX(c.PositionKeys[i].Value), mat); Matrix matAnim = Matrix.Transformation(Vector3.Zero, Quaternion.Identity, new Vector3(1, 1, 1), rotationCenter, toDX(c.RotationKeys[i].Value), pos); AnimationKey ak = new AnimationKey(); ak.animMat = matAnim; ak.time = time; ac.animKeys[i] = ak; } anim.channels[x++] = ac; } gm.animations.Add(anim); } } return(gm); }