黃岡最專業(yè)的公司網(wǎng)站建設(shè)平臺(tái)西安百度seo代理
👨??? 主頁(yè): gis分享者
👨??? 感謝各位大佬 點(diǎn)贊👍 收藏? 留言📝 加關(guān)注?!
👨??? 收錄于專欄:threejs gis工程師
文章目錄
- 一、🍀前言
- 1.1 ??THREE.CircleGeometry 圓形幾何體
- 二、🍀創(chuàng)建THREE.CircleGeometry 二維平面圓形幾何體
- 1. ??實(shí)現(xiàn)思路
- 2. ??代碼樣例
一、🍀前言
本文詳細(xì)介紹如何基于threejs在三維場(chǎng)景中創(chuàng)建THREE.CircleGeometry 二維平面圓形幾何體,親測(cè)可用。希望能幫助到您。一起學(xué)習(xí),加油!加油!
1.1 ??THREE.CircleGeometry 圓形幾何體
THREE.CircleGeometry是歐式幾何的一個(gè)簡(jiǎn)單形狀,它由圍繞著一個(gè)中心點(diǎn)的三角分段的數(shù)量所構(gòu)造,由給定的半徑來(lái)延展。 同時(shí)它也可以用于創(chuàng)建規(guī)則多邊形,其分段數(shù)量取決于該規(guī)則多邊形的邊數(shù)。
構(gòu)造函數(shù):
CircleGeometry(radius : Float, segments : Integer, thetaStart : Float, thetaLength : Float)
radius — 圓形的半徑,默認(rèn)值為1
segments — 分段(三角面)的數(shù)量,最小值為3,默認(rèn)值為8。
thetaStart — 第一個(gè)分段的起始角度,默認(rèn)為0。(three o’clock position)
thetaLength — 圓形扇區(qū)的中心角,通常被稱為“θ”(西塔)。默認(rèn)值是2*Pi,這使其成為一個(gè)完整的圓。
屬性:
.parameters : Object
一個(gè)包含著構(gòu)造函數(shù)中每個(gè)參數(shù)的對(duì)象。在對(duì)象實(shí)例化之后,對(duì)該屬性的任何修改都不會(huì)改變這個(gè)幾何體。
二、🍀創(chuàng)建THREE.CircleGeometry 二維平面圓形幾何體
1. ??實(shí)現(xiàn)思路
- 1、初始化renderer渲染器
- 2、初始化Scene三維場(chǎng)景scene。
- 3、初始化PerspectiveCamera透視相機(jī)camera,定義相機(jī)位置 camera.position,設(shè)置相機(jī)方向camera.lookAt。
- 4、初始化THREE.SpotLight聚光燈光源spotLight,設(shè)置spotLight的位置信息,場(chǎng)景scene中添加spotLight。
- 5、加載幾何模型:創(chuàng)建THREE.MeshNormalMaterial法向量材質(zhì)meshMaterial,創(chuàng)建THREE.MeshBasicMaterial基礎(chǔ)材質(zhì)wireFrameMat,設(shè)置wireFrameMat基礎(chǔ)材質(zhì)的線框wireframe為true,通過(guò)THREE.SceneUtils.createMultiMaterialObject方法傳入THREE.CircleGeometry圓形幾何體geom和meshMaterial、wireFrameMat材質(zhì)等參數(shù)創(chuàng)建平面幾何體網(wǎng)格組circle,scene場(chǎng)景中添加circle。具體代碼參考代碼樣例。
- 6、加入gui控制,控制創(chuàng)建的圓形幾何體半徑、分段數(shù)、起始角度、圓形扇區(qū)的中心角。加入stats監(jiān)控器,監(jiān)控幀數(shù)信息。
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>
效果如下: