将CSS过滤器应用于片段着色器

时间:2020-07-28 19:11:52

标签: javascript css filter fragment-shader

我正在尝试将CS​​S过滤器应用到window.js中平面元素的片段着色器。我对此很陌生,在如何实现这一目标方面我很难缠。我能够做到这一点的唯一方法是将其应用于画布CSS,有人告诉我我不想这样做。任何帮助将不胜感激。

.psych body {margin:0;padding:0;background:#000;}
@-webkit-keyframes psychedelic{
  0% {
-webkit-filter: hue-rotate(0deg) saturate(2) invert(0);
  } 
  50% {
-webkit-filter: hue-rotate(360deg) saturate(2) invert(0);
  }
  100% {
-webkit-filter: hue-rotate(0deg) saturate(2) invert(0);
  }
}

.psych img{
width:100%;height:100%;
-webkit-animation: psychedelic linear 10s infinite;
-moz-animation: psychedelic linear 10s infinite;
-o-animation: psychedelic linear 10s infinite;
animation: psychedelic linear 10s infinite;
}

1 个答案:

答案 0 :(得分:1)

基于您在此处发布的片段着色器:How to call multiple planes in javascript?

这是相同的片段着色器,它每10秒旋转色相并饱和最终颜色:

var fs = `
    #ifdef GL_ES
    precision mediump float;
    #endif

    // get our varyings
    varying vec3 vVertexPosition;
    varying vec2 vTextureCoord;

    // the uniform we declared inside our javascript
    uniform float uTime;

    // our texture sampler (default name, to use a different name please refer to the documentation)
    uniform sampler2D planeTexture;


    vec3 hueRotate(vec3 col, float hue) {
        vec3 k = vec3(0.57735, 0.57735, 0.57735);
        float cosAngle = cos(hue);
        return col * cosAngle + cross(k, col) * sin(hue) + k * dot(k, col) * (1.0 - cosAngle);
    }

    vec3 saturate(vec3 rgb, float adjustment) {
        vec3 W = vec3(0.2125, 0.7154, 0.0721);
        vec3 intensity = vec3(dot(rgb, W));
        return mix(intensity, rgb, adjustment);
    }


    void main() {
        // get our texture coords
        vec2 textureCoord = vTextureCoord;

        // displace our pixels along both axis based on our time uniform and texture UVs
        // this will create a kind of water surface effect
        // try to comment a line or change the constants to see how it changes the effect
        // reminder : textures coords are ranging from 0.0 to 1.0 on both axis
        const float PI = 3.141592;

        textureCoord.x += (
            sin(textureCoord.x * 10.0 + ((uTime * (PI / 10.0)) * 0.031))
            + sin(textureCoord.y * 10.0 + ((uTime * (PI / 10.489)) * 0.047))
        ) * 0.0075;

        textureCoord.y += (
            sin(textureCoord.y * 15.0 + ((uTime * (PI / 10.023)) * 0.023))
            + sin(textureCoord.x * 15.0 + ((uTime * (PI / 10.1254)) * 0.067))
        ) * 0.0125;

        vec4 color = texture2D(planeTexture, textureCoord);

        // hue rotation from 0 to PI in 10 seconds
        float hueRotation = cos(uTime / 600.0) * PI;
        color.rgb = hueRotate(color.rgb, hueRotation);

        // saturate
        color.rgb = saturate(color.rgb, 2.0);

        gl_FragColor = color;
    }
`;

干杯