1. 程式人生 > >01-THREE.JS第一個場景

01-THREE.JS第一個場景

添加 width ear scene mode lan oot itl set

THREE.JS第一個場景

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://cdn.bootcss.com/three.js/r67/three.js"></script>
        <script src="https://cdn.bootcss.com/stats.js/r10/Stats.min.js"></script
> <style> body{ margin:0; overflow:hidden; } </style> </head> <body> <!-- 作者: 時間:2018-10-29 描述:狀態 --> <div id="Stats-output"></
div> <!-- 作者: 時間:2018-10-29 描述:顯示動畫效果 --> <div id="WebGL-output"></div> <script> function init(){ var stats = initStats(); //創建一個場景
var scene = new THREE.Scene(); //創建一個相機 var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000); //創建一個渲染器 var renderer = new THREE.WebGLRenderer(); //渲染器的背景顏色#EEEEEE renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0)); //渲染器的大小 renderer.setSize(window.innerWidth, window.innerHeight); //是否顯示陰影 renderer.shadowMapEnabled = true; //創建一個地面幾何 寬60 高20 寬分成1份 高分成1份 var planeGeometry = new THREE.PlaneGeometry(60, 20, 1, 1); //地面的材料是光照材料 顏色是#FFFFFF var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff}); //合成材料和幾何 var plane = new THREE.Mesh(planeGeometry, planeMaterial); //地面允許接收陰影 plane.receiveShadow = true; //地面坐標 plane.rotation.x = -0.5 * Math.PI; //地面本來是豎著的 要逆時針旋轉90度 -90 * Math.PI / 180; plane.position.x = 15; plane.position.y = 0; plane.position.z = 0; // 在場景中添加地面 scene.add(plane); // 創建一個正方體 使用基本材料 允許正方體有陰影 var cubeGeometry = new THREE.BoxGeometry(4, 4, 4); var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000}); var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.castShadow = true; // 正方體的坐標 cube.position.x = -4; cube.position.y = 3; cube.position.z = 0; // 把正方體添加到場景中 scene.add(cube); //球體 基本材料 var sphereGeometry = new THREE.SphereGeometry(4, 20, 20); var sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff}); var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); // 球體坐標 允許有陰影 sphere.position.x = 20; sphere.position.y = 0; sphere.position.z = 2; sphere.castShadow = true; // 把球體添加到場景中去 scene.add(sphere); // 相機的坐標 相機的鏡頭朝向場景 camera.position.x = -30; camera.position.y = 40; camera.position.z = 30; camera.lookAt(scene.position); // 添加自然光 自然光不會產生陰影 var ambientLight = new THREE.AmbientLight(0x0c0c0c); scene.add(ambientLight); // 添加點光源 點光源會產生陰影 var spotLight = new THREE.SpotLight(0xffffff); spotLight.position.set(-40, 60, -10); spotLight.castShadow = true; //允許點光源產生陰影 scene.add(spotLight); //將渲染的場景添加到dom元素中去 document.getElementById("WebGL-output").appendChild(renderer.domElement); // 調用渲染場景的循環 var step = 0; renderScene(); function renderScene() { //更新FPS值 stats.update(); // 旋轉正方體 cube.rotation.x += 0.02; cube.rotation.y += 0.02; cube.rotation.z += 0.02; // 移動球體 step += 0.04; sphere.position.x = 20 + ( 10 * (Math.cos(step))); sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step))); // render using requestAnimationFrame requestAnimationFrame(renderScene); renderer.render(scene, camera); } //在一個小圖裏顯示每秒顯示的幀數(FPS) function initStats(){ var stats = new Stats(); stats.setMode(0); // 0: fps, 1: ms stats.domElement.style.position = absolute; stats.domElement.style.left = 0px; stats.domElement.style.top = 0px; document.getElementById("Stats-output").appendChild(stats.domElement); return stats; } } window.onload = init; </script> </body> </html>

01-THREE.JS第一個場景