three写一个洋流特效
时间: 2024-05-10 14:14:13 浏览: 115
threejs写的一些特效
这里给出一个使用Three.js实现的海洋洋流特效的代码示例:
```javascript
// 创建场景、相机和渲染器
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建海面
var geometry = new THREE.PlaneGeometry(100, 100, 20, 20);
var material = new THREE.MeshPhongMaterial({color: 0x0077be, side: THREE.DoubleSide});
var plane = new THREE.Mesh(geometry, material);
plane.rotation.x = -Math.PI / 2;
scene.add(plane);
// 创建洋流
var flowGeometry = new THREE.Geometry();
var flowMaterial = new THREE.LineBasicMaterial({color: 0xffffff});
var flowPoints = [];
for (var i = 0; i < 20; i++) {
var x = Math.random() * 100 - 50;
var z = Math.random() * 100 - 50;
var y = Math.random() * 10 - 5;
flowPoints.push(new THREE.Vector3(x, y, z));
flowGeometry.vertices.push(flowPoints[i]);
}
var flow = new THREE.Line(flowGeometry, flowMaterial);
scene.add(flow);
// 创建光源
var light = new THREE.PointLight(0xffffff, 1, 100);
light.position.set(0, 10, 0);
scene.add(light);
// 添加动画
var animate = function () {
requestAnimationFrame(animate);
// 更新洋流
for (var i = 0; i < flowPoints.length; i++) {
flowPoints[i].x += Math.random() * 0.1 - 0.05;
flowPoints[i].z += Math.random() * 0.1 - 0.05;
flowPoints[i].y = Math.sin((i + flowPoints.length) * 0.1 + Date.now() * 0.001) * 2;
flowGeometry.verticesNeedUpdate = true;
}
renderer.render(scene, camera);
};
animate();
```
这段代码创建了一个海面和一个随机移动的洋流,并在洋流中添加了一个正弦波动的效果。你可以在自己的项目中使用这段代码,也可以根据需要进行修改和优化。
阅读全文