three.js environment
时间: 2024-01-04 07:20:15 浏览: 119
Three.js是一个用于创建3D图形的JavaScript库。它提供了一个强大的环境,可以用来创建各种各样的3D场景和效果。在Three.js中,我们可以创建包括天空、水、地形等在内的各种环境效果。
关于Three.js环境的介绍和演示,以下是一个示例:
```javascript
// 创建场景
var scene = new THREE.Scene();
// 创建相机
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// 创建渲染器
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建天空盒子
var skyGeometry = new THREE.SphereGeometry(500, 60, 40);
var skyMaterial = new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load('sky.jpg'), side: THREE.BackSide });
var sky = new THREE.Mesh(skyGeometry, skyMaterial);
scene.add(sky);
// 创建水面
var waterGeometry = new THREE.PlaneBufferGeometry(10000, 10000);
var water = new THREE.Water(waterGeometry, {
textureWidth: 512,
textureHeight: 512,
waterNormals: new THREE.TextureLoader().load('water.jpg', function (texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
}),
alpha: 1.0,
sunDirection: new THREE.Vector3(),
sunColor: 0xffffff,
waterColor: 0x001e0f,
distortionScale: 3.7,
});
water.rotation.x = -Math.PI / 2;
scene.add(water);
// 创建光源
var light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(1, 1, 1).normalize();
scene.add(light);
// 动画循环
function animate() {
requestAnimationFrame(animate);
water.material.uniforms.time.value += 1.0 / 60.0;
renderer.render(scene, camera);
}
animate();
```
这个示例创建了一个包含天空和水面的Three.js环境。你可以根据自己的需求修改和扩展这个示例,添加更多的元素和效果。
阅读全文