gpt4 book ai didi

opengl - GLSL 多纹理混合纹理

转载 作者:行者123 更新时间:2023-12-04 02:09:42 25 4
gpt4 key购买 nike

我正在尝试在 OpenGL 中实现多重纹理。使用的纹理基于给定顶点的表面法线 - 它越垂直,第二个纹理就越可见。

Here是我目前所拥有的。

我现在想将边缘混合在一起,而不是那种硬边。是否可以以这种方式混合纹理?如果可以,我该怎么做?

这是我的片段着色器代码:

#version 150

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCamera;
in vec3 playerPosition;
in vec4 vertexPosition;

in float blendPosition;
in float visibility;

out vec4 out_Color;

uniform sampler2D texture0;
uniform sampler2D texture1;

uniform vec3 skyColour;
uniform vec3 light_colour;
uniform float shineDamper;
uniform float reflectivity;

void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);

float nDot1 = dot(unitNormal,unitLightVector);
float brightness = max(nDot1,0.2);
vec3 diffuse = brightness * light_colour;

vec3 unitToCamera = normalize(toCamera);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);

float specular = dot(reflectedLightDirection, unitToCamera);
specular = max(specular,0.0);
float damped = pow(specular,shineDamper);
vec3 finalSpecular = damped * reflectivity * light_colour;

out_Color = (vec4(diffuse,1.0) * texture(texture0,pass_textureCoords)) + vec4(-20,-20,0.0,0.0);
out_Color = (vec4(diffuse,1.0) * texture(texture0,pass_textureCoords));
out_Color = mix(vec4(skyColour,1.0),out_Color,visibility);

if(vertexPosition.y < -6.1 || surfaceNormal.y < 0.6){
out_Color = (vec4(diffuse,1.0) * texture(texture1,pass_textureCoords)) + vec4(-20,-20,0.0,0.0);
out_Color = (vec4(diffuse,1.0) * texture(texture1,pass_textureCoords));
out_Color = mix(vec4(diffuse,1.0) * texture(texture0,pass_textureCoords),out_Color,1);
out_Color = mix(vec4(skyColour,1.0),out_Color,visibility);
}
if(playerPosition.y < -6.1){
out_Color = mix(vec4(0.0,0.3,0.5,1.0),out_Color,0.1);
}
}

编辑:

这是任何感兴趣的人的新片段着色器代码

更新的片段着色器代码:

#version 150

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCamera;
in vec3 playerPosition;
in vec4 vertexPosition;

in float blendPosition;
in float visibility;

out vec4 out_Color;

uniform sampler2D texture0;
uniform sampler2D texture1;

uniform vec3 skyColour;
uniform vec3 light_colour;
uniform float shineDamper;
uniform float reflectivity;

void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);

float nDot1 = dot(unitNormal,unitLightVector);
float brightness = max(nDot1,0.2);
vec3 diffuse = brightness * light_colour;

vec3 unitToCamera = normalize(toCamera);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);

float specular = dot(reflectedLightDirection, unitToCamera);
specular = max(specular,0.0);
float damped = pow(specular,shineDamper);
vec3 finalSpecular = damped * reflectivity * light_colour;

out_Color.a = 1;

vec4 fog = vec4(skyColour,1.0);
vec4 diffusion = vec4(diffuse,1.0);

float a = clamp((unitNormal.y - .6)*5 + .5, 0, 0.7);
vec3 texture0_colour = (mix(fog,diffusion * texture(texture0,pass_textureCoords),visibility)).rgb;
vec3 texture1_colour = (mix(fog,diffusion * texture(texture1,pass_textureCoords),visibility)).rgb;

out_Colour.rgb = mix(texture1_colour,texture0_colour,a);

}

最佳答案

要根据 a 值混合两个纹理,您可以这样做:

float a = ...;
vec3 color0 = texture(texture0, pass_textureCoords).rgb;
vec3 color1 = texture(texture1, pass_textureCoords).rgb;
out_Color.rgb = mix(color0, color1, a);

假设您的 unitNormal = (0,1,0) 是向上的方向,如代码所示,则值

float a = clamp(unitNormal.y, 0, 1);

将导致两个纹理之间的平滑过渡。但是,您可能想要更清晰的过渡,在这种情况下,您可以移动和缩放 unitNormal.y 值以调整过渡开始和结束的位置:

float a = clamp((unitNormal.y - .6)*5 + .5, 0, 1);

关于opengl - GLSL 多纹理混合纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39779908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com