Skip to content

Commit 045fac9

Browse files
authored
fix: MaterialInstance API (#204)
There were some differences between the TS types and the native methods. This PR also aligned MaterialInstance implementation with the Material implementation.
1 parent 739ebec commit 045fac9

File tree

5 files changed

+155
-35
lines changed

5 files changed

+155
-35
lines changed

package/cpp/core/RNFMaterialImpl.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void MaterialImpl::setDefaultFloatParameter(std::string name, double value) {
3030
std::unique_lock lock(_mutex);
3131

3232
if (!_material->hasParameter(name.c_str())) {
33-
throw std::runtime_error("MaterialWrapper::setDefaultFloatParameter: Material does not have parameter \"" + name + "\"!");
33+
throw std::runtime_error("MaterialWrapper::setFloatParameter: Material does not have parameter \"" + name + "\"!");
3434
}
3535

3636
_material->setDefaultParameter(name.c_str(), (float)value);
@@ -40,7 +40,7 @@ void MaterialImpl::setDefaultIntParameter(std::string name, int value) {
4040
std::unique_lock lock(_mutex);
4141

4242
if (!_material->hasParameter(name.c_str())) {
43-
throw std::runtime_error("MaterialWrapper::setDefaultIntParameter: Material does not have parameter \"" + name + "\"!");
43+
throw std::runtime_error("MaterialWrapper::setIntParameter: Material does not have parameter \"" + name + "\"!");
4444
}
4545

4646
_material->setDefaultParameter(name.c_str(), value);
@@ -59,11 +59,11 @@ void MaterialImpl::setDefaultTextureParameter(std::string name, Texture* texture
5959
void MaterialImpl::setDefaultFloat3Parameter(std::string name, std::vector<double> vector) {
6060
std::unique_lock lock(_mutex);
6161
if (vector.size() != 3) {
62-
throw std::runtime_error("setDefaultFloat3Parameter: RGB vector must have 3 elements!");
62+
throw std::runtime_error("setFloat3Parameter: RGB vector must have 3 elements!");
6363
}
6464

6565
if (!_material->hasParameter(name.c_str())) {
66-
throw std::runtime_error("setDefaultFloat3Parameter: Material does not have parameter \"" + name + "\"!");
66+
throw std::runtime_error("setFloat3Parameter: Material does not have parameter \"" + name + "\"!");
6767
}
6868

6969
float x = vector[0];
@@ -76,11 +76,11 @@ void MaterialImpl::setDefaultFloat3Parameter(std::string name, std::vector<doubl
7676
void MaterialImpl::setDefaultFloat4Parameter(std::string name, std::vector<double> vector) {
7777
std::unique_lock lock(_mutex);
7878
if (vector.size() != 4) {
79-
throw std::runtime_error("setDefaultFloat4Parameter: RGBA vector must have 4 elements!");
79+
throw std::runtime_error("setFloat4Parameter: RGBA vector must have 4 elements!");
8080
}
8181

8282
if (!_material->hasParameter(name.c_str())) {
83-
throw std::runtime_error("setDefaultFloat4Parameter: Material does not have parameter \"" + name + "\"!");
83+
throw std::runtime_error("setFloat4Parameter: Material does not have parameter \"" + name + "\"!");
8484
}
8585

8686
double r = vector[0];
@@ -99,11 +99,11 @@ void MaterialImpl::setDefaultMat3fParameter(std::string name, std::vector<double
9999
std::unique_lock lock(_mutex);
100100

101101
if (!_material->hasParameter(name.c_str())) {
102-
throw std::runtime_error("MaterialWrapper::setDefaultMat3fParameter: Material does not have parameter \"" + name + "\"!");
102+
throw std::runtime_error("MaterialWrapper::setMat3fParameter: Material does not have parameter \"" + name + "\"!");
103103
}
104104

105105
if (value.size() != 9) {
106-
throw std::runtime_error("MaterialWrapper::setDefaultMat3fParameter: Value vector must have 9 elements!");
106+
throw std::runtime_error("MaterialWrapper::setMat3fParameter: Value vector must have 9 elements!");
107107
}
108108

109109
math::mat3f matrix = math::mat3f((float)value[0], (float)value[1], (float)value[2], (float)value[3], (float)value[4], (float)value[5],

package/cpp/core/RNFMaterialInstanceWrapper.cpp

Lines changed: 121 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@
66
#include "RNFCullingModeEnum.h"
77
#include "RNFTransparencyModeEnum.h"
88
#include <filament/Material.h>
9+
#include <math/mat3.h>
910

1011
namespace margelo {
1112
void MaterialInstanceWrapper::loadHybridMethods() {
1213
registerHybridMethod("setCullingMode", &MaterialInstanceWrapper::setCullingMode, this);
1314
registerHybridMethod("setTransparencyMode", &MaterialInstanceWrapper::setTransparencyMode, this);
1415
registerHybridMethod("changeAlpha", &MaterialInstanceWrapper::changeAlpha, this);
15-
registerHybridMethod("setParameter", &MaterialInstanceWrapper::setParameter, this);
16-
registerHybridMethod("setDefaultFloat4Parameter", &MaterialInstanceWrapper::setBaseColorSRGB, this);
16+
registerHybridMethod("setFloat4Parameter", &MaterialInstanceWrapper::setFloatParameter, this);
17+
registerHybridMethod("setIntParameter", &MaterialInstanceWrapper::setIntParameter, this);
18+
registerHybridMethod("setFloat3Parameter", &MaterialInstanceWrapper::setFloat3Parameter, this);
19+
registerHybridMethod("setFloat4Parameter", &MaterialInstanceWrapper::setFloat4Parameter, this);
20+
registerHybridMethod("setMat3fParameter", &MaterialInstanceWrapper::setMat3fParameter, this);
21+
registerHybridMethod("getFloatParameter", &MaterialInstanceWrapper::getFloatParameter, this);
22+
registerHybridMethod("getIntParameter", &MaterialInstanceWrapper::getIntParameter, this);
23+
registerHybridMethod("getFloat3Parameter", &MaterialInstanceWrapper::getFloat3Parameter, this);
24+
registerHybridMethod("getFloat4Parameter", &MaterialInstanceWrapper::getFloat4Parameter, this);
25+
registerHybridMethod("getMat3fParameter", &MaterialInstanceWrapper::getMat3fParameter, this);
1726
registerHybridGetter("getName", &MaterialInstanceWrapper::getName, this);
1827
}
1928

@@ -50,31 +59,80 @@ void MaterialInstanceWrapper::changeAlpha(double alpha) {
5059
changeAlpha(_materialInstance, alpha);
5160
}
5261

53-
void MaterialInstanceWrapper::setParameter(std::string name, double value) {
62+
void MaterialInstanceWrapper::setFloatParameter(std::string name, double value) {
5463
std::unique_lock lock(_mutex);
5564

5665
const Material* material = _materialInstance->getMaterial();
57-
5866
if (!material->hasParameter(name.c_str())) {
59-
throw std::runtime_error("MaterialInstanceWrapper::setParameter: Material does not have parameter \"" + name + "\"!");
67+
throw std::runtime_error("MaterialInstanceWrapper::setFloatParameter: Material does not have parameter \"" + name + "\"!");
6068
}
6169

6270
_materialInstance->setParameter(name.c_str(), (float)value);
6371
}
6472

65-
void MaterialInstanceWrapper::setBaseColorSRGB(std::vector<double> rgba) {
73+
void MaterialInstanceWrapper::setIntParameter(std::string name, int value) {
74+
std::unique_lock lock(_mutex);
75+
76+
const Material* material = _materialInstance->getMaterial();
77+
if (!material->hasParameter(name.c_str())) {
78+
throw std::runtime_error("MaterialInstanceWrapper::setIntParameter: Material does not have parameter \"" + name + "\"!");
79+
}
80+
81+
_materialInstance->setParameter(name.c_str(), value);
82+
}
83+
84+
void MaterialInstanceWrapper::setFloat3Parameter(std::string name, std::vector<double> vector) {
6685
std::unique_lock lock(_mutex);
86+
if (vector.size() != 3) {
87+
throw std::runtime_error("MaterialInstanceWrapper::setFloat3Parameter: RGB vector must have 3 elements!");
88+
}
89+
90+
const Material* material = _materialInstance->getMaterial();
91+
if (!material->hasParameter(name.c_str())) {
92+
throw std::runtime_error("MaterialInstanceWrapper::setFloat3Parameter: Material does not have parameter \"" + name + "\"!");
93+
}
6794

68-
if (rgba.size() != 4) {
69-
throw std::runtime_error("MaterialInstanceWrapper::setDefaultFloat4Parameter: RGBA vector must have 4 elements!");
95+
float x = vector[0];
96+
float y = vector[1];
97+
float z = vector[2];
98+
99+
_materialInstance->setParameter(name.c_str(), math::float3({x, y, z}));
100+
}
101+
102+
void MaterialInstanceWrapper::setFloat4Parameter(std::string name, std::vector<double> vector) {
103+
std::unique_lock lock(_mutex);
104+
if (vector.size() != 4) {
105+
throw std::runtime_error("MaterialInstanceWrapper::setFloat4Parameter: RGBA vector must have 4 elements!");
106+
}
107+
108+
const Material* material = _materialInstance->getMaterial();
109+
if (!material->hasParameter(name.c_str())) {
110+
throw std::runtime_error("MaterialInstanceWrapper::setFloat4Parameter: Material does not have parameter \"" + name + "\"!");
111+
}
112+
113+
double r = vector[0];
114+
double g = vector[1];
115+
double b = vector[2];
116+
double a = vector[3];
117+
118+
_materialInstance->setParameter(name.c_str(), math::float4({r, g, b, a}));
119+
}
120+
121+
void MaterialInstanceWrapper::setMat3fParameter(std::string name, std::vector<double> value) {
122+
std::unique_lock lock(_mutex);
123+
124+
const Material* material = _materialInstance->getMaterial();
125+
if (!material->hasParameter(name.c_str())) {
126+
throw std::runtime_error("MaterialInstanceWrapper::setMat3fParameter: Material does not have parameter \"" + name + "\"!");
70127
}
71128

72-
double r = rgba[0];
73-
double g = rgba[1];
74-
double b = rgba[2];
75-
double a = rgba[3];
129+
if (value.size() != 9) {
130+
throw std::runtime_error("MaterialInstanceWrapper::setMat3fParameter: Value vector must have 9 elements!");
131+
}
76132

77-
_materialInstance->setParameter("baseColorFactor", math::float4({r, g, b, a}));
133+
math::mat3f matrix = math::mat3f((float)value[0], (float)value[1], (float)value[2], (float)value[3], (float)value[4], (float)value[5],
134+
(float)value[6], (float)value[7], (float)value[8]);
135+
_materialInstance->setParameter(name.c_str(), matrix);
78136
}
79137

80138
std::string MaterialInstanceWrapper::getName() {
@@ -83,4 +141,54 @@ std::string MaterialInstanceWrapper::getName() {
83141
return _materialInstance->getName();
84142
}
85143

144+
double MaterialInstanceWrapper::getFloatParameter(std::string name) {
145+
const Material* material = _materialInstance->getMaterial();
146+
if (!material->hasParameter(name.c_str())) {
147+
throw std::runtime_error("MaterialInstanceWrapper::getFloatParameter: Material does not have parameter \"" + name + "\"!");
148+
}
149+
150+
return _materialInstance->getParameter<float>(name.c_str());
151+
}
152+
153+
int MaterialInstanceWrapper::getIntParameter(std::string name) {
154+
const Material* material = _materialInstance->getMaterial();
155+
if (!material->hasParameter(name.c_str())) {
156+
throw std::runtime_error("MaterialInstanceWrapper::getIntParameter: Material does not have parameter \"" + name + "\"!");
157+
}
158+
159+
return _materialInstance->getParameter<int>(name.c_str());
160+
}
161+
162+
std::vector<double> MaterialInstanceWrapper::getFloat3Parameter(std::string name) {
163+
const Material* material = _materialInstance->getMaterial();
164+
if (!material->hasParameter(name.c_str())) {
165+
throw std::runtime_error("MaterialInstanceWrapper::getFloat3Parameter: Material does not have parameter \"" + name + "\"!");
166+
}
167+
168+
math::float3 vector = _materialInstance->getParameter<math::float3>(name.c_str());
169+
return {vector.x, vector.y, vector.z};
170+
}
171+
172+
std::vector<double> MaterialInstanceWrapper::getFloat4Parameter(std::string name) {
173+
const Material* material = _materialInstance->getMaterial();
174+
if (!material->hasParameter(name.c_str())) {
175+
throw std::runtime_error("MaterialInstanceWrapper::getFloat4Parameter: Material does not have parameter \"" + name + "\"!");
176+
}
177+
178+
math::float4 vector = _materialInstance->getParameter<math::float4>(name.c_str());
179+
return {vector.r, vector.g, vector.b, vector.a};
180+
}
181+
182+
std::vector<double> MaterialInstanceWrapper::getMat3fParameter(std::string name) {
183+
const Material* material = _materialInstance->getMaterial();
184+
if (!material->hasParameter(name.c_str())) {
185+
throw std::runtime_error("MaterialInstanceWrapper::getMat3fParameter: Material does not have parameter \"" + name + "\"!");
186+
}
187+
188+
math::mat3f matrix = _materialInstance->getParameter<math::mat3f>(name.c_str());
189+
const float* matrixArray = matrix.asArray();
190+
return {matrixArray[0], matrixArray[1], matrixArray[2], matrixArray[3], matrixArray[4],
191+
matrixArray[5], matrixArray[6], matrixArray[7], matrixArray[8]};
192+
}
193+
86194
} // namespace margelo

package/cpp/core/RNFMaterialInstanceWrapper.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ class MaterialInstanceWrapper : public HybridObject {
2828
void setTransparencyMode(std::string mode);
2929
// Convenience method for updating baseColorFactor parameter
3030
void changeAlpha(double alpha);
31-
void setParameter(std::string name, double value);
32-
void setBaseColorSRGB(std::vector<double> rgba);
31+
void setFloatParameter(std::string name, double value);
32+
void setIntParameter(std::string name, int value);
33+
void setFloat3Parameter(std::string name, std::vector<double> vector);
34+
void setFloat4Parameter(std::string name, std::vector<double> vector);
35+
void setMat3fParameter(std::string name, std::vector<double> value);
36+
double getFloatParameter(std::string name);
37+
int getIntParameter(std::string name);
38+
std::vector<double> getFloat3Parameter(std::string name);
39+
std::vector<double> getFloat4Parameter(std::string name);
40+
std::vector<double> getMat3fParameter(std::string name);
3341
std::string getName();
3442

3543
public: // Internal API

package/cpp/core/RNFMaterialWrapper.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
namespace margelo {
1111
void MaterialWrapper::loadHybridMethods() {
1212
registerHybridMethod("createInstance", &MaterialWrapper::createInstance, this);
13-
registerHybridMethod("setDefaultFloatParameter", &MaterialWrapper::setDefaultFloatParameter, this);
13+
registerHybridMethod("setFloatParameter", &MaterialWrapper::setDefaultFloatParameter, this);
1414
registerHybridMethod("setDefaultTextureParameter", &MaterialWrapper::setDefaultTextureParameter, this);
1515
registerHybridMethod("getDefaultInstance", &MaterialWrapper::getDefaultInstance, this);
16-
registerHybridMethod("setDefaultMat3fParameter", &MaterialWrapper::setDefaultMat3fParameter, this);
17-
registerHybridMethod("setDefaultFloat3Parameter", &MaterialWrapper::setDefaultFloat3Parameter, this);
18-
registerHybridMethod("setDefaultFloat4Parameter", &MaterialWrapper::setDefaultFloat4Parameter, this);
19-
registerHybridMethod("setDefaultIntParameter", &MaterialWrapper::setDefaultIntParameter, this);
16+
registerHybridMethod("setMat3fParameter", &MaterialWrapper::setDefaultMat3fParameter, this);
17+
registerHybridMethod("setFloat3Parameter", &MaterialWrapper::setDefaultFloat3Parameter, this);
18+
registerHybridMethod("setFloat4Parameter", &MaterialWrapper::setDefaultFloat4Parameter, this);
19+
registerHybridMethod("setIntParameter", &MaterialWrapper::setDefaultIntParameter, this);
2020
}
2121
std::shared_ptr<MaterialInstanceWrapper> MaterialWrapper::createInstance() {
2222
return pointee()->createInstance();
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RGBA } from './Color'
1+
import { Float3, Float4, Mat3f } from './Math'
22

33
export type CullingMode = 'none' | 'back' | 'front' | 'frontAndBack'
44

@@ -8,11 +8,15 @@ export interface MaterialInstance {
88
setCullingMode(mode: CullingMode): void
99
setTransparencyMode(mode: TransparencyMode): void
1010
changeAlpha(alpha: number): void
11-
setParameter(name: string, value: number): void
12-
/**
13-
* Changes the base color of the material.
14-
* Assumes linear (0-1) linear sRGB color space.
15-
*/
16-
setBaseColorSRGB(color: RGBA): void
11+
setFloatParameter(name: string, value: number): void
12+
setIntParameter(name: string, value: number): void
13+
setMat3fParameter(name: string, value: Mat3f): void
14+
setFloat3Parameter(name: string, vector: Float3): void
15+
setFloat4Parameter(name: string, vector: Float4): void
16+
getFloatParameter(name: string): number
17+
getIntParameter(name: string): number
18+
getMat3fParameter(name: string): Mat3f
19+
getFloat3Parameter(name: string): Float3
20+
getFloat4Parameter(name: string): Float4
1721
readonly name: string
1822
}

0 commit comments

Comments
 (0)