-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canvas15.vue
159 lines (132 loc) · 3.73 KB
/
Canvas15.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
<template>
<canvas
:style="resizeSmall.style"
id="canvas"
>
</canvas>
</template>
<script setup>
import * as THREE from 'three';
import Stats from 'three/examples/jsm/libs/stats.module.js';
import vertexShader from '@/assets/glsl/15/shader.vert';
import fragmentShader from '@/assets/glsl/15/shader.frag';
// 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;
let stats;
let canvas, scene, renderer, camera;
// extras
let mesh;
// 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,
10000
);
const instances = 300;
const seedColor = new THREE.Vector3(Math.random(),Math.random(),Math.random());
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(new THREE.Color(seedColor.x, seedColor.y, seedColor.z));
// shaders setup
const uniforms = {
u_time: { value: 0 },
seedColor: { value: seedColor }
}
const spread = 12 + Math.random() * 10;
class CustomSinCurve extends THREE.Curve {
constructor( scale = 10 ) {
super();
this.scale = scale;
}
getPoint( t, optionalTarget = new THREE.Vector3() ) {
const tx = t * spread - spread/2;
const ty = Math.sin( 2 * Math.PI * t );
const tz = 0;
return optionalTarget.set( tx, ty, tz ).multiplyScalar( this.scale );
}
}
const accuracy = 210;
const path = new CustomSinCurve( 50 );
const geometry = new THREE.TubeGeometry( path, accuracy, 10, 4, false );
const material = new THREE.ShaderMaterial( {
vertexShader,
fragmentShader,
uniforms: uniforms,
transparent: true
} );
mesh = new THREE.InstancedMesh( geometry, material, instances );
const matrix = new THREE.Matrix4();
for ( let i = 0; i < instances; i ++ ) {
const position = new THREE.Vector3(
0,
Math.random()*i*10,
-i*10
);
matrix.setPosition(position.x, position.y, position.z);
mesh.setMatrixAt( i, matrix );
}
scene.add(mesh);
camera.position.set(0,-250,500);
camera.lookAt( new THREE.Vector3(0, 100, -500) );
// 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);
stats.update();
// 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();
};
}
console.log(renderer.info);
})
</script>