processingで3Dの図形を色を混ぜずに重ねたいです.
実行直後の向き(正面から)だと1枚目の画像のようになるのですが,マウスを動かして角度を変えると2枚目の画像のように黄色と黒が混ざってしまいます.
常に黒が上に来るようにしたいのですが,どうすればよいでしょうか?
/* @pjs font="TEMPSITC.TTF"; */ boolean isGrid = true, isAxis = true; void setup(){ size(500, 300, P3D); noStroke(); textFont(createFont("Tempus Sans ITC", 24)); frameRate(10); } void axis(char s, color c){ int len = 200; fill(c); stroke(c); if(isAxis){ box(len, 1, 1); pushMatrix(); translate(len / 2, 0, 0); sphere(3); text(s, 5, -5, 0); popMatrix(); } if(isGrid){ pushMatrix(); translate(0, -len / 2, -len / 2); int ngrids = 20, xs = len / ngrids, ys = len / ngrids; strokeWeight(1); for(int i = 1; i < ngrids; i++){ line(0, 0, ys * i, 0, len, ys * i); line(0, xs * i, 0, 0, xs * i, len); } popMatrix(); } } void drawAxis(char s, color c){ switch(s){ case 'X': axis(s, c); break; case 'Y': pushMatrix(); rotateZ(PI / 2); axis(s, c); popMatrix(); break; case 'Z': pushMatrix(); rotateY(-PI / 2); axis(s, c); popMatrix(); break; } } void coloredCube(){ pushMatrix(); scale(.5, .5, .5); noStroke(); beginShape(QUADS); fill(245, 223, 77); vertex(1, -1, -1); vertex(1, -1, 1); vertex(1, 1, 1); vertex(1, 1, -1); vertex(1, 1, 1); vertex(-1, 1, 1); vertex(-1, 1, -1); vertex(1, 1, -1); vertex(-1, 1, 1); vertex(1, 1, 1); vertex(1, -1, 1); vertex(-1, -1, 1); vertex(-1, -1, -1); vertex(-1, -1, 1); vertex( 1, -1, 1); vertex( 1, -1, -1); vertex(-1, 1, -1); vertex(-1, 1, 1); vertex(-1, -1, 1); vertex(-1, -1, -1); vertex(1, 1, -1); vertex(-1, 1, -1); vertex(-1, -1, -1); vertex(1, -1, -1); endShape(); popMatrix(); } void pillar(float length, float radius1 , float radius2, int r, int g, int b){ float x,y,z; pushMatrix(); noStroke(); fill(r,g,b); //top beginShape(TRIANGLE_FAN); y = -length / 2; vertex(0, y, 0); for(int deg = 0; deg <= 360; deg = deg + 10){ x = cos(radians(deg)) * radius1; z = sin(radians(deg)) * radius1; vertex(x, y, z); } endShape(); //bottom beginShape(TRIANGLE_FAN); y = length / 2; vertex(0, y, 0); for(int deg = 0; deg <= 360; deg = deg + 10){ x = cos(radians(deg)) * radius2; z = sin(radians(deg)) * radius2; fill(r,g,b); vertex(x, y, z); } endShape(); //side beginShape(TRIANGLE_STRIP); for(int deg =0; deg <= 360; deg = deg + 5){ x = cos(radians(deg)) * radius1; y = -length / 2; z = sin(radians(deg)) * radius1; vertex(x, y, z); x = cos(radians(deg)) * radius2; y = length / 2; z = sin(radians(deg)) * radius2; vertex(x, y, z); } endShape(); popMatrix(); } void drawShape(){ pushMatrix(); translate(0, 20, 0); scale(100, 40, 60); coloredCube(); popMatrix(); pushMatrix(); translate(40, 0, 0); rotateX(HALF_PI); fill(0); pillar(60,20,20,0,0,0); popMatrix(); pushMatrix(); translate(-40, 0, 0); rotateX(HALF_PI); pillar(60,20,20,0,0,0); popMatrix(); } void draw(){ background(255); translate(250, 150, -50); rotateX(map(mouseY, 0, height, PI, -PI)); rotateY(map(mouseX, 0, width, -PI, PI)); pushMatrix(); drawAxis('X', color(255, 0, 0, 60)); drawAxis('Y', color(0, 255, 0, 60)); drawAxis('Z', color(0, 0, 255, 60)); drawShape(); popMatrix(); }
あなたの回答
tips
プレビュー