沿矩形框的边缘进行纹理处理

时间:2018-12-19 10:11:14

标签: 3d glsl webgl texture-mapping

从这个答案here开始,我试图将仅包含一些透明部分(png)的纹理仅应用于矩形棱镜的边缘。

enter image description here

我的第一次尝试(左图)是使用线性过滤的4x4纹理。这是代码:

// Normal direction branching
// M: coordinates, MN: normals of the model, sX,sY,sZ. size of the box
if( (abs(MN.x) > abs(MN.y)) && (abs(MN.x) > abs(MN.z)) ) {
  // X axis
  texCoords = M.yz / vec2(sY, sZ);
} else if( (abs(MN.z) > abs(MN.x)) && (abs(MN.z) > abs(MN.y)) ) {
  // Z axis
  texCoords = M.xy / vec2(sX, sY);
} else {
  // Y axis
  texCoords = M.xz / vec2(sX, sZ);
}
texCoords += vec2(0.5);

我对这个结果感到不满意,因为盒子侧面有明显可见的伪影。 之后,我尝试使用距离函数和2x2纹理(中心图片),但是无法参数化映射的纹理到盒子边缘的距离。这是我第二次尝试的代码:

float squared(vec2 pos) {
  float at = (atan(pos.x, pos.y) + PI) / (2.0 * PI);
  float st = floor(at * 4.0 + 0.5) / 4.0;
  float dist = length(pos) * cos((at - st) * 2.0 * PI);
  return smoothstep(0.3, 1.0, dist);
}
// Here goes the same direction branching as above
texCoords *= squared(texCoords);
texCoords += vec2(0.5);

我正在尝试获得如右图所示的效果,该纹理将纹理应用于框的所有表面,模拟了边缘的漂亮方形阴影。

我现在正在接近距离函数,因此我不确定在这里应使用哪种方法,而且我想知道这是否是我要尝试的正确方法。

如何在箱子的周围绘制纹理坐标,直到与边缘保持一定距离?

1 个答案:

答案 0 :(得分:0)

这个问题有一个争议,所以我在这里发表我的成就。

这是我的最终结果:

Hello

我仍然必须了解是否可以避免轴分支,但是至少我对这个结果感到满意:

enter image description here

顺便说一句:

此图片的第一个示例使用了我的问题中提到的2x2纹理,而第二个示例使用了垂直翻转的相同纹理,而 blueish 是穿过颜色透明部分的材质颜色纹理。

相关问题