1 module sbylib.wrapper.assimp.material;
2 
3 import bindbc.assimp.types;
4 import sbylib.wrapper.assimp.constants : PropertyTypeInfo;
5 import sbylib.wrapper.assimp.functions : toRegularString, toArray, toEnum;
6 
7 struct Material {
8 
9     private const(aiMaterial)* mat;
10 
11     auto properties() {
12         return toArray!((const(aiMaterialProperty)* p) => MaterialProperty(p))
13             (mat.mNumProperties, mat.mProperties);
14     }
15 
16     string toString() {
17         import std.algorithm : map;
18         import std.array : array;
19         import sbylib.wrapper.assimp.functions : tree;
20 
21         return tree("Material", properties.map!(p => p.toString).array);
22     }
23 }
24 
25 struct MaterialProperty {
26 
27     private const(aiMaterialProperty)* prop;
28 
29     string key() {
30         return prop.mKey.toRegularString;
31     }
32 
33     PropertyTypeInfo type() {
34         return prop.mType.toEnum!PropertyTypeInfo;
35     }
36 
37     uint semantic() { // used for only texture property
38         return prop.mSemantic;
39     }
40 
41     uint index() { // used for only texture property
42         return prop.mIndex;
43     }
44 
45     auto data(Type)() {
46         static if (is(Type == float)) {
47             assert(type == PropertyTypeInfo.Float);
48             return *(cast(float*)prop.mData);
49         } else static if (is(Type == string)) {
50             import std.conv : to;
51             assert(type == PropertyTypeInfo.String);
52             return to!string(cast(char*)(prop.mData+4));
53         } else static if (is(Type == int)) {
54             assert(type == PropertyTypeInfo.Integer);
55             return *(cast(int*)prop.mData);
56         } else static if (is(Type == ubyte[])) {
57             assert(type == PropertyTypeInfo.Buffer);
58             return prop.mData[0..prop.mDataLength];
59         }
60     }
61     
62     string toString() {
63         import std.conv : to;
64         import std.format : format;
65 
66         string value;
67         final switch (type) {
68             case PropertyTypeInfo.Float:
69                 value = this.data!float.to!string;
70                 break;
71             case PropertyTypeInfo.String:
72                 value = this.data!string;
73                 break;
74             case PropertyTypeInfo.Integer:
75                 value = this.data!int.to!string;
76                 break;
77             case PropertyTypeInfo.Buffer:
78                 value = "<Buffer>";
79                 break;
80         }
81         return format!"%s : %s"(key, value);
82     }
83 }