ATS Snow Shader

Questions à propos du scripting Shader.
Avatar de l’utilisateur
axel
Messages : 1924
Inscription : 26 Avr 2012 09:10
Localisation : Lille - Dunkerque
Contact :

Re: ATS Snow Shader

Message par axel » 21 Août 2012 16:07

bah, merci quand même, c'est peut être une piste.

Avatar de l’utilisateur
axel
Messages : 1924
Inscription : 26 Avr 2012 09:10
Localisation : Lille - Dunkerque
Contact :

Re: ATS Snow Shader

Message par axel » 03 Sep 2012 07:57

J'ai finalement réussi à faire ce que je voulais en bidouillant avec ce qui est contenu dans le built-in shaders.
A savoir: Diffuse+bump+specular
Et +emission quand on est en mode neige. Je trouvais que le shader était très sombre, avec un peu d'emission lumineuse cela rend la neige un peu plus crédible.

Voilà donc ce shader. Par contre comme je n'y connais rien en shader, et bien que cela fonctionne, il y a peut être des incohérences, ou des choses inutiles qui pourraient être optimisées. Si jamais vous y voyez des grosses bourdes, pouvez-vous me les indiquer?.
Merci d'avance.

Code : Tout sélectionner

Shader "Nature/snowShader Bumped Diffuse" {
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
	_Shininess ("Shininess", Range (0.03, 1)) = 0.078125	
	_MainTex ("Base (RGBA)", 2D) = "white" {}
	_BumpMap ("Normalmap", 2D) = "bump" {}
	_Emission ("Emmisive Color", Color) = (0,0,0,0)
}

SubShader {
	Tags { "RenderType"="Opaque" }
	pass {
	       Material {
          	  Shininess [_Shininess]
              Specular [_SpecColor]
              Emission [_Emission] 
        	}
        	ColorMaterial AmbientAndDiffuse
        	Lighting On
			SeparateSpecular On
		}
	LOD 300
	
CGPROGRAM

#pragma surface surf BlinnPhong vertex:snow
#pragma target 3.0
#include "UnityCG.cginc"


sampler2D _MainTex;
sampler2D _SnowTex;
sampler2D _BumpMap;
fixed4 _Color;
fixed4 _Emission;
half _Shininess;

float _snowShininess;
float _SnowAmount;
float _SnowStartHeight;
sampler2D _SnowTexture;

struct Input {
	float2 uv_MainTex;
	float2 uv_BumpMap;
	fixed4 color : COLOR;
	float3 worldPos;
	fixed3 MyWorldNormal;
};


void snow (inout appdata_full v, out Input o) {	
    o.MyWorldNormal = normalize(mul((float3x3)_Object2World, v.normal));
}


void surf (Input IN, inout SurfaceOutput o) {

	fixed4 col = tex2D(_MainTex, IN.uv_MainTex) * _Color;
	fixed4 snow = tex2D(_SnowTex, IN.uv_MainTex);
	o.Gloss = col.a;

	o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
	
	// get snow texture // distribution might be stored in alpha
	half4 snowtex = tex2D( _SnowTexture, IN.uv_MainTex);
	
	// clamp(IN.MyWorldNormal.y + 0.6, 0, 1) = allows snow even on orthogonal surfaces // (1-col.b) = take the blue channel to get some kind of heightmap
	float snowAmount = (_SnowAmount * (clamp(IN.MyWorldNormal.y + 0.6, 0, 1)) * (1-col.b) *.8 + clamp(o.Normal.y, 0, 1) * _SnowAmount * .25);

	// clamp snow to _SnowStartHeight
	snowAmount = snowAmount * clamp((IN.worldPos.y - _SnowStartHeight)*.0125, 0, 1);
	
	// sharpen snow mask
	snowAmount = clamp(pow(snowAmount,6)*256, 0, 1);
	
	o.Alpha = 0.0;	

	// mix 
	o.Albedo = col.rgb * (1-snowAmount) + snowtex.rgb*snowAmount;
	
	
	// smooth normal
	o.Normal = normalize(lerp(o.Normal, float3(0,0,1), snowAmount*.50));
	o.Specular = _Shininess;
	o.Emission = snowtex.rgb*snowAmount/2 * _Emission;
}
ENDCG  
}

FallBack "Bumped Specular"
}

Répondre

Revenir vers « les Shaders »