需要電子檔書籍或者源碼可以Q羣:828202939 希望可以和大家一起學習、一起進步!!
如有錯別字或有理解不到位的地方,可以留言或者加微信15250969798,博主會及時修改!!!!!
博主的案例並不難,只是爲了更好的給想入門threeJS的同學一點點經驗!!!!!
本章節學習的內容可以從的官方文檔中找到
涉及的知識點博主已經從three源碼庫裏面摘要出來放在對應的註釋裏面
今天學習Helper裏面的DirectionalLightHelper助手!
順便加入了鼠標控制相機的組件OrbitControls,不懂可以先忽略!
效果圖:
代碼:
<html>
<head>
<title>Helper05-DirectionalLightHelper(方向光助手)</title>
<style>
body {
margin: 0;
}
canvas {
width: 100%;
height: 100%
}
</style>
</head>
<body>
<script src="../../../build/three.js"></script>
<script src="../../js/controls/OrbitControls.js"></script>
<script>
var camera, scene, renderer, geometry, material, animate, controls; //常用變量
var cube, light,helper; //自定義對象變量
var target = new THREE.Vector3(0, 0, 0);
init();
animate();
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0xcfcfcf);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 150);
camera.lookAt(target);
camera.updateProjectionMatrix(); //相機更新
// DirectionalLight( color, intensity )
light = new THREE.DirectionalLight(0xFFFFFF,2);
light.position.set(20,20,50);
// DirectionalLightHelper( light, size, color )
var helper = new THREE.DirectionalLightHelper(light, 50,0xFFFFFF);
scene.add(helper);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//OrbitControls控件操作模塊
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', function () {
renderer.render(scene, camera);
});
// BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments )
geometry = new THREE.BoxGeometry(40, 40, 40);
material = new THREE.MeshBasicMaterial({
color: 0x00ff00,
wireframe: false
}); //wireframe默認爲false
cube = new THREE.Mesh(geometry, material);
scene.add(cube);
}
function animate() {
requestAnimationFrame(animate);
controls.update();
cube.rotation.x += 0.01;
renderer.render(scene, camera);
};
</script>
</body>
</html>