szamitogepes-grafika/BenedekLaszlo_IH1RZJ.html

161 lines
4.4 KiB
HTML
Raw Normal View History

2024-09-24 18:02:01 +00:00
<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2024-09-24 19:00:41 +00:00
<title>Benedek László - IH1RZJ - első beadandó: Lego Batman</title>
2024-09-24 18:02:01 +00:00
<script async src="./dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "./js-r167/build/three.module.min.js",
"TrackballControls": "./js-r167/examples/jsm/controls/TrackballControls.js"
}
}
</script>
<style>
body,
html canvas {
margin: 0;
border: 0;
padding: 0;
}
canvas {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script type="module">
import * as THREE from "three";
import { TrackballControls } from "TrackballControls";
class SceneLoader {
2024-09-30 09:51:39 +00:00
static FromJson(data) {
let _scene = new THREE.Scene();
const geometryTypes = {
2024-09-24 18:02:01 +00:00
"CircleGeometry": THREE.CircleGeometry,
"PlaneGeometry": THREE.PlaneGeometry,
"BoxGeometry": THREE.BoxGeometry,
"SphereGeometry": THREE.SphereGeometry,
"ConeGeometry": THREE.ConeGeometry,
"CylinderGeometry": THREE.CylinderGeometry,
"TorusGeometry": THREE.TorusGeometry,
};
2024-09-30 09:51:39 +00:00
const materialTypes = {
2024-09-24 18:02:01 +00:00
"MeshBasicMaterial": THREE.MeshBasicMaterial
};
let geometries = {};
let materials = {};
let meshes = {};
for (let [key, value] of Object.entries(data.geometries)) {
geometries[key] = new geometryTypes[value.type](...value.options);
}
for (let [key, value] of Object.entries(data.materials)) {
materials[key] = new materialTypes[value.type](value.options);
}
function createMesh(data) {
let mesh;
2024-09-30 09:51:39 +00:00
let type = data.type ? data.type : "Mesh";
switch (type) {
case "empty":
mesh = new THREE.Object3D();
break;
case "Mesh":
mesh = new THREE.Mesh(geometries[data.geometry], materials[data.material]);
break;
case "LineSegments":
mesh = new THREE.LineSegments(geometries[data.geometry], materials[data.material]);
break;
default:
throw "unknown mesh type";
2024-09-24 18:02:01 +00:00
}
2024-09-30 09:51:39 +00:00
if (data.position) mesh.position.set(...data.position);
if (data.rotation) mesh.rotation.set(...Array.from(data.rotation, x => THREE.MathUtils.degToRad(x)));
if (data.scale) mesh.scale.set(...data.scale);
2024-09-24 18:02:01 +00:00
if (data.children) {
for (let [key, value] of Object.entries(data.children)) {
mesh.add(createMesh(value));
}
}
return mesh;
}
for (let [key, value] of Object.entries(data.meshes)) {
2024-09-30 09:51:39 +00:00
_scene.add(meshes[key] = createMesh(value));
2024-09-24 18:02:01 +00:00
}
2024-09-30 09:51:39 +00:00
return _scene;
2024-09-24 18:02:01 +00:00
}
};
2024-09-30 09:51:39 +00:00
const fov = 75;
const near = 0.01;
const far = 1000;
const rotateSpeed = 5.0;
const panSpeed = 1.0;
let canvas, renderer, scene, camera, controls;
init().then(animate());
async function init() {
canvas = document.querySelector("#canvas");
renderer = new THREE.WebGLRenderer({ antialias: true, canvas: canvas, alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xadadad);
camera = new THREE.PerspectiveCamera(fov, window.innerWidth / window.innerHeight, near, far);
camera.position.set(0, 10, 50);
controls = new TrackballControls(camera, canvas);
controls.rotateSpeed = rotateSpeed;
controls.panSpeed = panSpeed;
scene = await fetch("scene.json")
.then((response) => response.json())
.catch(reason => console.log(`fetch failed: ${reason}`))
.then((json) => SceneLoader.FromJson(json));
window.addEventListener("resize", handleWindowResize, false);
}
function handleWindowResize() {
console.log(`resize: ${window.innerWidth}x${window.innerHeight}`)
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render();
}
function animate() {
requestAnimationFrame(animate);
controls.update();
render();
}
function render() {
renderer.render(scene, camera);
}
2024-09-24 18:02:01 +00:00
</script>
</body>
</html>