Shader HLSL bug & questions

Questions à propos du scripting Shader.
Avatar de l’utilisateur
F@B
Messages : 1844
Inscription : 01 Août 2013 10:41
Contact :

Re: Shader HLSL bug & questions

Message par F@B » 04 Avr 2014 16:10

bon c'est pas parfait mais ça progresse!! l'implémentation des ombres est pas évidente!

il faut égalempent faut DX11 pour modifier le Zdeph, sauf si vous connaissez un autre moyen? du coup j'ai des soucis de Zdeph avec les ombres...

j'ai réglé mes problèmes de référentiels et d’échelle. La il me faudrait un petit offset sur les blades pour éviter les répétition de la texture un peu trop présente si j'augmente la densité.

Web player à tester ici

Image
ʕ·͡ᴥ·ʔ ==> Mon Portfolio <== ʕ·͡ᴥ·ʔ

Merci de lire et de prendre en considération la Nétiquette des Forums avant de poster un sujet !

Avatar de l’utilisateur
F@B
Messages : 1844
Inscription : 01 Août 2013 10:41
Contact :

Re: Shader HLSL bug & questions

Message par F@B » 04 Avr 2014 16:16

ha! j'oubliais, voila le shader, c'est encore en WIP, donc tout aide ou commentaire est le bienvenue!

Code : Tout sélectionner

Shader "Custom/CustomVolumetricGrass" {  	 
	Properties {
		_Color ("Color", Color) = (1, 1, 1, 1) 
		//_ShadowColor ("_ShadowColor", Color) = (0, 0, 0, 1) 
		_grass ("Base (RGB) Trans (A)", 2D) = "white" {}   
		_ground ("ground", 2D) = "white" {}
		_ground_height ("ground height", Range (-0.01, 0.1)) =0   
		_windnoise ("windnoise", 2D) = "white" {}    
        _Wind ("Wind", Range (0.01, 0.1)) =0.02
        _Zdepth_offset ("Zdepth offset", Range (-5.0, 5)) =0
        _density("density", Range (6, 55)) =16
		//_xoffset("x offset", Range (1, 50)) =10
		//_yoffset("y offset", Range (1, 50)) =10
		_Cutoff ("Base Alpha cutoff", Range (0,.9)) = .1

	}
SubShader 
	{
        
		// Pass to render grass
		Pass {
			Tags { "Queue"="Transparent" "RenderType"="Transparent"  "LightMode" = "ForwardBase"}
			//Tags { "LightMode" = "ForwardBase" }
			//Cull off
			Lighting On 
			ZWrite On // don't occlude other objects
			ZTest Less        
			Blend SrcAlpha OneMinusSrcAlpha  
			LOD 800				  

			CGPROGRAM
			#pragma vertex vert  
			#pragma fragment frag
			#pragma target 5.0
			#pragma multi_compile_fwdbase
            #pragma multi_compile_fwdadd_fullshadows
            #include "UnityCG.cginc"
            #include "AutoLight.cginc"				
			
            uniform float4 _Color; 
			//uniform float4 _ShadowColor; 
			uniform sampler2D _grass; 
			float4 _grass_ST;
			uniform sampler2D _ground;
			float4 _ground_ST;
			uniform sampler2D _windnoise;
			uniform float _Wind;
			uniform float _Zdepth_offset;
			uniform float _density;
			uniform float _xoffset;
			uniform float _yoffset;
			//uniform float _Cutoff;
			uniform float _ground_height;
			//sampler2D _ShadowMapTexture;
            fixed4 _LightColor0; // Colour of the light used in this pass.
            
            struct VertData
			{
				float4 vertex		: POSITION;
				float3 norm			: NORMAL;
				float2 texCoord	 	: TEXCOORD0;
				float2 texCoord2	: TEXCOORD1;
				float3 tang  	 	: TANGENT;
				float4 vertColor 	: COLOR;           	

			};
       	
			struct FragData
			{
				float4 pos 				: SV_POSITION;
				float2 oTexCoord	  	: TEXCOORD0;
				float2 oTexCoord2	  	: TEXCOORD1;
				float3 oEyeDirTan      	: TEXCOORD2;
				float4 oPositionViewProj: TEXCOORD3;
				float4 oVertColor		: COLOR;
				LIGHTING_COORDS(4,5) 
			};
			
			struct Fragout  
			{  
				float4 color	: COLOR;
				float depth		: DEPTH; 
			};
			
			//-----------------------------------------------------------------------------  
			// Vertex                                                                          
			//-----------------------------------------------------------------------------  
			FragData vert( VertData v )
			{  
			   	FragData o;  
                
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex); //oPosition must be output to satisy pipeline.
				o.oPositionViewProj = o.pos;

				o.oTexCoord = v.texCoord;
				o.oTexCoord2 = float2((v.texCoord2.x+_Time.y*_Wind)/2,(v.texCoord2.y+_Time.y*_Wind)/2); // offset second texture coordinate
				// according to time for wind texture

				float3 eyeDirO = -(_WorldSpaceCameraPos.xyz - mul(_Object2World, v.vertex).xyz ) ; //eye vector in object space

				float3 binormal = cross(v.tang,v.norm);
				float3x3 TBNMatrix = float3x3(v.tang,binormal,v.norm); 

				o.oEyeDirTan = normalize(mul(TBNMatrix,eyeDirO)); // eye vector in tangent space
				o.oVertColor = v.vertColor;
			
				TRANSFER_VERTEX_TO_FRAGMENT(o); 

			   	return o;
			}
				
						 
			//Set everything which is constant in the fragment shader, the values used are for the moderate height data set.  
#define MAX_RAYDEPTH min(5,_density/3) //Number of iterations.
#define PLANE_NUM _density //Number of grass slice grid planes per unit in tangent space.
#define PLANE_NUM_INV (1.0 /PLANE_NUM)
#define PLANE_NUM_INV_DIV2 (PLANE_NUM_INV/2)
#define GRASS_SLICE_NUM 8 // Number of grass slices in texture grassblades.
#define GRASS_SLICE_NUM_INV (1.0/GRASS_SLICE_NUM)
#define GRASS_SLICE_NUM_INV_DIV2 (GRASS_SLICE_NUM_INV/2)
#define GRASSDEPTH GRASS_SLICE_NUM_INV +_ground_height //Depth set to inverse of number of grass slices so no stretching occurs.
#define TC1_TO_TC2_RATIO 8 //Ratio of texture coordinate set 1 to texture coordinate set 2, used for the animation lookup.
#define PREMULT (GRASS_SLICE_NUM_INV*PLANE_NUM) //Saves a multiply in the shader.
//#define AVERAGE_COLOR float4(0.32156,0.513725,0.0941176,1.0) //Used to fill remaining opacity, can be replaced by a texture lookup.
			//#define AVERAGE_COLOR float4(0.32156,0.513725,0.0941176,1.0) //Used to fill remaining opacity, can be replaced by a texture lookup.  
			//#define AVERAGE_COLOR float4(0.0,0.0,0.0,0.0) //Used to fill remaining opacity, can be replaced by a texture lookup.  
			//-----------------------------------------------------------------------------  
			// Fragment                                                                          
			//-----------------------------------------------------------------------------  
			Fragout frag(FragData i)
			{	
				Fragout o;  
				//Initialize increments/decrements and per fragment constants
				o.color = float4(0.0,0.0,0.0,0.0);
			    float4 atten = LIGHT_ATTENUATION(i);// NEEDED FOR SHADOWS.
				//float4 shadow = SHADOW_ATTENUATION(i);
			 	float2 plane_offset = float2(0.0,0.0);	
			 	float2 uv_offset=float2(0.1,0.1);
			 	
			 	//scale texcoord
			 	i.oTexCoord = TRANSFORM_TEX(i.oTexCoord, _grass);
			 	i.oTexCoord2 = TRANSFORM_TEX(i.oTexCoord2, _grass);
			 				 	
			 	float3 rayEntry = float3(i.oTexCoord.x,i.oTexCoord.y,0.0);
				float zOffset = 0.0;
				bool zFlag = 1;


			 	//The signs of eyeDirTan determines if we increment or decrement along the tangent space axis
				//plane_correct, planemod and pre_dir_correct are used to avoid unneccessary if-conditions. 
				
			 	float2 signe = float2(sign(i.oEyeDirTan.x),sign(i.oEyeDirTan.y));	
			 	float2 plane_correct = float2((signe.x+1)*GRASS_SLICE_NUM_INV_DIV2,(signe.y+1)*GRASS_SLICE_NUM_INV_DIV2);
			 	float2 planemod = float2(floor(rayEntry.x*PLANE_NUM)/PLANE_NUM,floor(rayEntry.y*PLANE_NUM)/PLANE_NUM);
				float2 pre_dir_correct = float2((signe.x+1)*PLANE_NUM_INV_DIV2,(signe.y+1)*PLANE_NUM_INV_DIV2);

				int hitcount;
			 	for(hitcount =0; hitcount < MAX_RAYDEPTH % (MAX_RAYDEPTH+1); hitcount++) // %([MAX_RAYDEPTH]+1) speeds up compilation.
													 // It may proof to be faster to early exit this loop
													 // depending on the hardware used.
			 	{

					//Calculate positions of the intersections with the next grid planes on the u,v tangent space axis independently.
			 		float2 dir_correct = float2(signe.x*plane_offset.x+pre_dir_correct.x , signe.y*plane_offset.y+pre_dir_correct.y);			
					float2 distance = float2((planemod.x + dir_correct.x - rayEntry.x)/(i.oEyeDirTan.x) , (planemod.y + dir_correct.y - rayEntry.y)/(i.oEyeDirTan.y));
			 					
			 		float3 rayHitpointX = rayEntry + i.oEyeDirTan *distance.x;   
			  		float3 rayHitpointY = rayEntry + i.oEyeDirTan *distance.y;
					
					//Check if we hit the ground. If so, calculate the intersection and look up the ground texture and blend colors.

			  		if ((rayHitpointX.z <= -GRASSDEPTH)&& (rayHitpointY.z <= -GRASSDEPTH)) 	
			  		{
			  			float distanceZ = (-GRASSDEPTH)/i.oEyeDirTan.z; // rayEntry.z is 0.0 anyway 

			  			float3 rayHitpointZ = rayEntry + i.oEyeDirTan *distanceZ;
						float2 orthoLookupZ = float2(rayHitpointZ.x+i.oTexCoord.x,rayHitpointZ.y+i.oTexCoord.y);
									
			  			o.color = (o.color)+((1.0-o.color.w) * tex2D(_ground,orthoLookupZ));
			  			if(zFlag ==1) 
			  				zOffset = distanceZ; // write the distance from rayEntry to intersection
		  				zFlag = 0; //Early exit here if faster.		
			  		}  
			  		else
			 		{
			 			
			 			float2 orthoLookup; //Will contain texture lookup coordinates for grassblades texture.

			 			//check if we hit a u or v plane, calculate lookup accordingly with wind shear displacement.
						if(distance.x <= distance.y)
			 			{
			 				float4 windX = (tex2D(_windnoise,i.oTexCoord2+rayHitpointX.xy/TC1_TO_TC2_RATIO)-0.5)/4;
							//float4 windX=float4(0,0,0,0);
							
							float lookupX = (rayHitpointX.z+(planemod.x+signe.x*plane_offset.x)*PREMULT)-plane_correct.x;
							orthoLookup=float2(rayHitpointX.y+windX.x*(GRASSDEPTH+rayHitpointX.z),lookupX); 
							
							plane_offset.x += PLANE_NUM_INV; // increment/decrement to next grid plane on u axis
							if(zFlag==1) 
								zOffset = distance.x;
						}
						else {
							float4 windY = (tex2D(_windnoise,i.oTexCoord2+rayHitpointY.xy/TC1_TO_TC2_RATIO)-0.5)/4;
			 				//float4 windY=float4(0,0,0,0);
			 				
							float lookupY = (rayHitpointY.z+(planemod.y+signe.y*plane_offset.y)*PREMULT)-plane_correct.y;
							orthoLookup = float2(rayHitpointY.x+windY.y*(GRASSDEPTH+rayHitpointY.z) ,lookupY);
			 			
							plane_offset.y += PLANE_NUM_INV;  // increment/decrement to next grid plane on v axis
							if(zFlag==1) 
								zOffset = distance.y;
								
			  			}
			  			
			  			uv_offset+=float2(0.01,0.01);
			 	 		o.color += (1-o.color.w)*tex2D(_grass,orthoLookup);

			 	 		if(o.color.w >= 0.49)
			 	 			{zFlag = 0;}	//Early exit here if faster.
			  		}
				}	

			    
			    o.color += (1.0-o.color.w)*_Color; 
			    o.color.xyz *=atten.xyz ;//shadow
			     
			     //zOffset is along eye direction, transform and add to vertex position to get correct z-value.
			     i.oPositionViewProj += mul(i.oPositionViewProj ,float4(i.oEyeDirTan.xyz,1.2)*(zOffset*(_Zdepth_offset)));
			     //Divide by homogenous part.
			     o.depth = (i.oPositionViewProj.z)/i.oPositionViewProj.w;
     			      			 
     			 //debug
     			 //o.color.xyz=atten.xyz;
				 //o.color.x=i.oPositionViewProj.y;
     			return o;
			}  

			ENDCG
		}//pass
		
		// Pass shadow collector
		Pass {
			Name "ShadowCollector"
			Tags { "LightMode" = "ShadowCollector" }
			
			Fog {Mode Off}
			ZWrite On ZTest Less
			
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest
			#pragma multi_compile_shadowcollector
			
			#define SHADOW_COLLECTOR_PASS
			#include "UnityCG.cginc"
			
			float _ground_height;
			
			struct appdata {
				float4 vertex : POSITION;
			};
			
			struct v2f {
				V2F_SHADOW_COLLECTOR;
			};
			
			v2f vert (appdata_full v)
			{
				v2f o;
				v.vertex.xyz -= v.normal * _ground_height*4 ;
				TRANSFER_SHADOW_COLLECTOR(o)
				return o;
			}
			
			half4 frag (v2f i) : COLOR
			{
				SHADOW_COLLECTOR_FRAGMENT(i)
			}
			ENDCG
		}
		
			
	}//shader

}
ʕ·͡ᴥ·ʔ ==> Mon Portfolio <== ʕ·͡ᴥ·ʔ

Merci de lire et de prendre en considération la Nétiquette des Forums avant de poster un sujet !

Avatar de l’utilisateur
Loic Joint
Messages : 895
Inscription : 12 Déc 2011 11:49
Localisation : France (picardie)
Contact :

Re: Shader HLSL bug & questions

Message par Loic Joint » 05 Avr 2014 12:07

Waou ! Magnifique ça dit donc, j'y connais pas grand chose en programmation de shader mais c'est sublime, ca serait magnifique dans un jeu. C'est gourmand question ressource ?

Chapeau ein, c'est b'in bio comme ont dit par chez nous :)
Loic Joint - Graphiste pour le jeu vidéo

Mon portfolio :
http://www.loicjoint.com

Avatar de l’utilisateur
F@B
Messages : 1844
Inscription : 01 Août 2013 10:41
Contact :

Re: Shader HLSL bug & questions

Message par F@B » 05 Avr 2014 12:44

merci bien Loic, mais j'y connais rien non plus!! j’apprend, le wikibook que je donne en lien sur la premiere page est niquel pour comprendre ce qui se passe, a partir de la tu peux réussir a adapter des trucs sur GPUgem ou nvidia et autre (source de mon shader page précédent aussi), ce que j'ai fait d'ailleurs ci dessus donc pas un grand mérite ici je fais juste une adaptation.

je compte bien utiliser ce shader dans mon projet sous peu, c'est beaucoup moins lourds en ressource que générer des mesh et des LOD!

je poste bientot une nouvelle version ou l'herbe sera moins répétitive sur de grande distances. j'aimerais améliorer le Cutout de l'ombre mais je sort un peu les rames!
ʕ·͡ᴥ·ʔ ==> Mon Portfolio <== ʕ·͡ᴥ·ʔ

Merci de lire et de prendre en considération la Nétiquette des Forums avant de poster un sujet !

Avatar de l’utilisateur
F@B
Messages : 1844
Inscription : 01 Août 2013 10:41
Contact :

Re: Shader HLSL bug & questions

Message par F@B » 05 Avr 2014 16:42

nouvelle version du shader, avec un random sur l'UV pour ne pas avoir de répétition lorsqu'on augmente la densité.

il DX11 pour se shader, si quelqu'un a une solution pour gérer en DX9 un Zdeph en output de fragment je suis preneur?

MAJ du Web player ici

n'hésitez pas à tester, commentaire et critiques bienvenues.

le shader :

Code : Tout sélectionner

Shader "Custom/tuto test grass10" {  	 
	Properties {
		_Color ("Color", Color) = (1, 1, 1, 1) 
		//_ShadowColor ("_ShadowColor", Color) = (0, 0, 0, 1) 
		_grass ("Base (RGB) Trans (A)", 2D) = "white" {}   
		_ground ("ground", 2D) = "white" {}
		_ground_height ("ground height", Range (-0.01, 0.1)) =0   
		_shadow_bias ("shadow bias", Range (-1, 1)) =0  
		_windnoise ("windnoise", 2D) = "white" {}    
        _Wind ("Wind", Range (0.0, 0.005)) =0.002
        _Zdepth_offset ("Zdepth offset", Range (-5.0, 5)) =0
        _density("density", Range (6, 112)) =16
		_bending_angles ("Bending angles", Range(0,1)) = 0.85
		//_Cutoff ("Base Alpha cutoff", Range (0,.9)) = .1

	}
SubShader 
	{
        
		// Pass to render grass
		Pass {
			Tags { "Queue"="Transparent" "RenderType"="Transparent"  "LightMode" = "ForwardBase"}
			//Cull off
			Lighting On 
			ZWrite On // don't occlude other objects
			ZTest Less        
			Blend SrcAlpha OneMinusSrcAlpha 
			LOD 800				  

			CGPROGRAM
			#pragma vertex vert  
			#pragma fragment frag
			#pragma target 4.0
			#pragma multi_compile_fwdbase
            #pragma multi_compile_fwdadd_fullshadows
            #include "UnityCG.cginc"
            #include "AutoLight.cginc"				
			
            uniform float4 _Color; 
			//uniform float4 _ShadowColor; 
			uniform sampler2D _grass; 
			float4 _grass_ST;
			uniform sampler2D _ground;
			float4 _ground_ST;
			uniform sampler2D _windnoise;
			float4 _windnoise_ST;
			uniform float _Wind;
			uniform float _Zdepth_offset;
			uniform float _density;
			//uniform float _Cutoff;
			uniform float _ground_height;
            float _bending_angles;
			//sampler2D _ShadowMapTexture;
            fixed4 _LightColor0; // Colour of the light used in this pass.

            struct VertData
			{
				float4 vertex		: POSITION;
				float3 norm			: NORMAL;
				float2 texCoord	 	: TEXCOORD0;
				float2 texCoord2	: TEXCOORD1;
				float3 tang  	 	: TANGENT;
				float4 vertColor 	: COLOR;           	

			};
       	
			struct FragData
			{
				float4 pos 				: SV_POSITION;
				float2 oTexCoord	  	: TEXCOORD0;
				float2 oTexCoord2	  	: TEXCOORD1;
				float3 oEyeDirTan      	: TEXCOORD2;
				float4 oPositionViewProj: TEXCOORD3;
				float4 oVertColor		: COLOR;
				LIGHTING_COORDS(4,5) 
			};
			
			struct Fragout  
			{  
				float4 color	: COLOR;
				float depth		: DEPTH; 
			};
			
			//-----------------------------------------------------------------------------  
			// Vertex                                                                          
			//-----------------------------------------------------------------------------  
			FragData vert( VertData v )
			{  
			   	FragData o;  
                
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex); //oPosition must be output to satisy pipeline.
				o.oPositionViewProj = o.pos;

				o.oTexCoord = v.texCoord;
				o.oTexCoord2 = float2((v.texCoord2.x+_Time.y*_Wind)/2,(v.texCoord2.y+_Time.y*_Wind)/2); // offset second texture coordinate
				// according to time for wind texture

				float3 eyeDirO = -(_WorldSpaceCameraPos.xyz - mul(_Object2World, v.vertex).xyz ) ; //eye vector in object space

				float3 binormal = cross(v.tang,v.norm);
				float3x3 TBNMatrix = float3x3(v.tang,binormal,v.norm); 

				o.oEyeDirTan = normalize(mul(TBNMatrix,eyeDirO)); // eye vector in tangent space
				o.oVertColor = v.vertColor;
			
				TRANSFER_VERTEX_TO_FRAGMENT(o); 

			   	return o;
			}
				
						 
//Set everything which is constant in the fragment shader, the values used are for the moderate height data set.  
#define MAX_RAYDEPTH min(5,_density/2) //Number of iterations.
#define PLANE_NUM _density //Number of grass slice grid planes per unit in tangent space.
#define PLANE_NUM_INV (1.0 /PLANE_NUM)
#define PLANE_NUM_INV_DIV2 (PLANE_NUM_INV/2)
#define GRASS_SLICE_NUM 8 // Number of grass slices in texture grassblades.
#define GRASS_SLICE_NUM_INV (1.0/GRASS_SLICE_NUM)
#define GRASS_SLICE_NUM_INV_DIV2 (GRASS_SLICE_NUM_INV/2)
#define GRASSDEPTH GRASS_SLICE_NUM_INV +_ground_height //Depth set to inverse of number of grass slices so no stretching occurs.
#define TC1_TO_TC2_RATIO 2 //Ratio of texture coordinate set 1 to texture coordinate set 2, used for the animation lookup.
#define PREMULT (GRASS_SLICE_NUM_INV*PLANE_NUM) //Saves a multiply in the shader.
//#define AVERAGE_COLOR float4(0.32156,0.513725,0.0941176,1.0) //Used to fill remaining opacity, can be replaced by a texture lookup.
			//#define AVERAGE_COLOR float4(0.32156,0.513725,0.0941176,1.0) //Used to fill remaining opacity, can be replaced by a texture lookup.  
			//#define AVERAGE_COLOR float4(0.0,0.0,0.0,0.0) //Used to fill remaining opacity, can be replaced by a texture lookup.  
			//-----------------------------------------------------------------------------  
			// Fragment                                                                          
			//-----------------------------------------------------------------------------  
			Fragout frag(FragData i)
			{	
				Fragout o;  
				//Initialize increments/decrements and per fragment constants
				o.color = float4(0.0,0.0,0.0,0.0);
			    float4 atten = LIGHT_ATTENUATION(i);// NEEDED FOR SHADOWS.
				//float4 shadow = SHADOW_ATTENUATION(i);
			 	float2 plane_offset = float2(0.0,0.0);	
			 	
			 	//scale texcoord
			 	i.oTexCoord = TRANSFORM_TEX(i.oTexCoord, _grass);
			 	i.oTexCoord2 = TRANSFORM_TEX(i.oTexCoord2, _grass);
			 				 	
			 	float3 rayEntry = float3(i.oTexCoord.x,i.oTexCoord.y,0.0);
				float zOffset = 0.0;
				bool zFlag = 1;


			 	//The signs of eyeDirTan determines if we increment or decrement along the tangent space axis
				//plane_correct, planemod and pre_dir_correct are used to avoid unneccessary if-conditions. 
			 	float2 signe = float2(sign(i.oEyeDirTan.x),sign(i.oEyeDirTan.y));	
			 	float2 plane_correct = float2((signe.x+1)*GRASS_SLICE_NUM_INV_DIV2,(signe.y+1)*GRASS_SLICE_NUM_INV_DIV2);
			 	float2 planemod = float2(floor(rayEntry.x*PLANE_NUM)/PLANE_NUM,floor(rayEntry.y*PLANE_NUM)/PLANE_NUM);
				float2 pre_dir_correct = float2((signe.x+1)*PLANE_NUM_INV_DIV2,(signe.y+1)*PLANE_NUM_INV_DIV2);
 	 			
 	 			float bend=i.oEyeDirTan.z;
 	 			bend*=bend;
				i.oEyeDirTan.z*=(1-bend)*_bending_angles+(1-_bending_angles);

				int hitcount;
			 	for(hitcount =0; hitcount < MAX_RAYDEPTH % (MAX_RAYDEPTH+1); hitcount++) // %([MAX_RAYDEPTH]+1) speeds up compilation.
				// It may proof to be faster to early exit this loop
				// depending on the hardware used.
			 	{
					//Calculate positions of the intersections with the next grid planes on the u,v tangent space axis independently.
			 		float2 dir_correct = float2(signe.x*plane_offset.x+pre_dir_correct.x , signe.y*plane_offset.y+pre_dir_correct.y);			
					float2 distance = float2((planemod.x + dir_correct.x - rayEntry.x)/(i.oEyeDirTan.x) , (planemod.y + dir_correct.y - rayEntry.y)/(i.oEyeDirTan.y));
			 					
			 		float3 rayHitpointX = rayEntry + i.oEyeDirTan *distance.x;   
			  		float3 rayHitpointY = rayEntry + i.oEyeDirTan *distance.y;
					
					//Check if we hit the ground. If so, calculate the intersection and look up the ground texture and blend colors.

			  		if ((rayHitpointX.z <= -GRASSDEPTH)&& (rayHitpointY.z <= -GRASSDEPTH)) 	
			  		{
			  			float distanceZ = (-GRASSDEPTH)/i.oEyeDirTan.z; // rayEntry.z is 0.0 anyway 

			  			float3 rayHitpointZ = rayEntry + i.oEyeDirTan *distanceZ;
						float2 orthoLookupZ = float2(rayHitpointZ.x+i.oTexCoord.x,rayHitpointZ.y+i.oTexCoord.y);
									
			  			o.color = (o.color)+((1.0-o.color.w) * tex2D(_ground,orthoLookupZ));
			  			if(zFlag ==1) 
			  				zOffset = distanceZ; // write the distance from rayEntry to intersection
		  				zFlag = 0; //Early exit here if fuf_offsetaster.		
			  		}  
			  		else
			 		{
			 			float2 orthoLookup; //Will contain texture lookup coordinates for grassblades texture.
		 				float htmpX=tex2Dlod(_windnoise, float4(rayHitpointX.x+0.013,0,0,0)).x;
						float htmpY=tex2Dlod(_windnoise, float4(rayHitpointY.y+0.013,0,0,0)).y;
						
			 			//check if we hit a u or v plane, calculate lookup accordingly with wind shear displacement.
						if(distance.x <= distance.y)
			 			{
			 				float4 windX = (tex2D(_windnoise,i.oTexCoord2+rayHitpointX.xy/TC1_TO_TC2_RATIO)-0.5)/2;
							//float4 windX = float4(0,0,0,0);
							float lookupX = (rayHitpointX.z+(planemod.x+signe.x*plane_offset.x)*PREMULT)-plane_correct.x;
							orthoLookup=float2(rayHitpointX.y+windX.x*(GRASSDEPTH+rayHitpointX.z)+htmpX,lookupX); 
							
							plane_offset.x += PLANE_NUM_INV; // increment/decrement to next grid plane on u axis
							if(zFlag==1) 
								zOffset = distance.x;
						}
						else {
							float4 windY = (tex2D(_windnoise,i.oTexCoord2+rayHitpointY.xy/TC1_TO_TC2_RATIO)-0.5)/2;
			 				//float4 windY = float4(0,0,0,0);
							float lookupY = (rayHitpointY.z+(planemod.y+signe.y*plane_offset.y)*PREMULT)-plane_correct.y;
							orthoLookup = float2(rayHitpointY.x+windY.y*(GRASSDEPTH+rayHitpointY.z)+htmpY,lookupY);
			 							 						 			
							plane_offset.y += PLANE_NUM_INV;  // increment/decrement to next grid plane on v axis
							if(zFlag==1) 
								zOffset = distance.y;
								
			  			}
			  			
			 	 		o.color += (1-o.color.w)*tex2D(_grass,orthoLookup);

			 	 		if(o.color.w >= 0.49)
			 	 			{zFlag = 0;}	//Early exit here if faster.
			  		}
			  		
			  		//plane_offset+=PLANE_NUM_INV;
				}	

			    
			    o.color += (1.0-o.color.w)*_Color; 
			    o.color.xyz *=atten.xyz ;//shadow
			     
			     //zOffset is along eye direction, transform and add to vertex position to get correct z-value.
			     i.oPositionViewProj += mul(i.oPositionViewProj ,float4(i.oEyeDirTan.xyz,1.2)*(zOffset*(_Zdepth_offset)));
			     //Divide by homogenous part.
			     o.depth = (i.oPositionViewProj.z)/i.oPositionViewProj.w;
     			      			 
     			 //debug
     			 //o.color.xyz=atten.xyz;
				 //o.color.x=i.oPositionViewProj.y;
     			return o;
			}  


			ENDCG
		}//pass
		
		// Pass to render object as a shadow collector

    // note: editor needs this pass as it has a collector pass.

		Pass {
			Name "ShadowCollector"
			Tags { "LightMode" = "ShadowCollector" }
			
			Fog {Mode Off}
			ZWrite On ZTest Less

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest
			#pragma multi_compile_shadowcollector
			
			#define SHADOW_COLLECTOR_PASS
			#include "UnityCG.cginc"
			
			float _shadow_bias;
			
			struct appdata {
				float4 vertex : POSITION;
			};
			
			struct v2f {
				V2F_SHADOW_COLLECTOR;
			};
			
			v2f vert (appdata_full v)
			{
				v2f o;
				v.vertex.xyz -= v.normal * _shadow_bias ;
				TRANSFER_SHADOW_COLLECTOR(o)
				return o;
			}
			
			half4 frag (v2f i) : COLOR
			{
				SHADOW_COLLECTOR_FRAGMENT(i)
			}
			ENDCG
		}
		
			
	}//shader
	FallBack "Off"
}
ʕ·͡ᴥ·ʔ ==> Mon Portfolio <== ʕ·͡ᴥ·ʔ

Merci de lire et de prendre en considération la Nétiquette des Forums avant de poster un sujet !

Avatar de l’utilisateur
Loic Joint
Messages : 895
Inscription : 12 Déc 2011 11:49
Localisation : France (picardie)
Contact :

Re: Shader HLSL bug & questions

Message par Loic Joint » 06 Avr 2014 10:31

merci bien Loic, mais j'y connais rien non plus!! j’apprend, le wikibook que je donne en lien sur la premiere page est niquel pour comprendre ce qui se passe, a partir de la tu peux réussir a adapter des trucs sur GPUgem ou nvidia et autre (source de mon shader page précédent aussi), ce que j'ai fait d'ailleurs ci dessus donc pas un grand mérite ici je fais juste une adaptation.
He bé, chapeau ! Ca dois ouvrir de belles perspectives en effet :)
Moi je suis allergique au code donc je commence à me mettre aux shaders tout doucement avec Strumpy et Shader Forge, mais je pense pas que ce genre de choses soit réalisable avec ces outils ^^

Hate de voir la suite l'artiste :)
Loic Joint - Graphiste pour le jeu vidéo

Mon portfolio :
http://www.loicjoint.com

Avatar de l’utilisateur
F@B
Messages : 1844
Inscription : 01 Août 2013 10:41
Contact :

Re: Shader HLSL bug & questions

Message par F@B » 06 Avr 2014 22:11

merci Loic!!

un petit délire après quelques améliorations sur la transparence et les tangentes... serais-ce la voie pour faire de la fourrure...? :) par contre j'ai perdu mes ombres grrrr...... :'(

le web player ici

Image
ʕ·͡ᴥ·ʔ ==> Mon Portfolio <== ʕ·͡ᴥ·ʔ

Merci de lire et de prendre en considération la Nétiquette des Forums avant de poster un sujet !

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

Re: Shader HLSL bug & questions

Message par axel » 07 Avr 2014 11:14

Il est excellent ton shader. Il va falloir que je teste ça.
Merci pour le partage en tout cas.

Avatar de l’utilisateur
F@B
Messages : 1844
Inscription : 01 Août 2013 10:41
Contact :

Re: Shader HLSL bug & questions

Message par F@B » 07 Avr 2014 11:21

merci Alex! je partage une autre version bientot!

c'est l'avantage de s’être péter le genou et la cheville je bouffe de l'unity toute la journée! hihihi
ʕ·͡ᴥ·ʔ ==> Mon Portfolio <== ʕ·͡ᴥ·ʔ

Merci de lire et de prendre en considération la Nétiquette des Forums avant de poster un sujet !

Avatar de l’utilisateur
Max
Messages : 8771
Inscription : 30 Juil 2011 13:57
Contact :

Re: Shader HLSL bug & questions

Message par Max » 07 Avr 2014 11:51

Sympa en effet. Je vois que tu progresses à un bon rythme en tous les cas ;)
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

Répondre

Revenir vers « les Shaders »