1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using Oni.Imaging;
|
---|
5 |
|
---|
6 | namespace Oni.Motoko
|
---|
7 | {
|
---|
8 | internal class TextureDaeWriter
|
---|
9 | {
|
---|
10 | private readonly string outputDirPath;
|
---|
11 | private readonly Dictionary<InstanceDescriptor, Dae.Material> materials = new Dictionary<InstanceDescriptor, Dae.Material>();
|
---|
12 |
|
---|
13 | public TextureDaeWriter(string outputDirPath)
|
---|
14 | {
|
---|
15 | this.outputDirPath = outputDirPath;
|
---|
16 | }
|
---|
17 |
|
---|
18 | public Dae.Material WriteMaterial(InstanceDescriptor txmp)
|
---|
19 | {
|
---|
20 | Dae.Material material;
|
---|
21 |
|
---|
22 | if (!materials.TryGetValue(txmp, out material))
|
---|
23 | {
|
---|
24 | material = CreateMaterial(txmp);
|
---|
25 | materials.Add(txmp, material);
|
---|
26 | }
|
---|
27 |
|
---|
28 | return material;
|
---|
29 | }
|
---|
30 |
|
---|
31 | private Dae.Material CreateMaterial(InstanceDescriptor txmp)
|
---|
32 | {
|
---|
33 | var texture = TextureDatReader.Read(txmp);
|
---|
34 |
|
---|
35 | var imageFilePath = Utils.CleanupTextureName(txmp.Name) + ".tga";
|
---|
36 | imageFilePath = Path.Combine("images", imageFilePath);
|
---|
37 | TgaWriter.Write(texture.Surfaces[0], Path.Combine(outputDirPath, imageFilePath));
|
---|
38 |
|
---|
39 | string name = TextureNameToId(txmp);
|
---|
40 |
|
---|
41 | var image = new Dae.Image
|
---|
42 | {
|
---|
43 | FilePath = "./" + imageFilePath.Replace('\\', '/'),
|
---|
44 | Name = name
|
---|
45 | };
|
---|
46 |
|
---|
47 | var effectSurface = new Dae.EffectSurface(image);
|
---|
48 |
|
---|
49 | var effectSampler = new Dae.EffectSampler(effectSurface)
|
---|
50 | {
|
---|
51 | WrapS = texture.WrapU ? Dae.EffectSamplerWrap.Wrap : Dae.EffectSamplerWrap.None,
|
---|
52 | WrapT = texture.WrapV ? Dae.EffectSamplerWrap.Wrap : Dae.EffectSamplerWrap.None
|
---|
53 | };
|
---|
54 |
|
---|
55 | var effectTexture = new Dae.EffectTexture(effectSampler, "diffuse_TEXCOORD");
|
---|
56 |
|
---|
57 | var effect = new Dae.Effect
|
---|
58 | {
|
---|
59 | Name = name,
|
---|
60 | DiffuseValue = effectTexture,
|
---|
61 | TransparentValue = texture.HasAlpha ? effectTexture : null,
|
---|
62 | Parameters = {
|
---|
63 | new Dae.EffectParameter("surface", effectSurface),
|
---|
64 | new Dae.EffectParameter("sampler", effectSampler)
|
---|
65 | }
|
---|
66 | };
|
---|
67 |
|
---|
68 | var material = new Dae.Material
|
---|
69 | {
|
---|
70 | Name = name,
|
---|
71 | Effect = effect
|
---|
72 | };
|
---|
73 |
|
---|
74 | return material;
|
---|
75 | }
|
---|
76 |
|
---|
77 | private static string TextureNameToId(InstanceDescriptor txmp)
|
---|
78 | {
|
---|
79 | string name = Utils.CleanupTextureName(txmp.Name);
|
---|
80 |
|
---|
81 | if (name.StartsWith("Iteration", StringComparison.Ordinal))
|
---|
82 | {
|
---|
83 | //
|
---|
84 | // HACK: discard Iteration_NNN_ prefixes to avoid truncation of material
|
---|
85 | // names in Blender.
|
---|
86 | //
|
---|
87 |
|
---|
88 | name = name.Substring(9);
|
---|
89 |
|
---|
90 | if (char.IsDigit(name[0]) && char.IsDigit(name[1]) && char.IsDigit(name[2]) && name[3] == '_')
|
---|
91 | name = name.Substring(4);
|
---|
92 | }
|
---|
93 |
|
---|
94 | return name;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|