-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaterialManager.m
278 lines (216 loc) · 11.3 KB
/
MaterialManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
classdef MaterialManager
%MaterialManager Keeps track of all material definitions used by .obj
%files and then creates a master material file used by all
properties
MaterialsTable = java.util.Properties
end
methods
function obj = MaterialManager()
obj.MaterialsTable.put('White', [0 0 0 1]);
end
%Add a material to the list if it doesn't already exist
function UpdateMaterial(obj, Name, Color)
entry = obj.MaterialsTable.get(Name);
if(isempty(entry))
obj.MaterialsTable.put(Name, Color);
end
end
function CreateMaterialFile(obj, Path, FileName)
% CreateMaterialFile - The new material file only varies in the color of
% the verticies
if(~isempty(Path))
Path = [Path filesep];
end
TargetPath = [Path FileName];
hFile = fopen(TargetPath,'w');
enum = obj.MaterialsTable.propertyNames;
while enum.hasMoreElements
MaterialName = enum.nextElement;
obj.WriteMaterial(hFile, MaterialName);
end;
fclose(hFile);
end
function CreateColladaMaterialFile(obj, Path, FileName)
% CreateMaterialFile - The new material file only varies in the color of
% the verticies
if(~isempty(Path))
Path = [Path filesep];
end
TargetPath = [Path FileName];
DOM = CreateColladaFile();
obj.AppendColladaMaterials(DOM);
xmlwrite(TargetPath, DOM);
end
function AppendColladaMaterials(obj, DOM)
% AppendColladaMaterials - Append the materials to the collada file
DOMNode = DOM.getDocumentElement;
MatLibList = DOMNode.getElementsByTagName('library_materials');
MatLibNode = [];
if(isempty(MatLibList.item(0)))
MatLibNode = DOM.createElement('library_materials');
DOMNode.appendChild( MatLibNode);
else
MatLibNode = MatLibList.item(0);
end
EffectLibList = DOMNode.getElementsByTagName('library_effects');
EffectLibNode = [];
if(isempty(EffectLibList.item(0)))
EffectLibNode = DOM.createElement('library_effects');
DOMNode.appendChild( EffectLibNode);
else
EffectLibNode = EffectLibList.item(0);
end
enum = obj.MaterialsTable.propertyNames;
while enum.hasMoreElements
MaterialName = enum.nextElement;
obj.WriteColladaEffect(DOM, EffectLibNode, MaterialName);
obj.WriteColladaMaterial(DOM, MatLibNode, MaterialName);
end;
end
end
methods (Access = protected)
function WriteMaterial(obj, hFile, Name)
%Create an entry for the named material entry
data = obj.MaterialsTable.get(Name);
color = data(1:3)'
alpha = data(4);
fprintf(hFile, ['newmtl ' Name '\n']);
fprintf(hFile, '\t#Ambient Color\n');
fprintf(hFile, '\tKa 0 0 0\n');
fprintf(hFile, '\t#Diffuse Color\n');
fprintf(hFile, ['\tKd ' num2str(color) '\n']);
fprintf(hFile, '\t#Specular Color\n');
fprintf(hFile, ['\tKs ' num2str(color) '\n']);
fprintf(hFile, '\t#Alpha\n');
fprintf(hFile, ['\td ' num2str(alpha) '\n']);
fprintf(hFile, '\t#Specular Component\n');
fprintf(hFile, '\tNs 2.0\n');
fprintf(hFile, '\t#Illumination Model\n');
fprintf(hFile, '\tillum 2\n');
fprintf(hFile, '\n');
end
function obj = WriteColladaEffect(obj, DOM, EffectLibNode, MaterialName)
EffectID = [MaterialName '-fx'];
EffectNode = DOM.createElement('effect');
EffectNode.setAttribute('id', EffectID);
ProfileNode = DOM.createElement('profile_COMMON');
TechniqueNode = DOM.createElement('technique');
TechniqueNode.setAttribute('sid', 'common');
% PhongNode = obj.CreatePhongNode(DOM, MaterialName);
LambertNode = obj.CreateLambertNode(DOM, MaterialName);
% BlinnNode = obj.CreateBlinnNode(DOM, MaterialName);
% ConstantNode = obj.CreateConstantNode(DOM, MaterialName);
TechniqueNode.appendChild(LambertNode);
% TechniqueNode.appendChild(PhongNode);
% TechniqueNode.appendChild(BlinnNode);
% TechniqueNode.appendChild(ConstantNode);
ProfileNode.appendChild(TechniqueNode);
EffectNode.appendChild(ProfileNode);
EffectLibNode.appendChild(EffectNode);
end
function PhongNode = CreatePhongNode(obj, DOM, MaterialName)
data = obj.MaterialsTable.get(MaterialName);
Color = data(1:4);
PhongNode = DOM.createElement('phong');
EmissionNode = DOM.createElement('emission');
AmbientNode = DOM.createElement('ambient');
DiffuseNode = DOM.createElement('diffuse');
SpecularNode = DOM.createElement('specular');
ShininessNode = DOM.createElement('shininess');
ReflectiveNode = DOM.createElement('reflective');
ReflectivityNode = DOM.createElement('reflectivity');
RefractionIndexNode = DOM.createElement('index_of_refraction');
AddFloatElement(DOM, EmissionNode, 'color', [0 0 0 0]);
AddFloatElement(DOM, AmbientNode, 'color', [0 0 0 0]);
AddFloatElement(DOM, DiffuseNode, 'color', Color);
AddFloatElement(DOM, SpecularNode, 'color', Color);
AddFloatElement(DOM, ShininessNode, 'float', 2);
AddFloatElement(DOM, ReflectiveNode, 'color', Color);
AddFloatElement(DOM, ReflectivityNode, 'float', 0);
AddFloatElement(DOM, RefractionIndexNode, 'float', 0);
PhongNode.appendChild(EmissionNode);
PhongNode.appendChild(AmbientNode);
PhongNode.appendChild(DiffuseNode);
PhongNode.appendChild(SpecularNode);
PhongNode.appendChild(ShininessNode);
PhongNode.appendChild(ReflectiveNode);
PhongNode.appendChild(ReflectivityNode);
PhongNode.appendChild(RefractionIndexNode);
end
function LambertNode = CreateLambertNode(obj, DOM, MaterialName)
data = obj.MaterialsTable.get(MaterialName);
Color = data(1:4);
LambertNode = DOM.createElement('lambert');
% EmissionNode = DOM.createElement('emission');
% AmbientNode = DOM.createElement('ambient');
DiffuseNode = DOM.createElement('diffuse');
ReflectiveNode = DOM.createElement('reflective');
ReflectivityNode = DOM.createElement('reflectivity');
RefractionIndexNode = DOM.createElement('index_of_refraction');
% AddFloatElement(DOM, AmbientNode, 'color', [Color; 1]);
AddFloatElement(DOM, DiffuseNode, 'color', Color);
AddFloatElement(DOM, ReflectiveNode, 'color', Color);
AddFloatElement(DOM, ReflectivityNode, 'float', 0);
AddFloatElement(DOM, RefractionIndexNode, 'float', 0);
% LambertNode.appendChild(EmissionNode);
% LambertNode.appendChild(AmbientNode);
LambertNode.appendChild(DiffuseNode);
LambertNode.appendChild(ReflectiveNode);
LambertNode.appendChild(ReflectivityNode);
LambertNode.appendChild(RefractionIndexNode);
end
function BlinnNode = CreateBlinnNode(obj, DOM, MaterialName)
Color = obj.MaterialsTable.get(MaterialName);
BlinnNode = DOM.createElement('blinn');
EmissionNode = DOM.createElement('emission');
AmbientNode = DOM.createElement('ambient');
DiffuseNode = DOM.createElement('diffuse');
SpecularNode = DOM.createElement('specular');
ShininessNode = DOM.createElement('shininess');
ReflectiveNode = DOM.createElement('reflective');
ReflectivityNode = DOM.createElement('reflectivity');
RefractionIndexNode = DOM.createElement('index_of_refraction');
AddFloatElement(DOM, EmissionNode, 'color', [0 0 0 0]);
AddFloatElement(DOM, AmbientNode, 'color', [0 0 0 0]);
AddFloatElement(DOM, DiffuseNode, 'color', Color);
AddFloatElement(DOM, SpecularNode, 'color', Color);
AddFloatElement(DOM, ShininessNode, 'float', 2);
AddFloatElement(DOM, ReflectiveNode, 'color', Color);
AddFloatElement(DOM, ReflectivityNode, 'float', 0);
AddFloatElement(DOM, RefractionIndexNode, 'float', 0);
BlinnNode.appendChild(EmissionNode);
BlinnNode.appendChild(AmbientNode);
BlinnNode.appendChild(DiffuseNode);
BlinnNode.appendChild(SpecularNode);
BlinnNode.appendChild(ShininessNode);
BlinnNode.appendChild(ReflectiveNode);
BlinnNode.appendChild(ReflectivityNode);
BlinnNode.appendChild(RefractionIndexNode);
end
function ConstantNode = CreateConstantNode(obj, DOM, MaterialName)
Color = obj.MaterialsTable.get(MaterialName);
ConstantNode = DOM.createElement('constant');
EmissionNode = DOM.createElement('emission');
ReflectiveNode = DOM.createElement('reflective');
ReflectivityNode = DOM.createElement('reflectivity');
RefractionIndexNode = DOM.createElement('index_of_refraction');
AddFloatElement(DOM, EmissionNode, 'color', [0 0 0 0]);
AddFloatElement(DOM, ReflectiveNode, 'color', Color);
AddFloatElement(DOM, ReflectivityNode, 'float', 0);
AddFloatElement(DOM, RefractionIndexNode, 'float', 0);
ConstantNode.appendChild(EmissionNode);
ConstantNode.appendChild(ReflectiveNode);
ConstantNode.appendChild(ReflectivityNode);
ConstantNode.appendChild(RefractionIndexNode);
end
function obj = WriteColladaMaterial(obj, DOM, MatLibNode, MaterialName)
MaterialNode = DOM.createElement('material');
MaterialNode.setAttribute('id', MaterialName);
MaterialNode.setAttribute('name', MaterialName);
InstanceEffectNode = DOM.createElement('instance_effect');
InstanceEffectNode.setAttribute('url', ['#' MaterialName '-fx']);
MaterialNode.appendChild(InstanceEffectNode);
MatLibNode.appendChild(MaterialNode);
end
end
end