-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.html
237 lines (175 loc) · 5.17 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<style type="text/css">
body {
overflow: hidden;
margin: 0;
}
</style>
<link href="" rel="stylesheet">
</head>
<body>
<div id="container"></div>
<script type="x-shader/x-vertex" id="vertexShader">
// uniform float amplitude;
// attribute float size;
uniform float amplitude;
attribute vec3 vertexColor;
varying vec4 varColor;
void main()
{
varColor = vec4(vertexColor, 1.0);
vec4 pos = vec4(position, 1.0);
pos.z *= amplitude;
vec4 mvPosition = modelViewMatrix * pos;
gl_PointSize = 1.0;
gl_Position = projectionMatrix * mvPosition;
}
</script>
<script type="x-shader/x-fragment" id="fragmentShader">
varying vec4 varColor;
void main()
{
gl_FragColor = varColor;
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r62/three.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/175711/trackball_ctrl_r62.js"></script>
<script>
var container;
var camera, scene, renderer;
var controls;
var shaderUniforms, shaderAttributes;
var particles = [];
var particleSystem;
var imageWidth = 640;
var imageHeight = 800;
var imageData = null;
var animationTime = 0;
var animationDelta = 0.03;
init();
// tick();
function init() {
createScene();
createControls();
createPixelData();
window.addEventListener('resize', onWindowResize, false);
}
function createScene() {
container = document.getElementById('container');
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(20, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.z = 3000;
camera.lookAt(scene.position)
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 1);
container.appendChild(renderer.domElement);
}
function createControls() {
controls = new THREE.TrackballControls(camera);
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = true;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
}
function createPixelData() {
var image = document.createElement("img");
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
image.crossOrigin = "Anonymous";
image.onload = function() {
image.width = canvas.width = imageWidth;
image.height = canvas.height = imageHeight;
console.log(image.height);
context.fillStyle = context.createPattern(image, 'no-repeat');
context.fillRect(0, 0, imageWidth, imageHeight);
//context.drawImage(image, 0, 0, imageWidth, imageHeight);
imageData = context.getImageData(0, 0, imageWidth, imageHeight).data;
createPaticles();
tick();
};
image.src = "http://nos.netease.com/fps-pro/frontends/house_special_2019/yujunhao/image/someone.jpg";
}
function createPaticles() {
var colors = [];
var weights = [0.2126, 0.7152, 0.0722];
var c = 0;
var geometry, material;
var x, y;
var zRange = 400;
geometry = new THREE.Geometry();
geometry.dynamic = false;
x = imageWidth * -0.5;
y = imageHeight * 0.5;
shaderAttributes = {
vertexColor: {
type: "c",
value: []
}
};
shaderUniforms = {
amplitude: {
type: "f",
value: 0.5
}
};
var shaderMaterial = new THREE.ShaderMaterial({
attributes: shaderAttributes,
uniforms: shaderUniforms,
vertexShader: document.getElementById("vertexShader").textContent,
fragmentShader: document.getElementById("fragmentShader").textContent
})
for (var i = 0; i < imageHeight; i++) {
for (var j = 0; j < imageWidth; j++) {
var color = new THREE.Color();
color.setRGB(imageData[c] / 255, imageData[c + 1] / 255, imageData[c + 2] / 255);
shaderAttributes.vertexColor.value.push(color);
var weight = color.r * weights[0] +
color.g * weights[1] +
color.b * weights[2];
var vertex = new THREE.Vector3();
vertex.x = x;
vertex.y = y;
vertex.z = (zRange * -0.5) + (zRange * weight);
geometry.vertices.push(vertex);
c += 4;
x++;
}
x = imageWidth * -0.5;
y--;
}
console.log(geometry.vertices.length)
particleSystem = new THREE.ParticleSystem(geometry, shaderMaterial);
scene.add(particleSystem);
}
function tick() {
requestAnimationFrame(tick);
update();
render();
}
function update() {
shaderUniforms.amplitude.value = Math.sin(animationTime);
animationTime += animationDelta;
controls.update();
}
function render() {
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
</html>