< Summary

Line coverage
100%
Covered lines: 150
Uncovered lines: 0
Coverable lines: 150
Total lines: 336
Line coverage: 100%
Branch coverage
100%
Covered branches: 103
Total branches: 103
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Write(...)100%22100%
WriteHeaderText(...)100%44100%
WriteMaterial(...)100%4646100%
WriteColor(...)100%66100%
WriteMap(...)100%4545100%

File(s)

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.Media.WavefrontObj/dbbfcd213b7c54f49194f82fcd56e790cca193c3/JeremyAnsel.Media.WavefrontObj/JeremyAnsel.Media.WavefrontObj/ObjMaterialFileWriter.cs

#LineLine coverage
 1// <copyright file="ObjMaterialFileWriter.cs" company="Jérémy Ansel">
 2// Copyright (c) 2017, 2019 Jérémy Ansel
 3// </copyright>
 4// <license>
 5// Licensed under the MIT license. See LICENSE.txt
 6// </license>
 7
 8using System.Globalization;
 9
 10namespace JeremyAnsel.Media.WavefrontObj
 11{
 12    internal static class ObjMaterialFileWriter
 13    {
 14        public static void Write(ObjMaterialFile mtl, StreamWriter stream)
 15        {
 19816            WriteHeaderText(mtl, stream);
 17
 78018            foreach (ObjMaterial material in mtl.Materials)
 19            {
 19220                WriteMaterial(material, stream);
 21            }
 19822        }
 23
 24        private static void WriteHeaderText(ObjMaterialFile mtl, StreamWriter stream)
 25        {
 19826            if (string.IsNullOrEmpty(mtl.HeaderText))
 27            {
 19528                return;
 29            }
 30
 331            string[] headerLines = mtl.HeaderText!.Split('\n');
 32
 1833            foreach (string line in headerLines)
 34            {
 635                stream.Write('#');
 636                stream.WriteLine(line);
 37            }
 338        }
 39
 40        private static void WriteMaterial(ObjMaterial material, StreamWriter stream)
 41        {
 19242            stream.WriteLine("newmtl {0}", material.Name);
 43
 19244            if (material.AmbientColor != null)
 45            {
 1246                WriteColor("Ka", material.AmbientColor, stream);
 47            }
 48
 19249            if (material.DiffuseColor != null)
 50            {
 1251                WriteColor("Kd", material.DiffuseColor, stream);
 52            }
 53
 19254            if (material.EmissiveColor != null)
 55            {
 1256                WriteColor("Ke", material.EmissiveColor, stream);
 57            }
 58
 19259            if (material.SpecularColor != null)
 60            {
 1261                WriteColor("Ks", material.SpecularColor, stream);
 62            }
 63
 19264            if (material.TransmissionColor != null)
 65            {
 1266                WriteColor("Tf", material.TransmissionColor, stream);
 67            }
 68
 19269            stream.WriteLine("illum {0}", material.IlluminationModel);
 70
 19271            stream.Write("d");
 19272            if (material.IsHaloDissolve)
 73            {
 374                stream.Write(" -halo");
 75            }
 76
 19277            stream.Write(' ');
 19278            stream.WriteLine(material.DissolveFactor.ToString("F6", CultureInfo.InvariantCulture));
 79
 19280            stream.Write("Ns ");
 19281            stream.WriteLine(material.SpecularExponent.ToString("F6", CultureInfo.InvariantCulture));
 82
 19283            stream.WriteLine("sharpness {0}", material.Sharpness);
 84
 19285            stream.Write("Ni ");
 19286            stream.WriteLine(material.OpticalDensity.ToString("F6", CultureInfo.InvariantCulture));
 87
 19288            stream.Write("map_aat ");
 19289            if (material.IsAntiAliasingEnabled)
 90            {
 391                stream.WriteLine("on");
 92            }
 93            else
 94            {
 18995                stream.WriteLine("off");
 96            }
 97
 19298            if (material.AmbientMap != null)
 99            {
 54100                WriteMap("map_Ka", material.AmbientMap, stream);
 101            }
 102
 192103            if (material.DiffuseMap != null)
 104            {
 3105                WriteMap("map_Kd", material.DiffuseMap, stream);
 106            }
 107
 192108            if (material.EmissiveMap != null)
 109            {
 3110                WriteMap("map_Ke", material.EmissiveMap, stream);
 111            }
 112
 192113            if (material.SpecularMap != null)
 114            {
 3115                WriteMap("map_Ks", material.SpecularMap, stream);
 116            }
 117
 192118            if (material.SpecularExponentMap != null)
 119            {
 3120                WriteMap("map_Ns", material.SpecularExponentMap, stream);
 121            }
 122
 192123            if (material.DissolveMap != null)
 124            {
 3125                WriteMap("map_d", material.DissolveMap, stream);
 126            }
 127
 192128            if (material.DecalMap != null)
 129            {
 3130                WriteMap("decal", material.DecalMap, stream);
 131            }
 132
 192133            if (material.DispMap != null)
 134            {
 3135                WriteMap("disp", material.DispMap, stream);
 136            }
 137
 192138            if (material.BumpMap != null)
 139            {
 3140                WriteMap("bump", material.BumpMap, stream);
 141            }
 142
 192143            if (material.ReflectionMap.Sphere != null)
 144            {
 3145                WriteMap("refl -type sphere", material.ReflectionMap.Sphere, stream);
 146            }
 147
 192148            if (material.ReflectionMap.CubeTop != null)
 149            {
 3150                WriteMap("refl -type cube_top", material.ReflectionMap.CubeTop, stream);
 151            }
 152
 192153            if (material.ReflectionMap.CubeBottom != null)
 154            {
 3155                WriteMap("refl -type cube_bottom", material.ReflectionMap.CubeBottom, stream);
 156            }
 157
 192158            if (material.ReflectionMap.CubeFront != null)
 159            {
 3160                WriteMap("refl -type cube_front", material.ReflectionMap.CubeFront, stream);
 161            }
 162
 192163            if (material.ReflectionMap.CubeBack != null)
 164            {
 3165                WriteMap("refl -type cube_back", material.ReflectionMap.CubeBack, stream);
 166            }
 167
 192168            if (material.ReflectionMap.CubeLeft != null)
 169            {
 3170                WriteMap("refl -type cube_left", material.ReflectionMap.CubeLeft, stream);
 171            }
 172
 192173            if (material.ReflectionMap.CubeRight != null)
 174            {
 3175                WriteMap("refl -type cube_right", material.ReflectionMap.CubeRight, stream);
 176            }
 192177        }
 178
 179        private static void WriteColor(string statement, ObjMaterialColor color, StreamWriter stream)
 180        {
 60181            stream.Write(statement);
 182
 60183            if (color.IsSpectral)
 184            {
 30185                stream.Write(" spectral ");
 30186                stream.Write(color.SpectralFileName);
 187
 30188                if (color.SpectralFactor != 1.0f)
 189                {
 15190                    stream.Write(' ');
 15191                    stream.Write(color.SpectralFactor.ToString("F6", CultureInfo.InvariantCulture));
 192                }
 193
 30194                stream.WriteLine();
 195            }
 196            else
 197            {
 30198                if (color.UseXYZColorSpace)
 199                {
 15200                    stream.Write(" xyz");
 201                }
 202
 30203                stream.WriteLine(
 30204                    " {0} {1} {2}",
 30205                    color.Color.X.ToString("F6", CultureInfo.InvariantCulture),
 30206                    color.Color.Y.ToString("F6", CultureInfo.InvariantCulture),
 30207                    color.Color.Z.ToString("F6", CultureInfo.InvariantCulture));
 208            }
 30209        }
 210
 211        private static void WriteMap(string statement, ObjMaterialMap map, StreamWriter stream)
 212        {
 213            // TODO: write tests
 214
 99215            stream.Write(statement);
 216
 99217            if (!map.IsHorizontalBlendingEnabled)
 218            {
 3219                stream.Write(" -blenu off");
 220            }
 221
 99222            if (!map.IsVerticalBlendingEnabled)
 223            {
 3224                stream.Write(" -blenv off");
 225            }
 226
 99227            if (map.BumpMultiplier != 0.0f)
 228            {
 3229                stream.Write(" -bm ");
 3230                stream.Write(map.BumpMultiplier.ToString("F6", CultureInfo.InvariantCulture));
 231            }
 232
 99233            if (map.Boost != 0.0f)
 234            {
 3235                stream.Write(" -boost ");
 3236                stream.Write(map.Boost.ToString("F6", CultureInfo.InvariantCulture));
 237            }
 238
 99239            if (map.IsColorCorrectionEnabled)
 240            {
 3241                stream.Write(" -cc on");
 242            }
 243
 99244            if (map.IsClampingEnabled)
 245            {
 3246                stream.Write(" -clamp on");
 247            }
 248
 99249            if (map.ScalarChannel != ObjMapChannel.Luminance)
 250            {
 18251                stream.Write(" -imfchan ");
 252
 18253                switch (map.ScalarChannel)
 254                {
 255                    case ObjMapChannel.Red:
 3256                        stream.Write("r");
 3257                        break;
 258
 259                    case ObjMapChannel.Green:
 3260                        stream.Write("g");
 3261                        break;
 262
 263                    case ObjMapChannel.Blue:
 3264                        stream.Write("b");
 3265                        break;
 266
 267                    case ObjMapChannel.Matte:
 3268                        stream.Write("m");
 3269                        break;
 270
 271                    case ObjMapChannel.Depth:
 3272                        stream.Write("z");
 3273                        break;
 274
 275                    default:
 3276                        stream.Write("l");
 277                        break;
 278                }
 279            }
 280
 99281            if (map.ModifierBase != 0.0f || map.ModifierGain != 1.0f)
 282            {
 3283                stream.Write(" -mm ");
 3284                stream.Write(map.ModifierBase.ToString("F6", CultureInfo.InvariantCulture));
 3285                stream.Write(' ');
 3286                stream.Write(map.ModifierGain.ToString("F6", CultureInfo.InvariantCulture));
 287            }
 288
 99289            ObjVector3 offset = map.Offset;
 290
 99291            if (offset.X != 0.0f || offset.Y != 0.0f || offset.Z != 0.0f)
 292            {
 3293                stream.Write(" -o ");
 3294                stream.Write(offset.X.ToString("F6", CultureInfo.InvariantCulture));
 3295                stream.Write(' ');
 3296                stream.Write(offset.Y.ToString("F6", CultureInfo.InvariantCulture));
 3297                stream.Write(' ');
 3298                stream.Write(offset.Z.ToString("F6", CultureInfo.InvariantCulture));
 299            }
 300
 99301            ObjVector3 scale = map.Scale;
 302
 99303            if (scale.X != 1.0f || scale.Y != 1.0f || scale.Z != 1.0f)
 304            {
 3305                stream.Write(" -s ");
 3306                stream.Write(scale.X.ToString("F6", CultureInfo.InvariantCulture));
 3307                stream.Write(' ');
 3308                stream.Write(scale.Y.ToString("F6", CultureInfo.InvariantCulture));
 3309                stream.Write(' ');
 3310                stream.Write(scale.Z.ToString("F6", CultureInfo.InvariantCulture));
 311            }
 312
 99313            ObjVector3 turbulence = map.Turbulence;
 314
 99315            if (turbulence.X != 0.0f || turbulence.Y != 0.0f || turbulence.Z != 0.0f)
 316            {
 3317                stream.Write(" -t ");
 3318                stream.Write(turbulence.X.ToString("F6", CultureInfo.InvariantCulture));
 3319                stream.Write(' ');
 3320                stream.Write(turbulence.Y.ToString("F6", CultureInfo.InvariantCulture));
 3321                stream.Write(' ');
 3322                stream.Write(turbulence.Z.ToString("F6", CultureInfo.InvariantCulture));
 323            }
 324
 99325            if (map.TextureResolution != 0)
 326            {
 3327                stream.Write(" -texres ");
 3328                stream.Write(map.TextureResolution);
 329            }
 330
 99331            stream.Write(' ');
 99332            stream.WriteLine(map.FileName);
 99333        }
 334    }
 335}
 336