-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathRNFViewWrapper.cpp
More file actions
270 lines (234 loc) · 11.1 KB
/
Copy pathRNFViewWrapper.cpp
File metadata and controls
270 lines (234 loc) · 11.1 KB
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
#include "RNFViewWrapper.h"
namespace margelo {
void ViewWrapper::loadHybridMethods() {
registerHybridMethod("getAspectRatio", &ViewWrapper::getAspectRatio, this);
registerHybridMethod("createAmbientOcclusionOptions", &ViewWrapper::createAmbientOcclusionOptions, this);
//FIXME: registerHybridMethod("createBloomOptions", &ViewWrapper::createBloomOptions, this);
registerHybridMethod("setAmbientOcclusionOptions", &ViewWrapper::setAmbientOcclusionOptions, this);
registerHybridMethod("getAmbientOcclusionOptions", &ViewWrapper::getAmbientOcclusionOptions, this);
registerHybridMethod("createDynamicResolutionOptions", &ViewWrapper::createDynamicResolutionOptions, this);
registerHybridMethod("setDynamicResolutionOptions", &ViewWrapper::setDynamicResolutionOptions, this);
registerHybridMethod("getDynamicResolutionOptions", &ViewWrapper::getDynamicResolutionOptions, this);
registerHybridSetter("temporalAntiAliasingOptions", &ViewWrapper::setTemporalAntiAliasingOptions, this);
registerHybridGetter("screenSpaceRefraction", &ViewWrapper::isScreenSpaceRefractionEnabled, this);
registerHybridSetter("screenSpaceRefraction", &ViewWrapper::setScreenSpaceRefractionEnabled, this);
registerHybridGetter("postProcessing", &ViewWrapper::isPostProcessingEnabled, this);
registerHybridSetter("postProcessing", &ViewWrapper::setPostProcessingEnabled, this);
registerHybridGetter("shadowing", &ViewWrapper::isShadowingEnabled, this);
registerHybridSetter("shadowing", &ViewWrapper::setShadowingEnabled, this);
registerHybridGetter("dithering", &ViewWrapper::getDithering, this);
registerHybridSetter("dithering", &ViewWrapper::setDithering, this);
registerHybridGetter("antiAliasing", &ViewWrapper::getAntiAliasing, this);
registerHybridSetter("antiAliasing", &ViewWrapper::setAntiAliasing, this);
registerHybridMethod("projectWorldToScreen", &ViewWrapper::projectWorldToScreen, this);
registerHybridMethod("pickEntity", &ViewWrapper::pickEntity, this);
registerHybridMethod("getViewport", &ViewWrapper::getViewport, this);
}
double ViewWrapper::getAspectRatio() {
uint32_t height = pointee()->getViewport().height;
if (height == 0) {
return 0;
}
uint32_t width = pointee()->getViewport().width;
if (width == 0) {
return 0;
}
return (double)width / height;
}
void ViewWrapper::setAmbientOcclusionOptions(std::shared_ptr<AmbientOcclusionOptionsWrapper> options) {
if (!options) {
[[unlikely]];
throw std::invalid_argument("AmbientOcclusionOptions is null");
}
std::unique_lock lock(_mutex);
pointee()->setAmbientOcclusionOptions(*options.get());
}
std::shared_ptr<AmbientOcclusionOptionsWrapper> ViewWrapper::getAmbientOcclusionOptions() {
return std::make_shared<AmbientOcclusionOptionsWrapper>(pointee()->getAmbientOcclusionOptions());
}
std::shared_ptr<AmbientOcclusionOptionsWrapper> ViewWrapper::createAmbientOcclusionOptions() {
return std::make_shared<AmbientOcclusionOptionsWrapper>();
}
std::shared_ptr<DynamicResolutionOptionsWrapper> ViewWrapper::createDynamicResolutionOptions() {
return std::make_shared<DynamicResolutionOptionsWrapper>();
}
void ViewWrapper::setDynamicResolutionOptions(std::shared_ptr<DynamicResolutionOptionsWrapper> options) {
if (!options) {
[[unlikely]];
throw std::invalid_argument("DynamicResolutionOptions is null");
}
std::unique_lock lock(_mutex);
pointee()->setDynamicResolutionOptions(*options.get());
}
std::shared_ptr<DynamicResolutionOptionsWrapper> ViewWrapper::getDynamicResolutionOptions() {
return std::make_shared<DynamicResolutionOptionsWrapper>(pointee()->getDynamicResolutionOptions());
}
void ViewWrapper::setTemporalAntiAliasingOptions(std::unordered_map<std::string, double> options) {
TemporalAntiAliasingOptions temporalAntiAliasingOptions;
if (options.find("enabled") != options.end()) {
temporalAntiAliasingOptions.enabled = options["enabled"] == 1.0;
}
if (options.find("upscaling") != options.end()) {
temporalAntiAliasingOptions.upscaling = options["upscaling"] == 1.0;
}
if (options.find("filterInput") != options.end()) {
temporalAntiAliasingOptions.filterInput = options["filterInput"] == 1.0;
}
if (options.find("filterHistory") != options.end()) {
temporalAntiAliasingOptions.filterHistory = options["filterHistory"] == 1.0;
}
if (options.find("useYCoCg") != options.end()) {
temporalAntiAliasingOptions.useYCoCg = options["useYCoCg"] == 1.0;
}
if (options.find("boxClipping") != options.end()) {
if (options["boxClipping"] == 0.0) {
temporalAntiAliasingOptions.boxClipping = TemporalAntiAliasingOptions::BoxClipping::ACCURATE;
} else if (options["boxClipping"] == 1.0) {
temporalAntiAliasingOptions.boxClipping = TemporalAntiAliasingOptions::BoxClipping::CLAMP;
} else if (options["boxClipping"] == 2.0) {
temporalAntiAliasingOptions.boxClipping = TemporalAntiAliasingOptions::BoxClipping::NONE;
} else {
[[unlikely]];
throw std::invalid_argument("Invalid boxClipping value");
}
}
if (options.find("jitterPattern") != options.end()) {
if (options["jitterPattern"] == 0.0) {
temporalAntiAliasingOptions.jitterPattern = TemporalAntiAliasingOptions::JitterPattern::RGSS_X4;
} else if (options["jitterPattern"] == 1.0) {
temporalAntiAliasingOptions.jitterPattern = TemporalAntiAliasingOptions::JitterPattern::UNIFORM_HELIX_X4;
} else if (options["jitterPattern"] == 2.0) {
temporalAntiAliasingOptions.jitterPattern = TemporalAntiAliasingOptions::JitterPattern::HALTON_23_X8;
} else if (options["jitterPattern"] == 3.0) {
temporalAntiAliasingOptions.jitterPattern = TemporalAntiAliasingOptions::JitterPattern::HALTON_23_X16;
} else if (options["jitterPattern"] == 4.0) {
temporalAntiAliasingOptions.jitterPattern = TemporalAntiAliasingOptions::JitterPattern::HALTON_23_X32;
} else {
[[unlikely]];
throw std::invalid_argument("Invalid boxClipping value");
}
}
if (options.find("feedback") != options.end()) {
temporalAntiAliasingOptions.feedback = options["feedback"];
}
if (options.find("lodBias") != options.end()) {
temporalAntiAliasingOptions.lodBias = options["lodBias"];
}
if (options.find("sharpness") != options.end()) {
temporalAntiAliasingOptions.sharpness = options["sharpness"];
}
if (options.find("upscaling") != options.end()) {
temporalAntiAliasingOptions.upscaling = options["upscaling"];
}
if (options.find("preventFlickering") != options.end()) {
temporalAntiAliasingOptions.preventFlickering = options["preventFlickering"] == 1.0;
}
if (options.find("varianceGamma") != options.end()) {
temporalAntiAliasingOptions.varianceGamma = options["varianceGamma"] == 1.0;
}
if (options.find("historyReprojection") != options.end()) {
temporalAntiAliasingOptions.historyReprojection = options["historyReprojection"] == 1.0;
}
pointee()->setTemporalAntiAliasingOptions(temporalAntiAliasingOptions);
}
void ViewWrapper::setPostProcessingEnabled(bool enabled) {
pointee()->setPostProcessingEnabled(enabled);
}
bool ViewWrapper::isPostProcessingEnabled() {
return pointee()->isPostProcessingEnabled();
}
void ViewWrapper::setScreenSpaceRefractionEnabled(bool enabled) {
pointee()->setScreenSpaceRefractionEnabled(enabled);
}
bool ViewWrapper::isScreenSpaceRefractionEnabled() {
return pointee()->isScreenSpaceRefractionEnabled();
}
bool ViewWrapper::isShadowingEnabled() {
return pointee()->isShadowingEnabled();
}
void ViewWrapper::setShadowingEnabled(bool enabled) {
pointee()->setShadowingEnabled(enabled);
}
std::string ViewWrapper::getDithering() {
std::string ditheringStr;
EnumMapper::convertEnumToJSUnion(pointee()->getDithering(), &ditheringStr);
return ditheringStr;
}
void ViewWrapper::setDithering(const std::string& dithering) {
Dithering ditheringEnum;
EnumMapper::convertJSUnionToEnum(dithering, &ditheringEnum);
pointee()->setDithering(ditheringEnum);
}
std::string ViewWrapper::getAntiAliasing() {
std::string antiAliasingStr;
EnumMapper::convertEnumToJSUnion(pointee()->getAntiAliasing(), &antiAliasingStr);
return antiAliasingStr;
}
void ViewWrapper::setAntiAliasing(const std::string& antiAliasing) {
AntiAliasing antiAliasingEnum;
EnumMapper::convertJSUnionToEnum(antiAliasing, &antiAliasingEnum);
pointee()->setAntiAliasing(antiAliasingEnum);
}
std::vector<double> ViewWrapper::projectWorldToScreen(std::vector<double> worldCoordinates) {
if (worldCoordinates.size() != 3) {
[[unlikely]];
throw std::invalid_argument("worldCoordinates must be an array of 3 elements");
}
// Get camera
Camera& camera = pointee()->getCamera();
// Get viewport
Viewport viewport = pointee()->getViewport();
// Get projection matrix
math::mat4 projectionMatrix = camera.getProjectionMatrix();
// Get view matrix
math::mat4 viewMatrix = camera.getViewMatrix();
// World coords to vec4
math::float4 worldCoordinatesFloat4 = math::float4(worldCoordinates[0], worldCoordinates[1], worldCoordinates[2], 1.0f);
// Transform to camera view space
math::float4 viewSpaceCoordinates = viewMatrix * worldCoordinatesFloat4;
// Transform to clip space
math::float4 clipSpaceCoordinates = projectionMatrix * viewSpaceCoordinates;
// Normalize to device coordinates (NDC)
math::float2 ndc;
ndc.x = clipSpaceCoordinates.x / clipSpaceCoordinates.w;
ndc.y = clipSpaceCoordinates.y / clipSpaceCoordinates.w;
float screenWidth = viewport.width / _densityPixelRatio;
float screenHeight = viewport.height / _densityPixelRatio;
// Map NDC to screen coordinates
std::vector<double> screenCoordinates(2);
screenCoordinates[0] = (ndc.x + 1.0) * (screenWidth / 2.0);
screenCoordinates[1] = screenHeight * (1.0 - ((ndc.y + 1.0) / 2.0));
return screenCoordinates;
}
std::future<std::optional<std::shared_ptr<EntityWrapper>>> ViewWrapper::pickEntity(double x, double y) {
std::promise<std::optional<std::shared_ptr<EntityWrapper>>> promise;
std::future<std::optional<std::shared_ptr<EntityWrapper>>> future = promise.get_future();
// Adjust DP value from react native to actual viewport / screen pixel
x *= _densityPixelRatio;
y *= _densityPixelRatio;
// The y coordinate we receive has its origin at the top of the screen, but Filament expects it to be at the bottom
y = pointee()->getViewport().height - y;
Logger::log(TAG, "Picking entity at (%f, %f)...", x, y);
pointee()->pick(x, y, [promise = std::move(promise)](View::PickingQueryResult result) mutable {
Entity entity = result.renderable;
if (entity.isNull()) {
Logger::log(TAG, "No entity picked!");
promise.set_value(std::nullopt);
} else {
Logger::log(TAG, "Entity picked with id %d!", entity.getId());
std::shared_ptr<EntityWrapper> entityWrapper = std::make_shared<EntityWrapper>(entity);
promise.set_value(std::optional(entityWrapper));
}
});
return future;
}
std::unordered_map<std::string, int> ViewWrapper::getViewport() {
std::unordered_map<std::string, int> viewport;
Viewport vp = pointee()->getViewport();
viewport["width"] = vp.width;
viewport["height"] = vp.height;
viewport["bottom"] = vp.bottom;
viewport["left"] = vp.left;
return viewport;
}
} // namespace margelo