forked from markaren/threepp
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtransmission.cpp
More file actions
140 lines (122 loc) · 5.2 KB
/
Copy pathtransmission.cpp
File metadata and controls
140 lines (122 loc) · 5.2 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
#include "threepp/extras/imgui/RendererSettings.hpp"
#include "threepp/loaders/RGBELoader.hpp"
#include "threepp/threepp.hpp"
#include <cmath>
using namespace threepp;
int main() {
Canvas canvas("Transmission");
auto renderer = createRenderer(canvas);
renderer->toneMapping = ToneMapping::ACESFilmic;
renderer->toneMappingExposure = 1.0f;
auto scene = Scene::create();
auto camera = PerspectiveCamera::create(50, canvas.aspect(), 0.1f, 100);
camera->position.set(0, 0, 8);
scene->add(AmbientLight::create(0xffffff, 0.3f));
auto dirLight = DirectionalLight::create(0xffffff, 1.5f);
dirLight->position.set(5, 5, 5);
scene->add(dirLight);
RGBELoader hdrLoader;
if (auto hdrTex = hdrLoader.load(std::string(DATA_FOLDER) + "/textures/env/san_giuseppe_bridge/san_giuseppe_bridge_4k.hdr")) {
scene->background = hdrTex;
scene->environment = hdrTex;
}
// Colorful opaque background objects
std::array<Color, 6> colors = {
Color(0xe74c3c), Color(0x3498db), Color(0x2ecc71),
Color(0xf39c12), Color(0x9b59b6), Color(0x1abc9c)};
std::vector<std::shared_ptr<Mesh>> boxes;
auto boxGeo = BoxGeometry::create(1.2f, 1.2f, 1.2f);
for (int i = 0; i < 6; ++i) {
auto mat = MeshStandardMaterial::create();
mat->color = colors[i];
mat->roughness = 0.5f;
auto box = Mesh::create(boxGeo, mat);
float angle = static_cast<float>(i) / 6.f * math::TWO_PI;
box->position.set(std::cos(angle) * 3.0f, std::sin(angle) * 3.0f, -2.0f);
boxes.push_back(box);
scene->add(box);
}
// Glass sphere
auto sphereGeo = SphereGeometry::create(1.5f, 64, 32);
auto glassMat = MeshPhysicalMaterial::create();
glassMat->transmission = 1.0f;
glassMat->roughness = 0.0f;
glassMat->metalness = 0.0f;
glassMat->setIor(1.5f);
glassMat->thickness = 2.0f;
auto sphere = Mesh::create(sphereGeo, glassMat);
scene->add(sphere);
// ImGui params (local copies for sliders)
float transmission = glassMat->transmission;
float roughness = glassMat->roughness;
float metalness = glassMat->metalness;
float ior = glassMat->ior;
float thickness = glassMat->thickness;
float attenuationDistance = glassMat->attenuationDistance;
std::array<float, 3> attenuationColor = {1.f, 1.f, 1.f};
float iridescence = glassMat->iridescence;
float iridescenceIOR = glassMat->iridescenceIOR;
float iridescenceThicknessNm = glassMat->iridescenceThicknessNm;
RendererSettingsUi ui(canvas, *renderer, [&] {
if (ImGui::SliderFloat("Transmission", &transmission, 0.f, 1.f)) {
glassMat->transmission = transmission;
glassMat->needsUpdate();
}
if (ImGui::SliderFloat("Roughness", &roughness, 0.f, 1.f)) {
glassMat->roughness = roughness;
glassMat->needsUpdate();
}
if (ImGui::SliderFloat("Metalness", &metalness, 0.f, 1.f)) {
glassMat->metalness = metalness;
glassMat->needsUpdate();
}
if (ImGui::SliderFloat("IOR", &ior, 1.f, 2.333f)) {
glassMat->setIor(ior);
glassMat->needsUpdate();
}
if (ImGui::SliderFloat("Thickness", &thickness, 0.f, 5.f)) {
glassMat->thickness = thickness;
glassMat->needsUpdate();
}
if (ImGui::SliderFloat("Attenuation Dist.", &attenuationDistance, 0.f, 10.f)) {
glassMat->attenuationDistance = attenuationDistance;
glassMat->needsUpdate();
}
if (ImGui::ColorEdit3("Attenuation Color", attenuationColor.data())) {
glassMat->attenuationColor.setRGB(attenuationColor[0], attenuationColor[1], attenuationColor[2]);
glassMat->needsUpdate();
}
// KHR_materials_iridescence. Sweeping thickness is the interesting one:
// the hue cycles fastest in the 200-800 nm range and washes out to a
// flat achromatic tint above ~2000 nm, where the spectral terms die.
ImGui::SeparatorText("Iridescence");
if (ImGui::SliderFloat("Iridescence", &iridescence, 0.f, 1.f)) {
glassMat->iridescence = iridescence;
glassMat->needsUpdate();
}
if (ImGui::SliderFloat("Iridescence IOR", &iridescenceIOR, 1.f, 2.5f)) {
glassMat->iridescenceIOR = iridescenceIOR;
glassMat->needsUpdate();
}
if (ImGui::SliderFloat("Film Thickness (nm)", &iridescenceThicknessNm, 0.f, 1600.f)) {
glassMat->iridescenceThicknessNm = iridescenceThicknessNm;
glassMat->needsUpdate();
}
}, "Transmission");
OrbitControls controls(*camera, canvas);
canvas.onWindowResize([&](WindowSize size) {
camera->aspect = size.aspect();
camera->updateProjectionMatrix();
renderer->setSize(size);
});
Clock clock;
canvas.animate([&] {
auto t = clock.getElapsedTime();
for (size_t i = 0; i < boxes.size(); ++i) {
float angle = static_cast<float>(i) / 6.f * math::TWO_PI + t * 0.3f;
boxes[i]->position.set(std::cos(angle) * 3.0f, std::sin(angle) * 3.0f, -2.0f);
}
renderer->render(*scene, *camera);
ui.render();
});
}