关于cubemap的反射

关于cubemap的反射 本身的cubemap是静态的,而所谓的动态反射是因为视角在变化。

It should be noted that skyboxes are usually static and don't include any dynamic objects of the scene. However, “skyboxes” for reflection mapping are often rendered to include the scene from a certain point of view. This is, however, beyond the scope of this tutorial.

因为cubemap够大,因此反射只和角度有关,但是和物体与天空盒的距离无关

This computation actually becomes easier if the skybox is infinitely large: in that case the position of the surface point doesn't matter at all since its distance from the origin of the coordinate system is infinitely small compared to the size of the skybox; thus, only the direction of the reflected ray matters but not its position. Therefore, we can actually also think of a ray that starts in the center of a small skybox instead of a ray that starts somewhere in an infinitely large skybox.

Shader "Cg shader with reflection map" {
Properties {
    #include "UnityCG.cginc"
    struct vertexInput {
       float4 vertex : POSITION;
       float4 pos : SV_POSITION;
       vertexOutput output;

}

_Cube("Reflection Map", Cube) = "" {}

SubShader {

Pass {

CGPROGRAM

#pragma vertex vert
#pragma fragment frag
// User-specified uniforms
uniform samplerCUBE _Cube;

};

float3 normal : NORMAL;
struct vertexOutput {
float3 normalDir : TEXCOORD0;

};

float3 viewDir : TEXCOORD1;
vertexOutput vert(vertexInput input)

{

float4x4 modelMatrix = _Object2World;
float4x4 modelMatrixInverse = _World2Object;
// multiplication with unity_Scale.w is unnecessary

}}

关于最后的混合

output.viewDir = float3(mul(modelMatrix, input.vertex)

- float4(_WorldSpaceCameraPos, 1.0));//由物体的点到摄像机的点

output.normalDir = normalize(float3(
mul(float4(input.normal, 0.0), modelMatrixInverse)));

//由世界坐标转化为模型坐标

output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
return output;

}

float4 frag(vertexOutput input) : COLOR

{

float3 reflectedDir =
reflect(input.viewDir, normalize(input.normalDir));
return texCUBE(_Cube, reflectedDir);

}

ENDCG

}

// because we normalize transformed vectors
Shader "MADFINGER/Environment/newweapon" {
Properties {
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_Cube("Reflection Map", Cube) = "" {}
_SHLightingScale("LightProbe influence scale",float) = 1
}
SubShader {
Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"}
LOD 100
 
CGINCLUDE
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float3 _SpecDir;
float3 _SpecColor;
float _Shininess;
float _SHLightingScale;
samplerCUBE _Cube;
struct v2f {
    float4 pos : SV_POSITION;
    float2 uv : TEXCOORD0;
    fixed3 spec : TEXCOORD1;
    fixed3 SHLighting: TEXCOORD2;
    fixed3 normalDir:TEXCOORD3;
    fixed3 viewDir:TEXCOORD4;

};

v2f vert (appdata_full v)
 
 

{

v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord;
//
float3 worldNormal = mul((float3x3)_Object2World, v.normal);
float3 worldV = normalize(-WorldSpaceViewDir(v.vertex));
float3 refl = reflect(worldV, worldNormal);
float3 shl = ShadeSH9(float4(worldNormal,1));
//Cubemap反射 o.normalDir=normalize(float3(mul(float4(v.normal,0.0),_World2Object)));

float3 worldLightDir = _WorldSpaceLightPos0;
o.SHLighting    = shl * _SHLightingScale;
o.viewDir = float3(mul(_Object2World, v.vertex) -
float4(_WorldSpaceCameraPos, 1.0));
o.spec = normalize(shl) * pow(saturate(dot(worldLightDir, refl)),
_Shininess * 128) * 2;

return o; }

ENDCG

 

} }

    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma fragmentoption ARB_precision_hint_fastest
    fixed4 frag (v2f i) : COLOR
    {
        fixed3 reflectedDir= reflect(i.viewDir,normalize(i.normalDir));
        fixed4 c_cube=texCUBE(_Cube,reflectedDir);
        fixed4 c_tex    = tex2D (_MainTex, i.uv);
        fixed4 c = lerp(c_tex,c_cube,c_tex.a);
        c.rgb *= i.SHLighting;
        c.rgb += i.spec.rgb * c.a;

return c; }

ENDCG

posted @ 2015-06-19 21:56  蜡笔大新  阅读(465)  评论(0)    收藏  举报