Unity3d Shaderlab Simple Cube-chart reflection

Source: Internet
Author: User

Unity3d Shaderlab Simple Cube-chart reflection

Reflection is a key factor in a shader's simulation of a real-world environment that makes our shader rendering more visually striking because he takes advantage of the environment around us,

Let the shader reflect the outside scene information and reflect them to the material surface to simulate the external environment, so we will use the cubic graph "CubeMap" 6 textures to simulate the color situation of the environment.

First, create a Shader, create a material ball, prepare a cubic diagram, double-click Shader, and go to the Code Editor.

From the Properties to the cgprogram, and then to the surf, this function is relatively simple, first look at the code.

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

Shader "91ygame/cube1" {

Properties {

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

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

_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;

Samplercube _cubemap;

FLOAT4 _maintint;

float _reflamount;

struct Input {

FLOAT2 Uv_maintex;

FLOAT3 WORLDREFL;

};

void Surf (Input in, InOut surfaceoutput o) {

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

O.emission = Texcube (_CUBEMAP,IN.WORLDREFL). Rgb*_reflamount;

O.albedo = C.rgb;

O.alpha = C.A;

}

Endcg

}

FallBack "Diffuse"

}

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

With the simple modifications above, we also have a simple in-traditional reflection effect, which can be viewed directly in Unity.

In summary, the shader samples the cubic graph and gets a similar real-world reflection.

This is thanks to Unity 's built -in worldrefl attribute in the surface shader's Input structure , which provides us with the necessary reflection vectors directly.

Help us to sample the cubic chart correctly. We only need to use the WORLDREFL variable in the texcube function to get the correct reflection graph.

Unity3d Shaderlab Simple Cube-chart reflection

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.