UnityShader源码2017---学习笔记与自我拓展040

源自Reflect-Bumped,Reflect-BumpNolight,Reflect-BumpSpec,Reflect-BumpVertexLit,Reflect-Diffuse,

Reflect-Glossy,Reflect-Parallax,Reflect-ParallaxSpec,Reflect-VertexLit

这一次是Reflect系列的。主要就是对一个Cubemap进行了采样,用_MainTex的A通道控制反射的强度。

本来是要找一下这个函数WorldReflectionVector的实现的,翻了一遍没有找到。

然后我翻了一下这一系列的shader,发现没有什么知识点要记录。。。难道就这么水一次博客?(反正水了无数次了)

算了,着手把这个函数WorldReflectionVector实现一下吧

// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

Shader "ShaderStore/UnitShader2017/Legacy Shaders/Reflective/Bumped Diffuse" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    _MainTex ("Base (RGB) RefStrength (A)", 2D) = "white" {}
    _Cube ("Reflection Cubemap", Cube) = "_Skybox" { }
    _BumpMap ("Normalmap", 2D) = "bump" {}
}

SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 300

CGPROGRAM
#pragma surface surf Lambert nolightmap nofog
#pragma vertex vert 
#pragma exclude_renderers d3d11_9x
#pragma target 3.0

sampler2D _MainTex;
sampler2D _BumpMap;
samplerCUBE _Cube;

fixed4 _Color;
fixed4 _ReflectColor;

struct Input {
    float2 uv_MainTex;
    float2 uv_BumpMap;
    //float3 worldRefl;
    //INTERNAL_DATA

    float4 TtoW0;
    float4 TtoW1;
    float4 TtoW2;
};


void vert(inout appdata_full v,out Input data)
{
    UNITY_INITIALIZE_OUTPUT(Input,data);
    float3 viewDir = _WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld,v.vertex).xyz;
    
    float3 worldNormal = normalize(mul(v.normal,(float3x3)unity_WorldToObject));
    float3 worldTangent = normalize(mul((float3x3)unity_ObjectToWorld,v.tangent.xyz));
    float3 worldBinormal = cross(worldNormal,worldTangent) * v.tangent.w;

    data.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x,viewDir.x);//float4(worldTangent,viewDir.x);
    data.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y,viewDir.y);//float4(worldBinormal,viewDir.y);
    data.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z,viewDir.z);//float4(worldNormal,viewDir.z);
}

float3 WorldReflectionVector_CP(Input IN,float3 TangentNormal)
{
    float3x3 rotation = float3x3(IN.TtoW0.xyz,IN.TtoW1.xyz,IN.TtoW2.xyz);
    float3 viewDir = float3(IN.TtoW0.w,IN.TtoW1.w,IN.TtoW2.w);

    //float3 worldNormal = mul(TangentNormal,rotation);
   
    float3 worldNormal;
    worldNormal.x = dot(IN.TtoW0.xyz, TangentNormal);
    worldNormal.y = dot(IN.TtoW1.xyz, TangentNormal);
    worldNormal.z = dot(IN.TtoW2.xyz, TangentNormal);

    float3 r = reflect(-viewDir,worldNormal);
    return r;
}

void surf (Input IN, inout SurfaceOutput o) {
    fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    fixed4 c = tex * _Color;
    o.Albedo = c.rgb;

    o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));

    //float3 worldRefl = WorldReflectionVector (IN, o.Normal);
    float3 worldRefl = WorldReflectionVector_CP (IN, o.Normal);


    fixed4 reflcol = texCUBE (_Cube, worldRefl);
    reflcol *= tex.a;
    o.Emission = reflcol.rgb * _ReflectColor.rgb;
    o.Alpha = reflcol.a * _ReflectColor.a;
}
ENDCG
}

FallBack "Legacy Shaders/Reflective/VertexLit"
}

猜你喜欢

转载自blog.csdn.net/u012871784/article/details/81200848