Three.js – EdgesGeometry 几何体边框辅助线
EdgesGeometry
在老版本中叫做EdgesHelper
但是已经被移除。新版本赋予边框辅助参考线,更加灵活的使用方法,可以改变其材质以及其他属性,EdgesGeometry
的作用是可以帮助查看几何体对象的边缘。
示例:https://ithanmang.gitee.io/threejs/home/201808/20180809/01-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>
来源:https://blog.csdn.net/ithanmang/article/details/81531822
WEBGL学习网(WebGLStudy.COM)专注提供WebGL 、ThreeJS、BabylonJS等WEB3D开发案例源码下载。
声明信息:
1. 本站部分资源来源于用户上传和网络,如有侵权请邮件联系站长:1218436398@qq.com!我们将尽快处理。
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源打赏售价用于赞助本站提供的服务支出(包括不限服务器、网络带宽等费用支出)!
7.欢迎加QQ群学习交流:549297468 ,或者搜索微信公众号:WebGL学习网
WEBGL学习网 » Three.js – EdgesGeometry 几何体边框辅助线
WEBGL学习网 » Three.js – EdgesGeometry 几何体边框辅助线