👨??? 主頁: gis分享者
👨??? 感謝各位大佬 點贊👍 收藏? 留言📝 加關注?!
👨??? 收錄于專欄:threejs gis工程師
文章目錄
- 一、🍀前言
- 1.1 ??THREE.CircleGeometry 圓形幾何體
- 二、🍀創建THREE.CircleGeometry 二維平面圓形幾何體
- 1. ??實現思路
- 2. ??代碼樣例
一、🍀前言
本文詳細介紹如何基于threejs在三維場景中創建THREE.CircleGeometry 二維平面圓形幾何體,親測可用。希望能幫助到您。一起學習,加油!加油!
1.1 ??THREE.CircleGeometry 圓形幾何體
THREE.CircleGeometry是歐式幾何的一個簡單形狀,它由圍繞著一個中心點的三角分段的數量所構造,由給定的半徑來延展。 同時它也可以用于創建規則多邊形,其分段數量取決于該規則多邊形的邊數。
構造函數:
CircleGeometry(radius : Float, segments : Integer, thetaStart : Float, thetaLength : Float)
radius — 圓形的半徑,默認值為1
segments — 分段(三角面)的數量,最小值為3,默認值為8。
thetaStart — 第一個分段的起始角度,默認為0。(three o’clock position)
thetaLength — 圓形扇區的中心角,通常被稱為“θ”(西塔)。默認值是2*Pi,這使其成為一個完整的圓。
屬性:
.parameters : Object
一個包含著構造函數中每個參數的對象。在對象實例化之后,對該屬性的任何修改都不會改變這個幾何體。
二、🍀創建THREE.CircleGeometry 二維平面圓形幾何體
1. ??實現思路
- 1、初始化renderer渲染器
- 2、初始化Scene三維場景scene。
- 3、初始化PerspectiveCamera透視相機camera,定義相機位置 camera.position,設置相機方向camera.lookAt。
- 4、初始化THREE.SpotLight聚光燈光源spotLight,設置spotLight的位置信息,場景scene中添加spotLight。
- 5、加載幾何模型:創建THREE.MeshNormalMaterial法向量材質meshMaterial,創建THREE.MeshBasicMaterial基礎材質wireFrameMat,設置wireFrameMat基礎材質的線框wireframe為true,通過THREE.SceneUtils.createMultiMaterialObject方法傳入THREE.CircleGeometry圓形幾何體geom和meshMaterial、wireFrameMat材質等參數創建平面幾何體網格組circle,scene場景中添加circle。具體代碼參考代碼樣例。
- 6、加入gui控制,控制創建的圓形幾何體半徑、分段數、起始角度、圓形扇區的中心角。加入stats監控器,監控幀數信息。
2. ??代碼樣例
<!DOCTYPE html><html><head><title>THREE.CircleGeometry 二維平面圓形幾何體</title><script type="text/javascript" src="../libs/three.js"></script><script type="text/javascript" src="../libs/stats.js"></script><script type="text/javascript" src="../libs/dat.gui.js"></script><style>body {/* set margin to 0 and overflow to hidden, to go fullscreen */margin: 0;overflow: hidden;}</style>
</head>
<body><div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div><!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">// once everything is loaded, we run our Three.js stuff.function init() {var stats = initStats();// create a scene, that will hold all our elements such as objects, cameras and lights.var scene = new THREE.Scene();// create a camera, which defines where we're looking at.var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);// create a render and set the sizevar webGLRenderer = new THREE.WebGLRenderer();webGLRenderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));webGLRenderer.setSize(window.innerWidth, window.innerHeight);webGLRenderer.shadowMapEnabled = true;var circle = createMesh(new THREE.CircleGeometry(4, 10, 0.3 * Math.PI * 2, 0.3 * Math.PI * 2));// add the sphere to the scenescene.add(circle);// position and point the camera to the center of the scenecamera.position.x = -20;camera.position.y = 30;camera.position.z = 40;camera.lookAt(new THREE.Vector3(10, 0, 0));// add spotlight for the shadowsvar spotLight = new THREE.SpotLight(0xffffff);spotLight.position.set(-40, 60, -10);scene.add(spotLight);// add the output of the renderer to the html elementdocument.getElementById("WebGL-output").appendChild(webGLRenderer.domElement);// call the render functionvar step = 0;// setup the control guivar controls = new function () {// we need the first child, since it's a multimaterialthis.radius = 4;this.thetaStart = 0.3 * Math.PI * 2;this.thetaLength = 0.3 * Math.PI * 2;this.segments = 10;this.redraw = function () {// remove the old planescene.remove(circle);// create a new onecircle = createMesh(new THREE.CircleGeometry(controls.radius, controls.segments, controls.thetaStart, controls.thetaLength));// add it to the scene.scene.add(circle);};};var gui = new dat.GUI();gui.add(controls, 'radius', 0, 40).onChange(controls.redraw);gui.add(controls, 'segments', 0, 40).onChange(controls.redraw);gui.add(controls, 'thetaStart', 0, 2 * Math.PI).onChange(controls.redraw);gui.add(controls, 'thetaLength', 0, 2 * Math.PI).onChange(controls.redraw);render();function createMesh(geom) {// assign two materialsvar meshMaterial = new THREE.MeshNormalMaterial();meshMaterial.side = THREE.DoubleSide;var wireFrameMat = new THREE.MeshBasicMaterial();wireFrameMat.wireframe = true;// create a multimaterialvar mesh = THREE.SceneUtils.createMultiMaterialObject(geom, [meshMaterial, wireFrameMat]);return mesh;}function render() {stats.update();circle.rotation.y = step += 0.01;// render using requestAnimationFramerequestAnimationFrame(render);webGLRenderer.render(scene, camera);}function initStats() {var stats = new Stats();stats.setMode(0); // 0: fps, 1: ms// Align top-leftstats.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>
效果如下: