threeJs 是前端3D框架中最突出的一个;
优点:WebGL 前端工程师的工资,相比较于做vue,react 项目开发的工资普遍要高的多,竞争较少;
缺点:threeJs资源匮乏, 文档粗糙 ,three要有一定的数学基础.
下面 以下例子 入门第一篇 :
<script src="./build/three.js">script>
<style>
body {
margin: 0;
overflow: hidden;
}
style>
<body>
<div id="threeBox">div>
body>
<script>
function init() { /* 页面加载完成开始渲染Three内容 */}
window.onload = init;
script>
function init() {
// 创建场景
let scene = new THREE.Scene();
// 创建摄像机
let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000); // (角度 , 长款比 , 最近看到的距离, 最远)
camera.position.x = -30;
camera.position.y = 40;
camera.position.z = 30;
camera.lookAt(scene.position); //将相机指向场景
//创建渲染器 (画布)
let renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xeeeeee); //canvas画布颜色
renderer.setSize(window.innerWidth, window.innerHeight); //canvas 画布大小
// 将这个canvas元素插入到 html中
document.getElementById('threeBox').appendChild(renderer.domElement);
renderer.render(scene, camera);
}
window.onload = init;
let axes = new THREE.AxesHelper(30); //坐标系的长度
scene.add(axes); //坐标系添加到场景中
let planeGeometry = new THREE.PlaneGeometry(60, 20); // 创建地面的大小
let planeMaterial = new THREE.MeshStandardMaterial({ color: 0x999999 }); //地面的材质
let plane = new THREE.Mesh(planeGeometry, planeMaterial); // 创建地面
plane.rotation.x = -0.5 * Math.PI; // 旋转角度
plane.position.x = 15; // 设置地面 xyz 轴的位置
plane.position.y = 0;
plane.position.z = 0;
scene.add(plane); // 将地面添加到坐标轴中
let cubeGeometry = new THREE.BoxGeometry(4, 4, 4); //长宽高
let cubeMaterial = new THREE.MeshLambertMaterial({ color: 0xff0000 }); //材质颜色
let cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.x = 3; //三轴的位置
cube.position.y = 8;
cube.position.z = 3;
scene.add(cube);
let sphereGeometry = new THREE.SphereGeometry(2,20,20); //半径,圆滑程度
let sphereMaterial = new THREE.MeshLambertMaterial({ color: 'blue' }); //材质颜色
let sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);
sphere.position.x = 24; //三轴的位置
sphere.position.y = 4;
sphere.position.z = 4;
scene.add(sphere);
// 添加聚光灯
let point = new THREE.SpotLight(0xffffff); //设置灯光的颜色
point.position.set(80, 100, 80); //点光源位置
point.angle = Math.PI/10; //设置投影的程度
point.shadow.mapSize.set(1024,1024);
scene.add(point)
plane.receiveShadow = true; //地面被投影
sphere.castShadow = true; //球投影
cube.castShadow = true; //立方体投影
point.castShadow = true; //灯光投影
renderer.shadowMap.enabled=true; //让渲染器支持投影
下载地址: https://github.com/mrdoob/three.js/blob/dev/examples/js/controls/OrbitControls.js
<script type="text/javascript" src="../threeJs/three.js"></script>
<script src="../threeJs/OrbitControls.js"></script>
...
<script>
function init() {
// ... 在appendChlid 后面添加
let controls = new THREE.OrbitControls(camera,renderer.domElement);
controls.addEventListener('change',()=>{
renderer.render(scene, camera);
});
}
window.onload = init;
</script>
threeJs => https://github.com/mrdoob/three.js/blob/dev/build/three.js
OrbitControls.js => https://github.com/mrdoob/three.js/blob/dev/examples/js/controls/OrbitControls.js
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<script src="../threeJs/three.js">script>
<script src="../threeJs/OrbitControls.js">script>
<style>
body {
margin: 0;
overflow: hidden;
}
style>
<body>
<div id="threeBox">div>
<script>
function init() {
// 创建场景
let scene = new THREE.Scene();
// 创建摄像机
let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000);
//定位相机
camera.position.x = -30;
camera.position.y = 40;
camera.position.z = 30;
camera.lookAt(scene.position); //将相机指向场景
//创建渲染器 (画布)
var renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xeeeeee); //渲染初始颜色
renderer.setSize(window.innerWidth, window.innerHeight); //canvas 画面大小
//渲染设置 3d 投影
renderer.shadowMap.enabled=true;
//显示三维坐标
let axes = new THREE.AxesHelper(20); //坐标系的长度
scene.add(axes); //坐标系添加到场景中
// 创建地面的大小
let planeGeometry = new THREE.PlaneGeometry(60, 20); //地面的宽高
//地面上色
let planeMaterial = new THREE.MeshStandardMaterial({ color: 0x999999 });
// 创建地面
let plane = new THREE.Mesh(planeGeometry, planeMaterial);
// 设置地面的位置
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 15;
plane.position.y = 0;
plane.position.z = 0;
// //将地面添加到坐标轴中
scene.add(plane);
plane.receiveShadow = true;
// //添加正方形
let cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
let cubeMaterial = new THREE.MeshLambertMaterial({ color: 0xff0000 });
let cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.x = 3;
cube.position.y = 8;
cube.position.z = 3;
scene.add(cube);
cube.castShadow = true;
//添加球
let sphereGeometry = new THREE.SphereGeometry(2,20,20);
let sphereMaterial = new THREE.MeshLambertMaterial({ color: 'blue' });
let sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);
sphere.position.x = 24;
sphere.position.y = 4;
sphere.position.z = 4;
sphere.castShadow = true;
scene.add(sphere);
// 添加聚光灯
var point = new THREE.SpotLight(0xffffff);
point.position.set(80, 100, 80); //点光源位置
// 通过add方法插入场景中,不插入的话,渲染的时候不会获取光源的信息进行光照计算
point.angle = Math.PI/10;
point.shadow.mapSize.set(1024,1024)
scene.add(point)
point.castShadow = true;
document.getElementById('threeBox').appendChild(renderer.domElement);
renderer.render(scene, camera);
let controls = new THREE.OrbitControls(camera,renderer.domElement);
controls.addEventListener('change',()=>{
renderer.render(scene, camera);
});
}
window.onload = init;
script>
body>
html>