Unity Shader學(xué)習(xí)—GLSL編寫shader

在unity中,一般都是用Cg語言編寫頂點(diǎn)片元著色器,或者直接使用表面著色器,這兩者的編寫語言都是Cg,除了Cg外,我們還可以使用OpenGL編寫。

用Cg編寫的方式稱為HLSL,用OpenGL編寫的方式稱為GLSL。

那么,我們習(xí)慣上使用Cg,也就是HLSL在unity中編寫shader,但偶爾換個(gè)口味也是可以的。
這里介紹的就是使用GLSL編寫shader。

當(dāng)然,這里介紹的只是語法結(jié)構(gòu),并不是涉及很多,而我本身也不是很擅長(zhǎng)去編寫這類shader。

通過這個(gè)語法結(jié)構(gòu),我們可以將一些用OpenGL編寫的著色器拿到unity中使用。

OK,直接來看一下代碼:

簡(jiǎn)單的改變顏色shader代碼

Shader "LearnShader/GLSL/GLSLTest"{
    Properties{
        _Color("Color",Color) = (1,1,1,1)
    }
    SubShader{
        Tags{
            "Queue" = "Geometry" 
            "RenderType" = "Opaque"
            "PreviewType" = "Plane"
        }
        Pass{
            GLSLPROGRAM
            #ifdef VERTEX
            void main(){
                gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
            }
            #endif
            #ifdef FRAGMENT
            uniform vec4 _Color;
            void main(){
                gl_FragColor = _Color;
            }
            #endif
            ENDGLSL
        }
    }
}

OK,如果你直接把這段代碼拷貝進(jìn)unity中的話會(huì)報(bào)錯(cuò)的。
在編寫shader之間,我們需要對(duì)unity做一點(diǎn)點(diǎn)改變,使其支持GLSL編寫shader。

找到桌面上的unity快捷方式鍵,右鍵,打開屬性面板,如下圖:

Paste_Image.png

在目標(biāo)那一欄中,最后面添加:-force-opengl
如果你的unity是打開的,那么就關(guān)閉重啟一下。
然后就是正常的創(chuàng)建shader代碼,然后將上述代碼寫好,保存即可。

需要我們注意的是:

  1. 如果你想通過inspector面板調(diào)節(jié)shader屬性,那么除了在Properties塊中定義屬性外,在Pass塊中定義屬性需要在其類型前面加上 uniform 關(guān)鍵字。
  2. GLSL的代碼段是被包括在 GLSLPROGRAM 與ENDGLSL之間。
  3. 頂點(diǎn)與片元著色器,都是通過 #ifdef 與 #endif 加上 VERTEX 與 FRAGMENT 關(guān)鍵字確定的。
  4. 頂點(diǎn)與片元著色器中的類似gl_Position等是OpenGL中的語法。

以上就是關(guān)于使用GLSL編寫shader的語法結(jié)構(gòu)。

最后給出一個(gè)比較有意思的shader,也是使用GLSL編寫的。

代碼如下:

Shader "LearnShader/GLSL/GLSLTest"{
    Properties{
        _MainTex("Base(RGB)",2D) = "white"{}
    }
    SubShader{
        Tags{"RenderType" = "Opaque" "Queue" = "Geometry"}
        Pass{
            GLSLPROGRAM
            #ifdef VERTEX
            void main(){
                gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
            }
            #endif
            #ifdef FRAGMENT
            uniform sampler2D _MainTex;
            const float size = 0.5;

            void main(){
               vec2 realSize = vec2(textureSize(_MainTex, 0));
               float ratio = (realSize.x > realSize.y) ? 
               realSize.y/realSize.x : realSize.x/realSize.y;
               
               vec2 texSize = vec2(512., 512.);
               vec2 xy = gl_FragCoord.xy;
               
               if(realSize.x > realSize.y) {
                  xy.x = xy.x * ratio;
               }
               else
![Animation.gif](http://upload-images.jianshu.io/upload_images/5301156-17a2bc8700542539.gif?imageMogr2/auto-orient/strip)
{
                  xy.y = xy.y * ratio;
               }
               
               vec2 center = vec2(texSize/2.);

               float maxV = dot(center, center);
               float minV = floor(maxV*(1. - size));
               float diff = maxV - minV;
               
               vec2 uv = xy / texSize;
               
               vec4 srcColor = texture2D(_MainTex, uv);

               float dx = center.x - xy.x;
               float dy = center.y - xy.y;
               
               float dstSq = pow(dx, 2.) + pow(dy, 2.);
               
               float v = (dstSq / diff);
               float r = clamp(srcColor.r + v, 0., 1.);
               float g = clamp(srcColor.g + v, 0., 1.);
               float b = clamp(srcColor.b + v, 0., 1.);
               
               gl_FragColor = vec4( r, g, b, 1.0 );
            }
            #endif
            ENDGLSL
        }
    }
}

結(jié)果:

Animation.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容