Last active
August 9, 2018 06:48
-
-
Save xhsdnn/82bef03f7f996a8b82dc32546e9c7873 to your computer and use it in GitHub Desktop.
three.js绘制多边形的顶点矩阵
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 前提需要先引入three.js | |
* n —— 正多边形边数 | |
* r —— 正多边形所在外接圆的半径 | |
* cx —— 正多边形所在外接圆的圆形X轴坐标(默认为:0) | |
* cy —— 正多边形所在外接圆的圆形Y轴坐标(默认为:0) | |
*/ | |
function drawPolygon(n ,r, cx = 0, cy = 0) { | |
let vertexs = []; | |
for(let i=0; i<n; i++) { | |
let x = cx + r*Math.sin(2*Math.PI/n*i); | |
let y = cy + r*Math.cos(2*Math.PI/n*i); | |
vertexs.push(new THREE.Vector3(x, y, 0)); | |
/* 此处也可以直接将数据存入数组中,如果没使用three.js可在WebGL中做顶点坐标使用 */ | |
// vertexs.push(x, y, 0); | |
} | |
return vertexs; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let scene = new THREE.Scene(); | |
let geometry = new THREE.Geometry(); | |
geometry.vertices = drawPolygon(6, 3); | |
let material=new THREE.LineBasicMaterial({ | |
color:0xffffff | |
}); | |
let polygon=new THREE.LineLoop(geometry,material); | |
scene.add(polygon); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment