Unity Shader reflect

Source: Internet
Author: User
Tags mul reflection
Adapted from the CG tutorial-the authoritative guide to programmable real-time graphics.

The purpose of this article is not to teach you how to achieve reflection, but to let you learn to migrate CG, HLSL, GLSL to unity [unity shader data is rare, but which of the preceding is relatively large]

Reflection Vector Calculation Formula R=I-2N (n*i) Note n*i is the point multiply I incident ray, N normal vector

Function implementation:
FLOAT3 reflect (FLOAT3 i,float3 N)
{
Return I-2.0*n*dot (n,i);
}

Shader "CG Shader Reflect" {
Properties {
Environmentmap ("Environment Map", Cube) = "" "{}
Reflectivity ("Reflectivity1", float) = 1//reflection coefficient determines reflection strength
Decalmap ("Decalmap", 2D) = "White" {}
}
subshader{
pass{
Cgprogram
#pragma vertex vert//Vertex programming entry
#pragma fragment Frag//Fragment Programming entry
#include "unitycg.cginc"//Note introduction
struct vertinput{//defined in UNITYCG can refer to Appdata_base, Appdata_tan, Appdata_full
FLOAT4 position:position;
FLOAT2 texcoordw:texcoord0;
FLOAT3 Normal1:normal;
};
struct vertoutput{
FLOAT4 oposition:sv_position;
FLOAT2 otexcoord:texcoord0;
FLOAT3 R:texcoord1;
};
The parameters of the uniform type need to be defined in the properties
Uniform Samplercube Environmentmap;
Uniform float reflectivity;
Uniform sampler2d Decalmap;
Vertoutput Vert (Vertinput input)
{
Vertoutput o;
O.oposition=mul (unity_matrix_mvp,input.position),//UNITY_MATRIX_MVP variable, is the Model view projection matrix (modelviewproj) in the corresponding graph, which is defined in UNITY Must be written like this.
O.OTEXCOORD=INPUT.TEXCOORDW;
FLOAT3 Positionw=mul (_object2world,input.position). Xyz;//_object2world model matrix, transfer local coordinates to world coordinates
FLOAT3 N=mul ((float3x3) _object2world,input.normal1);
N=normalize (N);
FLOAT3 i=positionw-_worldspacecamerapos;//calculates the incident light, which needs to be computed in the world coordinate system. Position of the _worldspacecamerapos viewpoint in world coordinates
O.r=reflect (i,n);//calculation of reflected light reflect system self-band function
return o;
}

FLOAT4 Frag (vertoutput output): COLOR
{
Float4 Reflectioncolor=texcube (environmentmap,output. R);
FLOAT4 decalcolor=tex2d (Decalmap,output.otexcoord);
FLOAT4 Color1=lerp (decalcolor,reflectioncolor,reflectivity);
return color1;
}
Endcg
}
}
}

The above example code appears _object2world,_worldspacecamerapos inexplicable wonderful parameters, from the Unitycg.cginc file, in the CG, HLSL, GLSL to transplant, Special parameters need to refer to the definition of the Unitycg.cginc file.

Unity Shader Learning Materials: http://en.wikibooks.org/wiki/Cg_Programming/Unity

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.