/
Canvas18.vue
201 lines (167 loc) · 4.73 KB
/
Canvas18.vue
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
<template>
<canvas
:style="resizeSmall.style"
id="canvas"
>
</canvas>
</template>
<script setup>
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import Stats from 'three/examples/jsm/libs/stats.module.js';
import { SimplexNoise } from 'three/examples/jsm/math/SimplexNoise.js';
// dev vs prod, displaying stats/controls/recording accordingly
const dev = false;
const capture = false;
// record purposes
let capturer;
let recordingStop = 0;
let clock;
let delta = 0;
const deltaStep = 0.5;
const deltaStop = 1;
const frameRate = 1;
// app config
const appConfig = useAppConfig();
const appColors = appConfig.colors;
let stats;
let canvas, scene, renderer, camera;
const noise = new SimplexNoise();
// canvas sizes and record properties
const props = defineProps({
small: String,
record: String
})
// resizing canvas accordingly with the composable function compResize
const resizeSmall = computed(() => {
return compResize(props.small);
})
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
70,
resizeSmall._value.width / resizeSmall._value.height,
1,
3000
);
canvas = document.getElementById("canvas");
renderer = new THREE.WebGLRenderer({ antialias : true, canvas});
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize(resizeSmall._value.width, resizeSmall._value.height);
renderer.setClearColor(appColors.white);
const index = [];
let idxInc = 0; // index increment
const colors = [];
const lines = [];
// noise
const aOff = 0.01;
const bOff = 0.01;
let aInc = 0;
let bInc = 0;
let noiseVal;
// noiseZ
const aZOff = 0.002;
const bZOff = 0.01;
let aZInc = 0;
let bZInc = 0;
let noiseValZ;
const lineNumber = 50 + 50*Math.random();
const detail = 800; // number of points for a line
const detailFactor = 0.8 / 4; // x spread between each point
const lineSpacing = 2;
const spread = 1 + Math.random()*4;
let spreadInc = spread;
let geometryH;
let line;
for ( let i = -lineNumber/2; i < lineNumber/2; i ++ ) {
spreadInc -= spread/lineNumber;
const curvePoints = [];
for ( let j = -detail/2; j < detail/2; j++) {
aInc += aOff;
bInc += bOff;
noiseVal = noise.noise(aInc, bInc);
curvePoints.push(
new THREE.Vector3(
j * detailFactor, // x
noiseVal * spreadInc * (THREE.MathUtils.mapLinear(j, -detail/2, detail/2, 0, spreadInc)) + (i * lineSpacing), // y
0
)
);
colors.push(0, 0, 0);
index.push(idxInc, idxInc+1);
idxInc++;
}
const curve = new THREE.SplineCurve(curvePoints);
const points = curve.getPoints( detail );
colors.push(0, 0, 0);
idxInc++;
// noise Z
aZInc = 0;
bZInc = 0;
points.forEach((p, idx) => {
aZInc += aZOff;
bZInc += bZOff;
noiseValZ = noise.noise(aZInc, bZInc);
p.z = noiseValZ;
return p;
});
lines.push(points);
}
const material = new THREE.LineBasicMaterial( {
vertexColors: true
} );
geometryH = new THREE.BufferGeometry().setFromPoints( lines.flat() );
geometryH.setIndex(index);
geometryH.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
line = new THREE.LineSegments( geometryH, material );
// duplicating
const geometryV = new THREE.BufferGeometry();
geometryV.copy(geometryH);
const line2 = new THREE.LineSegments( geometryV, material );
line2.rotation.z = -Math.PI/2;
line2.rotation.y = -Math.PI;
line2.position.y -= lineSpacing/2;
line2.position.x -= lineSpacing/2;
scene.add( line );
scene.add( line2 );
camera.position.set(0,0,100);
camera.lookAt( scene.position );
// STATS AND CONTROLS
stats = new Stats();
if (dev) {
const controls = new OrbitControls( camera, renderer.domElement );
const domContainer = document.body.appendChild( stats.dom );
domContainer.style.top = "";
domContainer.style.bottom = "0";
}
// RECORDING SET UP
if (dev && capture) {
capturer = compInitCapture(capturer, props.record, clock, frameRate);
}
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
// RECORDING CYCLE
if (dev && capture) {
delta += deltaStep;
if (recordingStop < 1) {
recordingStop = compRecordCapture(capturer, canvas, recordingStop, delta, deltaStop);
}
}
}
onMounted(() => {
init();
animate();
window.addEventListener("resize", function() {
compOnWindowResize(camera, renderer, window);
});
const randBtn = document.getElementById('randomize');
if (randBtn != undefined) {
randBtn.onclick = function() {
init();
};
}
dev ? console.log(renderer.info) : undefined ;
})
</script>