【Processing笔记】NO.6

process标签页
在这里插入图片描述
在这里插入图片描述
可以把一些程序写在其他的标签页中,要用的时候调用就可以了。


用函数来输出一个渐变色图案
在这里插入图片描述

void setup()
{
  size(800,800);
}

void draw()
{
  background(0,0,0);
  drawtarget(width/2,height/2,10,30);
  drawtarget(300,150,9,30);
  drawtarget(600,200,15,30);
}
void drawtarget(int posX,int posY,int numRings,int ringSize)
{
  noStroke();
  for(int i=numRings;i>0;i--)
  {
    int size =i*ringSize;
    
    int gray =i*(255/numRings);
    
    fill(gray,gray,gray);
    ellipse(posX,posY,size,size);
  }
}

定时器
使一个白色正方体在3到5秒出现一次

void setup()
{
  size(400,400);
}

void draw()
{
  background(0,0,0);
  if(millis()>3000&&millis()<5000)
  {
    fill(255,255,255);
    rect(width/2,height/2,100,100);
  }
}

**mills()**返回开始运行当前程序时的毫秒数。
在程序开始的时候计时器开始计时,程序关闭时计时器清零。


P3D渲染器
在平面X,Y的基础上增加了一个深度值(depth)。
在这里插入图片描述
size(a,b,P3D);P3D没有具体的值,P3D趋近于无穷。

void setup()
{
  size(400,400,P3D);
}

void draw()
{
  background(125);
  translate(100,100);
  fill(125,0,0);
  rect(0,0,100,100);
}

在2D下 translate(a,b)表示在(0,0)坐标在左移a像素,下移b像素。
在这里插入图片描述
通常在画3D图形时经常会用translate()将初始位置移动width/2和height/2使得屏幕中心成为(0,0)坐标。

int posZ=0;
void setup()
{
  size(400,400,P3D);
}

void draw()
{
  background(125);
  translate(width/3,height/3,posZ);
  fill(125,0,0);
  rect(0,0,100,100);
  posZ++;
}

z轴数值增加,图像放大。


将当前变换矩阵嵌入和弹出矩阵堆栈。

int posZ=0;
void setup()
{
  size(400,400,P3D);
}

void draw()
{
  background(125);
  pushMatrix();
  translate(width/3,height/3,posZ);
  fill(125,0,0);
  rotate(PI/4);
  rect(0,0,100,100);
  popMatrix();
  
  fill(0,125,0);
  rect(0,0,100,100);
}

pushMatrix()和popMatrix()必须成对出现。
在这里插入图片描述


绘制立方体


void setup()
{
  size(400,400,P3D);
}

void draw()
{
  background(125);
  
  pushMatrix();
  translate(width/2-50,height/2-50,-mouseX);
  fill(125,0,0);
  box(50.0); 
  popMatrix();
  
  pushMatrix();
  translate(100,100,-mouseY);
  fill(0,125,0);
  box(45,55,50);
  popMatrix();
}

box(a);表示绘制一个边长为a立方体;
box(a,b,c)表示绘制一个长为a,宽为b,高为c。
3D图形都是基于(0,0)点开始绘制的,所以要配合translate函数使用。
绘制不同的3D图像,需要用到不同的translate移动位置需要分别放在pushMatrix,popMatrix函数里。
在这里插入图片描述


球体
在这里插入图片描述
在写这个程序的时候报错,查看错误之后才发现,size()函数中没有加P3D,以后一定要记住了。

void setup()
{
  size(400,400,P3D);
}

void draw()
{
  background(125);
  pushMatrix();
  translate(width/2,height/2,mouseX);
  fill(0,255,0);
  sphere(100.0);
  popMatrix();
}

在这里插入图片描述
sphere()球体函数只有一个参数。


定向光

void setup()
{
  size(400,400,P3D);
}

void draw()
{
  background(0);
  pushMatrix();
  translate(width/2,height/2,-mouseX);
  
  noStroke();
  directionalLight(255,0,0,1,0,0);
  directionalLight(0,255,0,1,1,0);
  directionalLight(0,125,255,-1,0,0);
  sphere(80);
  
  popMatrix();
}

在这里插入图片描述
directionalLight(a,b,c,d,e,f);a,b,c是颜色的RGB值,d,e,f是x,y,z的单位向量。
添加定向光。定向光来自一个方向:直角撞击表面时会更强,而以柔和的角度撞击时会更弱。撞击表面后,定向光会在所有方向上散射。灯光需要包含在draw()中,以在循环程序中保持持久性。将它们放在循环程序的setup()中将导致它们仅在第一次循环时才起作用。


环境光

void setup()
{
  size(400,400,P3D);
}

void draw()
{
  background(0);
  pushMatrix();
  translate(width/2,height/2,-mouseX);
  
  noStroke();
  directionalLight(255,0,0,1,1,0);
  ambientLight(0,125,10);
  sphere(80);
  
  popMatrix();
}

ambientLight()环境光,环境光单独使用的场景比较少,一般是在结合其他类型的光源后才会变得更加有用。
在这里插入图片描述


自定义3D图形

void setup()
{
  size(400,400,P3D);
}

void draw()
{
  background(0);
 
 beginShape();
 vertex(100,100,0);
 vertex(300,300,-300);
 vertex(150,350,-200);
 endShape();
 translate(150,200,-200);
 sphere(100.0);
}

在这里插入图片描述

发布了28 篇原创文章 · 获赞 3 · 访问量 904

猜你喜欢

转载自blog.csdn.net/wangpuqing1997/article/details/104921090