-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBufferGeometry-Shader.html
147 lines (93 loc) · 2.97 KB
/
BufferGeometry-Shader.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - raw shader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<script src="js/three.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">
uniform float time;
attribute vec4 color;
varying vec4 vColor;
varying vec3 pos;
void main() {
vColor = color;
pos = position;
vec3 newPos = vec3(position.x, position.y * sin(2.0*position.x + 0.2*time), position.z);
gl_Position = projectionMatrix * modelViewMatrix * vec4( newPos, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform float time;
varying vec4 vColor;
varying vec3 pos;
void main() {
vec4 color = vec4( vColor );
color.r = cos(time*2.0);
color.g = sin(pos.y*30.0);
gl_FragColor = color;
}
</script>
<script>
var camera, scene, renderer, mesh;
init();
render();
function init() {
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10 );
camera.position.z = 2;
scene = new THREE.Scene();
// geometry
var triangles = 1500;
var geometry = new THREE.BufferGeometry();
var vertices = new Float32Array( triangles * 3 * 3 );
for ( var i = 0, l = triangles * 3 * 3; i < l; i += 3 ) {
vertices[ i ] = Math.random() - 0.5;
vertices[ i + 1 ] = Math.random() - 0.5;
vertices[ i + 2 ] = Math.random() - 0.5;
}
geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
var colors = new Uint8Array( triangles * 3 * 4 );
for ( var i = 0, l = triangles * 3 * 4; i < l; i += 4 ) {
colors[ i ] = Math.random() * 255;
colors[ i + 1 ] = Math.random() * 255;
colors[ i + 2 ] = Math.random() * 255;
colors[ i + 3 ] = Math.random() * 255;
}
geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 4, true ) );
// material
var material = new THREE.ShaderMaterial( {
uniforms: {
time: { value: 1.0 }
},
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
side: THREE.DoubleSide,
transparent: true
} );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
}
//
function render() {
requestAnimationFrame( render );
var time = performance.now();
mesh.rotation.y += 0.01;
mesh.material.uniforms.time.value = time * 0.005;
renderer.render( scene, camera );
}
</script>
</body>
</html>