Unity shader sequence frame animation code, by the way, complain about the unity shader system

This article is reproduced from: https://www.cnblogs.com/hellohuan/p/3512784.html Author: hellohuan Please indicate the statement when reprinting.
1. I
saw some people in the UNITY forum asking for unity shader sequence frame animation. I am good at writing shaders, so I wrote a CG shader.
The code is very simple, it is to transform the UV sampling sequence frame map, the number of rows and columns of the art configuration and the transformation speed.
Shader "HELLOHUAN/Hello_Sequence" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    _SizeX ("列数", Float) = 4
    _SizeY ("行数", Float) = 4
    _Speed ("播放速度",Float) = 200
}

SubShader {
    Tags { "RenderType"="Opaque"}
    LOD 200

CGPROGRAM
#pragma surface surf Lambert alpha

sampler2D _MainTex;
fixed4 _Color;

uniform fixed _SizeX;
uniform fixed _SizeY;
uniform fixed _Speed;

struct Input {
    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
    
    int index = floor(_Time .x * _Speed);
    int indexY = index/_SizeX;
    int indexX = index - indexY*_SizeX;
    float2 testUV = float2(IN.uv_MainTex.x /_SizeX, IN.uv_MainTex.y /_SizeY);
    
    testUV.x += indexX/_SizeX;
    testUV.y += indexY/_SizeY;
    
    fixed4 c = tex2D(_MainTex, testUV) * _Color;
    o.Albedo = c.rgb;
    
    o.Alpha = c.a;
    //o.Albedo = float3( floor(_Time .x * _Speed) , 1.0, 1.0);
}
ENDCG
}

Fallback "Transparent/VertexLit"
}

shader, UI


2. In the
afternoon, I saw the problem of transparent sorting in the art scene, and immediately asked them why a large piece of ground should be made of transparent material, and how refreshing it is to use hollowing out. After a while, the art came to ask me, there is no hollowed-out shader in Unity, I opened it. The editor took a worried look, and it is really basically transparent materials. The expert replied that in some mobile platforms, hollowing out will be more expensive than transparent. After a few years of rendering, this worldview collapsed, experts This is groundless, it has been tested on various real machines, and the reason given is: the OPENGLES2 standard does not support AlphaTest, it is all done with discare in the Shader, and the implementation of mobile hardware is all kinds of strange, at least the hollowing efficiency of a baseline machine like IPHONE4 is very dismal. The expert also gave a benchMark icon as proof.
                                  On some platforms, the frame rate of Blend is high
Of course I believed it. I still wrote a corresponding hollowing shader for the art, and gave suggestions. The efficiency of transparent hollowing is very exaggerated. It is better for the art to dig holes to achieve hollowing. (The company's art is not so hypocritical. The editor dumps more than a dozen times a day and doesn't say anything. Everyone is working hard, so why bother).

My feeling about the process of writing the Shader, why is the rendering state not exposed to the outside for art adjustment, does it have to be written in the Shader? Unity rendering also supports transparent sorting and tweening, but the "transparence + 1" code must be written in the Shader. This Nima, I want to write N Shaders to achieve the same Shader calculation, but the effect of different rendering states? Not to mention the increase in the number of file resources, the consumption of Shader switching can be larger than the consumption of rendering state switching. Is it a mobile graphics card architecture? What is the difference between OGL and DIRECTX rendering? Unity is also a cross-platform engine.

I have to admire the Shader system that I participated in writing in the previous company. I miss the Shader template editor that although it is plagiarized from Unreal, it makes the artist feel like writing a program by pulling nodes and connecting topological relationships. There is also a Shader instance editor that can be configured separately for specific instance resources, which controls certain shader variables and rendering states, and sorts and renders by shader type during rendering. It is more efficient to write the engine yourself.

However, as soon as the Shader editor at that time came out, there were also production problems. First, the artistic creation ability was too powerful, and the RGB of the color map could be transformed into a matrix. In the end, even the algorithm could not be understood, but it could be considered "dazzling". cool" effect. Second, too many shader creations are difficult to manage, and changes will have a greater impact on existing resources. The work of adding review templates and adding new templates has gloriously fallen on the shoulders of graphics programmers such as me. After tooling, it is still hard.
 
Front-end time research GB, the design of GameBryo's ShaderTree is very good, which is exactly the same as the previous Shader editor. Each node contains its own INPUT, ShaderConst, VSCOde, PSCode, and each node is connected through input and output. There are also many questions:
1. It is too difficult to expand. I once tried to add a calculation node with edge light to the ShaderTree, and it took a lot of effort.
2. There are too many permutations and combinations. The finally generated shader is saved locally through various switch mappings called GUIDs and compiled into binary files. I have also used this mechanism. That is, when the permutation and combination are released, the number is uncontrollable (Shader must be pre-generated).
GB's entire Shader system is very easy to expand, you can use ShaderTree by default, you can customize Shader, and support many languages.
 
In the maintained Gamebryo project, more than 20 Shaders were used in the whole project (the ShaderTree that comes with GB was not used), and more than a dozen are commonly used. It is easy to use and manage, but it is inconvenient to expand. UNITY also provides some built-in Shaders, which can be selected and used by artists, and also supports adding CG or Shader codes by themselves, but must follow its ShaderLib format.
 
I used to despise the previous editor of the shader assembly line. I felt that it was convenient for art, but it caused more engineering problems. However, UNITY's method is too irresponsible. What kind of plan is more suitable for the product, only the product itself knows. .
 
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324040409&siteId=291194637