forked from AliceO2Group/DebugGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
three.html
160 lines (133 loc) · 6.96 KB
/
three.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; background: grey; }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
new THREE.TextureLoader().load( "texture.png", ( map ) => {
fetch("data.json").then(
response => response.json()
).then( data => {
console.log(data);
var guniforms = {
tTex: { type: 't', value: null },
};
var ImVS = [
"attribute float alpha;",
"varying vec3 vColor;",
"varying vec2 vUv;",
"varying float vAlpha;",
"void main() {",
"vColor = color;",
"vUv = uv;",
"vAlpha = alpha;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}" ].join("\n");
var ImFS = [
"varying vec3 vColor;",
"varying vec2 vUv;",
"varying float vAlpha;",
"uniform sampler2D tTex;",
"void main() {",
"gl_FragColor = vec4( vColor, texture2D( tTex, vUv ).a * vAlpha );",
"}" ].join("\n");
var material = new THREE.ShaderMaterial( {
uniforms: guniforms,
vertexShader: ImVS,
fragmentShader: ImFS,
vertexColors: THREE.VertexColors,
transparent: true,
side: THREE.DoubleSide } );
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera( 0, window.innerWidth, 0, window.innerHeight, -1, 1 );
camera.position.z = 1;
var MAX_VERTICES = 65535;
geometry = new THREE.BufferGeometry();
geometry.setIndex( new THREE.BufferAttribute( new Uint16Array ( MAX_VERTICES ), 1) );
geometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( MAX_VERTICES * 3 ), 3 ) );
geometry.setAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( MAX_VERTICES * 2 ), 2 ) );
geometry.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( MAX_VERTICES * 3 ), 3 ) );
geometry.setAttribute( 'alpha', new THREE.BufferAttribute( new Float32Array( MAX_VERTICES ), 1 ) );
geometry.dynamic = true;
geometry.offsets = [ { start: 0, index: 0, count: 0 } ];
var mesh = new THREE.Mesh( geometry, material );
mesh.frustumCulled = false;
scene.add( mesh );
const posArr = geometry.attributes.position.array;
const colorArr = geometry.attributes.color.array;
const alphaArr = geometry.attributes.alpha.array;
const uvArray = geometry.attributes.uv.array;
const indexArray = geometry.index.array;
function updateBuffers(data) {
let idx = 0;
let index_idx = 0;
let index_length = 0;
let vtx_length = 0;
let all_cmds = []
for (const cmd of data) {
all_cmds.push(...cmd.cmd);
for (let i = 0; i < cmd.vtx.length; ++i) {
const raw_color = cmd.vtx[i][2];
posArr[idx*3] = cmd.vtx[i][0];
posArr[idx*3+1] = cmd.vtx[i][1];
posArr[idx*3+2] = 0;
colorArr[idx*3] = (raw_color & 255)/255;
colorArr[idx*3+1] = (raw_color>>8 & 255)/255;
colorArr[idx*3+2] = (raw_color>>16 & 255)/255;
alphaArr[idx] = (raw_color>>24 & 255)/255;
uvArray[idx*2] = cmd.vtx[i][3];
uvArray[idx*2+1] = 1-cmd.vtx[i][4];
idx++;
}
for (let i = 0; i < cmd.idx.length; ++i) {
indexArray[index_idx] = cmd.idx[i] + vtx_length;
index_idx++;
}
index_length += cmd.idx.length;
vtx_length += cmd.vtx.length;
}
geometry.attributes.position.needsUpdate = true;
geometry.attributes.uv.needsUpdate = true;
geometry.attributes.color.needsUpdate = true;
geometry.attributes.alpha.needsUpdate = true;
geometry.index.needsUpdate = true;
return all_cmds;
}
map.needsUpdate = true;
map.minFilter = map.magFilter = THREE.NearestFilter;
guniforms.tTex.value = map;
const renderer = new THREE.WebGLRenderer();
renderer.autoClear = false;
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.setScissorTest(true);
var frame_counter = 0;
var picture_counter = 0;
const animate = function () {
requestAnimationFrame(animate);
if (frame_counter % 10 == 0) {
all_cmds = updateBuffers(data[picture_counter]);
let curr_base = 0;
renderer.clear();
for (const cmd of all_cmds) {
renderer.setScissor(cmd.clp[0], window.innerHeight - cmd.clp[3], cmd.clp[2] - cmd.clp[0], cmd.clp[3] - cmd.clp[1]);
geometry.setDrawRange(curr_base, cmd.cnt);
renderer.render( scene, camera );
curr_base += cmd.cnt;
}
picture_counter = (picture_counter+1) % data.length;
}
frame_counter++;
}
animate();
});
});
</script>
</body>
</html>