Page 1 sur 2

[Resolu] Récupérer l'UV2 dans un shader

Publié : 08 Oct 2015 15:35
par boubouk50
Salut,

Pour un besoin spécifique, je cherche à créer un shader assez simple, qui allie couleur diffuse, texture diffuse, lightmapping et réflexion cubemap.
J'y suis presque, il ne me manque 'que' le 2e set d'UV pour appliquer correctement ma lightmap.

Code : Tout sélectionner

// Unlit lightmapped reflective shader
// Diffuse texture (UV1)* Lightmap texture (UV2) + Reflection * ReflectionColor

Shader "MyCustomShaders/LightMap/Reflective" {
	Properties {
		_DiffuseColor ("Diffuse Color", Color) = (1, 1, 1, 1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_LightMapTex ("Lightmap (RGB)", 2D) = "white" {}
		_Cube ("Reflection Cubemap", CUBE) = "_Skybox" {}
		_ReflectColor ("Reflection Color", Color) = (0.25, 0.25, 0.25, 1)
	}

	SubShader {
		Lighting Off //Pas de gestion de lumière

		Tags { "RenderType"="Opaque" }
		LOD 100

		CGPROGRAM
		#pragma surface surf NoLighting
		
		fixed4 LightingNoLighting (SurfaceOutput s, fixed3 lightDir, fixed atten)
		{
			fixed4 c;
			c.rgb = s.Albedo;
			c.a = s.Alpha;
			return c;
		}
		
		sampler2D _MainTex;
		sampler2D _LightMapTex;
		samplerCUBE _Cube;
		fixed4 _ReflectColor;
		fixed4 _DiffuseColor;
		
		struct Input {
		    half2 uv_MainTex : TEXCOORD0;
		    half2 uv_LightMapTex : TEXCOORD1;  //C'est ça marrez vous!  ;)
		    half3 worldRefl;
		    half3 viewDir;
		};
		
		void surf (Input IN, inout SurfaceOutput o) {
		    fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
		    fixed4 lm = tex2D(_LightMapTex, IN.uv_LightMapTex);
		    fixed3 reflcol = texCUBE (_Cube, IN.worldRefl);
		    fixed rim = 1.1 - dot (IN.viewDir, o.Normal);           //faux fresnel
		    o.Albedo = _DiffuseColor.rgb * tex.rgb * lm.rgb;      //Diffuse * couleur Diffuse * lightmap
		    o.Emission = reflcol.rgb * rim * _ReflectColor.rgb;   //couleur réflexion * réflexion * faux fresnel
		}
		ENDCG
	}
}
Toute remarque d'autre nature est la bienvenue, ce code n'est le résultat que de bidouillage et de déduction.

Merci.

Re: Récupérer l'UV2 dans un shader

Publié : 08 Oct 2015 16:00
par boubouk50
La recherche continue...

Certains ont réussi à accéder au texcoord1 en ajoutant le vertex shader:

Code : Tout sélectionner

Input vert (appdata_full v) {
        	Input o;
        	o.uv_LightMapTex = v.texcoord1.xy;
        	return o;
        }
Mais toujours rien de mon côté.

Re: Récupérer l'UV2 dans un shader

Publié : 08 Oct 2015 20:49
par F@B
huuum en effet il doit falloir passer ton UV2 de ton vertex a ton fragment, et avec les surface il faut surement regarder du coté des macro du CGINC.

j'ai pas trop de temps en ce moment boubouk mais fait moi signe ou "up" ton post si tu trouves pas mieux que j'essaye de voir ce que je peux faire.
(en gros je te promets mon aide pour plus tard! lol)

Re: Récupérer l'UV2 dans un shader

Publié : 09 Oct 2015 09:28
par boubouk50
Perso, je sèche. Je vois pas où ça coince.
Unity me dit bien que mon mesh possède un uv2.
Partout où je cherche, la solution est de passer par le vertex shader si l'UV2 n'est pas automatique avec TEXCOORD1.
Je me demande donc quelle option d'import ou autre je dois (dés)activer pour y avoir accès. Actuellement, l'uv2 est le même que l'uv1.
Une autre façon de récupérer l'uv2: (qui fait la même chose que la première)
(J'ai aussi inclus #include "UnityCG.cginc" pour accéder à appdata_full même si ça ne gueulais pas)

Code : Tout sélectionner

Input vert (inout appdata_full v) {
	Input o;
	o.uv_MainTex = v.texcoord.xy;
	o.uv_LightMapTex = v.texcoord1.xy;
	return o;
}

Re: Récupérer l'UV2 dans un shader

Publié : 09 Oct 2015 09:52
par boubouk50
Breaking news:

Dans les options d'import, j'ai coché Swap UVs pour le fun: du coup je me retrouve maintenant avec l'uv2 à la place de l'uv1. l'uv2 est donc bien présent mais quoique je fasse j'ai toujours le même set d'uv pour uv1 et uv2.

Re: Récupérer l'UV2 dans un shader

Publié : 09 Oct 2015 15:00
par ZJP
Il ne manque pas un

Code : Tout sélectionner

UNITY_INITIALIZE_OUTPUT(Input,o);
dans ton code?

Edit :
Une solution (bizarre?!) : http://forum.unity3d.com/threads/proble ... st-1655466

Re: Récupérer l'UV2 dans un shader

Publié : 09 Oct 2015 15:11
par boubouk50
Nope, ça ne change rien.
UNITY_INITIALIZE_OUTPUT

Re: Récupérer l'UV2 dans un shader

Publié : 09 Oct 2015 15:13
par ZJP
Edit2:

Un souci avec le "prefix" utilisé pour accéder à ton uv2?! : http://answers.unity3d.com/questions/71 ... hader.html


Edit3 :
Autre solution pour un deuxième uv : http://stackoverflow.com/questions/1913 ... le-uv-sets

Re: Récupérer l'UV2 dans un shader

Publié : 09 Oct 2015 15:36
par boubouk50
POIUFGCZUOPCVZ0OQ%XOMUQJBMOJNBXOC%!

Merci ZJP! J'avais pas lu les commentaires: changed the folowing float2 uv_AoTex : TEXCOORD1; -> float2 uv2_AoTex : TEXCOORD1
Le nommage est donc aussi important que l'index du TEXCOORD...

Code : Tout sélectionner

// Unlit lightmapped reflective shader
// Diffuse texture (UV1)* Lightmap texture (UV2) + Reflection * ReflectionColor

Shader "MyCustomShaders/LightMap/Reflective" {
	Properties {
		_DiffuseColor ("Diffuse Color", Color) = (1, 1, 1, 1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_LightMapTex ("Lightmap (RGB)", 2D) = "white" {}
		_Cube ("Reflection Cubemap", CUBE) = "_Skybox" {}
		_ReflectColor ("Reflection Color", Color) = (0.25, 0.25, 0.25, 1)
		_Fresnel ("Fresnel Intensity", Float) = 1.0
	}

	SubShader {
		Lighting Off //Pas de gestion de lumière

		Tags { "RenderType"="Opaque" }
		LOD 100

		CGPROGRAM
		#pragma surface surf NoLighting
		#include "UnityCG.cginc"

		fixed4 LightingNoLighting (SurfaceOutput s, fixed3 lightDir, fixed atten)
		{
			fixed4 c;
			c.rgb = s.Albedo;
			c.a = s.Alpha;
			return c;
		}
	  
		sampler2D _MainTex;
		sampler2D _LightMapTex;
		samplerCUBE _Cube;
		fixed4 _ReflectColor;
		fixed4 _DiffuseColor;
		float _Fresnel;
	  
		struct Input {
			half2 uv_MainTex : TEXCOORD0;
			half2 uv2_LightMapTex : TEXCOORD1; //Faut mettre un petit 2 après uv même si on précise bien TEXCOORD1. Stupide.
			half3 worldRefl;
			half3 viewDir;
		};
	  
		void surf (Input IN, inout SurfaceOutput o) {
			fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
			fixed4 lm = tex2D(_LightMapTex, IN.uv2_LightMapTex);
			fixed3 reflcol = texCUBE (_Cube, IN.worldRefl);
			fixed rim = _Fresnel - dot (IN.viewDir, o.Normal);
			o.Albedo = _DiffuseColor.rgb * tex.rgb * lm.rgb;
			o.Emission = reflcol.rgb * rim * _ReflectColor.rgb;
		}

		ENDCG
	}
}

Re: Récupérer l'UV2 dans un shader

Publié : 09 Oct 2015 15:51
par ZJP
POIUFGCZUOPCVZ0OQ%XOMUQJBMOJNBXOC%!
:-D