• GLSL in ShaderLab


    Syntax

      However, use of raw GLSL is only recommended for testing, or when you know you will only target Mac OS X or OpenGL ES 2.0 compatible mobile devices. In majority of normal cases, Unity will cross-compile Cg/HLSL into optimized GLSL (this is done by default for mobile platforms, and can be optionally turned on for desktop platforms via #pragma glsl).

      GLSL program snippets are written between GLSLPROGRAM and ENDGLSL keywords.

      In GLSL, all shader function entry points have to be called main(). When Unity loads the GLSL shader, it loads the source once for the vertex program, with VERTEX preprocessor define, and once more for the fragment program, with FRAGMENT preprocessor define. So the way to separate vertex and fragment program parts in GLSL snippet is to surround them with #ifdef VERTEX.. #endif and #ifdef FRAGMENT .. #endif. Each GLSL snippet must contain both a vertex program and a fragment program.

      Standard include files match those provided for Cg shaders; they just have .glslinc extension: UnityCG.glslinc.

      Vertex shader inputs come from predefined GLSL variables (gl_Vertex, gl_MultiTexCoord0, ...) or are user defined attributes. Usually only the tangent vector needs a user defined attribute:

        attribute vec4 Tangent;
    

      Data from vertex to fragment programs is passed through varying variables, for example:

        varying vec3 lightDir; // vertex shader computes this, fragment shader uses this

    Examples

      为了使用ShaderLab中的Property,需像下面这样搞:

    Properties
    {
        //可以从编辑器中选择颜色
        _Color ("Main Color", Color) = (1,0.5,0.5,1)
    }
    
    #ifdef FRAGMENT
    //这就是从Unity编辑器给GLSL shader传递数据的方法,定义uniforms类型变量
    uniform vec4 _Color ;
    void main()
    {
        gl_FragColor = _Color;
    }
    #endif 

      完整代码如下所示:

    Shader "simple_Color_Shader"
            {
                Properties
                {
                       //可以从编辑器中选择颜色
                       _Color ("Main Color", Color) = (1,0.5,0.5,1)
                }
                SubShader
                {
                Tags { "Queue" = "Geometry" }
                Pass
                {
                GLSLPROGRAM
                #ifdef VERTEX
                void main()
                {
                   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
                }
                #endif  
            
                #ifdef FRAGMENT
                //这就是从Unity编辑器给GLSL shader传递数据的方法,定义uniforms类型变量
                uniform vec4 _Color ;
                void main()
                {
                   gl_FragColor = _Color;
                }
                #endif
                ENDGLSL
                }
                }
            }
    View Code

    参考:

    1、http://www.58player.com/blog-2532-69127.html

    2、file://localhost/Applications/Unity/Unity.app/Contents/Documentation/Documentation/Components/SL-GLSLShaderPrograms.html

  • 相关阅读:
    H.264编码之DCT变换原理
    颜色空间转换
    jdbc 大数据存储 图片读取
    spring 事务配置
    Cglib代理
    jdk代理
    spring装配bean
    map 常用方法
    list map set常用方法之list
    calendar 类 用法
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3878245.html
Copyright © 2020-2023  润新知