<html>
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script type="module">
import * as THREE from 'https://unpkg.com/three/build/three.module.js';
//建立一个场景,放置物体、灯光和摄像机的地方
const scene = new THREE.Scene();
//透视相机,用来模拟人眼所看到的景象,它是3D场景的渲染中使用得最普遍的投影模式。
const camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
//渲染器,用WebGL渲染出场景
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 texture = new THREE.TextureLoader().load('./img/980.jpg');
//纹理材质
const material = new THREE.MeshBasicMaterial( { map: texture } );
//组合几何体与纹理
const cube = new THREE.Mesh( geometry, material );
//加入场景
scene.add( cube );
camera.position.z = 5;
function animate() {
requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
}
animate();
</script>
</body>
</html>
Threejs实现纹理贴图
最新推荐文章于 2024-07-16 09:50:03 发布