请马上登录,朋友们都在花潮里等着你哦:)
您需要 登录 才可以下载或查看,没有账号?立即注册
x
<script type="module">
import * as THREE from 'https://esm.sh/three'; // 导入three模块
// 创建场景、相机和渲染器
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true }); // 反锯齿
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建一个立方体几何体
const geometry = new THREE.BoxGeometry(1, 1, 1);
// 创建两个每个面颜色随机的标准材质
const materials1 = Array.from( {length: 6}, () => new THREE.MeshStandardMaterial({ color: 0xffffff * Math.random() }) );
const materials2 = Array.from( {length: 6}, () => new THREE.MeshStandardMaterial({ color: 0xffffff * Math.random() }) );
// 创建网格对象
const cube1 = new THREE.Mesh(geometry, materials1);
const cube2 = new THREE.Mesh(geometry, materials2);
// 设置两个立方体的位置
cube1.position.set(-2, 0, 0);
cube2.position.set(2, 0, 0);
// 两个立方体加入到场景
scene.add(cube1);
scene.add(cube2);
// 添加半球光源 : 上白、下黑、强度1,模拟从天空到地面渐变的自然光
const hemiLight = new THREE.HemisphereLight(0xffffff, 0x000000, 1);
hemiLight.position.set(0, 100, 0); // 光源位置
scene.add(hemiLight);
// 设置相机位置
camera.position.z = 10;
// 渲染循环(自动执行)
(function animate() {
requestAnimationFrame(animate);
cube1.rotation.x -= 0.01;
cube1.rotation.y -= 0.01;
cube2.rotation.x += 0.01;
cube2.rotation.y += 0.01;
renderer.render(scene, camera);
})();
</script>
<style> body { margin: 0; } </style>
|