예제 #1
0
        private static void ProcessAssimpNodeMeshesRecursively(Ai.Node aiNode, Ai.Scene aiScene, Dictionary <string, NodeInfo> nodeLookup, ref int nextBoneIndex, Dictionary <int, List <int> > nodeToBoneIndices, List <Matrix4x4> boneInverseBindMatrices, List <Vector3> transformedVertices, ModelConverterOptions options)
        {
            if (aiNode.HasMeshes)
            {
                var nodeInfo           = nodeLookup[AssimpConverterCommon.UnescapeName(aiNode.Name)];
                var node               = nodeInfo.Node;
                var nodeWorldTransform = node.WorldTransform;
                Matrix4x4.Invert(nodeWorldTransform, out var nodeInverseWorldTransform);

                foreach (var aiMeshIndex in aiNode.MeshIndices)
                {
                    var aiMesh     = aiScene.Meshes[aiMeshIndex];
                    var aiMaterial = aiScene.Materials[aiMesh.MaterialIndex];
                    var geometry   = ConvertAssimpMeshToGeometry(aiMesh, aiMaterial, nodeLookup, ref nextBoneIndex, nodeToBoneIndices, boneInverseBindMatrices, ref nodeWorldTransform, ref nodeInverseWorldTransform, transformedVertices, options);

                    if (!nodeInfo.IsMeshAttachment)
                    {
                        node.Attachments.Add(new NodeMeshAttachment(geometry));
                    }
                    else
                    {
                        node.Parent.Attachments.Add(new NodeMeshAttachment(geometry));
                        node.Parent.RemoveChildNode(node);
                    }
                }
            }

            foreach (var aiNodeChild in aiNode.Children)
            {
                ProcessAssimpNodeMeshesRecursively(aiNodeChild, aiScene, nodeLookup, ref nextBoneIndex, nodeToBoneIndices, boneInverseBindMatrices, transformedVertices, options);
            }
        }
예제 #2
0
        private static TextureInfo ConvertTexture(Ai.TextureSlot aiTextureSlot, string baseDirectoryPath)
        {
            var relativeFilePath = aiTextureSlot.FilePath;
            var fullFilePath     = Path.GetFullPath(Path.Combine(baseDirectoryPath, relativeFilePath));
            var textureName      = AssimpConverterCommon.UnescapeName(Path.GetFileNameWithoutExtension(relativeFilePath) + ".dds");

            Texture texture;

            if (!File.Exists(fullFilePath))
            {
                texture = Texture.CreateDefaultTexture(textureName);
            }
            else if (relativeFilePath.EndsWith(".dds", StringComparison.InvariantCultureIgnoreCase))
            {
                texture = new Texture(textureName, TextureFormat.DDS, File.ReadAllBytes(fullFilePath));
            }
            else
            {
                var bitmap = new Bitmap(fullFilePath);
                texture = TextureEncoder.Encode(textureName, TextureFormat.DDS, bitmap);
            }

            return(TextureInfo.GetTextureInfo(texture));
        }
예제 #3
0
        private static Mesh ConvertAssimpMeshToGeometry(Ai.Mesh aiMesh, Ai.Material material, Dictionary <string, NodeInfo> nodeLookup, ref int nextBoneIndex, Dictionary <int, List <int> > nodeToBoneIndices, List <Matrix4x4> boneInverseBindMatrices, ref Matrix4x4 nodeWorldTransform, ref Matrix4x4 nodeInverseWorldTransform, List <Vector3> transformedVertices, ModelConverterOptions options)
        {
            if (!aiMesh.HasVertices)
            {
                throw new Exception("Assimp mesh has no vertices");
            }

            var geometry = new Mesh();
            var geometryTransformedVertices = new Vector3[aiMesh.VertexCount];

            geometry.Vertices = aiMesh.Vertices
                                .Select(x => new Vector3(x.X, x.Y, x.Z))
                                .ToArray();

            for (int i = 0; i < geometry.Vertices.Length; i++)
            {
                geometryTransformedVertices[i] = Vector3.Transform(geometry.Vertices[i], nodeWorldTransform);
            }

            transformedVertices.AddRange(geometryTransformedVertices);

            if (aiMesh.HasNormals)
            {
                geometry.Normals = aiMesh.Normals
                                   .Select(x => new Vector3(x.X, x.Y, x.Z))
                                   .ToArray();
            }

            if (aiMesh.HasTextureCoords(0))
            {
                geometry.TexCoordsChannel0 = aiMesh.TextureCoordinateChannels[0]
                                             .Select(x => new Vector2(x.X, x.Y))
                                             .ToArray();
            }

            if (aiMesh.HasTextureCoords(1))
            {
                geometry.TexCoordsChannel1 = aiMesh.TextureCoordinateChannels[1]
                                             .Select(x => new Vector2(x.X, x.Y))
                                             .ToArray();
            }

            if (aiMesh.HasTextureCoords(2))
            {
                geometry.TexCoordsChannel2 = aiMesh.TextureCoordinateChannels[2]
                                             .Select(x => new Vector2(x.X, x.Y))
                                             .ToArray();
            }

            if (aiMesh.HasVertexColors(0))
            {
                geometry.ColorChannel0 = aiMesh.VertexColorChannels[0]
                                         .Select(x => ( uint )(( byte )(x.B * 255f) | ( byte )(x.G * 255f) << 8 | ( byte )(x.R * 255f) << 16 | ( byte )(x.A * 255f) << 24))
                                         .ToArray();
            }
            else if (options.GenerateVertexColors)
            {
                geometry.ColorChannel0 = new uint[geometry.VertexCount];
                for (int i = 0; i < geometry.ColorChannel0.Length; i++)
                {
                    geometry.ColorChannel0[i] = 0xFFFFFFFF;
                }
            }

            if (aiMesh.HasVertexColors(1))
            {
                geometry.ColorChannel1 = aiMesh.VertexColorChannels[1]
                                         .Select(x => ( uint )(( byte )(x.B * 255f) | ( byte )(x.G * 255f) << 8 | ( byte )(x.R * 255f) << 16 | ( byte )(x.A * 255f) << 24))
                                         .ToArray();
            }

            if (aiMesh.HasFaces)
            {
                geometry.TriangleIndexFormat = aiMesh.VertexCount <= ushort.MaxValue ? TriangleIndexFormat.UInt16 : TriangleIndexFormat.UInt32;
                geometry.Triangles           = aiMesh.Faces
                                               .Select(x => new Triangle(( uint )x.Indices[0], ( uint )x.Indices[1], ( uint )x.Indices[2]))
                                               .ToArray();
            }

            if (aiMesh.HasBones)
            {
                geometry.VertexWeights = new VertexWeight[geometry.VertexCount];
                for (int i = 0; i < geometry.VertexWeights.Length; i++)
                {
                    geometry.VertexWeights[i].Indices = new byte[4];
                    geometry.VertexWeights[i].Weights = new float[4];
                }

                var vertexWeightCounts = new int[geometry.VertexCount];

                for (var i = 0; i < aiMesh.Bones.Count; i++)
                {
                    var aiMeshBone = aiMesh.Bones[i];

                    // Find node index for the bone
                    var boneLookupData = nodeLookup[AssimpConverterCommon.UnescapeName(aiMeshBone.Name)];
                    int nodeIndex      = boneLookupData.Index;

                    // Calculate inverse bind matrix
                    var boneNode   = boneLookupData.Node;
                    var bindMatrix = boneNode.WorldTransform * nodeInverseWorldTransform;

                    if (options.ConvertSkinToZUp)
                    {
                        bindMatrix *= YToZUpMatrix;
                    }

                    Matrix4x4.Invert(bindMatrix, out var inverseBindMatrix);

                    // Get bone index
                    int boneIndex;
                    if (!nodeToBoneIndices.TryGetValue(nodeIndex, out var boneIndices))
                    {
                        // No entry for the node was found, so we add a new one
                        boneIndex = nextBoneIndex++;
                        nodeToBoneIndices.Add(nodeIndex, new List <int>()
                        {
                            boneIndex
                        });
                        boneInverseBindMatrices.Add(inverseBindMatrix);
                    }
                    else
                    {
                        // Entry for the node was found
                        // Try to find the bone index based on whether the inverse bind matrix matches
                        boneIndex = -1;
                        foreach (int index in boneIndices)
                        {
                            if (boneInverseBindMatrices[index].Equals(inverseBindMatrix))
                            {
                                boneIndex = index;
                            }
                        }

                        if (boneIndex == -1)
                        {
                            // None matching inverse bind matrix was found, so we add a new entry
                            boneIndex = nextBoneIndex++;
                            nodeToBoneIndices[nodeIndex].Add(boneIndex);
                            boneInverseBindMatrices.Add(inverseBindMatrix);
                        }
                    }

                    foreach (var aiVertexWeight in aiMeshBone.VertexWeights)
                    {
                        int vertexWeightCount = vertexWeightCounts[aiVertexWeight.VertexID]++;

                        geometry.VertexWeights[aiVertexWeight.VertexID].Indices[vertexWeightCount] = ( byte )boneIndex;
                        geometry.VertexWeights[aiVertexWeight.VertexID].Weights[vertexWeightCount] = aiVertexWeight.Weight;
                    }
                }
            }

            geometry.MaterialName   = AssimpConverterCommon.UnescapeName(material.Name);
            geometry.BoundingBox    = BoundingBox.Calculate(geometry.Vertices);
            geometry.BoundingSphere = BoundingSphere.Calculate(geometry.BoundingBox.Value, geometry.Vertices);
            geometry.Flags         |= GeometryFlags.Flag80000000;

            return(geometry);
        }
예제 #4
0
        private static Node ConvertAssimpNodeRecursively(Assimp.Scene aiScene, Ai.Node aiNode, Dictionary <string, NodeInfo> nodeLookup, ref int nextIndex, ModelConverterOptions options)
        {
            aiNode.Transform.Decompose(out var scale, out var rotation, out var translation);

            // Create node
            var node = new Node(AssimpConverterCommon.UnescapeName(aiNode.Name),
                                new Vector3(translation.X, translation.Y, translation.Z),
                                new Quaternion(rotation.X, rotation.Y, rotation.Z, rotation.W),
                                new Vector3(scale.X, scale.Y, scale.Z));



            if (!IsMeshAttachmentNode(aiNode))
            {
                // Convert properties
                ConvertAssimpMetadataToProperties(aiNode.Metadata, node);

                if (options.SetFullBodyNodeProperties)
                {
                    if (node.Name == "See User Defined Properties")
                    {
                        TryAddProperty(node.Properties, new UserIntProperty("NiSortAdjustNode", 0));
                    }
                    else if (node.Name.EndsWith("root") || node.Name == "Bip01")
                    {
                        TryAddProperty(node.Properties, new UserIntProperty("KFAccumRoot", 0));
                    }
                    else if (sFullBodyObjectNames.Contains(node.Name))
                    {
                        TryAddFullBodyObjectProperties(node.Properties, node.Name);
                    }
                }

                if (!nodeLookup.ContainsKey(node.Name))
                {
                    // Add to lookup
                    nodeLookup.Add(node.Name, new NodeInfo(aiNode, node, nextIndex++, false));
                }
                else
                {
                    throw new Exception($"Duplicate node name '{node.Name}'");
                }

                // Is this a camera?
                var index = -1;
                if ((index = aiScene.Cameras.FindIndex(x => x.Name == node.Name)) != -1)
                {
                    var aiCamera = aiScene.Cameras[index];
                    var camera   = new Camera(-aiCamera.Direction.ToNumerics(), aiCamera.Up.ToNumerics(), aiCamera.Position.ToNumerics(),
                                              aiCamera.ClipPlaneNear, aiCamera.ClipPlaneFar, MathHelper.RadiansToDegrees(aiCamera.FieldOfview),
                                              aiCamera.AspectRatio, 0
                                              )
                    {
                        Version = options.Version
                    };

                    node.Attachments.Add(new NodeCameraAttachment(camera));
                }
                else if ((index = aiScene.Lights.FindIndex(x => x.Name == node.Name)) != -1)
                {
                    var aiLight   = aiScene.Lights[index];
                    var lightType = LightType.Point;
                    switch (aiLight.LightType)
                    {
                    case Ai.LightSourceType.Directional:
                        lightType = LightType.Type1;
                        break;

                    case Ai.LightSourceType.Point:
                    case Ai.LightSourceType.Ambient:
                        lightType = LightType.Point;
                        break;

                    case Ai.LightSourceType.Spot:
                        lightType = LightType.Spot;
                        break;
                    }

                    var light = new Light
                    {
                        Version        = options.Version,
                        AmbientColor   = aiLight.ColorAmbient.ToNumerics(),
                        DiffuseColor   = aiLight.ColorDiffuse.ToNumerics(),
                        SpecularColor  = aiLight.ColorSpecular.ToNumerics(),
                        AngleInnerCone = aiLight.AngleInnerCone,
                        AngleOuterCone = aiLight.AngleOuterCone,
                        Type           = lightType,
                        Flags          = LightFlags.Bit1 | LightFlags.Bit2
                    };
                    node.Attachments.Add(new NodeLightAttachment(light));
                }

                // Process children
                foreach (var aiNodeChild in aiNode.Children)
                {
                    if (aiNodeChild.Name == "RootNode")
                    {
                        // For compatibility with old exports
                        // Merge children of 'RootNode' node with actual root node
                        foreach (var aiFakeRootNodeChild in aiNodeChild.Children)
                        {
                            var childNode = ConvertAssimpNodeRecursively(aiScene, aiFakeRootNodeChild, nodeLookup, ref nextIndex, options);
                            node.AddChildNode(childNode);
                        }
                    }
                    else
                    {
                        var childNode = ConvertAssimpNodeRecursively(aiScene, aiNodeChild, nodeLookup, ref nextIndex, options);
                        node.AddChildNode(childNode);
                    }
                }
            }
            else
            {
                nodeLookup.Add(node.Name, new NodeInfo(aiNode, node, -1, true));
            }

            return(node);
        }
예제 #5
0
        private static Material ConvertMaterialAndTextures(Ai.Material aiMaterial, ModelPackConverterOptions options, string baseDirectoryPath, TextureDictionary textureDictionary)
        {
            // Convert all textures
            TextureInfo diffuseTexture = null;

            if (aiMaterial.HasTextureDiffuse)
            {
                diffuseTexture = ConvertTexture(aiMaterial.TextureDiffuse, baseDirectoryPath);
            }

            TextureInfo lightmapTexture = null;

            if (aiMaterial.HasTextureLightMap)
            {
                lightmapTexture = ConvertTexture(aiMaterial.TextureLightMap, baseDirectoryPath);
            }

            TextureInfo displacementTexture = null;

            if (aiMaterial.HasTextureDisplacement)
            {
                displacementTexture = ConvertTexture(aiMaterial.TextureDisplacement, baseDirectoryPath);
            }

            TextureInfo opacityTexture = null;

            if (aiMaterial.HasTextureOpacity)
            {
                opacityTexture = ConvertTexture(aiMaterial.TextureOpacity, baseDirectoryPath);
            }

            TextureInfo normalTexture = null;

            if (aiMaterial.HasTextureNormal)
            {
                normalTexture = ConvertTexture(aiMaterial.TextureNormal, baseDirectoryPath);
            }

            TextureInfo heightTexture = null;

            if (aiMaterial.HasTextureHeight)
            {
                heightTexture = ConvertTexture(aiMaterial.TextureHeight, baseDirectoryPath);
            }

            TextureInfo emissiveTexture = null;

            if (aiMaterial.HasTextureEmissive)
            {
                emissiveTexture = ConvertTexture(aiMaterial.TextureEmissive, baseDirectoryPath);
            }

            TextureInfo ambientTexture = null;

            if (aiMaterial.HasTextureAmbient)
            {
                ambientTexture = ConvertTexture(aiMaterial.TextureAmbient, baseDirectoryPath);
            }

            TextureInfo specularTexture = null;

            if (aiMaterial.HasTextureSpecular)
            {
                specularTexture = ConvertTexture(aiMaterial.TextureSpecular, baseDirectoryPath);
            }

            TextureInfo reflectionTexture = null;

            if (aiMaterial.HasTextureReflection)
            {
                reflectionTexture = ConvertTexture(aiMaterial.TextureReflection, baseDirectoryPath);
            }

            // Convert material
            Material material     = null;
            string   materialName = AssimpConverterCommon.UnescapeName(aiMaterial.Name);

            switch (options.MaterialPreset)
            {
            case MaterialPreset.FieldTerrain:
            {
                if (diffuseTexture != null)
                {
                    textureDictionary.Add(diffuseTexture.Texture);
                    material = MaterialFactory.CreateFieldTerrainMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat));
                }
            }
            break;

            case MaterialPreset.FieldTerrainVertexColors:
            {
                if (diffuseTexture != null)
                {
                    textureDictionary.Add(diffuseTexture.Texture);
                    material = MaterialFactory.CreateFieldTerrainVertexColorsMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat));
                }
            }
            break;

            case MaterialPreset.FieldTerrainCastShadow:
            {
                if (diffuseTexture != null)
                {
                    textureDictionary.Add(diffuseTexture.Texture);
                    material = MaterialFactory.CreateFieldTerrainCastShadowMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat));
                }
            }
            break;

            case MaterialPreset.CharacterSkinP5:
            case MaterialPreset.CharacterSkinFB:
            {
                if (diffuseTexture != null)
                {
                    textureDictionary.Add(diffuseTexture.Texture);
                    string shadowTextureName = diffuseTexture.Name;

                    if (ambientTexture != null)
                    {
                        textureDictionary.Add(ambientTexture.Texture);
                        shadowTextureName = ambientTexture.Name;
                    }

                    // TODO: transparency
                    var hasTransparency = HasAlpha(diffuseTexture.PixelFormat);

                    if (options.MaterialPreset == MaterialPreset.CharacterSkinP5)
                    {
                        material = MaterialFactory.CreateCharacterSkinP5Material(materialName, diffuseTexture.Name, shadowTextureName,
                                                                                 hasTransparency);
                    }
                    else
                    {
                        material = MaterialFactory.CreateCharacterSkinFBMaterial(materialName, diffuseTexture.Name, shadowTextureName,
                                                                                 hasTransparency);
                    }
                }
            }
            break;

            case MaterialPreset.PersonaSkinP5:
            {
                if (diffuseTexture != null)
                {
                    textureDictionary.Add(diffuseTexture.Texture);
                    string shadowTextureName   = diffuseTexture.Name;
                    string specularTextureName = diffuseTexture.Name;

                    if (ambientTexture != null)
                    {
                        textureDictionary.Add(ambientTexture.Texture);
                        shadowTextureName = ambientTexture.Name;
                    }

                    if (specularTexture != null)
                    {
                        textureDictionary.Add(specularTexture.Texture);
                        specularTextureName = specularTexture.Name;
                    }

                    material = MaterialFactory.CreatePersonaSkinP5Material(materialName, diffuseTexture.Name, specularTextureName, shadowTextureName);
                }
            }
            break;

            case MaterialPreset.CharacterClothP4D:
            {
                if (diffuseTexture != null)
                {
                    textureDictionary.Add(diffuseTexture.Texture);
                    material = MaterialFactory.CreateCharacterClothP4DMaterial(materialName, diffuseTexture.Name,
                                                                               HasAlpha(diffuseTexture.PixelFormat));
                }
            }
            break;

            case MaterialPreset.CharacterSkinP3DP5D:
            {
                if (diffuseTexture != null)
                {
                    textureDictionary.Add(diffuseTexture.Texture);
                    material = MaterialFactory.CreateCharacterSkinP3DP5DMaterial(materialName, diffuseTexture.Name,
                                                                                 HasAlpha(diffuseTexture.PixelFormat));
                }
            }
            break;
            }

            // Create dummy material if none was created
            if (material == null)
            {
                material = new Material(materialName);
            }

            return(material);
        }