码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3d《Shader篇》漫反射

时间:2015-06-23 17:12:50      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

Unity3d《Shader篇》漫反射

 

Shader "Custom/Ambient" {
    
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _Color("Diffuse Material Color", Color) = (1,1,1,1)
        _SpecColor("Specular Color", Color) = (1,1,1,1)
        _Shininess("Shininess", Float) = 10
    }

    SubShader
    {
        Pass
        {
            Tags{ "LightMode" = "ForwardBase" }
            
            CGPROGRAM
            
            #pragma exclude_renderers gles

            #pragma vertex vert
            #pragma fragment frag
            
            sampler2D _MainTex;
            
            uniform float4 _Color;
            uniform float4 _SpecColor;
            uniform float _Shininess;
            
            uniform float4 _LightColor0;
            
            struct vertexInput
            {
                float2 texCoord : TEXCOORD0;
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };
            
            struct vertexOutput
            {
                float4 pos : SV_POSITION;
                float4 color : COLOR;
                float2 texCoord : TEXCOORD0;
            };
            
            vertexOutput vert( vertexInput input )
            {
                vertexOutput output;
                float4x4 modelMatrix = _Object2World;
                
                float3 normalDir = normalize(float3(mul(_Object2World, float4(input.normal,0.0))));
                float3 lightDir = normalize(float3(_WorldSpaceLightPos0));
                float3 viewDir = normalize(_WorldSpaceCameraPos - float3(mul(_Object2World,input.vertex)));
                
                float specularRefection = float3(_LightColor0)*float3(_SpecColor)*pow(max(0.0,dot(reflect(-lightDir,normalDir),viewDir)),_Shininess);
                float3 diffuseReflection = float3(_LightColor0)*float3(_Color)*max(0.0,dot(normalDir,lightDir));
                
                float3 ambientLighting = float3(UNITY_LIGHTMODEL_AMBIENT)*float3(_Color);
                
                output.color = float4(ambientLighting+diffuseReflection+specularRefection,1.0);
                output.pos = mul(UNITY_MATRIX_MVP,input.vertex);
                output.texCoord = input.texCoord;
                
                return output;
                
            }
            
            float4 frag(vertexOutput input) : COLOR
            {
                float4 col = tex2D(_MainTex,input.texCoord)*_Color;
                return input.color*col;    
                
            }
            
            ENDCG
        }
    }
    
    
    FallBack "Diffuse"
}

 

Unity3d《Shader篇》漫反射

标签:

原文地址:http://www.cnblogs.com/mrblue/p/4595608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!