<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mesh Properties</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();
// create the ground 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;
scene.add(plane);
// add ambient lighting
let ambientLight = new THREE.AmbientLight(0x3c3c3c);
scene.add(ambientLight);
// add spotlight
let spotLight = new THREE.SpotLight(0xffffff, 1, 180, Math.PI/4);
spotLight.shadow.mapSize.height = 2048;
spotLight.shadow.mapSize.width = 2048;
spotLight.position.set(-40, 30, 30);
spotLight.castShadow = true;
scene.add(spotLight);
var material = new THREE.MeshLambertMaterial({color: 0x44ff44});
var geometry = new THREE.BoxGeometry(5, 8, 3);
var cube = new THREE.Mesh(geometry, material);
cube.position.y = 4;
cube.castShadow = true;
scene.add(cube);
// attributes which can be modified in GUI
const controls = {
"scaleX" : 1,
"scaleY" : 1,
"scaleZ" : 1,
"positionX" : 0,
"positionY" : 4,
"positionZ" : 0,
"rotationX" : 0,
"rotationY" : 0,
"rotationZ" : 0,
"scale" : 1,
"translateX" : 0,
"translateY" : 0,
"translateZ" : 0,
"visible" : true,
"translate" : function(){
cube.translateX(this.translateX);
cube.translateY(this.translateY);
cube.translateZ(this.translateZ);
this.positionX = cube.position.x;
this.positionY = cube.position.y;
this.positionZ = cube.position.z;
},
};
// init GUI
initGUI();
renderScene();
function initGUI(){
let gui = new dat.GUI();
let guiScale = gui.addFolder('scale');
guiScale.add(controls, 'scaleX', 0, 5);
guiScale.add(controls, 'scaleY', 0, 5);
guiScale.add(controls, 'scaleZ', 0, 5);
let guiPosition = gui.addFolder('position');
let contX = guiPosition.add(controls, 'positionX', -10, 10);
let contY = guiPosition.add(controls, 'positionY', -4, 20);
let contZ = guiPosition.add(controls, 'positionZ', -10, 10);
contX.listen();
contX.onChange(function (value) {
cube.position.x = controls.positionX;
});
contY.listen();
contY.onChange(function (value) {
cube.position.y = controls.positionY;
});
contZ.listen();
contZ.onChange(function (value) {
cube.position.z = controls.positionZ;
});
let guiRotation = gui.addFolder('rotation');
guiRotation.add(controls, 'rotationX', -4, 4);
guiRotation.add(controls, 'rotationY', -4, 4);
guiRotation.add(controls, 'rotationZ', -4, 4);
let guiTranslate = gui.addFolder('translate');
guiTranslate.add(controls, 'translateX', -10, 10);
guiTranslate.add(controls, 'translateY', -10, 10);
guiTranslate.add(controls, 'translateZ', -10, 10);
guiTranslate.add(controls, 'translate');
gui.add(controls, 'visible');
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function renderScene(){
trackballControls.update(clock.getDelta());
stats.update();
cube.visible = controls.visible;
cube.rotation.x = controls.rotationX;
cube.rotation.y = controls.rotationY;
cube.rotation.z = controls.rotationZ;
cube.scale.set(controls.scaleX, controls.scaleY, controls.scaleZ);
requestAnimationFrame(renderScene);
renderer.render(scene, camera);
}
}
</script>
</body>
</html>
运行结果:
网友评论