zoukankan      html  css  js  c++  java
  • Vertex and fragment programs

    Vertex and fragment programs

      When you use vertex and fragment programs (the so called "programmable pipeline"), most of the hardcoded functionality ("fixed function pipeline") in the graphics hardware is switched off. For example, using a vertex program turns off standard 3D transformations, lighting and texture coordinate generation completely. Similarly, using a fragment program replaces any texture combine modes that would be defined in SetTexture commands; thus SetTexture commands are not needed.

    Using Cg in ShaderLab

      Shaders in ShaderLab are usually written in Cg programming language by embedding "Cg snippets" in the shader text. Cg snippets are compiled into low-level shader assembly by the Unity editor, and the final shader that is included in your game's data files only contains this low-level assembly. 

      Note that because Cg code is compiled by the editor, you can't create Cg shaders from scripts at runtime.(OpenGL的glCompileShader提供运行时创建Shader的能力)

      In general, Cg snippets are placed inside Pass blocks. They look like this:

      

      The following example demonstrates a complete shader with Cg programs that renders object normals as colors:  

    Shader "Tutorial/Display Normals" {
    SubShader {
        Pass {
    
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #include "UnityCG.cginc"
    
    struct v2f {
        float4 pos : SV_POSITION;
        float3 color : COLOR0;
    };
    
    v2f vert (appdata_base v)
    {
        v2f o;
        o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
        o.color = v.normal * 0.5 + 0.5;
        return o;
    }
    
    half4 frag (v2f i) : COLOR
    {
        return half4 (i.color, 1);
    }
    ENDCG
    
        }
    }
    Fallback "VertexLit"
    } 
    View Code

      

    The whole Cg snippet is written between CGPROGRAM and ENDCG keywords. At the start compilation directives are given as #pragma statements:

    • #pragma vertex name tells that the code contains a vertex program in the given function (vert here).
    • #pragma fragment name tells that the code contains a fragment program in the given function (frag here).

    Following the compilation directives is just plain Cg code. We start by including a built-in Cg file:

        #include UnityCg.cginc

    The UnityCg.cginc file contains commonly used declarations and functions so that the shaders can be kept smaller (see shader include files page for details). Here we'll use appdata_basestructure from that file. We could just define them directly in the shader and not include the file of course.

    Using shader properties in Cg code】  

      When you define properties in the shader, you give them a name like _Color or _MainTex. To use them in Cg you just have to define a variable of a matching name and type. Unity will automatically set Cg variables that have names matching with shader properties.  

    Shader "Tutorial/Textured Colored" {
    Properties {
        _Color ("Main Color", Color) = (1,1,1,0.5)
        _MainTex ("Texture", 2D) = "white" { }
    }
    SubShader {
        Pass {
    
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    
    #include "UnityCG.cginc"
    
    float4 _Color;
    sampler2D _MainTex;
    
    struct v2f {
        float4  pos : SV_POSITION;
        float2  uv : TEXCOORD0;
    };
    
    float4 _MainTex_ST;
    
    v2f vert (appdata_base v)
    {
        v2f o;
        o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
        o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
        return o;
    }
    
    half4 frag (v2f i) : COLOR
    {
        half4 texcol = tex2D (_MainTex, i.uv);
        return texcol * _Color;
    }
    ENDCG
    
        }
    }
    Fallback "VertexLit"
    } 
    View Code

    参考:file://localhost/Applications/Unity/Unity.app/Contents/Documentation/Documentation/Manual/ShaderTut2.html

  • 相关阅读:
    原型模型
    单例模式
    C# 委托 delegate
    C#泛型
    客户端注册Cannot execute request on any known server解决
    SpringCloud 学习(二)-2 :Securing The Eureka Server
    SpringCloud 学习(二)-1 :服务注册与发现Eureka扩展
    解决网速慢时maven仓库访问慢
    SpringCloud 学习(二) :服务注册与发现Eureka
    SpringCloud版本问题
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3874916.html
Copyright © 2011-2022 走看看