<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Geometries</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.x = -50;
camera.position.y = 30;
camera.position.z = 20;
camera.lookAt(new THREE.Vector3(-10, 0, 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();
// add a plane
let planeGeometry = new THREE.PlaneGeometry(60, 40, 1, 1);
let planeMaterial = new THREE.MeshLambertMaterial({
color: 0xffffff
});
let plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true;
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 0;
plane.position.y = 0;
plane.position.z = 0;
scene.add(plane);
// add ambient lighting
let ambientLight = new THREE.AmbientLight(0x555555);
scene.add(ambientLight);
// add spotlight
let spotLight = new THREE.SpotLight(0xffffff, 1.2, 150, Math.PI / 4, 0, 2);
spotLight.shadow.mapSize.height = 1024;
spotLight.shadow.mapSize.width = 1024;
spotLight.position.set(-40, 30, 30);
spotLight.castShadow = true;
scene.add(spotLight);
addGeometries(scene);
// attributes which can be modified in GUI
const controls = {
};
// init GUI
initGUI();
renderScene();
function initGUI(){
let gui = new dat.GUI();
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function addGeometries(scene) {
let geometries = [];
geometries.push(new THREE.CylinderGeometry(1, 4, 4));
geometries.push(new THREE.BoxGeometry(2, 2, 2));
geometries.push(new THREE.SphereGeometry(2));
geometries.push(new THREE.IcosahedronGeometry(4));
let points = [];
let detail = 0.1;
let radius = 3;
for(let angle = 0.0; angle<Math.PI; angle+=detail){
points.push(new THREE.Vector3(Math.cos(angle) * radius, 0, Math.sin(angle) * radius));
}
geometries.push(new THREE.LatheGeometry(points, 12));
geometries.push(new THREE.OctahedronGeometry(3));
geometries.push(new THREE.TetrahedronGeometry(3));
geometries.push(new THREE.TorusGeometry(3, 1, 10, 10));
geometries.push(new THREE.TorusKnotGeometry(3, 0.5, 50, 20));
let j = 0;
geometries.forEach((v, i) => {
let material = new THREE.MeshLambertMaterial({
color : Math.random() * 0xffffff
});
let mesh = new THREE.Mesh(v, material);
mesh.castShadow = true;
mesh.position.x = -24 + ((i % 4) * 12);
mesh.position.y = 4;
mesh.position.z = -8 + (j * 12);
if ((i + 1) % 4 == 0) j++;
scene.add(mesh);
});
}
function renderScene(){
trackballControls.update(clock.getDelta());
stats.update();
requestAnimationFrame(renderScene);
renderer.render(scene, camera);
}
}
</script>
</body>
</html>
运行结果:
网友评论