3Dサンプル(sketch_3D_MyTrain)スケッチ
3Dサンプル(sketch_3D_MyTrain)スケッチは以下となります。
//sketch_3D_MyTrain
void setup()
{
size(740, 660, P3D);
}
void draw()
{
background(0, 256, 256);
lights();
translate(width / 2, height / 2);
rotateY(map(mouseX, 0, width, -PI/1.5, PI/1.5));
rotateX(map(mouseY, 0, height, PI/1.5, -PI/1.5));
noStroke();
fill(240, 240, 230);
translate(0,0,200);
OB_B1();//新幹線ボディー
pushMatrix();
translate(0, -4.4,-49);
OB_A5();//車輪
translate(0,0,-24.8);
OB_A5();//車輪
translate(0,0,-152);
OB_A5();//車輪
translate(0,0,-24.8);
OB_A5();//車輪
popMatrix();
}
void OB_A5()//X軸回転体(車輪)
{
int sides=16;//分割数
int Sn=11;//面数
float Px[]={-11,-11,-10,-10,-8,-8,8,8,10,10,11,11};//x座標
float R[]={0.1,1.1,1.1,4.4,4.4,1.1,1.1,4.4,4.4,1.1,1.1,0.1};//回転物半径
int i,j;
float angleIncrement = TWO_PI/sides;
pushMatrix();
//translate(60, 36.3,-145);
for(j=0;j<Sn-1;j++)
{
float angle = 0;
beginShape(QUAD_STRIP);
for (i = 0; i < sides + 1; ++i)
{
vertex(Px[j],R[j]*cos(angle), R[j]*sin(angle));
vertex(Px[j+1],R[j+1]*cos(angle), R[j+1]*sin(angle));
angle += angleIncrement;
}
endShape(CLOSE);
}
popMatrix();
}
void Wind()//窓
{
int Sn=13;//個数
int i;
float W=5.6,H=5.6,P=10.3;
fill(20, 20, 40);
for(i=0;i<Sn;i++)
{
beginShape();
vertex(17.9,-21.6,-104.5-P*i);
vertex(17.9,-21.6-H,-104.5-P*i);
vertex(17.9,-21.6-H,-104.5-W-P*i);
vertex(17.9,-21.6,-104.5-W-P*i);
endShape(CLOSE);
beginShape();
vertex(-17.9,-21.6,-104.5-P*i);
vertex(-17.9,-21.6-H,-104.5-P*i);
vertex(-17.9,-21.6-H,-104.5-W-P*i);
vertex(-17.9,-21.6,-104.5-W-P*i);
endShape(CLOSE);
}
}
void OB_B1()//新幹線ボディー
{
int Sn=15;//面数
float Pz[]={0,-3.3,-9.6,-24.7,-40.4,-41.3,-62.5,-82.3,-83.2,-216.6,-218,-258.4,-259.4,-273.5,-273.5};//z座標
float Py[]={8.2,10,10.7,12.4,14.5,16.3,19.3,20.1,19.2,19.2,20.4,20.4,19.2,19.2,19.2};//Yオフセット
float W[]={0.3,18.9,26.3,31.1,33.7,33.9,35.6,35.6,35.6,35.6,35.6,35.6,35.6,35.6,0.3};//幅
float H[]={0.3,11.6,16.6,19.6,23.7,22.5,28.4,30.2,33.2,33.2,30.8,30.8,33.2,33.2,03};//高さ
float R1[]={0.1,6,8,8,8,8,8,8,8,8,8,8,8,8,01};//上側半径
float R2[]={0.1,4,5,5,4,3,3,3,3,3,3,3,3,3,0.1};//上側半径
float[][] X=new float[16][Sn];
float[][] Y=new float[16][Sn];
int i,j;
float sin0=0;float sin30=sin(PI/6);float sin60=sin(PI/3);float sin90=1;
float cos0=1;float cos30=cos(PI/6);float cos60=cos(PI/3);float cos90=0;
for(j=0;j<Sn;j++)
{
X[0][j]=W[j]/2-R1[j]+R1[j]*cos0;Y[0][j]=H[j]/2-R1[j]+R1[j]*sin0+Py[j];
X[1][j]=W[j]/2-R1[j]+R1[j]*cos30;Y[1][j]=H[j]/2-R1[j]+R1[j]*sin30+Py[j];
X[2][j]=W[j]/2-R1[j]+R1[j]*cos60;Y[2][j]=H[j]/2-R1[j]+R1[j]*sin60+Py[j];
X[3][j]=W[j]/2-R1[j]+R1[j]*cos90;Y[3][j]=H[j]/2-R1[j]+R1[j]*sin90+Py[j];
X[4][j]=-W[j]/2+R1[j]-R1[j]*sin0;Y[4][j]=H[j]/2-R1[j]+R1[j]*cos0+Py[j];
X[5][j]=-W[j]/2+R1[j]-R1[j]*sin30;Y[5][j]=H[j]/2-R1[j]+R1[j]*cos30+Py[j];
X[6][j]=-W[j]/2+R1[j]-R1[j]*sin60;Y[6][j]=H[j]/2-R1[j]+R1[j]*cos60+Py[j];
X[7][j]=-W[j]/2+R1[j]-R1[j]*sin90;Y[7][j]=H[j]/2-R1[j]+R1[j]*cos90+Py[j];
X[8][j]=-W[j]/2+R2[j]-R2[j]*cos0;Y[8][j]=-H[j]/2+R2[j]-R2[j]*sin0+Py[j];
X[9][j]=-W[j]/2+R2[j]-R2[j]*cos30;Y[9][j]=-H[j]/2+R2[j]-R2[j]*sin30+Py[j];
X[10][j]=-W[j]/2+R2[j]-R2[j]*cos60;Y[10][j]=-H[j]/2+R2[j]-R2[j]*sin60+Py[j];
X[11][j]=-W[j]/2+R2[j]-R2[j]*cos90;Y[11][j]=-H[j]/2+R2[j]-R2[j]*sin90+Py[j];
X[12][j]=W[j]/2-R2[j]+R2[j]*sin0;Y[12][j]=-H[j]/2+R2[j]-R2[j]*cos0+Py[j];
X[13][j]=W[j]/2-R2[j]+R2[j]*sin30;Y[13][j]=-H[j]/2+R2[j]-R2[j]*cos30+Py[j];
X[14][j]=W[j]/2-R2[j]+R2[j]*sin60;Y[14][j]=-H[j]/2+R2[j]-R2[j]*cos60+Py[j];
X[15][j]=W[j]/2-R2[j]+R2[j]*sin90;Y[15][j]=-H[j]/2+R2[j]-R2[j]*cos90+Py[j];
}
pushMatrix();
for(j=0;j<Sn-1;j++)
{
beginShape(QUAD_STRIP);
for (i = 0; i < 15; ++i)
{
fill(240, 240, 230);
if(j==5 && (i==3 || i==4)){fill(20, 20, 40);}
vertex(X[i][j],-Y[i][j], Pz[j]);
vertex(X[i][j+1], -Y[i][j+1], Pz[j+1]);
}
vertex(X[0][j],-Y[0][j], Pz[j]);
vertex(X[0][j+1], -Y[0][j+1], Pz[j+1]);
endShape(CLOSE);
}
Wind();
popMatrix();
}