Created
October 15, 2018 00:59
-
-
Save tek-nishi/8cff655448bdf1f7ca112e5e0bf60f55 to your computer and use it in GitHub Desktop.
ProcessingでSphere
This file contains hidden or 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
// | |
// 自前で球を生成する | |
// | |
// 分割数と半径の定義 | |
int div = 30; | |
float radius = 250; | |
// 頂点配列 | |
PVector[] vtx; | |
// テクスチャ座標 | |
PVector[] uv; | |
//PImage img; | |
float rotate_x = 0; | |
float rotate_y = 0; | |
void setup() { | |
size(800, 800, P3D); | |
// 必要な配列を確保 | |
vtx = new PVector[div * div * 6]; | |
uv = new PVector[div * div * 6]; | |
int k = 0; | |
for (int j = 0; j < div; j++) { | |
for (int i = 0; i < div; i++) { | |
float y1 = cos(PI * j / div); | |
float y2 = cos(PI * (j + 1) / div); | |
float r1 = sin(PI * j / div) * radius; | |
float r2 = sin(PI * (j + 1) / div) * radius; | |
float x1 = sin(TWO_PI * i / div); | |
float z1 = cos(TWO_PI * i / div); | |
float x2 = sin(TWO_PI * (i + 1) / div); | |
float z2 = cos(TWO_PI * (i + 1) / div); | |
// 三角形2枚ひと組で生成 | |
// 頂点座標 | |
vtx[k + 0] = new PVector(x1 * r1, y1 * radius, z1 * r1); | |
vtx[k + 1] = new PVector(x2 * r1, y1 * radius, z2 * r1); | |
vtx[k + 2] = new PVector(x1 * r2, y2 * radius, z1 * r2); | |
vtx[k + 3] = new PVector(x2 * r1, y1 * radius, z2 * r1); | |
vtx[k + 4] = new PVector(x2 * r2, y2 * radius, z2 * r2); | |
vtx[k + 5] = new PVector(x1 * r2, y2 * radius, z1 * r2); | |
// UV座標 | |
float u1 = i / float(div); | |
float u2 = (i + 1) / float(div); | |
float v1 = y1 * 0.5 + 0.5; | |
float v2 = y2 * 0.5 + 0.5; | |
uv[k + 0] = new PVector(u1, v1); | |
uv[k + 1] = new PVector(u2, v1); | |
uv[k + 2] = new PVector(u1, v2); | |
uv[k + 3] = new PVector(u2, v1); | |
uv[k + 4] = new PVector(u2, v2); | |
uv[k + 5] = new PVector(u1, v2); | |
k += 6; | |
} | |
} | |
//img = loadImage("UVCheckerMap01-512.png"); | |
// UV座標の指定は正規化座標 | |
//textureMode(NORMAL); | |
//noStroke(); | |
} | |
void draw() { | |
background(128); | |
translate(width / 2, height / 2); | |
rotateX(rotate_x); | |
rotateY(rotate_y); | |
rotate_x += 0.01; | |
rotate_y += 0.013; | |
// 配列内の頂点全てを使って三角形を描画 | |
beginShape(TRIANGLES); | |
//texture(img); | |
for (int i = 0; i < vtx.length; i++) { | |
vertex(vtx[i].x, vtx[i].y, vtx[i].z, uv[i].x, uv[i].y); | |
} | |
endShape(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment