<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rainy</title>
<script src="../../three-part/threejs/three.js"></script>
<script src="../../three-part/utils/stats.min.js"></script>
<script src="../../three-part/utils/dat.gui.min.js"></script>
<script src="../controls/TrackballControls.js"></script>
<script src="../util/util.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
init();
function init() {
// show FPS
let stats = initStats();
// resize
window.addEventListener('resize', onResize, false);
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(20, 40, 110);
camera.lookAt(new THREE.Vector3(20, 30, 0));
let renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0x000000));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.getElementById("container").appendChild(renderer.domElement);
// init trackball control
let trackballControls = initTrackballControls(camera, renderer);
let clock = new THREE.Clock();
let cloud;
// attributes which can be modified in GUI
const controls = {
"size" : 3,
"transparent" : true,
"opacity" : 0.6,
"color" : 0xffffff,
"sizeAttenuation" : true,
"redraw" : function(){
scene.remove(scene.getObjectByName("particle"));
createPointCloud();
}
};
// init GUI
initGUI();
createPointCloud();
renderScene();
function initGUI(){
let gui = new dat.GUI();
gui.add(controls, 'size', 0, 20).onChange(controls.redraw);
gui.add(controls, 'transparent').onChange(controls.redraw);
gui.add(controls, 'opacity', 0, 1).onChange(controls.redraw);
gui.addColor(controls, 'color').onChange(controls.redraw);
gui.add(controls, 'sizeAttenuation').onChange(controls.redraw);
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function createPointCloud() {
let texture = new THREE.TextureLoader().load("../assets/textures/particles/raindrop-3.png");
let geom = new THREE.Geometry();
let material = new THREE.PointsMaterial({
size: controls.size,
transparent: controls.transparent,
opacity: controls.opacity,
map: texture,
blending: THREE.AdditiveBlending,
sizeAttenuation: controls.sizeAttenuation,
color: controls.color
});
let range = 100;
for (let i = 0; i < 1500; i++) {
let particle = new THREE.Vector3(
Math.random() * range - range / 2,
Math.random() * range - range / 2,
1 + (i / 100)
);
particle.velocityY = 0.1 + Math.random() / 5;
particle.velocityX = (Math.random() - 0.5) / 3;
geom.vertices.push(particle);
}
cloud = new THREE.Points(geom, material);
cloud.sortParticles = true;
cloud.name = "particle";
scene.add(cloud);
}
function renderScene(){
trackballControls.update(clock.getDelta());
stats.update();
let vertices = cloud.geometry.vertices;
vertices.forEach(function (v) {
v.y = v.y - (v.velocityY);
v.x = v.x - (v.velocityX);
if (v.y <= 0) v.y = 60;
if (v.x <= -20 || v.x >= 20) v.velocityX = v.velocityX * -1;
});
cloud.geometry.verticesNeedUpdate = true;
requestAnimationFrame(renderScene);
renderer.render(scene, camera);
}
}
</script>
</body>
</html>
运行结果: