车削几何体
2019-11-12 本文已影响0人
Codifier
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Lathe Geometry</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 = -30;
camera.position.y = 40;
camera.position.z = 30;
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();
// add ambient light
let ambientLight = new THREE.AmbientLight("#606008", 1);
scene.add(ambientLight);
let directionalLight = new THREE.DirectionalLight("#ffffff");
directionalLight.position.set(-10, 50, -10);
directionalLight.intensity = 2;
directionalLight.castShadow = true;
scene.add(directionalLight);
// add a plane
let planeGeometry = new THREE.PlaneGeometry(100, 100, 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.set(0, -20, 0);
scene.add(plane);
let group, latheMesh;
// attributes which can be modified in GUI
const controls = {
"segments" : 10,
"phiStart" : 0,
"phiLength" : Math.PI * 2,
"redraw" : function(){
renderer.setClearColor(new THREE.Color(0x000000));
generateLathe(this.segments, this.phiStart, this.phiLength);
}
};
// init GUI
initGUI();
generateLathe(controls);
renderScene();
function initGUI(){
let gui = new dat.GUI();
gui.add(controls, 'segments', 0, 50, 1).onChange(function (e) {
controls.segments = e;
});
gui.add(controls, 'phiStart', 0, 2 * Math.PI, 0.1).onChange(function (e) {
controls.phiStart = e;
});
gui.add(controls, 'phiLength', 0, 2 * Math.PI, 0.1).onChange(function (e) {
controls.phiLength = e;
});
gui.add(controls, 'redraw');
}
function generateLathe() {
if(group){
scene.remove(group);
}
if(latheMesh){
scene.remove(latheMesh);
}
// random point
let points = [];
let height = 5;
let count = 30;
for(let i=0; i<count; ++i) {
points.push(new THREE.Vector2(
(Math.sin(i * 0.2) + Math.cos(i * 0.3)) * height + 12,
(i - count) + count / 2));
}
group = new THREE.Object3D(controls);
let material = new THREE.MeshBasicMaterial({
color: 0xff0000,
transparent: false
});
points.forEach(v => {
let spGeom = new THREE.SphereGeometry(0.2);
let spMesh = new THREE.Mesh(spGeom, material);
spMesh.position.set(v.x, v.y, 0);
group.add(spMesh);
});
scene.add(group);
/**
* LatheGeometry(points : Array, segments : Integer, phiStart : Float, phiLength : Float)
* points — Array of Vector2s. The x-coordinate of each point must be greater than zero.
* segments — the number of circumference segments to generate. Default is 12.
* phiStart — the starting angle in radians. Default is 0.
* phiLength — the radian (0 to 2PI) range of the lathed section 2PI is a closed lathe, less than 2PI is a portion. Default is 2PI.
* @type {LatheGeometry}
*/
let latheGeometry = new THREE.LatheGeometry(points, controls.segments, controls.phiStart, controls.phiLength);
let latheMaterial = new THREE.MeshNormalMaterial();
latheMesh = new THREE.Mesh(latheGeometry, latheMaterial);
scene.add(latheMesh);
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function renderScene(){
trackballControls.update(clock.getDelta());
stats.update();
requestAnimationFrame(renderScene);
renderer.render(scene, camera);
}
}
</script>
</body>
</html>
运行结果: