-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
388 lines (277 loc) · 9.82 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<!DOCTYPE html>
<html>
<head>
<title>Color Gradients</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="./build/three.js"></script>
<script src="./js/Detector.js"></script>
<script src="./js/libs/stats.min.js"></script>
<script src="./js/controls/OrbitControls.js"></script>
<script src="./js/_custom/ColorGradient_Influence.js"></script>
<script src="./scripts/jscolor.js"></script>
<link rel="stylesheet" type="text/css" href="./style/common.css">
<style>
#canvas2d_wrapper {
position: absolute;
top: 0; left: 0;
width: 400px;
height: 200px;
}
#drawingcanvas {
display: none;
width: 100%;
}
#btn_resetColors {
position: relative;
top: 0;
right: 0;
width: 200px;
height: 30px;
background-color: #f30;
color: #000;
}
#btn_resetPattern {
position: relative;
top: 0;
right: 0;
width: 200px;
height: 30px;
background-color: #f90;
color: #000;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="palette">
<input class="jscolor" id="color_0" onchange="update( id, this.jscolor )" value="cc66ff">
<input class="jscolor" id="color_1" onchange="update( id, this.jscolor )" value="0099ff">
<input class="jscolor" id="color_2" onchange="update( id, this.jscolor )" value="0099ff">
<input class="jscolor" id="color_3" onchange="update( id, this.jscolor )" value="0099ff">
<input class="jscolor" id="color_4" onchange="update( id, this.jscolor )" value="0099ff">
<input class="jscolor" id="color_5" onchange="update( id, this.jscolor )" value="0099ff">
<div id="btn_resetColors" onclick="testRefreshColor()">Reset Colors</div>
<div id="btn_resetPattern" onclick="testRefreshPattern()">Reset Pattern</div>
</div>
<div id="canvas2d_wrapper"><canvas id="drawingcanvas" width="1024" height="512" ></canvas></div>
<script id="vertexShader" type="x-shader/x-vertex">
varying vec2 vUv;
void main() {
vUv = uv;
// vUv += vec2( 0.1, 0.1 );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>
<script id="fragment_shader_screen" type="x-shader/x-fragment">
precision highp float;
uniform sampler2D tDiffuse;
uniform float uTime;
varying vec2 vUv;
const float speed = 15.0;
const float magnitude = 0.15;
void main() {
vec2 wavyCoord;
wavyCoord.s = vUv.s + (sin(uTime+vUv.t*speed) * magnitude);
wavyCoord.t = vUv.t + (cos(uTime+vUv.s*speed) * magnitude);
vec4 frameColor = texture2D(tDiffuse, wavyCoord);
gl_FragColor = frameColor;
// vec4 frameColor = texture2D(tDiffuse, vUv);
// float luminance = frameColor.r * 0.3 + frameColor.g * 0.59 + frameColor.b * 0.11;
// gl_FragColor = vec4(luminance, luminance, sin(uTime), frameColor.a);
}
</script>
<script type="text/javascript">
function update( id, jscolor ) {
var color = jscolor.toHEXString().slice(1,7);
var index = id.split('').pop();
BaseCanvas2d.colorscheme_1[ index ] = color;
}
function testRefreshPattern() {
BaseCanvas2d.reset2dPattern();
BaseCanvas2d.drawCanvas2d();
}
function testRefreshColor() {
BaseCanvas2d.drawCanvas2d();
}
// 获取一个canvas的 drawing context
var drawingCanvas = document.getElementById( 'drawingcanvas' );
var BaseCanvas2d = {
colorscheme_0: [],
colorscheme_1: [],
pattern: [],
ctx: null,
loadColorScheme: function( url ) {
this.reset2dPattern(); // 首先重排一下2d canvas的样式
fetch( './scripts/Color.json' ).then( res => {
return res.json().then( json => {
var data = json;
this.colorscheme_0 = data['Color_fantasy'][0].colors;
this.colorscheme_1 = data['Color_fantasy'][1].colors;
});
}).then( () => {
this.drawCanvas2d();
});
},
reset2dPattern: function( url ) {
var cWidth = 1024;
var cHeight = 512;
for( var i=0; i<2; ++i ) {
for( var j=0; j<2; ++j ) {
// 随机设定喷圈位置
var x = Math.random()*cWidth;
var y = Math.random()*cHeight;
// 随机设定喷圈尺寸
var radius_inner = Math.random() * 50 + 50;
var radius_outer = Math.random() * 300 + 200;
// 随机设定喷圈颜色索引
var indices = [ 0, 1, 2, 3, 4, 5 ];
var color_index_0 = this.pick(indices);
var color_index_1 = this.pick(indices);
var color_index_2 = this.pick(indices);
// 产生pattern子集
this.pattern.push({
x: x,
y: y,
radius_inner: radius_inner,
radius_outer: radius_outer,
color_indices: [ color_index_0, color_index_1, color_index_2 ]
})
}
}
},
drawCanvas2d: function() {
// 先绘制出背景底色
this.ctx = drawingCanvas.getContext( '2d' );
var grd = this.ctx.createLinearGradient( 0, 0, 0, 512 );
grd.addColorStop( 0.0, '#' + this.colorscheme_0[0] + 'ff' );
grd.addColorStop( 0.3, '#' + this.colorscheme_0[1] + 'ff' );
grd.addColorStop( 0.7, '#' + this.colorscheme_0[2] + 'ff' );
grd.addColorStop( 1.0, '#' + this.colorscheme_0[3] + 'ff' );
this.ctx.fillStyle = grd;
this.ctx.fillRect( 0, 0, 1024, 512 );
// 然后绘制主体圆圈颜色
for( var i=0; i<this.pattern.length; ++i ) {
var spray = this.pattern[i];
var grd = this.ctx.createRadialGradient( spray.x, spray.y, spray.radius_inner, spray.x, spray.y, spray.radius_outer);
var c1 = this.colorscheme_1[ spray.color_indices[0] ];
var c2 = this.colorscheme_1[ spray.color_indices[1] ];
var c3 = this.colorscheme_1[ spray.color_indices[2] ];
grd.addColorStop( 0.0, '#' + c1 );
grd.addColorStop( 0.2, '#' + c2 );
grd.addColorStop( 1.0, '#' + c3 + '00' );
this.ctx.fillStyle = grd;
this.ctx.fillRect( 0, 0, 1024, 512 );
}
},
pick: function( array ) {
var rand = Math.floor( Math.random()*array.length );
var result = array[rand];
array.splice( rand, 1 );
return result;
}
};
BaseCanvas2d.loadColorScheme();
function addCanvasDraw() {
var x = Math.random() * 1024;
var y = Math.random() * 512;
var radius_inner = Math.random() * 50 + 50;
var radius_outer = Math.random() * 100 + 400;
var grd = ctx.createRadialGradient( x, y, radius_inner, x, y, radius_outer);
var r1 = Math.floor( Math.random()*4 );
var r2 = Math.floor( Math.random()*4 );
var r3 = Math.floor( Math.random()*4 );
grd.addColorStop( 0.0, '#' + colors[r1] );
// grd.addColorStop( 0.3, '#' + colors[r2] );
// grd.addColorStop( 0.6, '#' + colors[r3] );
grd.addColorStop( 1.0, '#' + colors[r3] + '00' );
ctx.fillStyle = grd;
ctx.fillRect( 0, 0, 1024, 512 );
}
</script>
<script type="text/javascript">
var cameraRTT, camera, sceneRTT, sceneScreen, scene, renderer, zmesh1, zmesh2;
var frame = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var rtTexture, material, quad;
init();
animate();
function init() {
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.autoClear = false;
container.appendChild( renderer.domElement );
// ---- Step 1 : rtTexture rendered
// ----
// camera rendering texture
cameraRTT = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
cameraRTT.position.z = 400;
// scene rendered to texture
sceneRTT = new THREE.Scene();
// render target texture
rtTexture = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat } );
// things on the texture
canvasMaterial = new THREE.MeshBasicMaterial( {
map: new THREE.Texture( drawingcanvas ),
} );
var canvas = new THREE.Mesh( new THREE.PlaneBufferGeometry(window.innerWidth,window.innerHeight), canvasMaterial );
sceneRTT.add( canvas );
// ---- Step 2 : paint
// ----
// camera
camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 0, 10000 );
camera.position.z = 2400;
// Scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x0099ff);
//
quad_geo = new THREE.PlaneGeometry( window.innerWidth, window.innerHeight, 99, 59 );
quad_mat = new THREE.ShaderMaterial( {
uniforms: {
tDiffuse: { value: rtTexture.texture },
uTime: { value: 0.0 }
},
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragment_shader_screen' ).textContent,
depthWrite: false,
// side: THREE.DoubleSide,
// wireframe: true
} );
scene.add( new THREE.Mesh( quad_geo, quad_mat ) );
// ---- Step 3 : Morphing the quad by Influence
// ----
ifl_01 = new Influence( 240, -200, 0, 900 );
ifl_01.surround( quad_geo );
ifl_01.moveTo( 260, 160, 400 );
ifl_02 = new Influence( -240, 200, 0, 900 );
ifl_02.surround( quad_geo );
ifl_02.moveTo( -260, -160, 400 );
// Controls
var controls = new THREE.OrbitControls( camera, renderer.domElement );
// Stats Info
stats = new Stats();
container.appendChild( stats.dom );
}
function animate() {
requestAnimationFrame( animate );
stats.update();
render();
}
function render() {
frame++;
var uTime = frame/50;
quad_mat.uniforms.uTime.value = uTime;
quad_mat.needsUpdate = true;
quad_geo.verticesNeedUpdate = true;
canvasMaterial.map.needsUpdate = true;
// render
renderer.clear();
renderer.render( sceneRTT, cameraRTT, rtTexture, true );
renderer.render( scene, camera );
}
</script>
</body>
</html>