GLSL几个要点
限定符
在之前的博客中也提到了,GLSL中的限定符号主要有:
attritude:一般用于各个顶点各不相同的量。如顶点颜色、坐标等。 输入
uniform:一般用于对于3D物体中所有顶点都相同的量。比如光源位置,统一变换矩阵等。
varying:表示易变量,一般用于顶点着色器传递到片元着色器的量。输出,vetexshader和fragshader数据的传递
const:常量。
privatestaticfinalStringVERTEX_SHADER="attribute vec4 position;\n"+
"attribute vec4 inputTextureCoordinate;\n"+
"attribute vec4 inputTextureCoordinate2;\n"+
"\n"+
"varying vec2 textureCoordinate;\n"+
"varying vec2 textureCoordinate2;\n"+
"\n"+
"void main()\n"+
"{\n"+
" gl_Position = position;\n"+
" textureCoordinate = inputTextureCoordinate.xy;\n"+
" textureCoordinate2 = inputTextureCoordinate2.xy;\n"+
"}";
publicstaticfinalStringALPHA_BLEND_FRAGMENT_SHADER="varying highp vec2 textureCoordinate;\n"+
" varying highp vec2 textureCoordinate2;\n"+
"\n"+
" uniform sampler2D inputImageTexture;\n"+
" uniform sampler2D inputImageTexture2;\n"+
"\n"+
" uniform lowp float mixturePercent;\n"+
"\n"+
" void main()\n"+
" {\n"+
" lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);\n"+
" lowp vec4 textureColor2 = texture2D(inputImageTexture2, textureCoordinate2);\n"+
"\n"+
" gl_FragColor = vec4(mix(textureColor.rgb, textureColor2.rgb, textureColor2.a * mixturePercent), textureColor.a);\n"+
" }";
以上是一个典型的顶点着色器和片段着色器
inputTextureCoordinatetextureCoordinate2 顶点传入
position 绑定顶点坐标
inputImageTexture
inputImageTexture2 创建并绑定纹理
inputTextureCoordinate
inputTextureCoordinate2绑定纹理坐标
textureCoordinate2作为片段着色器的输出 最终生成gl_FragColor
对于抖音特效,其实就改变gl_Position 和 gl_FragColor