2
2
#include " RNFMaterialImpl.h"
3
3
#include < filament/Engine.h>
4
4
#include < filament/TextureSampler.h>
5
+ #include < math/mat3.h>
5
6
6
7
namespace margelo {
7
8
@@ -18,9 +19,11 @@ std::shared_ptr<MaterialInstanceWrapper> MaterialImpl::getDefaultInstance() {
18
19
std::unique_lock lock (_mutex);
19
20
20
21
MaterialInstance* materialInstance = _material->getDefaultInstance ();
21
- auto instance = std::make_shared<MaterialInstanceWrapper>(materialInstance);
22
- _instances.push_back (instance);
23
- return instance;
22
+ // Note: the default material instance isn't added to the list of instances.
23
+ // We only keep track of that list to destroy a material correctly (ie. all
24
+ // its instances have to be destroyed first). However, the default instance
25
+ // is not destroyable, so it can be ignored.
26
+ return std::make_shared<MaterialInstanceWrapper>(materialInstance);
24
27
}
25
28
26
29
void MaterialImpl::setDefaultFloatParameter (std::string name, double value) {
@@ -33,6 +36,16 @@ void MaterialImpl::setDefaultFloatParameter(std::string name, double value) {
33
36
_material->setDefaultParameter (name.c_str (), (float )value);
34
37
}
35
38
39
+ void MaterialImpl::setDefaultIntParameter (std::string name, int value) {
40
+ std::unique_lock lock (_mutex);
41
+
42
+ if (!_material->hasParameter (name.c_str ())) {
43
+ throw std::runtime_error (" MaterialWrapper::setDefaultIntParameter: Material does not have parameter \" " + name + " \" !" );
44
+ }
45
+
46
+ _material->setDefaultParameter (name.c_str (), value);
47
+ }
48
+
36
49
void MaterialImpl::setDefaultTextureParameter (std::string name, Texture* texture, TextureSampler sampler) {
37
50
std::unique_lock lock (_mutex);
38
51
@@ -43,22 +56,59 @@ void MaterialImpl::setDefaultTextureParameter(std::string name, Texture* texture
43
56
_material->setDefaultParameter (name.c_str (), texture, sampler);
44
57
}
45
58
46
- void MaterialImpl::setBaseColorSRGB (std::vector<double > rgba ) {
59
+ void MaterialImpl::setDefaultFloat3Parameter (std::string name, std:: vector<double > vector ) {
47
60
std::unique_lock lock (_mutex);
48
- if (rgba.size () != 4 ) {
49
- throw std::runtime_error (" MaterialInstanceWrapper::setBaseColorSRGB: RGBA vector must have 4 elements!" );
61
+ if (vector.size () != 3 ) {
62
+ throw std::runtime_error (" setDefaultFloat3Parameter: RGB vector must have 3 elements!" );
63
+ }
64
+
65
+ if (!_material->hasParameter (name.c_str ())) {
66
+ throw std::runtime_error (" setDefaultFloat3Parameter: Material does not have parameter \" " + name + " \" !" );
50
67
}
51
68
52
- double r = rgba[0 ];
53
- double g = rgba[1 ];
54
- double b = rgba[2 ];
55
- double a = rgba[3 ];
69
+ float x = vector[0 ];
70
+ float y = vector[1 ];
71
+ float z = vector[2 ];
56
72
57
- _material->setDefaultParameter (" baseColorFactor" , math::float4 ({r, g, b, a}));
73
+ _material->setDefaultParameter (name.c_str (), math::float3 ({x, y, z}));
74
+ }
75
+
76
+ void MaterialImpl::setDefaultFloat4Parameter (std::string name, std::vector<double > vector) {
77
+ std::unique_lock lock (_mutex);
78
+ if (vector.size () != 4 ) {
79
+ throw std::runtime_error (" setDefaultFloat4Parameter: RGBA vector must have 4 elements!" );
80
+ }
81
+
82
+ if (!_material->hasParameter (name.c_str ())) {
83
+ throw std::runtime_error (" setDefaultFloat4Parameter: Material does not have parameter \" " + name + " \" !" );
84
+ }
85
+
86
+ double r = vector[0 ];
87
+ double g = vector[1 ];
88
+ double b = vector[2 ];
89
+ double a = vector[3 ];
90
+
91
+ _material->setDefaultParameter (name.c_str (), math::float4 ({r, g, b, a}));
58
92
}
59
93
std::string MaterialImpl::getName () {
60
94
std::unique_lock lock (_mutex);
61
95
return _material->getName ();
62
96
}
63
97
98
+ void MaterialImpl::setDefaultMat3fParameter (std::string name, std::vector<double > value) {
99
+ std::unique_lock lock (_mutex);
100
+
101
+ if (!_material->hasParameter (name.c_str ())) {
102
+ throw std::runtime_error (" MaterialWrapper::setDefaultMat3fParameter: Material does not have parameter \" " + name + " \" !" );
103
+ }
104
+
105
+ if (value.size () != 9 ) {
106
+ throw std::runtime_error (" MaterialWrapper::setDefaultMat3fParameter: Value vector must have 9 elements!" );
107
+ }
108
+
109
+ math::mat3f matrix = math::mat3f ((float )value[0 ], (float )value[1 ], (float )value[2 ], (float )value[3 ], (float )value[4 ], (float )value[5 ],
110
+ (float )value[6 ], (float )value[7 ], (float )value[8 ]);
111
+ _material->setDefaultParameter (name.c_str (), matrix);
112
+ }
113
+
64
114
} // namespace margelo
0 commit comments