First cut out one:
To reflect the world around you.
All we need is a cubemap of the world around us.
First look at the reflection of the shader
Declare the variable first:
_cubemap cubemap that need reflection
_reflamount the intensity of the reflection
Properties {_maintex ("Base (RGB)", 2D) = "White" {}_maintint ("Diffuse color", color) = (1, 1, 1, 1) _cubemap ("Cubemap", CUBE) = "" {}_reflamount ("Reflection Amount", Range (0.01, 1)) = 0.5}
You also need to add a parameter to the input struct body
FLOAT3 WORLDREFL
Worldref is the reflection vector of world space
Built-in WORLDREFL to do cubic reflection
struct Input {float2 uv_maintex;float3 WORLDREFL;////WORLDREFL: Is the reflection vector for world space///built-in WORLDREFL to do cubic reflection (cubemap Reflection)//In order to calculate the reflection};
assigning values to
emission(divergence color of pixels) in the surf function
texcube function to get information on cubic graph textures
and multiply the
_reflamount reflection intensity we defined earlier.
O.emission = Texcube (_cubemap, IN.WORLDREFL). RGB * _REFLAMOUNT;
And then it's OK,
We've got a shader that can reflect the world around us .
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;}
The next problem to be solved is real-time ;
If we can update this cubemapin real time, we can reflect it in real time.
Next we create a C # script
All variables:
The CubeMap to update
Public Cubemap Cubmap;
here 's a note: We're going to create a new camera to place on the object to be reflected, as a sub- object of the object to be reflected.
Instead of the main camera
public Camera Cam;
The material here is the shader material above.
Private Material Curmat;
Public Cubemap Cubmap; public Camera Cam; Private Material Curmat;
Initialize the Curmat,
is to get the material we want to reflect.
Curmat = renderer.sharedmaterial;
Create a timer in the start function
Invokerepeating ("Updatechange", 1, 0.1f);
Call the Updatechange () function after 1 seconds, then call every 0.1 seconds
void Start () { invokerepeating ("Updatechange", 1, 0.1f); Curmat = renderer.sharedmaterial; }
Build this updatechange () function again
Keep the camera angle constant
Cam.transform.rotation = quaternion.identity;
This is our core function,
Rendertocubemap render to Cubic diagram, bake a static cubic map of the scene, assign parameters
Cam.camera.RenderToCubemap (CUBMAP);
And then pass this cubemap to the _CUBEMAP variable above shader, and it will be updated in real time.
Curmat.settexture ("_cubemap", Cubmap);
void Updatechange () { cam.transform.rotation = quaternion.identity; Cam.camera.RenderToCubemap (CUBMAP); Curmat.settexture ("_cubemap", Cubmap); }
Ok
first look at the effect :
It can be seen from fluctuating bodies of water that actually reflect the surrounding environment in real time.
Let us analyze the effects and efficiency issues,
Admittedly, although this method can do real-time reflection, but it is really "wasteful", and high definition will be very card
Look at the effect of CubeMap's facesize value on the effect
32:
Obviously, 32 is quite vague, quite "jagged"
64:
64 is also very vague, but better than 32, there are some bumps in the sense of
128:
128 effect feels good, but it's not particularly clear.
256:
By the time I was 256, I could say it was a simple scene with no cards in it.
At 512, there was a little bit of cards,
In fact, 256 in the game is also not practical,
Recommended under 1281
512:
512 is already very clear, there is a little card.
1024:
When I cut this 1024, my unity is almost stuck, the frame rate is less than 10fps, = =; it's a little too clear.
Look at 1024 cards so arrogant don't dare to try 2048 estimate also and 1024 similar,
The above is over,
This film does not need any picture resources you can try
All of the following code:
C #;
Using unityengine;using System.collections;public class d_cubmap:monobehaviour{public Cubemap Cubmap; public Camera Cam; Private Material Curmat; Use the This for initialization void Start () { invokerepeating ("Updatechange", 1, 0.1f); Curmat = renderer.sharedmaterial; } void Updatechange () { cam.transform.rotation = quaternion.identity; Cam.camera.RenderToCubemap (CUBMAP); Curmat.settexture ("_cubemap", Cubmap); }}
Shader
Shader "Custom/cubmap" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_maintint ("Diffuse color", Color) = (1, 1, 1, 1) _cubemap ("Cubemap", CUBE) = "" "{}_reflamount (" Reflection Amount ", Range (0.01, 1)) = 0.5}subshader {Tags {" Rendertype " = "Opaque"}lod 200cgprogram#pragma surface surf lambertsampler2d _maintex;samplercube _cubemap;float4 _maintint;float _ Reflamount;struct Input {float2 UV_MAINTEX;FLOAT3 WORLDREFL;////WORLDREFL: That is, the reflection vector for world space///built-in WORLDREFL to do cubic map reflection ( CubeMap Reflection)//In order to calculate reflection};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 = Texcube (_cubemap, IN.WORLDREFL). RGB * _ Reflamount;//c.rgb;o. Albedo = C.rgb;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}
---------by wolf96
Unity3d Real-time dynamic reflection around the world