Normal rendering of unity3d shaderlab cubic graphs

Source: Internet
Author: User

Normal rendering of unity3d shaderlab cubic graphs

In some cases, we want the cube to produce a normal effect on the material ball, to show more detail, such as the diamond-shaped glass, the surface of the ice.

With the number of frames, we can pass our normal map through another built-in parameter of the input struct. Let's step through it.

First, create a new Shader, in the new material ball, prepare the cube chart, normal map. Double-click shader to enter the Script Editor.

This code modification is still the same as the reflection mask of the previous cube diagram, so just look at the finished code directly.

Code Start---------------------------------------------------------------------

Shader "91ygame/reflectionnormal" {

Properties {

_maintint ("Diffuse Tint", Color) = (1,1,1,1)

_maintex ("Base (RGB)", 2D) = "White" {}

_normalmap ("Normal Map", 2D) = "" "{}

_cubemap ("CubeMap", CUBE) = "" "{}

_reflamount ("Reflection Amount", Range (0.1,3)) =0.5

}

Subshader {

Tags {"Rendertype" = "Opaque"}

LOD 200

Cgprogram

#pragma surface surf Lambert

Sampler2d _maintex;

Sampler2d _normalmap;

Samplercube _cubemap;

FLOAT4 _maintint;

float _reflamount;

struct Input {

FLOAT2 Uv_maintex;

FLOAT2 Uv_normalmap;

FLOAT3 WORLDREFL;

Internal_data

};

void Surf (Input in, InOut surfaceoutput o) {

Half4 C = tex2d (_maintex, In.uv_maintex);

FLOAT3 normals = Unpacknormal (tex2d (_normalmap,in.uv_normalmap)). RGB;

O.normal = normals;

O.emission=texcube (_cubemap,worldreflectionvector (In,o.normal)). Rgb*_reflamount;

O.albedo = C.rgb*_maintint;

O.alpha = C.A;

}

Endcg

}

FallBack "Diffuse"

}

Code End-----------------------------------------------------------------------

By contrasting the variables, we just swapped the previous mask map for the texture to be found. But we just want to modify our reflection cube effect by pixel-wise normal map,

In order to achieve this, we must get normal information on the surface of the object after the normal map is applied to the shader, so we get the RGB information of the Normals by the unpacknormal method .

and then pass it on to ouput of the output structure body Normal property. The normals of the shader surface are then modified. So we need to use it to modify our reflection,

with Input structure, so we used the built- in Internal_data variable to access the modified normals. the worldreflectionvector (in,o.normal) function is then calculated to calculate the reflection vectors of the game scene world.

(Worldreflectionvector) calculates each pixel in a function (Per-pixel) Reflection Vector (reflection vector) .

this is Unity provides us with a built-in function that allows us to complete the function. Finally, we have the following effects:

On the left is the discovery map effect we just completed, which is the last one with a matte effect.

Normal rendering of unity3d shaderlab cubic graphs

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.