-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
255 lines (223 loc) · 6.45 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>
primitive-ellipsoid by Damien Seguin (https://github.com/dmnsgn)
</title>
<style>
:root {
--color-dark: #404040;
--color-light: #f2f2f2;
--color-accent: #fd5e62;
}
body {
margin: 0;
overscroll-behavior: none;
font-family: sans-serif;
color: var(--color-light);
background-color: var(--color-dark);
}
main {
position: absolute;
width: 100%;
height: 100%;
}
h1 {
position: absolute;
top: 0;
left: 20px;
}
</style>
</head>
<body>
<main>
<h1>primitive-ellipsoid</h1>
</main>
<script async src="web_modules/es-module-shims.js" type="module"></script>
<script src="web_modules/import-map.json" type="importmap"></script>
<script type="module">
import createEllipsoid from "./index.js";
import { mat4 } from "gl-matrix";
import io from "pex-io";
import createContext from "pex-context";
import pexCam from "pex-cam";
import { orbiter as createOrbiter } from "pex-cam";
import { Pane } from "tweakpane";
const { perspective: createCamera } = pexCam;
const modeOptions = ["texture", "normal", "flat-shaded", "uv"];
const CONFIG = {
mode: "texture",
};
const pane = new Pane();
pane.addInput(CONFIG, "mode", {
options: modeOptions.map((value) => ({
text: value.toUpperCase(),
value,
})),
});
// Geometry
const geometry = createEllipsoid(0.5);
// Render
const ctx = createContext();
const camera = createCamera({
fov: Math.PI / 3,
aspect: ctx.gl.drawingBufferWidth / ctx.gl.drawingBufferHeight,
near: 0.1,
far: 100,
position: [0, 0, 3],
});
createOrbiter({ camera });
const drawLines = {
pipeline: ctx.pipeline({
vert: /* glsl */ `
attribute vec3 aPosition;
attribute vec4 aColor;
uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
varying vec4 vColor;
void main () {
vColor = aColor;
vec3 position = aPosition;
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(position, 1.0);
}`,
frag: /* glsl */ `
precision highp float;
varying vec4 vColor;
void main () {
gl_FragColor = vColor;
}`,
depthTest: true,
primitive: ctx.Primitive.Lines,
}),
attributes: {
aPosition: ctx.vertexBuffer([
[0, 0, 0],
[1, 0, 0],
[0, 0, 0],
[0, 1, 0],
[0, 0, 0],
[0, 0, 1],
]),
aColor: ctx.vertexBuffer([
[1, 0, 0, 1],
[1, 0.5, 0.5, 1],
[0, 1, 0, 1],
[0.5, 1, 0.5, 1],
[0, 0, 1, 1],
[0.5, 0.5, 1, 1],
]),
},
indices: ctx.indexBuffer([
[0, 1],
[2, 3],
[4, 5],
]),
uniforms: {
uProjectionMatrix: null,
uViewMatrix: null,
uModelMatrix: mat4.create(),
},
};
const drawCmd = {
pipeline: ctx.pipeline({
vert: /* glsl */ `
attribute vec3 aPosition;
attribute vec3 aNormal;
attribute vec2 aUv;
uniform mat4 uProjectionMatrix;
uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uInverseViewMatrix;
varying vec3 vPositionWorld;
varying vec3 vPositionView;
varying vec3 vNormal;
varying vec2 vUv;
void main() {
vNormal = aNormal;
vUv = aUv;
vPositionWorld = (uModelMatrix * vec4(aPosition, 1.0)).xyz;
vPositionView = (uViewMatrix * vec4(vPositionWorld, 1.0)).xyz;
gl_Position = uProjectionMatrix * vec4(vPositionView, 1.0);
}
`,
frag: /* glsl */ `
#extension GL_OES_standard_derivatives : enable
precision highp float;
varying vec3 vNormal;
varying vec2 vUv;
uniform sampler2D uBaseColorMap;
uniform float uMode;
varying vec3 vPositionWorld;
void main() {
if (uMode == 0.0) gl_FragColor = texture2D(uBaseColorMap, vUv);
if (uMode == 1.0) gl_FragColor = vec4(vNormal * 0.5 + 0.5, 1.0);
if (uMode == 2.0) {
vec3 fdx = vec3(dFdx(vPositionWorld.x), dFdx(vPositionWorld.y), dFdx(vPositionWorld.z));
vec3 fdy = vec3(dFdy(vPositionWorld.x), dFdy(vPositionWorld.y), dFdy(vPositionWorld.z));
vec3 normal = normalize(cross(fdx, fdy));
gl_FragColor = vec4(normal * 0.5 + 0.5, 1.0);
}
if (uMode == 3.0) gl_FragColor = vec4(vUv.xy, 0.0, 1.0);
}
`,
depthTest: true,
primitive: ctx.Primitive.Triangles,
}),
attributes: {
aPosition: ctx.vertexBuffer(geometry.positions),
aNormal: ctx.vertexBuffer(geometry.normals),
aUv: ctx.vertexBuffer(geometry.uvs),
},
indices: ctx.indexBuffer(geometry.cells),
uniforms: {
uProjectionMatrix: camera.projectionMatrix,
uViewMatrix: camera.viewMatrix,
uModelMatrix: mat4.create(),
uBaseColorMap: null,
},
};
const clearCmd = {
pass: ctx.pass({
clearColor: [0.05, 0.05, 0.05, 1],
}),
};
io.loadImage("examples/uv.jpg").then((image) => {
const uvMap = ctx.texture2D({
width: 1024,
height: 1024,
pixelFormat: ctx.PixelFormat.RGBA8,
encoding: ctx.Encoding.Linear,
min: ctx.Filter.Linear,
mag: ctx.Filter.Linear,
wrap: ctx.Wrap.Repeat,
encoding: ctx.Encoding.SRGB,
flipY: true,
data: image,
});
drawCmd.uniforms.uBaseColorMap = uvMap;
ctx.frame(() => {
ctx.submit(clearCmd);
drawLines.uniforms.uProjectionMatrix = camera.projectionMatrix;
drawLines.uniforms.uViewMatrix = camera.viewMatrix;
ctx.submit(drawLines);
ctx.submit(drawCmd, {
uniforms: { uMode: modeOptions.indexOf(CONFIG.mode) },
});
});
const onResize = () => {
ctx.gl.canvas.width = window.innerWidth;
ctx.gl.canvas.height = window.innerHeight;
camera.set({
aspect: ctx.gl.canvas.width / ctx.gl.canvas.height,
});
};
onResize();
window.addEventListener("resize", onResize);
});
</script>
</body>
</html>