<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hemisphere Light</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 a plane
// create texture
let textureGrass = new THREE.TextureLoader().load("../assets/textures/ground/grasslight-big.jpg");
textureGrass.wrapS = THREE.RepeatWrapping;
textureGrass.wrapT = THREE.RepeatWrapping;
textureGrass.repeat.set(10, 10);
let planeGeometry = new THREE.PlaneGeometry(1000, 1000, 20, 20);
let planeMaterial = new THREE.MeshLambertMaterial({
map: textureGrass
});
let plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true;
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 15;
plane.position.y = 0;
plane.position.z = 0;
scene.add(plane);
// add hemisphere light
/**
* HemisphereLight( skyColor : Integer, groundColor : Integer, intensity : Float )
* skyColor - (optional) hexadecimal color of the sky. Default is 0xffffff.
* groundColor - (optional) hexadecimal color of the ground. Default is 0xffffff.
* intensity - (optional) numeric value of the light's strength/intensity. Default is 1.
* @type {HemisphereLight}
*/
let hemiLight = new THREE.HemisphereLight(0xffffff, 0x00ff00, 0.6);
hemiLight.intensity = 1;
hemiLight.position.set(0, 500, 0);
scene.add(hemiLight);
// create a cube
let cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
let cubeMaterial = new THREE.MeshLambertMaterial({
color: 0xff3333
});
let cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true;
cube.position.x = -4;
cube.position.y = 3;
cube.position.z = 0;
scene.add(cube);
// add a sphere
let sphereGeometry = new THREE.SphereGeometry(4, 25, 25);
let sphereMaterial = new THREE.MeshPhongMaterial({
color: 0x7777ff
});
let sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.x = 10;
sphere.position.y = 5;
sphere.position.z = 10;
sphere.castShadow = true;
scene.add(sphere);
// attributes which can be modified in GUI
const controls = {
"skyColor" : hemiLight.color.getStyle(),
"groundColor" : hemiLight.groundColor.getStyle(),
"intensity" : hemiLight.intensity
};
// init GUI
initGUI();
renderScene();
function initGUI(){
let gui = new dat.GUI();
gui.addColor(controls, 'skyColor').onChange(function (e) {
hemiLight.color = new THREE.Color(e);
});
gui.addColor(controls, 'groundColor').onChange(function (e) {
hemiLight.groundColor = new THREE.Color(e);
});
gui.add(controls, 'intensity', 0, 5).onChange(function (e) {
hemiLight.intensity = e;
});
}
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>
运行结果:
网友评论