Triplanar normal mapping pour terrain

Questions à propos du scripting Shader.
Sébastien Rousseau
Messages : 1
Inscription : 04 Jan 2017 21:47

Triplanar normal mapping pour terrain

Message par Sébastien Rousseau » 05 Jan 2017 08:36

Bonjour,

Je travail sur un triplanar diffuse texturing shader built-in pour les terrains en modifiant la fonction Splatmapmix du fichier TerrainSplatmapCommon.cginc donné par Unity.

Code : Tout sélectionner

void SplatmapMix(Input IN, out half4 splat_control, out half weight, out fixed4 mixedDiffuse, inout fixed3 mixedNormal)
#endif
{
	fixed4 cX, cY, cZ;
	fixed3 o_normal;
	float3 worldNormal;
	o_normal = mixedNormal;
	worldNormal = WorldNormalVector(IN, mixedNormal);
	splat_control = tex2D(_Control, IN.tc_Control);
	weight = dot(splat_control, half4(1,1,1,1));

	#if !defined(SHADER_API_MOBILE) && defined(TERRAIN_SPLAT_ADDPASS)
		clip(weight == 0.0f ? -1 : 1);
	#endif

	// Normalize weights before lighting and restore weights in final modifier functions so that the overal
	// lighting result can be correctly weighted.
	splat_control /= (weight + 1e-3f);

	// we drop the sign because we do not distinguish positive and negative directions
	float3 blend = abs(worldNormal);

	// the values should sum to 1 but we must avoid dividing by 0
	blend /= blend.x + blend.y + blend.z + 0.001f;

	cX = 0.0f;
	cX += splat_control.r * tex2D(_Splat0, 0.1f*IN.worldPos.yz);
	cX += splat_control.g * tex2D(_Splat1, 0.1f*IN.worldPos.yz);
	cX += splat_control.b * tex2D(_Splat2, 0.1f*IN.worldPos.yz);
	cX += splat_control.a * tex2D(_Splat3, 0.1f*IN.worldPos.yz);

	cY = 0.0f;
	cY += splat_control.r * tex2D(_Splat0, 0.1f*IN.worldPos.xz);
	cY += splat_control.g * tex2D(_Splat1, 0.1f*IN.worldPos.xz);
	cY += splat_control.b * tex2D(_Splat2, 0.1f*IN.worldPos.xz);
	cY += splat_control.a * tex2D(_Splat3, 0.1f*IN.worldPos.xz);

	cZ = 0.0f;
	cZ += splat_control.r * tex2D(_Splat0, 0.1f*IN.worldPos.xy);
	cZ += splat_control.g * tex2D(_Splat1, 0.1f*IN.worldPos.xy);
	cZ += splat_control.b * tex2D(_Splat2, 0.1f*IN.worldPos.xy);
	cZ += splat_control.a * tex2D(_Splat3, 0.1f*IN.worldPos.xy);

	// blending
	mixedDiffuse = blend.x * cX + blend.y * cY + blend.z * cZ;

#ifdef _TERRAIN_NORMAL_MAP
	fixed4 nrmX = 0.0f;
	fixed4 nrmY = 0.0f;
	fixed4 nrmZ = 0.0f;

	nrmX += splat_control.r * tex2D(_Normal0, 0.1f*IN.worldPos.yz);
	nrmX += splat_control.g * tex2D(_Normal1, 0.1f*IN.worldPos.yz));
	nrmX += splat_control.b * tex2D(_Normal2, 0.1f*IN.worldPos.yz));
	nrmX += splat_control.a * tex2D(_Normal3, 0.1f*IN.worldPos.yz));
	
	nrmY += splat_control.r * tex2D(_Normal0, 0.1f*IN.worldPos.xz);
	nrmY += splat_control.g * tex2D(_Normal1, 0.1f*IN.worldPos.xz));
	nrmY += splat_control.b * tex2D(_Normal2, 0.1f*IN.worldPos.xz));
	nrmY += splat_control.a * tex2D(_Normal3, 0.1f*IN.worldPos.xz));

	nrmZ += splat_control.r * tex2D(_Normal0, 0.1f*IN.worldPos.xy);
	nrmZ += splat_control.g * tex2D(_Normal1, 0.1f*IN.worldPos.xy));
	nrmZ += splat_control.b * tex2D(_Normal2, 0.1f*IN.worldPos.xy));
 	nrmZ += splat_control.a * tex2D(_Normal3, 0.1f*IN.worldPos.xy))

	// blending
	mixedNormal = blend.x * UnpackNormal(nrmX) + blend.y * UnpackNormal(nrmY) + blend.z * UnpackNormal(nrmZ);
#endif

Le triplanar texturing fonctionne très bien ! Mais je n'arrive pas à ajouter le triplanar normal mapping. J'ai essayé plein de solutions différentes mais le terrain devient noir si je l'ajoute. Est ce que quelqu'un peut m'aider à comprendre le problème ?

Merci !

Répondre

Revenir vers « les Shaders »