Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the vertex fragments that can be textured in Shader Lab

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to achieve texture mapping vertex fragments in Shader Lab, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

There is a picture of a brick wall

Assign it to the material as a texture map, and assign the material to the cube in the scene

It doesn't involve lighting or shadows, it just assigns a map, it's not complicated.

Shader Code:

Shader "Custom/TestVFTexture" {Properties {_ MainTex ("MainTex", 2d) = "" {}} SubShader {pass {CGPROGRAM # pragma vertex vert # pragma fragment frag # include "unitycg.cginc" sampler2D _ MainTex; float4 _ MainTex_ST; struct v2f {float4 pos:POSITION Float2 uv:TEXCOORD0;}; v2f vert (appdata_base v) {v2fo; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.uv = TRANSFORM_TEX (v.texcoord, _ MainTex); return o } fixed4 frag (V2f IN): COLOR {fixed4 col = tex2D (_ MainTex, IN.uv); return col;} ENDCG}} 1

Since you want to assign a texture, of course you need to define a variable to store the texture in the Properties block.

_ MainTex ("MainTex", 2d) = "" {}

Type is 2d.

two

Again, you have to declare it in a CG program before you can use it.

Sampler2D _ MainTex

Type is sampler2D, note D capitalization.

Define a float2 in the structure to record uv information

Float2 uv:TEXCOORD0

Assign TEXCOORD0

three

Deal with this uv in the vertex program

O.uv = TRANSFORM_TEX (v.texcoord, _ MainTex)

Here TRANSFORM_TEX is defined in unitycg.cginc. Find the place where it is defined.

/ / Transforms 2D UV by scale/bias property#define TRANSFORM_TEX (tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)

Two parameters: tex and name

Tex comes from v.texcoord.

Name, this is _ MainTex.

Note: name##_ST.xy and name##_ST.zw

Name##_ST is _ MainTex_ST, and there is xyzw, which is a four-dimensional vector, which also needs to be declared.

Sampler2D _ MainTex;float4 _ MainTex_ST

If you don't declare, you will report an error.

So what is this treatment for?

After we have defined the map in the Properties block of shader, viewing the material on the Inspector panel of unity will automatically bring two attributes, Tilling and Offset.

These are both two-dimensional vectors, and Tilling represents the displacement in both directions, well, tiling, Offset is the displacement in both directions.

However, TRANSFORM_TEX must be done in shader for these two attributes to take effect correctly, and that's what TRANSFORM_TEX is for.

four

Calculate the corresponding color for the map in the fragment program

CG function tex2D (sampler2D tex, float2 s) to get the four-dimensional vector color.

In this example

Fixed4 col = tex2D (_ MainTex, IN.uv)

The above is how to realize the vertex fragments that can be textured in Shader Lab. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report