<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Point Cloud</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, 0, 150);
camera.lookAt(scene.position);
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" : 4,
"transparent" : true,
"opacity" : 0.6,
"vertexColors" : true,
"color" : 0xffffff,
"sizeAttenuation" : true,
"rotate" : true,
"redraw" : function(){
if(scene.getObjectByName("particles")){
scene.remove(scene.getObjectByName("particles"));
}
createParticles();
}
};
// init GUI
initGUI();
createParticles();
renderScene();
function initGUI(){
let gui = new dat.GUI();
gui.add(controls, 'size', 0, 10).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.addColor(controls, 'vertexColor').onChange(controls.redraw);
gui.add(controls, 'sizeAttenuation').onChange(controls.redraw);
gui.add(controls, 'rotate');
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function createGhostTexture() {
let canvas = document.createElement('canvas');
canvas.width = 32;
canvas.height = 32;
let ctx = canvas.getContext('2d');
// the body
ctx.translate(-81, -84);
ctx.fillStyle = "orange";
ctx.beginPath();
ctx.moveTo(83, 116);
ctx.lineTo(83, 102);
ctx.bezierCurveTo(83, 94, 89, 88, 97, 88);
ctx.bezierCurveTo(105, 88, 111, 94, 111, 102);
ctx.lineTo(111, 116);
ctx.lineTo(106.333, 111.333);
ctx.lineTo(101.666, 116);
ctx.lineTo(97, 111.333);
ctx.lineTo(92.333, 116);
ctx.lineTo(87.666, 111.333);
ctx.lineTo(83, 116);
ctx.fill();
// the eyes
ctx.fillStyle = "white";
ctx.beginPath();
ctx.moveTo(91, 96);
ctx.bezierCurveTo(88, 96, 87, 99, 87, 101);
ctx.bezierCurveTo(87, 103, 88, 106, 91, 106);
ctx.bezierCurveTo(94, 106, 95, 103, 95, 101);
ctx.bezierCurveTo(95, 99, 94, 96, 91, 96);
ctx.moveTo(103, 96);
ctx.bezierCurveTo(100, 96, 99, 99, 99, 101);
ctx.bezierCurveTo(99, 103, 100, 106, 103, 106);
ctx.bezierCurveTo(106, 106, 107, 103, 107, 101);
ctx.bezierCurveTo(107, 99, 106, 96, 103, 96);
ctx.fill();
// the pupils
ctx.fillStyle = "blue";
ctx.beginPath();
ctx.arc(101, 102, 2, 0, Math.PI * 2, true);
ctx.fill();
ctx.beginPath();
ctx.arc(89, 102, 2, 0, Math.PI * 2, true);
ctx.fill();
let texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
return texture;
}
function createParticles() {
let geom = new THREE.Geometry();
let material = new THREE.PointsMaterial({
size: controls.size,
transparent: controls.transparent,
opacity: controls.opacity,
vertexColors: controls.vertexColors,
sizeAttenuation: controls.sizeAttenuation,
map: createGhostTexture(),
color: new THREE.Color(controls.color)
});
let range = 500;
for (let i = 0; i < 15000; i++) {
let particle = new THREE.Vector3(
Math.random() * range - range / 2,
Math.random() * range - range / 2,
Math.random() * range - range / 2
);
geom.vertices.push(particle);
let color = new THREE.Color(controls.vertexColorValue);
let asHSL = {};
color.getHSL(asHSL);
color.setHSL(asHSL.h, asHSL.s, asHSL.l * Math.random());
geom.colors.push(color);
}
cloud = new THREE.Points(geom, material);
cloud.name = "particles";
scene.add(cloud);
}
function renderScene(){
trackballControls.update(clock.getDelta());
stats.update();
if (controls.rotate) {
cloud.rotation.x += 0.01;
cloud.rotation.z += 0.01;
}
requestAnimationFrame(renderScene);
renderer.render(scene, camera);
}
}
</script>
</body>
</html>
运行结果:
网友评论