Jump to content
3DCoat Forums

A "Simple?" change to one of 3dCoat's Shaders...


Daniko
 Share

Recommended Posts

  • Member

Hi, I will preface by saying i am not a programmer type, but wanted to make a change to one of the shaders i use with 3Dcoat.  I opened up the hlsl file in notepad++, and managed to understand enough to find the variable that needed changing to get what i want.

// Vertex shader

float4x4 g_WorldViewProjectionMatrix;
float4x4 g_WorldMatrix;
float3 g_ViewerPos;
float4 Sphere;
float OverallScale;
float4x4 ShadowTM;

struct VS_INPUT {
	float3 Pos       : POSITION;
	float3 Normal    : TEXCOORD0;
};

struct VS_OUTPUT {
	float4 Pos  : POSITION;	
	float3 N    : TEXCOORD1;
	float3 Ps   : TEXCOORD3;	
#ifdef SHADOWS
	float3 SPos : TEXCOORD2;
#endif
};

VS_OUTPUT main(const VS_INPUT In) {
	VS_OUTPUT Out;
	float4 P = float4(In.Pos, 1.0);
	Out.Pos = mul(P, g_WorldViewProjectionMatrix);
	Out.N = In.Normal;
	Out.Ps = In.Pos*0.005/OverallScale;
#ifdef SHADOWS
	Out.SPos = mul(P, ShadowTM);
	Out.SPos.xy += float2(1.0/4096.0f,1.0/4096.0);
#endif
	return Out;
}

// Pixel shader

struct VS_OUTPUT {
	float4 Pos  : POSITION;
	float3 N    : TEXCOORD1;
	float3 Ps   : TEXCOORD3;
#ifdef SHADOWS
	float3 SPos : TEXCOORD2;
#endif
};

sampler ShadowSampler;
sampler CustomSampler1;
sampler CustomSampler2;
sampler CustomSampler3;

float4 Color;
float4 CurrColor;
float4 ColorMod;
float4 SpecularColor;
float3 LDir;
float3 VDir;
float LDiffuse;
float LAmbient;
float Opacity;
float ShadowMin;
float Bumpness;
float Specularity;
float SpecularPower;
float4 LightColor;

float4 main( const VS_OUTPUT v ) : COLOR {
	float mpl=1.0;
	float4 mxy=tex2D(CustomSampler1,v.Ps.xy);
	float4 myz=tex2D(CustomSampler1,v.Ps.zy);
	float4 mzx=tex2D(CustomSampler1,v.Ps.zx);

	float4 nxy=tex2D(CustomSampler2,v.Ps.xy);
	float4 nyz=tex2D(CustomSampler2,v.Ps.zy);
	float4 nzx=tex2D(CustomSampler2,v.Ps.zx);

	float wxy=v.N.z*v.N.z;
	float wyz=v.N.x*v.N.x;
	float wzx=v.N.y*v.N.y;
	float3 dN=float3(nxy.x-0.5,0.5-nxy.y,0.0)*wxy*Bumpness;
	dN+=float3(0.0,nyz.x-0.5,0.5-nyz.y)*wyz*Bumpness;
	dN+=float3(0.5-nzx.y,0.0,nzx.x-0.5)*wzx*Bumpness;
	wxy*=wxy;
	wyz*=wyz;
	wzx*=wzx;
	mxy=(mxy*wxy+myz*wyz+mzx*wzx)/(wxy+wyz+wzx);
#ifdef SHADOWS
	float3 m=tex2D(ShadowSampler,v.SPos).xyz;	
	float3 d=float3(1.0,1.0/255.0,1.0/255.0/255.0);
	mpl=clamp(2.0-(v.SPos.z-dot(m,d))*120,ShadowMin,1);	
#endif
	float L=length(v.N);
	float3 N=normalize(v.N-dN);
	float3 refl = VDir-2.0*N*dot(VDir,N);
	float S = dot(refl,LDir);
	float dd=clamp(L-1.0,0.0,1.0);
	S=clamp(S,0.0,1.0);
	
#ifdef AOPASS
	float  D = LAmbient-LDiffuse*dot(N,LDir)*mpl;
#else
	float  D = -LDiffuse*dot(N,LDir)*mpl;	
#endif  
	float4 C=ColorMod*D*mxy*2+SpecularColor*pow(S,SpecularPower*mxy.w)*Specularity*mpl*mpl*mxy.w;
	float4 c1=tex2D(CustomSampler3,float2(dd,0));
	C=lerp(C,c1,dd*c1.w);
	C.w=Opacity;
	return C*LightColor;        
}

I highlighted the lines where i simply changed the .yz to .zy, and when viewing an example object in the vox room in 3d coat, i now have my bricks in this case, wrap around the wall or cube the way i was hoping.

post-38102-0-48428000-1405559350_thumb.j

 

however, when i bake this to a retopo mesh, the textures still project as in the .yz orientation.....

post-38102-0-84035300-1405559361_thumb.j

 

Can someone help me understand what changes i need to make so that the displayed voxel representation, and the low poly baked output are the same?

 

 

Link to comment
Share on other sites

  • Member

Hi, it seems the variables.xml is just exposing certain variables/slider options but i am changing a variable that isn't exposed through the interface.  The txtr_tree01 does project/wrap the texture the same way i am looking for, but doesn't have the bump-mapping.  If i could figure out how to add bump mapping to that shader, it would be fine too, but it seems easier for a total novice like me to just get the projection parameter changed in the shader with the bump-map.  I am trying to study some 3d math/hlsl shader documents to see if i can understand what is going on in the matrix-math area of the shader.... but ....ugh.....

Link to comment
Share on other sites

  • Member

What i mean is, the normal map bakes just fine in the above shader, and that is the result  want.  If i can just figure out how to get the .zy - .yz projection of the texture to bake as it is displayed in the viewport, i'm good to go.  The first image shows the viewport, the second image shows the result of baking, where the bricks go the wrong direction on the sides of the cube.

Link to comment
Share on other sites

  • Member

Right, so like i was saying, i want to get the brick-type wrap-around working with the normal-map shader.  What i've discovered is that the bake settings seem to be hard-wired/called from the method <id> of the variable.xml file.  So <ID>CubicTopSide</ID> method wraps the way i want, but doesn't bake the Normal mapping of the shader, and <ID>CubicNormalmap</ID> bakes the NormalMaps from the shader, but doesn't wrap around the object, regardless of the customized code in the .hlsl file itself.  Maybe i'm out of luck?  or maybe there is a place where i can modify the baking method? I guess this wasn't simple as i had hoped.   :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...