-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canvas24.vue
133 lines (110 loc) · 2.98 KB
/
Canvas24.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
<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 vertexShader from '@/assets/glsl/24/shader.vert';
import fragmentShader from '@/assets/glsl/24/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;
// app config
const appConfig = useAppConfig();
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,
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("#fff");
// shaders setup
const uniforms = {
u_time: { value: 0 },
u_seed: { value: Math.random() }
}
const geometry = new THREE.PlaneGeometry(5,5, 64, 64);
const material = new THREE.ShaderMaterial({
vertexShader,
fragmentShader,
uniforms: uniforms
})
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
camera.position.set(0,0,5);
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);
const time = - performance.now() * 0.0005;
renderer.render(scene, camera);
stats.update();
// rendering actions
mesh.material.uniforms.u_time.value = time;
// 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>