-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
195 lines (187 loc) · 8.35 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
body {
background: #000;
margin: 0;
color: #fff;
font-family: Helvetica, Arial;
}
canvas {
width: 100%;
height: 100vh;
vertical-align: top;
}
input,
select {
vertical-align: middle;
}
#selector {
position: absolute;
top: 0;
left: 0;
}
</style>
<script>
const url = new URL(location);
if (url.searchParams.get('webgpu')) {
if (!navigator.gpu) {
document.write('<h1>WebGPU is not supported on your platform so far.</h1>');
}
document.write('<script src="./dist/redcube.webgpu.js"><\/script>');
} else {
document.write('<script src="./dist/redcube.js"><\/script>');
}
</script>
</head>
<body>
<div>
<canvas id="canvas"></canvas>
<div id="selector"></div>
</div>
<script>
fetch('https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/model-index.json')
.then(r => r.json())
.then(samples => {
const name = localStorage.getItem('sample') || 'DamagedHelmet';
const canvas = document.getElementById('canvas');
const select = document.createElement('select');
select.onchange = () => {
localStorage.setItem('sample', select.value);
window.location.reload();
}
samples.forEach((it) => {
const option = document.createElement('option');
option.innerHTML = it.name;
option.value = it.name;
select.appendChild(option);
});
select.value = name;
selector.appendChild(select);
// ['bloom', 'ssao', 'shadow', 'light'].forEach(name => {
// const label = document.createElement('label');
// label.style.padding = '0 0 0 10px';
// label.innerHTML = name;
// const checkbox = document.createElement('input');
// checkbox.type = 'checkbox';
// checkbox.title = name;
// checkbox.checked = Boolean(localStorage.getItem(name));
// checkbox.onchange = () => {
// if (checkbox.checked) {
// localStorage.setItem(name, name);
// } else {
// localStorage.removeItem(name);
// }
// window.location.reload();
// };
// label.prepend(checkbox);
// selector.appendChild(label);
// });
const span = document.createElement('span');
span.style.padding = '0 10px';
span.innerHTML = 'light position';
const slider = document.createElement('input');
slider.oninput = () => {
renderer.light.update(Number(slider.value));
renderer.renderer.reflow = true;
renderer.renderer.needUpdateView = true;
};
slider.type = 'range';
slider.min = '0';
slider.max = `${Math.PI*2}`;
slider.step = '0.1';
slider.title = 'light';
span.prepend(slider);
selector.appendChild(span);
slider.value = 0;
const envname = localStorage.getItem('env') || 'env2';
{
const select = document.createElement('select');
select.onchange = () => {
localStorage.setItem('env', select.value);
window.location.reload();
}
['env', 'env1', 'env2', 'env3', 'env4'].forEach((it) => {
const option = document.createElement('option');
option.innerHTML = it;
option.value = it;
select.appendChild(option);
});
select.value = envname;
selector.appendChild(select);
}
const mode = localStorage.getItem('mode') || 'pbr';
{
const select = document.createElement('select');
select.onchange = () => {
localStorage.setItem('mode', select.value);
window.location.reload();
}
['phong', 'pbr'].forEach((it) => {
const option = document.createElement('option');
option.innerHTML = it;
option.value = it;
select.appendChild(option);
});
select.value = mode;
selector.appendChild(select);
}
//https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/
window.renderer = new redcube.RedCube(
'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/' + name + '/glTF/' + name + '.gltf',
canvas,
[],
`src/images/${localStorage.getItem('env') || 'env2'}.hdr`,
mode
);
renderer.init(() => {
if (renderer.parse.cameras.length) {
const animations = document.createElement('select');
animations.innerHTML = '<option hidden>animations</option>';
renderer.parse.tracks.forEach((it, i) => {
const option = document.createElement('option');
option.innerHTML = `animation${i}`;
option.value = i;
animations.appendChild(option);
});
animations.onchange = () => {
renderer.renderer.currentTrack = animations.value;
}
const variants = document.createElement('select');
variants.innerHTML = '<option hidden>variants</option>';
renderer.scene.variants.forEach((it,i) => {
const option = document.createElement('option');
option.innerHTML = it.name;
option.value = i;
variants.appendChild(option);
});
variants.onchange = () => {
renderer.setVariant(variants.value);
}
const select = document.createElement('select');
select.onchange = () => {
renderer.ioc._updateDep('camera', renderer.parse.cameras.find((it) => {
return Number(select.value) === it.name;
}));
renderer.renderer.needUpdateView = true;
renderer.draw();
}
renderer.parse.cameras.forEach((it) => {
const option = document.createElement('option');
option.innerHTML = it.name;
option.value = it.name;
select.appendChild(option);
});
select.value = renderer.parse.cameras[ renderer.parse.cameras.length - 1].name;
selector.appendChild(select);
selector.appendChild(animations);
selector.appendChild(variants);
}
});
});
</script>
</body>
</html>