Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.8 kB
2
Indexable
Never
varying vec2 v_texCoord;
varying vec3 v_normal;

void main() {
    v_texCoord = uv;
    v_normal = normalize(normalMatrix * normal);
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}


uniform sampler2D u_diffuseTexture;
uniform sampler2D u_maskTexture;
uniform sampler2D u_normalMap;
uniform sampler2D u_roughnessMap; // Nowy uniform dla roughness mapy
uniform vec3 u_color;
uniform vec3 u_lightDirection;
uniform vec3 u_ambientLight;

varying vec2 v_texCoord;
varying vec3 v_normal;

void main() {
    vec2 flippedUV = vec2(v_texCoord.x, 1.0 - v_texCoord.y);

    // Pobieranie kolorów z tekstur
    vec4 diffuseColor = texture2D(u_diffuseTexture, flippedUV);
    vec4 maskColor = texture2D(u_maskTexture, flippedUV);
    vec3 normal = texture2D(u_normalMap, flippedUV).rgb;

    // Pobieranie roughness mapy z domyślną wartością 0.5
    float roughness = texture2D(u_roughnessMap, flippedUV).r * 0.5;

    // Normalizacja normal mapy z zakresu [0, 1] do [-1, 1]
    normal = normalize(normal * 2.0 - 1.0);

    // Obliczanie oświetlenia
    vec3 lightDir = normalize(u_lightDirection);
    float diff = max(dot(normal, lightDir), 0.0);
    
    // Roughness wpływa na specular highlight
    vec3 specular = vec3(pow(diff, 1.0 / (roughness + 0.01))); // Specular z uwzględnieniem roughness

    vec3 ambient = u_ambientLight * diffuseColor.rgb;
    vec3 diffuse = diff * diffuseColor.rgb;
    vec3 colorWithMask = mix(diffuseColor.rgb, u_color, step(0.5, maskColor.b));

    // Finalny kolor z uwzględnieniem roughness
    vec3 finalColor = ambient + (diffuse + specular) * colorWithMask;

    gl_FragColor = vec4(finalColor, diffuseColor.a);
}





const diffuseTexture = new THREE.TextureLoader().load('path/to/your/diffuseTexture.png');
const maskTexture = new THREE.TextureLoader().load('path/to/your/maskTexture.png');
const normalTexture = new THREE.TextureLoader().load('path/to/your/normalMap.png');
const roughnessTexture = new THREE.TextureLoader().load('path/to/your/roughnessMap.png'); // Ładowanie roughness map

const customShaderMaterial = new THREE.ShaderMaterial({
    vertexShader: vertexShaderCode,  // Zdefiniuj kod vertex shader
    fragmentShader: fragmentShaderCode,  // Zdefiniuj kod fragment shader
    uniforms: {
        u_diffuseTexture: { value: diffuseTexture },
        u_maskTexture: { value: maskTexture },
        u_normalMap: { value: normalTexture },
        u_roughnessMap: { value: roughnessTexture }, // Przypisanie roughness mapy
        u_color: { value: new THREE.Color(1, 0, 0) }, // Kolor
        u_lightDirection: { value: new THREE.Vector3(1, 1, 1).normalize() }, // Kierunek światła
        u_ambientLight: { value: new THREE.Vector3(0.2, 0.2, 0.2) } // Światło otoczenia
    }
});
Leave a Comment