Unityshader Example 11: snow-covered material

Source: Internet
Author: User
Tags mul pow

Snow Material

Overview
snow material is my own name for this material, since it is snow, as the name implies, snow is falling from the sky, so the snow is on the surface of the object upward, regardless of how your model is placed, snow is guaranteed to be in the object facing up (in unity is the y-axis positive direction) of the surface, as shown:



Implementation Principle

to ensure that the upward surface of the snow, in fact, the normal direction of the model surface and the world coordinate space of the y-axis is consistent with the same amount of snow, or snow is less snow, so the normal direction of the model from model space to the world space, and then with the y-axis positive direction FLOAT3 (0,1,0) do dot product , according to the result to confirm the direction of the method and the angle of the y-axis size, so as to confirm whether the snow; this is the same as the previous edge light material, the difference is that it is to consider the model normal and line of sight direction of the angle;

Shader Code Implementation
finally to the code to achieve, think of a little excitement, hey. In view of the more complex VF writing lights, this example of the material is not implemented lighting support, nonsense said, start with the property definition, at least two posters, and then define a parameter to control the amount of snow:
Properties {_maintex ("Base (RGB)", 2D) = "White" {}_snowtex ("snowtexture (RGB)", 2D) = "White" {}_snowcount ("Snowcount", R Ange (0.0, 1)) = 0.078}

define the input structure body and the output structure body;
struct appdata_t {float4 vertex:position;float2 texcoord:texcoord0;float3 normal:normal;}; struct V2F {float4 vertex:sv_position;half2 texcoord:texcoord0;half2 snowuv:texcoord1  ; fixed4 snow:color;//defines a A four-dollar number is used to pass the snow position to the Frag function Unity_fog_coords (1)};

Next is the more critical part of the Vert function, where you get the normal of the model, and convert the normal direction to the world space and the y-axis to do dot product, to determine the snow surface, the key code is as follows:
FLOAT3 Worldnormal = Mul ((float3x3) _object2world, v.normal);//convert normals to World space float rim = 1-saturate (dot (FLOAT3), Worldnormal);//The dot product operation of the normal direction of world space and y-axis O.texcoord = Transform_tex (V.texcoord, _maintex); o.snowuv = Transform_tex ( V.texcoord, _snowtex); o.snow = POW (rim,_snowcount*64);//control of the amount of snow, this value will be passed into the Frag function to mix two posters

The Frag function is simple and uses the value passed in the vert i.snow to blend two maps to make snow-covered effects. Here is the complete code
VF version Code
Shader "PENGLU/UNLIT/SNOWVF" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_snowtex ("snowtexture (RGB)", 2D) = "whit E "{}_snowcount (" Snowcount ", Range (0.0, 1)) = 0.078}subshader {Tags {" rendertype "=" Opaque "}lod 100Pass {Cgprogram#pra GMA Vertex Vert#pragma fragment Frag#pragma multi_compile_fog#include "Unitycg.cginc" struct appdata_t {float4 Vertex:po Sition;float2 texcoord:texcoord0;float3 Normal:normal;}; struct V2F {float4 vertex:sv_position;half2 texcoord:texcoord0;half2 snowuv:texcoord1; fixed4 Snow:color; Unity_fog_coords (1)};sampler2d _maintex;sampler2d _snowtex;float4 _maintex_st,_snowtex_st;float _SnowCount;v2f Vert (appdata_t v) {v2f O;o.vertex = Mul (UNITY_MATRIX_MVP, V.vertex); FLOAT3 worldnormal = Mul ((float3x3) _object2world, v.normal); float rim = 1-saturate (dot (float3 (0,1,0), worldnormal)), O.texcoord = Transform_tex (V.texcoord, _maintex); o.snowuv = TRANSFORM_ TEX (V.texcoord, _snowtex); o.snow = POW (rim,_snowcount*64); Unity_transfer_fog (O,o.vertex); return o;} Fixed4 Frag (v2f i): sv_target{fixed4 col = tex2d (_maintex, I.texcoord); Fixed4 snow = tex2d (_snowtex,i.snowuv); col = Lerp (Snow,col,saturate (I.snow)); Unity_apply_fog (I.fogcoord, col); Unity_opaque_alpha (COL.A); return col;} ENDCG}}}


implementation of the effect with normal map
The above code is calculated directly from the normals of the vertices, so you can see that the effect of snow is still a bit rough, so you can consider the effect of relationship serve with normal maps more precise and delicate. The same shader does not achieve the effect of light, or a unlit material, the principle is similar, so do not do too much parsing, the complete code is the effect as follows:

VF Version Code 02:


Shader "PENGLU/UNLIT/SNOWBUMPVF" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_bumpmap ("Basebump", 2D) = "Bump" { }_snowtex ("Snowtexture (RGB)", 2D) = "White" {}_snowcount ("Snowcount", Range (0.01, 1)) = 0.078}subshader {Tags {"Renderty PE "=" Opaque "}lod 200Pass {cgprogram#pragma vertex vert#pragma fragment Frag#pragma multi_compile_fog#include" UNITYCG. Cginc "struct v2f {float4 vertex:sv_position;half2 texcoord:texcoord0;half2 snowuv:texcoord1;//Fixed4 Snow:color ; float3 TANGENT:TEXCOORD2;FLOAT3 BINORMAL:TEXCOORD3;FLOAT3 Normal:texcoord4; Unity_fog_coords (1)};sampler2d _maintex,_bumpmap;sampler2d _snowtex;float4 _maintex_st,_snowtex_st;float _ snowcount;v2f Vert (Appdata_full v) {v2f O;o.vertex = Mul (UNITY_MATRIX_MVP, V.vertex); o.tangent = V.tangent.xyz;o.normal = V.normal;o.binormal=cross (v.normal,v.tangent.xyz) *v.tangent.w;o.texcoord = Transform_tex (V.texcoord, _MainTex); o . Snowuv = Transform_tex (V.texcoord, _snowtex); Unity_transfer_fog (O,o.vertex); return o;} Fixed4 Frag (v2f i): sv_target{fixed4 col = tex2d (_maintex, I.texcoord); Fixed4 snow = tex2d (_SNOWTEX,I.SNOWUV); float3x3 Rotati ON=FLOAT3X3 (i.tangent.xyz,i.binormal,i.normal); FLOAT3 N = Unpacknormal (tex2d (_bumpmap,i.texcoord)); N=normalize (Mul (n,rotation)); n = Mul ((float3x3) _object2world, N), float rim = 1-saturate (dot (float3 (0,1,0), N)), float4 Lerpsnow = POW (rim,_snowcount* Col = Lerp (snow,col,saturate (Lerpsnow)); Unity_apply_fog (I.fogcoord, col); Unity_opaque_alpha (COL.A); return col;} ENDCG}}}


VF Version Code 02 effect:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Unityshader Example 11: snow-covered material

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.