如何将shadershop公式转换为glsl [英] how to convert shadershop formula into glsl

查看:79
本文介绍了如何将shadershop公式转换为glsl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在学习着色器的一些基本知识,并且想出了一个很棒的视觉工具:

而且我可以在glsl中将其转换:

然后我继续,我在shadershop中创建了一个二维公式:

但是,我只是不知道如何像以前一样将公式转换为glsl.

任何建议将不胜感激,谢谢:)

更新

我再次尝试根据@ Rabbid76的建议转换公式:

但是我仍然很难理解:

  1. 如何将公式分为U和V
  2. 如何处理公式中的矩阵

解决方案

I have been learning some basic of shaders recently and I came up with a great visual tools : shadershop

But I am having trouble to convert the formula I created in this site into glsl.

A simple example, I created a formula in this site:

And I am able to convert this in glsl:

And then I moved on, I created a two dimension formula on shadershop :

But this I just have no clue how to convert this formula into glsl just like I did before.

Any advice will be appreciated, thanks :)

UPDATE

I tried again to convert the formula according to @Rabbid76 's advice:

But still I am having trouble to understan :

  1. how to split the formula to U and V
  2. how to deal with the matrix in the formula

解决方案

The formula of shadershop can be expressed as follows:

vec2  x1x2 = inverse(m) * vec2(x1, x2);
float x    = -sin(x1x2.x - x1x2.y);

where m is a 2x2 matrix.

e.g.

mat2 m = mat2(
    0.1, 0.0,
    0.5, 1.0
);

For the formula for the inverse matrix see www.mathwords.com (in GLSL ES 1.00 there is no function for the inverse matrix) :

float det_m = m[0][0]*m[1][1] - m[0][1]*m[1][0];
mat2  inv_m = mat2(m[1][1], -m[0][1], -m[1][0], m[0][0]) / det_m;

The full fragment shader code may look like this:

void main()
{
    vec2 st = 2.0 * gl_FragCoord.xy / resolution.xy - 1.0;

    vec2 scale = vec2(1.5, 1.5);
    st *= scale;

    mat2 m = mat2(
        0.1, 0.0,
        0.5, 1.0
    );

    vec2 x1x2 = vec2(st.x, 0.0);
    float det_m = m[0][0]*m[1][1] - m[0][1]*m[1][0];
    if ( det_m != 0.0 )
    { 
      mat2 inv_m = mat2(m[1][1], -m[0][1], -m[1][0], m[0][0]) / det_m;
      x1x2 = inv_m * st.xy;
    }
    float x = -sin(x1x2.x - x1x2.y);

    vec3 color = vec3( x, x, abs(x) );
    gl_FragColor = vec4(color, 1.0);
}

See the preview:

这篇关于如何将shadershop公式转换为glsl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆