イラスト、モデリング、Unity、VR関連

unityとかblenderとかvr関連の作業メモ

織り機

int row = 20;
int cln = 4;
int ma = new int[row][cln];
int mb = new int[cln][cln];
int mc = new int[cln][row];
int mp = new int[row][row];

float cell_size;
int repeat = 10;
int mass_cell_size;
void setup(){
  size(1024,1024);
  noStroke();  
  cell_size = height/row/repeat;
  mass_cell_size = height/repeat;
}

void draw(){
  ma = initialize_mtx(ma);  
  mb = initialize_mtx(mb);
  mc = initialize_mtx(mc);
  mp = multMtx(multMtx(ma,trMtx(mb)),mc);
 
  for(int i=0;i<repeat;i++){
    for(int j=0;j<repeat;j++){
      draw_p(mp,i*mass_cell_size,j*mass_cell_size,color(0),color(255));
    }
  }
  noLoop();
}
void keyPressed(){

  if(keyCode==ENTER){
    String fname = str(day())+str(minute())+str(second())+str(millis())+".png";
    save(fname);
    PGraphics pg = createGraphics(width,height);
    pg.beginDraw();
    pg.tint(0,0);
    PImage img = loadImage(fname);
    img.loadPixels();
    for(int j=0;j<width;j++){
      for(int k=0;k<height;k++){
        int c = img.get(j,k);
        if(!compare_color(c,color(0))){
          pg.fill(c);
          pg.stroke(c);
          pg.square(j,k,1);
        }          
      }
    }
    pg.endDraw();      
    pg.save("trans"+fname);
  }
  if(key=='n'){
    loop();
  }
}

boolean compare_color(int a,int b){
  return (red(a)==red(b) && green(a)==green(b) && blue(a)==blue(b));
}

void print_mtx(int m) {
  for(int i=0;i<m.length;i++){
    for(int j=0;j<m[0].length;j++){
      print(m[i][j]);
    }
    println("");
  }
}

int initialize_mtx(int m){ 
  for(int i=0;i<m.length;i++){
    for(int j=0;j<m[0].length;j++){
      m[i][j] = round(random(0,1));      
    }
  }
  return m;
}

void draw_p(int m,float sx,float sy,color c1,color c2){
  float y = sy;
  for(int i =0;i<m.length;i++){
    float x = sx;
    for(int j = 0; j < m[0].length;j++){
      if(m[i][j] == 0){
        fill(c1);
      }else{
        fill(c2);
      }
      if(j==m[0].length-1 || i==m.length-1){ //なぜか入る線,というか微妙に描画されない場所対策
        square(x,y,cell_size*2);
      }else{
        square(x,y,cell_size);
      }
        
      x += cell_size;
    }
    y += cell_size;
  }
}

//転置行列
int trMtx(int a){
  int b = new int[a[0].length][a.length];
  for(int i=0;i<a.length;i++){
    for(int j=0;j<a[0].length;j++){
      b[j][i] = a[i][j];
    }
  }
  return b;
}

//行列の掛け算
int multMtx(int a,int b){
  int c = new int[a.length][b[0].length];
  for(int i=0;i<a.length;i++){
    for(int j=0;j<b[0].length;j++){
      int sum=0;
      for(int k=0;k<b.length;k++){
        sum += a[i][k] * b[k][j];
      }
      c[i][j] = sum;
    }
  }
  return c;
}