-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBasicMaterials.js
89 lines (76 loc) · 2.46 KB
/
BasicMaterials.js
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
'use strict'
var BasicMaterials = (function () {
//singleton pattern follows http://www.dofactory.com/javascript/singleton-design-pattern
var instance = null;
var fallback = new THREE.MeshNormalMaterial();
function createInstance() {
var obj = {
Get: function (id) {
if (this.hasOwnProperty(id))
return this[id];
else
return null;
},
Set: function (id, material) {
this[id] = material;
},
Groups: {}
};
return obj;
}
return {
Get: function (id) {
if (!instance) {
instance = createInstance();
}
var result = instance.Get(id);
return result ? result : fallback;
},
Set: function (id, material) {
if (!instance) {
instance = createInstance();
}
instance.Set(id, material);
},
ParseAndSet: function (id, m) {
if (instance && instance.hasOwnProperty(id)) {
return instance.Get(id);
}
else {
var mat;
var M = JSON.parse(m);
if (M.hasOwnProperty("Specular")) {
mat = new THREE.MeshPhongMaterial();
mat.specular = new THREE.Color(M.Specular[0], M.Specular[1], M.Specular[2]);
mat.shininess = M.Shininess;
}
else {
mat = new THREE.MeshLambertMaterial();
}
mat.ambient = new THREE.Color(M.Ambient[0], M.Ambient[1], M.Ambient[2]);
mat.color = new THREE.Color(M.Diffuse[0], M.Diffuse[1], M.Diffuse[2]);
this.Set(id, mat);
return mat;
}
},
Clear: function(){
//clear groups
if (instance) {
instance.Groups = {};
instance = createInstance();
}
},
Add: function (shape) {
if (!instance) {
instance = createInstance();
}
var mid = shape.appearance.material;
if (!instance.Groups.hasOwnProperty(mid))
instance.Groups[mid] = new Array();
instance.Groups[mid].push(shape);
},
Groups: function () {
return instance.Groups;
}
};
})();