forked from Soft8Soft/verge3d-code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_buffergeometry_selective_draw.html
229 lines (148 loc) · 6.19 KB
/
webgl_buffergeometry_selective_draw.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D webgl - buffergeometry - selective - draw</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<script type="x-shader/x-vertex" id="vertexshader">
attribute float visible;
varying float vVisible;
attribute vec3 vertColor;
varying vec3 vColor;
void main() {
vColor = vertColor;
vVisible = visible;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="fragmentshader">
varying float vVisible;
varying vec3 vColor;
void main() {
if (vVisible > 0.0) {
gl_FragColor = vec4(vColor, 1.0);
} else {
discard;
}
}
</script>
</head>
<body>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> buffergeometry - selective - draw
<div id="title"></div>
<div id="ui"><a href="#" id="hideLines">CULL SOME LINES</a> - <a href="#" id="showAllLines">SHOW ALL LINES</a></div>
</div>
<script type="module">
import * as v3d from '../build/v3d.module.js';
import Stats from './jsm/libs/stats.module.js';
let camera, scene, renderer, stats;
let geometry, mesh;
const numLat = 100;
const numLng = 200;
let numLinesCulled = 0;
init();
animate();
function init() {
renderer = new v3d.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
scene = new v3d.Scene();
camera = new v3d.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 10);
camera.position.z = 3.5;
scene.add(new v3d.AmbientLight(0x444444));
stats = new Stats();
document.body.appendChild(stats.dom);
window.addEventListener('resize', onWindowResize, false);
addLines(1.0);
const hideLinesButton = document.getElementById('hideLines');
hideLinesButton.addEventListener('click', hideLines);
const showAllLinesButton = document.getElementById('showAllLines');
showAllLinesButton.addEventListener('click', showAllLines);
}
function addLines(radius) {
geometry = new v3d.BufferGeometry();
const linePositions = new Float32Array(numLat * numLng * 3 * 2);
const lineColors = new Float32Array(numLat * numLng * 3 * 2);
const visible = new Float32Array(numLat * numLng * 2);
for (let i = 0; i < numLat; ++ i) {
for (let j = 0; j < numLng; ++ j) {
const lat = (Math.random() * Math.PI) / 50.0 + i / numLat * Math.PI;
const lng = (Math.random() * Math.PI) / 50.0 + j / numLng * 2 * Math.PI;
const index = i * numLng + j;
linePositions[index * 6 + 0] = 0;
linePositions[index * 6 + 1] = 0;
linePositions[index * 6 + 2] = 0;
linePositions[index * 6 + 3] = radius * Math.sin(lat) * Math.cos(lng);
linePositions[index * 6 + 4] = radius * Math.cos(lat);
linePositions[index * 6 + 5] = radius * Math.sin(lat) * Math.sin(lng);
const color = new v3d.Color(0xffffff);
color.setHSL(lat / Math.PI, 1.0, 0.2);
lineColors[index * 6 + 0] = color.r;
lineColors[index * 6 + 1] = color.g;
lineColors[index * 6 + 2] = color.b;
color.setHSL(lat / Math.PI, 1.0, 0.7);
lineColors[index * 6 + 3] = color.r;
lineColors[index * 6 + 4] = color.g;
lineColors[index * 6 + 5] = color.b;
// non-0 is visible
visible[index * 2 + 0] = 1.0;
visible[index * 2 + 1] = 1.0;
}
}
geometry.setAttribute('position', new v3d.BufferAttribute(linePositions, 3));
geometry.setAttribute('vertColor', new v3d.BufferAttribute(lineColors, 3));
geometry.setAttribute('visible', new v3d.BufferAttribute(visible, 1));
geometry.computeBoundingSphere();
const shaderMaterial = new v3d.ShaderMaterial({
vertexShader: document.getElementById('vertexshader').textContent,
fragmentShader: document.getElementById('fragmentshader').textContent
});
mesh = new v3d.LineSegments(geometry, shaderMaterial);
scene.add(mesh);
updateCount();
}
function updateCount() {
const str = '1 draw call, ' + numLat * numLng + ' lines, ' + numLinesCulled + ' culled (<a target="_blank" href="http://callum.com">author</a>)';
document.getElementById('title').innerHTML = str.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function hideLines() {
for (let i = 0; i < geometry.attributes.visible.array.length; i += 2) {
if (Math.random() > 0.75) {
if (geometry.attributes.visible.array[i + 0]) {
++ numLinesCulled;
}
geometry.attributes.visible.array[i + 0] = 0;
geometry.attributes.visible.array[i + 1] = 0;
}
}
geometry.attributes.visible.needsUpdate = true;
updateCount();
}
function showAllLines() {
numLinesCulled = 0;
for (let i = 0; i < geometry.attributes.visible.array.length; i += 2) {
geometry.attributes.visible.array[i + 0] = 1;
geometry.attributes.visible.array[i + 1] = 1;
}
geometry.attributes.visible.needsUpdate = true;
updateCount();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
const time = Date.now() * 0.001;
mesh.rotation.x = time * 0.25;
mesh.rotation.y = time * 0.5;
stats.update();
renderer.render(scene, camera);
}
</script>
</body>
</html>