Ejemplo n.º 1
0
        private static int[] DeconstructMaterials(PokemonModel pokemonModel, GLTFRoot gltfRoot)
        {
            var materials = new int[pokemonModel.MaterialsLength];

            for (var i = 0; i < pokemonModel.MaterialsLength; ++i)
            {
                var material     = pokemonModel.Materials(i).GetValueOrDefault();
                var gltfMaterial = new GLTFMaterial
                {
                    PbrMetallicRoughness = new GLTFPBRMetallicRoughness(),
                    Name      = material.Name,
                    AlphaMode = GLTFAlphaMode.BLEND
                };

                for (var j = 0; j < material.TexturesLength; ++j)
                {
                    var texture     = material.Textures(j).GetValueOrDefault();
                    var textureName = $"{pokemonModel.Textures(texture.Id)}.png";
                    var mapping     = texture.Mapping.GetValueOrDefault();
                    var textureInfo = new GLTFTextureInfo
                    {
                        Index = FindOrCreateTexture(gltfRoot, textureName, mapping)
                    };

                    switch (texture.Channel)
                    {
                    case "Col0Tex":
                        gltfMaterial.PbrMetallicRoughness.BaseColorTexture = textureInfo;
                        break;

                    case "EmissionMaskTex":
                        gltfMaterial.EmissiveTexture = textureInfo;
                        gltfMaterial.EmissiveFactor  = new Vector3(1, 1, 1);
                        ;
                        break;

                    case "AmbientTex":
                        gltfMaterial.OcclusionTexture = textureInfo;
                        break;

                    case "NormalMapTex":
                        gltfMaterial.NormalTexture = textureInfo;
                        break;
                    }
                }

                var colorUVScale      = new Vector2(1, 1);
                var colorUVTransation = new Vector2();
                var normalUVScale     = new Vector2();
                for (var j = 0; j < material.ValuesLength; ++j)
                {
                    var value = material.Values(j).GetValueOrDefault();
                    if (string.IsNullOrWhiteSpace(value.Name))
                    {
                        continue;
                    }
                    switch (value.Name)
                    {
                    case "ColorUVScaleU":
                        colorUVScale.X = value.Value;
                        break;

                    case "ColorUVScaleV":
                        colorUVScale.Y = value.Value;
                        break;

                    case "ColorUVTranslateU":
                        colorUVTransation.X += value.Value;
                        break;

                    case "ColorUVTranslateV":
                        colorUVTransation.Y += value.Value;
                        break;

                    case "ColorBaseU":
                        colorUVTransation.X += value.Value;
                        break;

                    case "ColorBaseY":
                        colorUVTransation.Y += value.Value;
                        break;

                    case "NormalMapUVScaleU":
                        normalUVScale.X += value.Value;
                        break;

                    case "NormalMapUVScaleV":
                        normalUVScale.Y += value.Value;
                        break;
                    }
                }

                var colorTransform = new KHRTextureTransform
                {
                    Offset = colorUVTransation,
                    Scale  = colorUVScale
                };
                var normalTransform = new KHRTextureTransform
                {
                    Offset = normalUVScale
                };
                //colorTransform.Insert(gltfMaterial.OcclusionTexture, gltfRoot);
                colorTransform.Insert(gltfMaterial.PbrMetallicRoughness.MetallicRoughnessTexture, gltfRoot);
                //colorTransform.Insert(gltfMaterial.EmissiveTexture, gltfRoot);
                normalTransform.Insert(gltfMaterial.NormalTexture, gltfRoot);

                materials[i] = gltfRoot.Materials.Count;
                gltfRoot.Materials.Add(gltfMaterial);
            }

            return(materials);
        }