通过球体表面的位移映射处理输出高图
Processing output highmap by displacement mapping on surface of a sphere
通过球体表面上的位移映射处理输出高图,就像在球体表面上绘制山丘。
import peasy.*;
PShape ico1;
int level = 6;
float hgt=0;
String[] imgname={"0.jpg","1.jpg","2.jpg"};
PImage[] images = new PImage[imgname.length];
int textImg=0;
float pz;
PVector rotation = new PVector();
PVector velocity = new PVector();
float rotationSpeed = 0.02;
PShader texs;
void setup() {
size(1000, 1000, P3D);
for(int i = 0; i<imgname.length;i++)images[i] = loadImage(imgname[i]);
images[0].loadPixels();
ico1 = createIcosahedron(level,textImg); //see example of processing for code of class Icosahedron,but init() and create() in a method createIcosahedron(level,textImg) and return the class Icosahedron.
noLoop();
for (int i = 0 ; i<ico1.getVertexCount(); i+=1){
PVector vtemp=ico1.getVertex(i);
pz=brightness(images[0].get((int)(ico1.getTextureU(i)),(int)(ico1.getTextureV(i))))*16;
vtemp=vtemp.set(vtemp.x,vtemp.y,pz);
ico1.setVertex(i,vtemp);
}
loop();
}
void draw() {
background(240);
translate(width/2, height/2);
scale(250);
//pointLight(255, 255, 255, 2*(mouseX-width/2), 2*(mouseY-height/2), 500);
shader(texs);
ambientLight(100, 102, 126);
shape(ico1);
}
}
}
上面遗漏了一些不相关的代码,上面显示运行时没有输出。请帮助显示错误。
请参阅 class 二十面体代码的处理示例,但在方法 createIcosahedron(level,textImg) 中的 init() 和 create() 以及 return class 二十面体。您可以在 google 中搜索 "processing Icosahedron" 以获取代码,但这并不重要。
听起来你想修改基于高度贴图纹理的细分二十面体的3D几何,有点像Processing > Examples > Demos > Graphics > Mesh Tweening:
请记住,您是在不是平面的表面上执行此操作,因此偏移 z 可能无法获得您想要的效果,因为 z 会根据您在二十面体的哪一侧而改变。
这部分:
PVector vtemp=ico1.getVertex(i);
pz=brightness(images[0].get((int)(ico1.getTextureU(i)),(int)(ico1.getTextureV(i))))*16;
vtemp=vtemp.set(vtemp.x,vtemp.y,pz);
ico1.setVertex(i,vtemp);
需要改变。
您需要计算该顶点位置的法线(垂直):这将告诉您考虑二十面体表面上的角度的方向。
用高程值(uv 映射亮度值)缩放(乘以)此向量会在相同方向上偏移整个法线,但相对于二十面体更向外或向内。
只需将顶点位置添加到缩放后的顶点法线即可将其平移到正确的位置。类似于:
PVector vtemp = ico1.getVertex(i);
float elevation = brightness(images[0].get((int)(ico1.getTextureU(i)),(int)(ico1.getTextureV(i))))*16
PVector vNormal = getVertexNormal(vtemp);
PVector elevatedVertex = PVector.add(vtemp, PVector.mult(vNormal,elevation));
ico1.setVertex(i,elevatedVertex);
其中 getVertexNormal()
将计算或检索该顶点的预计算顶点法线。那里有多种资源,这是 Computer Graphics StackExchange
上的一个
这是使用 Blender 的直观解释:
我建议查看 HE_Mesh Processing library(您可以通过 Contribution Manager 轻松安装),因为它会提供大量 3D 几何工具。
此外,如果您还可以查看位移贴图 and/or normal mapping
通过球体表面上的位移映射处理输出高图,就像在球体表面上绘制山丘。
import peasy.*;
PShape ico1;
int level = 6;
float hgt=0;
String[] imgname={"0.jpg","1.jpg","2.jpg"};
PImage[] images = new PImage[imgname.length];
int textImg=0;
float pz;
PVector rotation = new PVector();
PVector velocity = new PVector();
float rotationSpeed = 0.02;
PShader texs;
void setup() {
size(1000, 1000, P3D);
for(int i = 0; i<imgname.length;i++)images[i] = loadImage(imgname[i]);
images[0].loadPixels();
ico1 = createIcosahedron(level,textImg); //see example of processing for code of class Icosahedron,but init() and create() in a method createIcosahedron(level,textImg) and return the class Icosahedron.
noLoop();
for (int i = 0 ; i<ico1.getVertexCount(); i+=1){
PVector vtemp=ico1.getVertex(i);
pz=brightness(images[0].get((int)(ico1.getTextureU(i)),(int)(ico1.getTextureV(i))))*16;
vtemp=vtemp.set(vtemp.x,vtemp.y,pz);
ico1.setVertex(i,vtemp);
}
loop();
}
void draw() {
background(240);
translate(width/2, height/2);
scale(250);
//pointLight(255, 255, 255, 2*(mouseX-width/2), 2*(mouseY-height/2), 500);
shader(texs);
ambientLight(100, 102, 126);
shape(ico1);
}
}
}
上面遗漏了一些不相关的代码,上面显示运行时没有输出。请帮助显示错误。 请参阅 class 二十面体代码的处理示例,但在方法 createIcosahedron(level,textImg) 中的 init() 和 create() 以及 return class 二十面体。您可以在 google 中搜索 "processing Icosahedron" 以获取代码,但这并不重要。
听起来你想修改基于高度贴图纹理的细分二十面体的3D几何,有点像Processing > Examples > Demos > Graphics > Mesh Tweening:
请记住,您是在不是平面的表面上执行此操作,因此偏移 z 可能无法获得您想要的效果,因为 z 会根据您在二十面体的哪一侧而改变。
这部分:
PVector vtemp=ico1.getVertex(i);
pz=brightness(images[0].get((int)(ico1.getTextureU(i)),(int)(ico1.getTextureV(i))))*16;
vtemp=vtemp.set(vtemp.x,vtemp.y,pz);
ico1.setVertex(i,vtemp);
需要改变。
您需要计算该顶点位置的法线(垂直):这将告诉您考虑二十面体表面上的角度的方向。
用高程值(uv 映射亮度值)缩放(乘以)此向量会在相同方向上偏移整个法线,但相对于二十面体更向外或向内。
只需将顶点位置添加到缩放后的顶点法线即可将其平移到正确的位置。类似于:
PVector vtemp = ico1.getVertex(i);
float elevation = brightness(images[0].get((int)(ico1.getTextureU(i)),(int)(ico1.getTextureV(i))))*16
PVector vNormal = getVertexNormal(vtemp);
PVector elevatedVertex = PVector.add(vtemp, PVector.mult(vNormal,elevation));
ico1.setVertex(i,elevatedVertex);
其中 getVertexNormal()
将计算或检索该顶点的预计算顶点法线。那里有多种资源,这是 Computer Graphics StackExchange
这是使用 Blender 的直观解释:
我建议查看 HE_Mesh Processing library(您可以通过 Contribution Manager 轻松安装),因为它会提供大量 3D 几何工具。
此外,如果您还可以查看位移贴图 and/or normal mapping