Three.js利用頂點(diǎn)繪制立方體的方法詳解
前言
之前我們?cè)趯W(xué)些WebGL基礎(chǔ)的時(shí)候每天都是在一直研究頂點(diǎn)位置,法向量,繪制下標(biāo)什么的。雖然復(fù)雜,但是畢竟原生,性能沒(méi)得說(shuō)。
three.js也給我們提供了相關(guān)的接口供我們使用原生的方法繪制模型,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
下面是我的個(gè)人一個(gè)案例。
首先,我創(chuàng)建了一個(gè)空白的形狀:
//立方體 var cubeGeometry = new THREE.Geometry();
立方體的形狀如下:
// 創(chuàng)建一個(gè)立方體 // v6----- v5 // /| /| // v1------v0| // | | | | // | |v7---|-|v4 // |/ |/ // v2------v3
然后添加了立方體的頂點(diǎn),一共8個(gè)
//創(chuàng)建立方體的頂點(diǎn) var vertices = [ new THREE.Vector3(10, 10, 10), //v0 new THREE.Vector3(-10, 10, 10), //v1 new THREE.Vector3(-10, -10, 10), //v2 new THREE.Vector3(10, -10, 10), //v3 new THREE.Vector3(10, -10, -10), //v4 new THREE.Vector3(10, 10, -10), //v5 new THREE.Vector3(-10, 10, -10), //v6 new THREE.Vector3(-10, -10, -10) //v7 ]; cubeGeometry.vertices = vertices;
接著通過(guò)頂點(diǎn)的坐標(biāo)生成了立方體的面
//創(chuàng)建立方的面 var faces=[ new THREE.Face3(0,1,2), new THREE.Face3(0,2,3), new THREE.Face3(0,3,4), new THREE.Face3(0,4,5), new THREE.Face3(1,6,7), new THREE.Face3(1,7,2), new THREE.Face3(6,5,4), new THREE.Face3(6,4,7), new THREE.Face3(5,6,1), new THREE.Face3(5,1,0), new THREE.Face3(3,2,7), new THREE.Face3(3,7,4) ]; cubeGeometry.faces = faces;
在這里需要注意:
(1)面是由三個(gè)頂點(diǎn)組成的一個(gè)三角形面,也是WebGL的實(shí)現(xiàn)面的方式。如果需要一個(gè)長(zhǎng)方形,那就需要由兩個(gè)三角形組合而成。
(2)如果要繪制的面是朝向相機(jī)的,那這個(gè)面的頂點(diǎn)的書寫方式是逆時(shí)針繪制的,比如圖上模型的第一個(gè)面的添加里面書寫的是(0,1,2)。
(3)如果能使模型有燈光的效果,還需要設(shè)置法向量,讓three.js自動(dòng)生成即可,如下
//生成法向量 cubeGeometry.computeFaceNormals();
當(dāng)前的這些步驟只是生成了形狀,還需要和以前一樣設(shè)置一個(gè)紋理,再通過(guò)THTEE.Mesh()方法生成網(wǎng)格
var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff});
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
這樣就實(shí)現(xiàn)了一個(gè)立方體的繪制:

全部代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
html, body {
margin: 0;
height: 100%;
}
canvas {
display: block;
}
</style>
</head>
<body onload="draw();">
</body>
<script src="build/three.js"></script>
<script src="examples/js/controls/OrbitControls.js"></script>
<script src="examples/js/libs/stats.min.js"></script>
<script src="examples/js/libs/dat.gui.min.js"></script>
<script>
var renderer;
function initRender() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
//告訴渲染器需要陰影效果
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默認(rèn)的是,沒(méi)有設(shè)置的這個(gè)清晰 THREE.PCFShadowMap
document.body.appendChild(renderer.domElement);
}
var camera;
function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 40, 100);
camera.lookAt(new THREE.Vector3(0, 0, 0));
}
var scene;
function initScene() {
scene = new THREE.Scene();
}
//初始化dat.GUI簡(jiǎn)化試驗(yàn)流程
var gui;
function initGui() {
//聲明一個(gè)保存需求修改的相關(guān)數(shù)據(jù)的對(duì)象
gui = {
lightY: 30, //燈光y軸的位置
cubeX: 25, //立方體的x軸位置
cubeY: 10, //立方體的x軸位置
cubeZ: -5 //立方體的z軸的位置
};
var datGui = new dat.GUI();
//將設(shè)置屬性添加到gui當(dāng)中,gui.add(對(duì)象,屬性,最小值,最大值)
datGui.add(gui, "lightY", 0, 100);
datGui.add(gui, "cubeX", -30, 30);
datGui.add(gui, "cubeY", -30, 30);
datGui.add(gui, "cubeZ", -30, 30);
}
var light;
function initLight() {
scene.add(new THREE.AmbientLight(0x444444));
light = new THREE.PointLight(0xffffff);
light.position.set(15, 30, 10);
//告訴平行光需要開(kāi)啟陰影投射
light.castShadow = true;
scene.add(light);
}
var cube;
function initModel() {
//輔助工具
var helper = new THREE.AxisHelper(10);
scene.add(helper);
// 創(chuàng)建一個(gè)立方體
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
//立方體
var cubeGeometry = new THREE.Geometry();
//創(chuàng)建立方體的頂點(diǎn)
var vertices = [
new THREE.Vector3(10, 10, 10), //v0
new THREE.Vector3(-10, 10, 10), //v1
new THREE.Vector3(-10, -10, 10), //v2
new THREE.Vector3(10, -10, 10), //v3
new THREE.Vector3(10, -10, -10), //v4
new THREE.Vector3(10, 10, -10), //v5
new THREE.Vector3(-10, 10, -10), //v6
new THREE.Vector3(-10, -10, -10) //v7
];
cubeGeometry.vertices = vertices;
//創(chuàng)建立方的面
var faces=[
new THREE.Face3(0,1,2),
new THREE.Face3(0,2,3),
new THREE.Face3(0,3,4),
new THREE.Face3(0,4,5),
new THREE.Face3(1,6,7),
new THREE.Face3(1,7,2),
new THREE.Face3(6,5,4),
new THREE.Face3(6,4,7),
new THREE.Face3(5,6,1),
new THREE.Face3(5,1,0),
new THREE.Face3(3,2,7),
new THREE.Face3(3,7,4)
];
cubeGeometry.faces = faces;
//生成法向量
cubeGeometry.computeFaceNormals();
var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff});
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.x = 25;
cube.position.y = 5;
cube.position.z = -5;
//告訴立方體需要投射陰影
cube.castShadow = true;
scene.add(cube);
//底部平面
var planeGeometry = new THREE.PlaneGeometry(100, 100);
var planeMaterial = new THREE.MeshLambertMaterial({color: 0xaaaaaa});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -0.5 * Math.PI;
plane.position.y = -0;
//告訴底部平面需要接收陰影
plane.receiveShadow = true;
scene.add(plane);
}
//初始化性能插件
var stats;
function initStats() {
stats = new Stats();
document.body.appendChild(stats.dom);
}
//用戶交互插件 鼠標(biāo)左鍵按住旋轉(zhuǎn),右鍵按住平移,滾輪縮放
var controls;
function initControls() {
controls = new THREE.OrbitControls(camera, renderer.domElement);
// 如果使用animate方法時(shí),將此函數(shù)刪除
//controls.addEventListener( 'change', render );
// 使動(dòng)畫循環(huán)使用時(shí)阻尼或自轉(zhuǎn) 意思是否有慣性
controls.enableDamping = true;
//動(dòng)態(tài)阻尼系數(shù) 就是鼠標(biāo)拖拽旋轉(zhuǎn)靈敏度
//controls.dampingFactor = 0.25;
//是否可以縮放
controls.enableZoom = true;
//是否自動(dòng)旋轉(zhuǎn)
controls.autoRotate = false;
//設(shè)置相機(jī)距離原點(diǎn)的最遠(yuǎn)距離
controls.minDistance = 50;
//設(shè)置相機(jī)距離原點(diǎn)的最遠(yuǎn)距離
controls.maxDistance = 200;
//是否開(kāi)啟右鍵拖拽
controls.enablePan = true;
}
function render() {
renderer.render(scene, camera);
}
//窗口變動(dòng)觸發(fā)的函數(shù)
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
//更新控制器
render();
//更新性能插件
stats.update();
//更新相關(guān)位置
light.position.y = gui.lightY;
cube.position.x = gui.cubeX;
cube.position.y = gui.cubeY;
cube.position.z = gui.cubeZ;
controls.update();
requestAnimationFrame(animate);
}
function draw() {
initGui();
initRender();
initScene();
initCamera();
initLight();
initModel();
initControls();
initStats();
animate();
window.onresize = onWindowResize;
}
</script>
</html>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用Three.js具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Javascript基于OOP實(shí)實(shí)現(xiàn)探測(cè)器功能代碼實(shí)例
這篇文章主要介紹了Javascript基于OOP實(shí)實(shí)現(xiàn)探測(cè)器功能代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
java與javascript之間json格式數(shù)據(jù)互轉(zhuǎn)介紹
對(duì)象轉(zhuǎn)為字符串:通過(guò)JSON.encode方法,這個(gè)是json.js里面的方法,引入到當(dāng)前文件就可以了,下面整理的比較詳細(xì)一點(diǎn),感興趣的朋友不要錯(cuò)過(guò)2013-10-10
JSON與String互轉(zhuǎn)的實(shí)現(xiàn)方法(Javascript)
下面小編就為大家?guī)?lái)一篇JSON與String互轉(zhuǎn)的實(shí)現(xiàn)方法(Javascript) 。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,祝大家游戲愉快哦2016-09-09
List the Codec Files on a Computer
List the Codec Files on a Computer...2007-06-06
Javascript 獲取鏈接(url)參數(shù)的方法[正則與截取字符串]
有時(shí)我們需要在客戶端獲取鏈接參數(shù),一個(gè)常見(jiàn)的方法是將鏈接當(dāng)做字符串,按照鏈接的格式分解,然后獲取對(duì)應(yīng)的參數(shù)值。本文給出的就是這個(gè)流程的具體實(shí)現(xiàn)方法。2010-02-02

