Подписывайтесь на наш Telegram и не пропускайте важные новости! Перейти

Вопрос Шейдер жидкого стекла

Начинающий
Начинающий
Статус
Оффлайн
Регистрация
6 Июн 2025
Сообщения
201
Реакции
1
1772396326643.png
решил сделать себе жидкое стекло но тут появилась проблемка не работает преломление
прогнал пару раз через клод он сделать преломление не смог

GlassBuilder:
Expand Collapse Copy
package wtf.mimilove.client.render.builders.impl;

import wtf.mimilove.client.render.builders.AbstractBuilder;
import wtf.mimilove.client.render.builders.states.QuadColorState;
import wtf.mimilove.client.render.builders.states.QuadRadiusState;
import wtf.mimilove.client.render.builders.states.SizeState;
import wtf.mimilove.client.render.renderers.impl.BuiltGlass;

public final class GlassBuilder extends AbstractBuilder<BuiltGlass> {

    private SizeState size;
    private QuadRadiusState radius;
    private QuadColorState color;
    private float smoothness;

    public GlassBuilder size(SizeState size) {
        this.size = size;
        return this;
    }

    public GlassBuilder radius(QuadRadiusState radius) {
        this.radius = radius;
        return this;
    }

    public GlassBuilder color(QuadColorState color) {
        this.color = color;
        return this;
    }

    public GlassBuilder smoothness(float smoothness) {
        this.smoothness = smoothness;
        return this;
    }

    @Override
    protected BuiltGlass _build() {
        return new BuiltGlass(
            this.size,
            this.radius,
            this.color,
            this.smoothness
        );
    }

    @Override
    protected void reset() {
        this.size = SizeState.NONE;
        this.radius = QuadRadiusState.NO_ROUND;
        this.color = QuadColorState.WHITE;
        this.smoothness = 6.0f;
    }

}

glass.fsh:
Expand Collapse Copy
#version 150

#client_import <client:common.glsl>

in vec2 FragCoord;
in vec2 TexCoord;
in vec4 FragColor;

uniform sampler2D Sampler0;
uniform vec2 Size;
uniform vec4 Radius;
uniform float Smoothness;
uniform float Time;

out vec4 OutColor;

vec2 randomVec2(vec2 co) {
    return fract(sin(vec2(dot(co, vec2(127.1, 311.7)),
                          dot(co, vec2(269.5, 183.3)))) * 43758.5453);
}

vec3 sampleWithNoise(vec2 uv, float timeOffset, float mipLevel) {
    vec2 texSize = textureSize(Sampler0, 0);
    vec2 offset = randomVec2(uv + vec2(Time + timeOffset)) / texSize.x;
    return textureLod(Sampler0, uv + offset * pow(2.0, mipLevel), mipLevel - 1.0).rgb;
}

vec3 getBlurredColor(vec2 uv, float mipLevel) {
    return (sampleWithNoise(uv, 0.0, mipLevel) +
            sampleWithNoise(uv, 0.25, mipLevel) +
            sampleWithNoise(uv, 0.5, mipLevel) +
            sampleWithNoise(uv, 0.75, mipLevel) +
            sampleWithNoise(uv, 1.0, mipLevel) +
            sampleWithNoise(uv, 1.25, mipLevel) +
            sampleWithNoise(uv, 1.5, mipLevel) +
            sampleWithNoise(uv, 1.75, mipLevel) +
            sampleWithNoise(uv, 2.0, mipLevel)) * 0.1;
}

vec3 saturate(vec3 color, float factor) {
    float gray = dot(color, vec3(0.299, 0.587, 0.114));
    return mix(vec3(gray), color, factor);
}

vec2 computeRefractOffset(float sdf) {
    if (sdf < 0.1) {
        return vec2(0.0);
    }
    vec2 grad = vec2(dFdx(sdf), dFdy(sdf));
    float gradLen = length(grad);
    if (gradLen < 0.001) {
        return vec2(0.0);
    }
    grad = grad / gradLen;
    float offsetAmount = pow(abs(sdf), 12.0) * -0.1;
    return grad * offsetAmount;
}


float highlight(float sdf) {
    if (sdf < 0.1) {
        return 0.0;
    }
    vec2 grad = vec2(dFdx(sdf), dFdy(sdf));
    float gradLen = length(grad);
    if (gradLen < 0.001) {
        return 0.0;
    }
    grad = grad / gradLen;
    return 1.0 - clamp(pow(1.0 - abs(dot(grad, vec2(-1.0, 1.0))), 0.5), 0.0, 1.0);
}

void main() {
    vec2 center = Size * 0.5;
    vec2 centeredUV = center - (FragCoord * Size);
    
    float sdf = rdist(centeredUV, center - 1.0, Radius);
    float normalizedInside = (sdf / (Size.y * 0.5)) + 1.0;
    float edgeBlendFactor = pow(normalizedInside, 12.0);
    
    vec3 baseTex = texture(Sampler0, TexCoord).rgb;
    
    vec2 sampleUV = TexCoord + computeRefractOffset(normalizedInside);
    float mipLevel = mix(3.5, 1.5, edgeBlendFactor);
    
    vec3 blurredTex = getBlurredColor(sampleUV, mipLevel) * 0.8 + 0.2;
    blurredTex = mix(blurredTex, pow(saturate(blurredTex, 2.0), vec3(0.5)), edgeBlendFactor);
    blurredTex += mix(0.0, 0.5, clamp(highlight(normalizedInside) * pow(edgeBlendFactor, 5.0), 0.0, 1.0));
    
    float boxMask = ralpha(Size, FragCoord, Radius, Smoothness);
    vec3 finalColor = mix(baseTex, blurredTex, vec3(boxMask));
    
    vec4 color = vec4(finalColor, 1.0) * FragColor;
    color.a *= boxMask;
    
    if (color.a == 0.0) {
        discard;
    }
    
    OutColor = color;
}

glass.vsh:
Expand Collapse Copy
#version 150

#client_import <client:common.glsl>

in vec3 Position;
in vec4 Color;

uniform mat4 ModelViewMat;
uniform mat4 ProjMat;

out vec2 FragCoord;
out vec2 TexCoord;
out vec4 FragColor;

void main() {
    gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0);

    FragCoord = rvertexcoord(gl_VertexID);
    TexCoord = gl_Position.xy * 0.5 + 0.5;
    FragColor = Color;
}
Заранее всем спасибо
 
Назад
Сверху Снизу