2014-03-02 55 views
3

我已經開始爲我的大學團隊項目創建一些着色器,但是我遇到了一些與我的水着色器有關的問題。我正在嘗試創建一個使用兩個重疊的法線貼圖。我的統一着色器的問題

雖然在編輯器中一切看起來都不錯,但當我發佈到網頁播放器時,場景看起來就像是不亮。

繼承人我的shader代碼:

// 
// Filename : WaterShader.shader 
// Version : 2.0 
// Date : 1st March 2014 
// 

Shader "Flight/WaterShader/2.0" 
{ 
    // Set up variables so we can access them in inspector mode 
    Properties 
    { 
     // Variable to control the colour tint 
     _Color ("Base Color", Color) = (1, 1, 1, 1) 

     // Variables for specular 
     _SpecularColor ("Specular Color", Color) = (1, 1, 1, 1) 
     _SpecularAmount ("Shininess", Float) = 10 

     // Variable for setting the base texture 
     _MainTex ("Water Texture", 2D) = "white" { } 

     // Variables to set the normal map 1 
     _BumpMapA ("Normal Map", 2D) = "bump" { } 
     _BumpDepthA ("Depth", Range(0.25, 10.0)) = 1 
     // Variables to set the normal map 2 
     _BumpMapB ("Normal Map", 2D) = "bump" { } 
     _BumpDepthB ("Depth", Range(0.25, 10.0)) = 1 
    } 

    SubShader 
    { 
     pass 
     { 
      Tags { "RenderType" = "Opaque" } 
      Lighting On 

      CGPROGRAM 

      #pragma vertex vert 
      #pragma fragment frag 
      #pragma exclude_renderers flash 

      // Variables 
      sampler2D _MainTex; 
      float4 _MainTex_ST; 

      sampler2D _BumpMapA; 
      float4 _BumpMapA_ST; 
      float _BumpDepthA; 

      sampler2D _BumpMapB; 
      float4 _BumpMapB_ST; 
      float _BumpDepthB; 

      float4 _Color; 
      float4 _SpecularColor; 
      float _SpecularAmount; 
      float4 _LightColor0; 

      struct vertexInput 
      { 
       float4 vertex : POSITION; 
       float3 normal : NORMAL; 
       float4 texcoord : TEXCOORD0; 
       float4 tangent : TANGENT; 
      }; 

      struct vertexOutput 
      { 
       float4 pos : SV_POSITION; 
       float4 tex : TEXCOORD0; 
       float4 posWorld : TEXCOORD1; 
       float3 normalWorld : TEXCOORD2; 
       float3 tangentWorld : TEXCOORD3; 
       float3 binormalWorld : TEXCOORD4; 
      }; 

      vertexOutput vert(vertexInput input) 
      { 
       vertexOutput output; 

       output.pos = mul(UNITY_MATRIX_MVP, input.vertex); 
       output.tex = input.texcoord; 
       output.posWorld = mul(_Object2World, input.vertex); 

       output.normalWorld = normalize(mul(float4(input.normal, 0.0f), _World2Object).xyz); 
       output.tangentWorld = normalize(mul(_Object2World, input.tangent).xyz); 
       output.binormalWorld = normalize(cross(output.normalWorld, output.tangentWorld) * input.tangent.w); 

       return output; 
      } 

      float4 frag(vertexOutput input) : COLOR 
      { 
       // Set up variables 
       float3 viewDirection; 
       float3 lightDirection; 
       float3 normalDirection; 

       float lightIntensity; 

       float4 normalColorA; 
       float4 normalColorB; 
       float4 normalColor; 

       float3 normalLocalA; 
       float3 normalLocalB; 
       float3 normalLocal; 

       float3x3 normalWorld; 

       float4 textureColor; 
       float3 diffuseColor; 
       float3 specularColor; 
       float3 lightColor; 
       float4 finalColor; 

       // Begin calculations 

       // Calculate the angle we are looking at the pixel 
       viewDirection = normalize(_WorldSpaceCameraPos.xyz - input.posWorld.xyz); 

       if(_WorldSpaceLightPos0.w == 0.0) 
       { 
        lightIntensity = 1.0; 
        lightDirection = normalize(_WorldSpaceLightPos0.xyz); 
       } 
       else 
       { 
        float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - input.posWorld.xyz; 
        float distance = length(fragmentToLightSource); 
        lightIntensity = 1.0/distance; 
        lightDirection = normalize(fragmentToLightSource); 
       } 

       // Sample the textures 
       textureColor = tex2D(_MainTex, input.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw); 
       normalColorA = tex2D(_BumpMapA, input.tex.xy * _BumpMapA_ST.xy + _BumpMapA_ST.zw); 
       normalColorB = tex2D(_BumpMapB, input.tex.xy * _BumpMapB_ST.xy + _BumpMapB_ST.zw); 

       // Expand the normals and set the intensity of the normal map 
       normalLocalA = float3(2.0 * normalColorA.ag - float2(1.0, 1.0), 0.0); 
       normalLocalA.z = _BumpDepthA; 

       normalLocalB = float3(2.0 * normalColorB.ag - float2(1.0, 1.0), 0.0); 
       normalLocalB.z = _BumpDepthB; 

       // Combine the two normals 
       normalLocal = normalize(normalLocalA + normalLocalB); 


       // Calculate the normal in the world 
       normalWorld = float3x3(input.tangentWorld, input.binormalWorld, input.normalWorld); 
       normalDirection = normalize(mul(normalLocal, normalWorld)); 

       // Calculate lighting 
       diffuseColor = lightIntensity * _LightColor0.xyz * saturate(dot(normalDirection, lightDirection)); 
       specularColor = diffuseColor * _SpecularColor.xyz * pow(saturate(dot(reflect(-lightDirection, normalDirection), viewDirection)), _SpecularAmount); 

       // Combine lighting 
       lightColor = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseColor + specularColor; 

       // Apply lighting to the texture color 
       textureColor = float4(textureColor.xyz * lightColor * _Color.xyz, 1.0); 

       return textureColor; 
      } 

      ENDCG 
     } 
    } 
    FallBack "Specular" 
} 

在編輯器中,它看起來是這樣的:

Editor Mode

凡在Web播放它看起來像這樣:

Webplayer

任何人都能幫助我看到我出錯的地方嗎? :)

+0

替換當前SubShader參數我是一個團結小白但Web播放絕對支持所有獨立的發動機運行時支持着色器的功能?它看起來不亮 – Charleh

回答

1

您需要在場景中爲網絡播放器單獨傳遞像素光。與

SubShader 
{ 
    Tags {"RenderType" = "Opaque"}  
    Pass 
    { 
     Tags {"LightMode" = "ForwardAdd"}  
    } 
    Pass 
    { 
     CGPROGRAM 
     #pragma vertex vert 
     #pragma fragment frag 
     #pragma exclude_renderers flash 
+0

完美的謝謝:) – user2990037

+0

我不明白這個語法......第一遍使用的vert/frag着色器是什麼?它只有標籤,它是否使用一些內置的默認值?或者在下一個回合中回退到cgprogram? – benblo