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>