38 - three.js 笔记 - EdgesGeometry 几何体边框辅助线
程序员文章站
2022-05-27 13:51:53
...
EdgesGeometry
在老版本中叫做EdgesHelper
但是已经被移除。新版本赋予边框辅助参考线,更加灵活的使用方法,可以改变其材质以及其他属性,EdgesGeometry
的作用是可以帮助查看几何体对象的边缘。
示例:EdgesGeometry.html
构造函数
EdgesGeometry( geometry : Geometry, thresholdAngle : Integer )
参数
geometry :几何体对象
thresholdAngle :只有当相邻的面法线的角度(在一定程度上)超过这个值时,才会呈现出一条边。默认= 1度
使用步骤
1、首先,需要创建一个几何体,例如此处创建一个立方体
let cubeGeometry = new THREE.BoxGeometry(50, 50 ,50);
2、然后,将立方体对象传给EdgesGeometry
的构造函数,此时就创建了边缘几何体对象,但是还需要创阿建边框材质
let cubeEdges = new THREE.EdgesGeometry(cubeGeometry, 1);
3、创建基础线条材质,并创建线框分段THREE.LineSegments
let edgesMtl = new THREE.LineBasicMaterial({color: 0xff0000});
// edgesMtl.depthTest = false; 深度测试,若开启则是边框透明的效果
let cubeLine = new THREE.LineSegments(cubeEdges, edgesMtl);
4、最后将创建的线框加入的几何体中
cube.add(cubeLine);
scene.add(cube);
效果
示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>EdgesGeometry 边缘几何</title>
<style>
body {
margin: 0;
overflow: hidden; /*溢出隐藏*/
}
</style>
<script src="../../libs/build/three-r93.min.js"></script>
<script src="../../libs/examples/js/controls/OrbitControls.js"></script>
<script src="../../libs/examples/js/libs/dat.gui.min.js"></script>
<script src="../../libs/examples/js/libs/stats.min.js"></script>
<script src="../../libs/examples/js/Detector.js"></script>
<script src="../../libs/examples/js/utils/SceneUtils.js"></script>
</head>
<body>
<script>
let stats = initStats();
let scene, camera, renderer, circle, controls, guiControls;
/* 场景 */
function initScene() {
scene = new THREE.Scene();
}
/* 相机 */
function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.position.set(0, 10, 200);
camera.lookAt(new THREE.Vector3(0, 0, 0));
}
/* 渲染器 */
function initRenderer() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
/* 灯光 */
function initLight() {
scene.add(new THREE.AmbientLight(0xCCCCCC));
let spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(100, 500, 400);
scene.add(spotLight);
}
/* 控制器 */
function initControls() {
controls = new THREE.OrbitControls(camera, renderer.domElement);
// 添加惯性
controls.enableDamping = true;
}
/* 调试插件 */
let gui;
function initGui() {
guiControls = new function () {
};
gui = new dat.GUI({width:300});
}
/* 场景中的内容 */
let cube;
function initContent() {
let cubeGeometry = new THREE.BoxGeometry(50, 50 ,50);
let material = new THREE.MeshPhongMaterial();
let edgesMtl = new THREE.LineBasicMaterial({color: 0xff0000});
cube = new THREE.Mesh(cubeGeometry, material);
cube.position.x = -55;
// THREE.EdgesHelper has been removed. Use THREE.EdgesGeometry instead.
let cubeEdges = new THREE.EdgesGeometry(cubeGeometry, 1);
// edgesMtl.depthTest = false;
let cubeLine = new THREE.LineSegments(cubeEdges, edgesMtl);
let cylinderGeometry = new THREE.CylinderGeometry(25, 25, 50, 10);
let cylinder = new THREE.Mesh(cylinderGeometry, material);
cylinder.position.x = 55;
let cylinderEdges= new THREE.EdgesGeometry(cylinderGeometry, 1);
let cylinderLine = new THREE.LineSegments(cylinderEdges, edgesMtl);
let coneGeometry = new THREE.ConeGeometry(25, 50);
let cone = new THREE.Mesh(coneGeometry, material);
let coneEdges = new THREE.EdgesGeometry(coneGeometry, 1);
let coneLine = new THREE.LineSegments(coneEdges, edgesMtl);
cube.add(cubeLine);
cylinder.add(cylinderLine);
cone.add(coneLine);
scene.add(cylinder);
scene.add(cube);
scene.add(cone);
}
/* 性能插件 */
function initStats() {
let stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
document.body.appendChild(stats.domElement);
return stats;
}
/* 更新 */
function update() {
stats.update();
controls.update();
}
/* 初始化 */
function init() {
// 兼容性判断,若不兼容会提示信息
if (!Detector.webgl) Detector.addGetWebGLMessage();
initScene();
initCamera();
initRenderer();
initLight();
initControls();
initContent();
initGui();
window.addEventListener('resize', onWindowResize, false);
}
/* 窗口变动触发的方法 */
function onWindowResize() {
// 重新设置相机的宽高比
camera.aspect = window.innerWidth / window.innerHeight;
// 更新相机投影矩阵
camera.updateProjectionMatrix();
// 更新渲染器大小
renderer.setSize(window.innerWidth, window.innerHeight);
}
/* 循环渲染 */
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
update();
}
/* 页面绘制完后加载 */
window.onload = function () {
init();
animate();
};
</script>
</body>
</html>
上一篇: 家里有个熊孩子
下一篇: 一转身就会错过一打好姑娘