| | 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 | |
|
| | 8 | | using System.Globalization; |
| | 9 | |
|
| | 10 | | namespace JeremyAnsel.Media.WavefrontObj |
| | 11 | | { |
| | 12 | | internal static class ObjMaterialFileWriter |
| | 13 | | { |
| | 14 | | public static void Write(ObjMaterialFile mtl, StreamWriter stream) |
| | 15 | | { |
| 198 | 16 | | WriteHeaderText(mtl, stream); |
| | 17 | |
|
| 780 | 18 | | foreach (ObjMaterial material in mtl.Materials) |
| | 19 | | { |
| 192 | 20 | | WriteMaterial(material, stream); |
| | 21 | | } |
| 198 | 22 | | } |
| | 23 | |
|
| | 24 | | private static void WriteHeaderText(ObjMaterialFile mtl, StreamWriter stream) |
| | 25 | | { |
| 198 | 26 | | if (string.IsNullOrEmpty(mtl.HeaderText)) |
| | 27 | | { |
| 195 | 28 | | return; |
| | 29 | | } |
| | 30 | |
|
| 3 | 31 | | string[] headerLines = mtl.HeaderText!.Split('\n'); |
| | 32 | |
|
| 18 | 33 | | foreach (string line in headerLines) |
| | 34 | | { |
| 6 | 35 | | stream.Write('#'); |
| 6 | 36 | | stream.WriteLine(line); |
| | 37 | | } |
| 3 | 38 | | } |
| | 39 | |
|
| | 40 | | private static void WriteMaterial(ObjMaterial material, StreamWriter stream) |
| | 41 | | { |
| 192 | 42 | | stream.WriteLine("newmtl {0}", material.Name); |
| | 43 | |
|
| 192 | 44 | | if (material.AmbientColor != null) |
| | 45 | | { |
| 12 | 46 | | WriteColor("Ka", material.AmbientColor, stream); |
| | 47 | | } |
| | 48 | |
|
| 192 | 49 | | if (material.DiffuseColor != null) |
| | 50 | | { |
| 12 | 51 | | WriteColor("Kd", material.DiffuseColor, stream); |
| | 52 | | } |
| | 53 | |
|
| 192 | 54 | | if (material.EmissiveColor != null) |
| | 55 | | { |
| 12 | 56 | | WriteColor("Ke", material.EmissiveColor, stream); |
| | 57 | | } |
| | 58 | |
|
| 192 | 59 | | if (material.SpecularColor != null) |
| | 60 | | { |
| 12 | 61 | | WriteColor("Ks", material.SpecularColor, stream); |
| | 62 | | } |
| | 63 | |
|
| 192 | 64 | | if (material.TransmissionColor != null) |
| | 65 | | { |
| 12 | 66 | | WriteColor("Tf", material.TransmissionColor, stream); |
| | 67 | | } |
| | 68 | |
|
| 192 | 69 | | stream.WriteLine("illum {0}", material.IlluminationModel); |
| | 70 | |
|
| 192 | 71 | | stream.Write("d"); |
| 192 | 72 | | if (material.IsHaloDissolve) |
| | 73 | | { |
| 3 | 74 | | stream.Write(" -halo"); |
| | 75 | | } |
| | 76 | |
|
| 192 | 77 | | stream.Write(' '); |
| 192 | 78 | | stream.WriteLine(material.DissolveFactor.ToString("F6", CultureInfo.InvariantCulture)); |
| | 79 | |
|
| 192 | 80 | | stream.Write("Ns "); |
| 192 | 81 | | stream.WriteLine(material.SpecularExponent.ToString("F6", CultureInfo.InvariantCulture)); |
| | 82 | |
|
| 192 | 83 | | stream.WriteLine("sharpness {0}", material.Sharpness); |
| | 84 | |
|
| 192 | 85 | | stream.Write("Ni "); |
| 192 | 86 | | stream.WriteLine(material.OpticalDensity.ToString("F6", CultureInfo.InvariantCulture)); |
| | 87 | |
|
| 192 | 88 | | stream.Write("map_aat "); |
| 192 | 89 | | if (material.IsAntiAliasingEnabled) |
| | 90 | | { |
| 3 | 91 | | stream.WriteLine("on"); |
| | 92 | | } |
| | 93 | | else |
| | 94 | | { |
| 189 | 95 | | stream.WriteLine("off"); |
| | 96 | | } |
| | 97 | |
|
| 192 | 98 | | if (material.AmbientMap != null) |
| | 99 | | { |
| 54 | 100 | | WriteMap("map_Ka", material.AmbientMap, stream); |
| | 101 | | } |
| | 102 | |
|
| 192 | 103 | | if (material.DiffuseMap != null) |
| | 104 | | { |
| 3 | 105 | | WriteMap("map_Kd", material.DiffuseMap, stream); |
| | 106 | | } |
| | 107 | |
|
| 192 | 108 | | if (material.EmissiveMap != null) |
| | 109 | | { |
| 3 | 110 | | WriteMap("map_Ke", material.EmissiveMap, stream); |
| | 111 | | } |
| | 112 | |
|
| 192 | 113 | | if (material.SpecularMap != null) |
| | 114 | | { |
| 3 | 115 | | WriteMap("map_Ks", material.SpecularMap, stream); |
| | 116 | | } |
| | 117 | |
|
| 192 | 118 | | if (material.SpecularExponentMap != null) |
| | 119 | | { |
| 3 | 120 | | WriteMap("map_Ns", material.SpecularExponentMap, stream); |
| | 121 | | } |
| | 122 | |
|
| 192 | 123 | | if (material.DissolveMap != null) |
| | 124 | | { |
| 3 | 125 | | WriteMap("map_d", material.DissolveMap, stream); |
| | 126 | | } |
| | 127 | |
|
| 192 | 128 | | if (material.DecalMap != null) |
| | 129 | | { |
| 3 | 130 | | WriteMap("decal", material.DecalMap, stream); |
| | 131 | | } |
| | 132 | |
|
| 192 | 133 | | if (material.DispMap != null) |
| | 134 | | { |
| 3 | 135 | | WriteMap("disp", material.DispMap, stream); |
| | 136 | | } |
| | 137 | |
|
| 192 | 138 | | if (material.BumpMap != null) |
| | 139 | | { |
| 3 | 140 | | WriteMap("bump", material.BumpMap, stream); |
| | 141 | | } |
| | 142 | |
|
| 192 | 143 | | if (material.ReflectionMap.Sphere != null) |
| | 144 | | { |
| 3 | 145 | | WriteMap("refl -type sphere", material.ReflectionMap.Sphere, stream); |
| | 146 | | } |
| | 147 | |
|
| 192 | 148 | | if (material.ReflectionMap.CubeTop != null) |
| | 149 | | { |
| 3 | 150 | | WriteMap("refl -type cube_top", material.ReflectionMap.CubeTop, stream); |
| | 151 | | } |
| | 152 | |
|
| 192 | 153 | | if (material.ReflectionMap.CubeBottom != null) |
| | 154 | | { |
| 3 | 155 | | WriteMap("refl -type cube_bottom", material.ReflectionMap.CubeBottom, stream); |
| | 156 | | } |
| | 157 | |
|
| 192 | 158 | | if (material.ReflectionMap.CubeFront != null) |
| | 159 | | { |
| 3 | 160 | | WriteMap("refl -type cube_front", material.ReflectionMap.CubeFront, stream); |
| | 161 | | } |
| | 162 | |
|
| 192 | 163 | | if (material.ReflectionMap.CubeBack != null) |
| | 164 | | { |
| 3 | 165 | | WriteMap("refl -type cube_back", material.ReflectionMap.CubeBack, stream); |
| | 166 | | } |
| | 167 | |
|
| 192 | 168 | | if (material.ReflectionMap.CubeLeft != null) |
| | 169 | | { |
| 3 | 170 | | WriteMap("refl -type cube_left", material.ReflectionMap.CubeLeft, stream); |
| | 171 | | } |
| | 172 | |
|
| 192 | 173 | | if (material.ReflectionMap.CubeRight != null) |
| | 174 | | { |
| 3 | 175 | | WriteMap("refl -type cube_right", material.ReflectionMap.CubeRight, stream); |
| | 176 | | } |
| 192 | 177 | | } |
| | 178 | |
|
| | 179 | | private static void WriteColor(string statement, ObjMaterialColor color, StreamWriter stream) |
| | 180 | | { |
| 60 | 181 | | stream.Write(statement); |
| | 182 | |
|
| 60 | 183 | | if (color.IsSpectral) |
| | 184 | | { |
| 30 | 185 | | stream.Write(" spectral "); |
| 30 | 186 | | stream.Write(color.SpectralFileName); |
| | 187 | |
|
| 30 | 188 | | if (color.SpectralFactor != 1.0f) |
| | 189 | | { |
| 15 | 190 | | stream.Write(' '); |
| 15 | 191 | | stream.Write(color.SpectralFactor.ToString("F6", CultureInfo.InvariantCulture)); |
| | 192 | | } |
| | 193 | |
|
| 30 | 194 | | stream.WriteLine(); |
| | 195 | | } |
| | 196 | | else |
| | 197 | | { |
| 30 | 198 | | if (color.UseXYZColorSpace) |
| | 199 | | { |
| 15 | 200 | | stream.Write(" xyz"); |
| | 201 | | } |
| | 202 | |
|
| 30 | 203 | | stream.WriteLine( |
| 30 | 204 | | " {0} {1} {2}", |
| 30 | 205 | | color.Color.X.ToString("F6", CultureInfo.InvariantCulture), |
| 30 | 206 | | color.Color.Y.ToString("F6", CultureInfo.InvariantCulture), |
| 30 | 207 | | color.Color.Z.ToString("F6", CultureInfo.InvariantCulture)); |
| | 208 | | } |
| 30 | 209 | | } |
| | 210 | |
|
| | 211 | | private static void WriteMap(string statement, ObjMaterialMap map, StreamWriter stream) |
| | 212 | | { |
| | 213 | | // TODO: write tests |
| | 214 | |
|
| 99 | 215 | | stream.Write(statement); |
| | 216 | |
|
| 99 | 217 | | if (!map.IsHorizontalBlendingEnabled) |
| | 218 | | { |
| 3 | 219 | | stream.Write(" -blenu off"); |
| | 220 | | } |
| | 221 | |
|
| 99 | 222 | | if (!map.IsVerticalBlendingEnabled) |
| | 223 | | { |
| 3 | 224 | | stream.Write(" -blenv off"); |
| | 225 | | } |
| | 226 | |
|
| 99 | 227 | | if (map.BumpMultiplier != 0.0f) |
| | 228 | | { |
| 3 | 229 | | stream.Write(" -bm "); |
| 3 | 230 | | stream.Write(map.BumpMultiplier.ToString("F6", CultureInfo.InvariantCulture)); |
| | 231 | | } |
| | 232 | |
|
| 99 | 233 | | if (map.Boost != 0.0f) |
| | 234 | | { |
| 3 | 235 | | stream.Write(" -boost "); |
| 3 | 236 | | stream.Write(map.Boost.ToString("F6", CultureInfo.InvariantCulture)); |
| | 237 | | } |
| | 238 | |
|
| 99 | 239 | | if (map.IsColorCorrectionEnabled) |
| | 240 | | { |
| 3 | 241 | | stream.Write(" -cc on"); |
| | 242 | | } |
| | 243 | |
|
| 99 | 244 | | if (map.IsClampingEnabled) |
| | 245 | | { |
| 3 | 246 | | stream.Write(" -clamp on"); |
| | 247 | | } |
| | 248 | |
|
| 99 | 249 | | if (map.ScalarChannel != ObjMapChannel.Luminance) |
| | 250 | | { |
| 18 | 251 | | stream.Write(" -imfchan "); |
| | 252 | |
|
| 18 | 253 | | switch (map.ScalarChannel) |
| | 254 | | { |
| | 255 | | case ObjMapChannel.Red: |
| 3 | 256 | | stream.Write("r"); |
| 3 | 257 | | break; |
| | 258 | |
|
| | 259 | | case ObjMapChannel.Green: |
| 3 | 260 | | stream.Write("g"); |
| 3 | 261 | | break; |
| | 262 | |
|
| | 263 | | case ObjMapChannel.Blue: |
| 3 | 264 | | stream.Write("b"); |
| 3 | 265 | | break; |
| | 266 | |
|
| | 267 | | case ObjMapChannel.Matte: |
| 3 | 268 | | stream.Write("m"); |
| 3 | 269 | | break; |
| | 270 | |
|
| | 271 | | case ObjMapChannel.Depth: |
| 3 | 272 | | stream.Write("z"); |
| 3 | 273 | | break; |
| | 274 | |
|
| | 275 | | default: |
| 3 | 276 | | stream.Write("l"); |
| | 277 | | break; |
| | 278 | | } |
| | 279 | | } |
| | 280 | |
|
| 99 | 281 | | if (map.ModifierBase != 0.0f || map.ModifierGain != 1.0f) |
| | 282 | | { |
| 3 | 283 | | stream.Write(" -mm "); |
| 3 | 284 | | stream.Write(map.ModifierBase.ToString("F6", CultureInfo.InvariantCulture)); |
| 3 | 285 | | stream.Write(' '); |
| 3 | 286 | | stream.Write(map.ModifierGain.ToString("F6", CultureInfo.InvariantCulture)); |
| | 287 | | } |
| | 288 | |
|
| 99 | 289 | | ObjVector3 offset = map.Offset; |
| | 290 | |
|
| 99 | 291 | | if (offset.X != 0.0f || offset.Y != 0.0f || offset.Z != 0.0f) |
| | 292 | | { |
| 3 | 293 | | stream.Write(" -o "); |
| 3 | 294 | | stream.Write(offset.X.ToString("F6", CultureInfo.InvariantCulture)); |
| 3 | 295 | | stream.Write(' '); |
| 3 | 296 | | stream.Write(offset.Y.ToString("F6", CultureInfo.InvariantCulture)); |
| 3 | 297 | | stream.Write(' '); |
| 3 | 298 | | stream.Write(offset.Z.ToString("F6", CultureInfo.InvariantCulture)); |
| | 299 | | } |
| | 300 | |
|
| 99 | 301 | | ObjVector3 scale = map.Scale; |
| | 302 | |
|
| 99 | 303 | | if (scale.X != 1.0f || scale.Y != 1.0f || scale.Z != 1.0f) |
| | 304 | | { |
| 3 | 305 | | stream.Write(" -s "); |
| 3 | 306 | | stream.Write(scale.X.ToString("F6", CultureInfo.InvariantCulture)); |
| 3 | 307 | | stream.Write(' '); |
| 3 | 308 | | stream.Write(scale.Y.ToString("F6", CultureInfo.InvariantCulture)); |
| 3 | 309 | | stream.Write(' '); |
| 3 | 310 | | stream.Write(scale.Z.ToString("F6", CultureInfo.InvariantCulture)); |
| | 311 | | } |
| | 312 | |
|
| 99 | 313 | | ObjVector3 turbulence = map.Turbulence; |
| | 314 | |
|
| 99 | 315 | | if (turbulence.X != 0.0f || turbulence.Y != 0.0f || turbulence.Z != 0.0f) |
| | 316 | | { |
| 3 | 317 | | stream.Write(" -t "); |
| 3 | 318 | | stream.Write(turbulence.X.ToString("F6", CultureInfo.InvariantCulture)); |
| 3 | 319 | | stream.Write(' '); |
| 3 | 320 | | stream.Write(turbulence.Y.ToString("F6", CultureInfo.InvariantCulture)); |
| 3 | 321 | | stream.Write(' '); |
| 3 | 322 | | stream.Write(turbulence.Z.ToString("F6", CultureInfo.InvariantCulture)); |
| | 323 | | } |
| | 324 | |
|
| 99 | 325 | | if (map.TextureResolution != 0) |
| | 326 | | { |
| 3 | 327 | | stream.Write(" -texres "); |
| 3 | 328 | | stream.Write(map.TextureResolution); |
| | 329 | | } |
| | 330 | |
|
| 99 | 331 | | stream.Write(' '); |
| 99 | 332 | | stream.WriteLine(map.FileName); |
| 99 | 333 | | } |
| | 334 | | } |
| | 335 | | } |
| | 336 | |
|