【发布时间】:2021-12-08 06:46:49
【问题描述】:
首先是截图:
我正在尝试根据网格上点的高度混合网格上的多个纹理。
现在我想要实现的是边界处的平滑混合(不像目前的锐线)。
我还希望边框稍微随机,并有一个控制随机性的因素。
这是我当前的代码:
#version 430 core
out vec4 FragColor;
#define NUM_TEXTURE_LAYERS 8
uniform vec3 _LightPosition;
uniform vec3 _LightColor;
in float height;
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoord;
uniform sampler2D _DiffuseTextures[NUM_TEXTURE_LAYERS];
uniform vec3 _DiffuseTexturesHeights[NUM_TEXTURE_LAYERS];
vec4 GetTextureColorBasedOnHeight(vec2 coord){
for(int i=0;i<NUM_TEXTURE_LAYERS;i++){
if(height > _DiffuseTexturesHeights[i].x && height < _DiffuseTexturesHeights[i].y){
return texture(_DiffuseTextures[i], coord*_DiffuseTexturesHeights[i].z);
}
}
return vec4(0.0f);
}
void main()
{
vec3 objectColor = vec3(1, 1, 1);
objectColor = GetTextureColorBasedOnHeight(TexCoord).xyz;
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(_LightPosition - FragPos);
float diff = max(dot(norm, lightDir), 0.0f);
vec3 diffuse = diff * _LightColor;
vec3 result = (vec3(0.2, 0.2, 0.2) + diffuse) * objectColor;
FragColor = vec4(result, 1.0);
}
我确实尝试了随机边界,但这不是很好,问题仍然是混合!
这里是随机的代码:
vec2 hash( vec2 p ) // replace this by something better
{
p = vec2( dot(p,vec2(127.1,311.7)), dot(p,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(p)*43758.5453123);
}
float noise( in vec2 p )
{
const float K1 = 0.366025404; // (sqrt(3)-1)/2;
const float K2 = 0.211324865; // (3-sqrt(3))/6;
vec2 i = floor( p + (p.x+p.y)*K1 );
vec2 a = p - i + (i.x+i.y)*K2;
float m = step(a.y,a.x);
vec2 o = vec2(m,1.0-m);
vec2 b = a - o + K2;
vec2 c = a - 1.0 + 2.0*K2;
vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
vec3 n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0)));
return dot( n, vec3(70.0) );
}
vec4 GetTextureColorBasedOnHeight(vec2 coord){
for(int i=0;i<NUM_TEXTURE_LAYERS;i++){
float nv=noise(coord*0.01f);
if(height+nv > _DiffuseTexturesHeights[i].x && height+nv < _DiffuseTexturesHeights[i].y){
return texture(_DiffuseTextures[i], coord*_DiffuseTexturesHeights[i].z);
}
}
return vec4(0.0f);
}
【问题讨论】:
标签: c++ opengl glsl textures blending