import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
1000
);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const parentMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const cube = new THREE.Mesh(geometry, material);
const parentCub = new THREE.Mesh(geometry, parentMaterial);
parentCub.add(cube);
scene.add(parentCub);
cube.position.set(-3, 0, 0);
parentCub.position.set(3, 0, 0);
parentCub.scale.set(2, 2, 2);
cube.scale.set(2, 2, 2);
parentCub.rotation.x = Math.PI/4;
cube.rotation.x = Math.PI/4;
camera.position.z = 5;
camera.position.y = 2;
camera.position.x = 5;
camera.lookAt(0,0,0)
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.autoRotate = true;
controls.autoRotateSpeed = 4;
function animate() {
controls.update();
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
})
let btn = document.createElement('button');
btn.innerHTML = "点击全屏";
btn.style.position = "absolute";
btn.style.top = "10px";
btn.style.left = "10px";
btn.onclick = function() {
document.body.requestFullscreen();
}
document.body.appendChild(btn);
let exitBtn = document.createElement('button');
exitBtn.innerHTML = "退出全屏";
exitBtn.style.position = "absolute";
exitBtn.style.top = "10px";
exitBtn.style.left = "100px";
exitBtn.style.zIndex = 1000;
exitBtn.onclick = function() {
document.exitFullscreen();
}
document.body.appendChild(exitBtn);