This repository has been archived by the owner on Nov 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (195 loc) · 4.95 KB
/
index.js
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
var Geometry = require("gl-geometry");
var glShader = require("gl-shader");
var glslify = require("glslify");
var THREE = require("three");
var dat = require("dat.gui");
var Empty = Object.freeze([]);
// var geometry = new THREE.PlaneBufferGeometry(2, 2);
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
var geometry = new THREE.BoxGeometry(WIDTH, WIDTH, 1);
// var camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
var camera = new THREE.PerspectiveCamera(45, 1.5, 1, 1000);
var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer();
var startTime;
var uniforms;
var mesh;
function init() {
var container = document.createElement("div");
document.body.appendChild(container);
camera.position.z = 100;
scene.add(camera);
// Uniform state
var state = {
fractalBounds: {
x: -0.611,
y: 0.74486,
z: 0.0,
w: 0.3
},
easings: {
x: 3,
y: 4,
z: 5
},
localPosition: {
x: 0.5,
y: 2.1,
z: 0.3
},
offset: {
x: 0.0,
y: 0.0,
z: 0.0
},
intervals: {
x: 0.225,
y: 0.732,
z: 0.997
}
};
// Geometry
uniforms = {
time: { value: 1.0 },
resolution: { value: new THREE.Vector2() },
uFractalBounds: {
type: "4v",
value: new THREE.Vector4(
state.fractalBounds.x,
state.fractalBounds.y,
state.fractalBounds.z,
state.fractalBounds.w
)
},
uEasings: {
type: "3v",
value: new THREE.Vector3(
state.easings.x,
state.easings.y,
state.easings.z
)
},
uLocalPosition: {
type: "3v",
value: new THREE.Vector3(
state.localPosition.x,
state.localPosition.y,
state.localPosition.z
)
},
uIntervals: {
type: "3v",
value: new THREE.Vector3(
state.intervals.x,
state.intervals.y,
state.intervals.z
)
},
uOffset: {
type: "2v",
value: new THREE.Vector2(
state.offset.x,
state.offset.y
)
},
uResolution: {
type: "2v",
value: new THREE.Vector2(window.innerWidth, window.innerHeight)
},
uTime: { value: 1.0 }
};
var customMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: glslify("./vert.vert"),
fragmentShader: glslify("./frag.frag")
});
mesh = new THREE.Mesh(geometry, customMaterial);
scene.add(mesh);
mesh.rotation.set(0., 0., 0.);
// Renderer
// renderer.setPixelRatio(window.devicePixelRatio);
renderer.preserveDrawingBuffer = true;
renderer.setClearColor(0x000000, 1);
container.appendChild(renderer.domElement);
onWindowResize();
window.addEventListener("resize", onWindowResize, false);
// GUI
var gui = new dat.GUI();
var MIN_STEP = 0.01;
var folder = gui.addFolder("Position");
folder
.add(state.localPosition, "x", -100.0, 100.0, MIN_STEP)
.name("X")
.onChange(function(value) {
state.localPosition.x = value;
uniforms.uLocalPosition.value = new THREE.Vector3(
state.localPosition.x,
state.localPosition.y,
state.localPosition.z
);
});
folder
.add(state.localPosition, "y", -100.0, 100.0, MIN_STEP)
.name("Y")
.onChange(function(value) {
state.localPosition.y = value;
uniforms.uLocalPosition.value = new THREE.Vector3(
state.localPosition.x,
state.localPosition.y,
state.localPosition.z
);
});
folder
.add(state.localPosition, "z", -100.0, 100.0, MIN_STEP)
.name("Z")
.onChange(function(value) {
state.localPosition.z = value;
uniforms.uLocalPosition.value = new THREE.Vector3(
state.localPosition.x,
state.localPosition.y,
state.localPosition.z
);
});
var folder = gui.addFolder("Offset");
folder
.add(state.offset, "x", -1.0, 1.0, MIN_STEP)
.name("X")
.onChange(function(value) {
state.offset.x = value;
uniforms.uOffset.value = new THREE.Vector2(
state.offset.x,
state.offset.y
);
});
folder
.add(state.offset, "y", -1.0, 1.0, MIN_STEP)
.name("Y")
.onChange(function(value) {
state.offset.y = value;
uniforms.uOffset.value = new THREE.Vector2(
state.offset.x,
state.offset.y
);
});
// Start
startTime = Date.now();
}
function onWindowResize() {
uniforms.uResolution = {
type: "2v",
value: new THREE.Vector2(window.innerWidth, window.innerHeight)
};
camera.aspect = window.innerWidth / window.innerHeight;
// camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate(timestamp) {
requestAnimationFrame(animate);
uniforms.uTime.value = timestamp / 1000;
mesh.rotation.y = Math.sin(uniforms.uTime.value * 0.3) + Math.PI;
mesh.rotation.x = Math.sin(uniforms.uTime.value * 0.3) + Math.PI;
renderer.render(scene, camera);
}
init();
animate();