【问题标题】:Processing 3.4 - Diffusive shading vertices on water simulation处理 3.4 - 水模拟上的漫反射着色顶点
【发布时间】:2018-11-02 23:35:25
【问题描述】:

我最近刚刚编写了这段代码来模拟海洋中的水流:

// Global variables
int cols,rows;
int scl = 15;

int w = 3800;
int h = 1600;

float flying = 0;

float[][] terrain;

void setup() {
  size(600, 600, P3D);
  cols = w / scl;
  rows = h / scl;
  terrain = new float[cols][rows];
}

void draw() {

  fill(255, 255, 255);

  flying -= 0.01;

  float yoff = flying;
  for (int y = 0; y < rows; y++) {
    float xoff = 0;
      for (int x = 0; x < cols; x++) {
        terrain[x][y] = map(noise(xoff,yoff), 0, 1, -50, 50);
      xoff += 0.05;
  }
  yoff += 0.05;
 }

  background(0);

  stroke(255);

  translate(width/2, height/2+50);
  rotateX(PI/3);
  translate(-w/2,-h/2);
  for (int y = 0; y < rows-1; y++) {
     beginShape(TRIANGLE_STRIP);

  for (int x = 0; x < cols; x++) {

   vertex(x*scl, y*scl, terrain[x][y]);
   vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
 // rect(x*scl, y*scl, scl, scl);  
    }
    endShape();
  }
}

但是,您可以看到它没有阴影,只是纯白色。我不知道如何遮蔽它,我到处都看过,但我不知道如何将它翻译成我的代码。

谁能帮助解释如何做或修改我的代码以添加着色器? (不一定要光面,漫射就好)

任何形式的帮助都将不胜感激!

【问题讨论】:

标签: java processing vertex-shader


【解决方案1】:

一般的“我该怎么做”类型的问题很难回答,因为实现特定目标的方法不止一种。我建议您从谷歌搜索“处理着色器”或检查处理reference 开始。您还可以查看 Processing 附带的示例(转到文件 -> 示例)以查看使用着色器的代码示例。

但我还要指出,您并不需要为此使用着色器。您可以只使用标准的fill() 函数。这是一个简单的例子:

for (int x = 0; x < cols; x++) {

  fill(0, 100 + terrain[x][y], 200 + terrain[x][y]);

  vertex(x*scl, y*scl, terrain[x][y]);
  vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
}

这里我使用你的terrain 数组来决定颜色,但这只是我尝试的第一件事。我敢肯定还有很多其他方法可以解决这个问题。

【讨论】:

    猜你喜欢
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多