spiral_calendar/view/graphics.js

109 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-11-30 16:19:45 +03:00
import * as THREE from 'three';
2023-11-30 19:31:13 +03:00
import { SpiralSegment } from './spiral.js';
2023-11-30 16:19:45 +03:00
const spiralHeight = 2;
let spiralMaterial = new THREE.MeshPhongMaterial({color: 0xffffff});
2023-12-01 12:57:41 +03:00
let distance = 5;
2023-11-30 16:19:45 +03:00
const scene = new THREE.Scene();
var canvas = document.querySelector('#canvas');
const renderer = new THREE.WebGLRenderer({ antialias: true, canvas });
document.body.appendChild( renderer.domElement );
const camera = new THREE.PerspectiveCamera(75, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
2023-12-01 12:57:41 +03:00
camera.position.z = distance;
2023-11-30 16:19:45 +03:00
// setting light
const pointLight = new THREE.PointLight( 0xffffff, 1);
pointLight.position.set( 0, 0, 1 );
const ambientLight = new THREE.AmbientLight(0xffffff, 10);
scene.add(pointLight);
scene.add(ambientLight);
let segments = []
2023-11-30 19:31:13 +03:00
for (let i = 0; i < 40; i ++) {
let color = `#${(Math.floor(Math.random() * 16777215)).toString(16)}`;
let material = new THREE.MeshPhongMaterial({color: color});
segments.push(new SpiralSegment(i - 20, spiralHeight, material).getSegment());
scene.add(segments[i]);
2023-11-30 16:19:45 +03:00
}
2023-12-01 12:57:41 +03:00
export const rotateCamera = (x, y) => {
camera.rotateX(THREE.MathUtils.degToRad(x))
camera.rotateY(THREE.MathUtils.degToRad(y))
console.log(`Rotating camera by (${x};${y})`)
}
export const getCameraRotation = () => {
return camera.rotation
}
export const getCameraQuaternion = () => {
return camera.quaternion
}
export const getCameraPosition = () => {
return camera.position
}
export const setCameraPostion = (x, y ,z) => {
if (x != undefined) camera.position.x = x;
if (y != undefined) camera.position.y = y;
if (z != undefined) camera.position.z = z;
console.log(`Setting camera position to (${x};${y};${z})`)
}
export const getCanvasSize = () => {
return {width: canvas.width, height: canvas.height}
}
export const getDistance = () => {
return distance
}
export const setDistance = (d) => {
distance = d;
}
// console.log((THREE.MathUtils.radToDeg(camera.rotation._y)));
// camera.rotateY(THREE.MathUtils.degToRad(90));
// console.log((THREE.MathUtils.radToDeg(camera.rotation._y)));
// camera.rotateY(THREE.MathUtils.degToRad(90));
// console.log((THREE.MathUtils.radToDeg(camera.rotation._y)));
// camera.rotateY(THREE.MathUtils.degToRad(90));
// console.log((THREE.MathUtils.radToDeg(camera.rotation._y)));
// camera.rotateY(THREE.MathUtils.degToRad(90));
// console.log((THREE.MathUtils.radToDeg(camera.rotation._y)));
2023-11-30 16:19:45 +03:00
//main loop
function animate() {
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
2023-12-01 12:57:41 +03:00
camera.lookAt(0, 0, 0);
2023-11-30 16:19:45 +03:00
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) renderer.setSize(width, height, false);
return needResize;
}
animate();