Spaces:
Running

MOP_tutorial / _prev.html
AK51's picture
Upload 50 files
3de135f verified
Raw History Blame Contribute Delete
3.47 kB
<!doctype html>
<html><head><meta charset="utf-8">
<style>body{margin:0;background:#000}div{height:380px;position:relative}p{position:absolute;left:6px;top:0;color:#fff;font:14px sans-serif}</style>
<script type="importmap">{"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"}}</script>
<script>
const _r=window.requestAnimationFrame.bind(window);
window.addEventListener("error",e=>document.body.insertAdjacentHTML("afterbegin","<pre style=color:red>"+e.message+"</pre>"));
window.addEventListener("unhandledrejection",e=>document.body.insertAdjacentHTML("afterbegin","<pre style=color:red>"+e.reason+"</pre>"));
</script></head><body>
<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
const names = (new URLSearchParams(location.search).get('m') || 'voyager,voyagerA,galileo,cassini,juno,hubble,hubbleB,jwst,jwstB,juice,clipper').split(',');
const view = new URLSearchParams(location.search).get('v') || '1,0.6,1.2';
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(360, 380); renderer.toneMapping = THREE.ACESFilmicToneMapping; renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const loader = new GLTFLoader().setMeshoptDecoder(MeshoptDecoder);
const pmrem = new THREE.PMREMGenerator(renderer);
const sky = await new THREE.TextureLoader().loadAsync('textures/milkyway.jpg');
sky.mapping = THREE.EquirectangularReflectionMapping; sky.colorSpace = THREE.SRGBColorSpace;
const envTex = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;
for (const n of names) {
const div = document.createElement('div'); document.body.appendChild(div);
const scene = new THREE.Scene(); scene.background = new THREE.Color(0x02030a);
scene.environment = envTex; scene.environmentIntensity = 0.55;
const sun = new THREE.DirectionalLight(0xfff3e0, 4); sun.position.set(5, 3, 4); sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048); Object.assign(sun.shadow.camera, { left: -2, right: 2, top: 2, bottom: -2, near: 0.1, far: 20 }); sun.shadow.bias = -0.0005; sun.shadow.normalBias = 0.02;
scene.add(sun, new THREE.AmbientLight(0x404860, 0.15));
const gltf = await loader.loadAsync(`models/${n}.glb`);
const obj = gltf.scene;
obj.updateMatrixWorld(true); const box = new THREE.Box3().setFromObject(obj, true), size = box.getSize(new THREE.Vector3()), c = box.getCenter(new THREE.Vector3());
const s = 2 / Math.max(size.x, size.y, size.z);
obj.position.sub(c).multiplyScalar(s); obj.scale.setScalar(s);
obj.traverse(o => { if (o.isMesh) { o.castShadow = o.receiveShadow = true; } });
scene.add(obj);
const cam = new THREE.PerspectiveCamera(40, 360 / 380, 0.01, 100);
const v = view.split(',').map(Number); cam.position.set(...v).normalize().multiplyScalar(3.4); cam.lookAt(0, 0, 0);
renderer.render(scene, cam);
const img = new Image(); img.src = renderer.domElement.toDataURL(); img.style.width = '100%'; div.appendChild(img);
div.insertAdjacentHTML('beforeend', `<p>${n} ${size.x.toFixed(1)}x${size.y.toFixed(1)}x${size.z.toFixed(1)}</p>`);
}
document.title = 'done';
</script></body></html>