Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
6 registered members (AndrewAMD, Ayumi, degenerate_762, 7th_zorro, VoroneTZ, HoopyDerFrood), 1,268 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
LucasJoshua, Baklazhan, Hanky27, firatv, wandaluciaia
19053 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Blending detail mapping #361860
03/05/11 15:52
03/05/11 15:52
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

Joined: Nov 2008
Posts: 946
Well, I'm new to shader programming, so I guess this could be a rather dumb question, but hoewever.

I got a simple diffuse mapping shader (followed the shader tutorials) and I added detail mapping to it. So far, so good, it looks as it should and everything, but now I want to be able to kinda blend the detail map in and out using a numerical expression. Like, the expression is 1, detail map is fully featured, expression is 0, no detail mapping at all, expression is 0.5, detail map is blended over the color map by 50 percent. I dont want to blend between the color- and the detail map (that could be easily done using lerp), but rather determine how strong the detail map influences the final result.

The pixel shader which does the diffuse- and detail mapping looks like this:

Code:
// Pixel shader

float4 DiffusePS(in float2 InTex : TEXCOORD0,
                 in float3 InNormal: TEXCOORD1,
		in float4 inPos : TEXCOORD2) : COLOR
{

	// calculate ambient lighting:
	float4 Ambient = ambientIntensity*vecAmbient;
	// calculate diffuse lighting:
	float4 Diffuse = diffuseIntensity*SunColor*saturate(dot(-vecSunDir,normalize(InNormal)));
	// get pixel color from color map, add from normal map
	float4 colorMapping = tex2D(ColorMapSampler,InTex);
	float4 detailMapping = tex2D(DetailMapSampler,InTex*2);
	// compose
	float4 Color = colorMapping*detailMapping;
	//Color.rgb *= (detailMapping.r+detailMapping.g+detailMapping.b)*0.66;
	Color.a *= fAlpha;  // consider alpha value
//	Color.a = saturate(Color.a);
	// calculate final color
	return (Ambient+Diffuse)*Color;
}



Anybody knows how to achieve what I described (hope I described it well grin )?

EDIT: Meh, while I'm at it, anybody knows how to calculate the average color of a texture? Using the algorithm I got, I only get a monochrome result, which isn't really what I intended:

Code:
float4 calculateAveragePS_00(in float2 InTex : TEXCOORD0) : COLOR
{
	
	float4 Color = (0,0,0,1);
	
	Color.r += tex2D(ColorMapSampler,InTex).r;
	Color.g += tex2D(ColorMapSampler,InTex).g;
	Color.b += tex2D(ColorMapSampler,InTex).b;
	
	Color.r /= InTex;
	Color.g /= InTex;
	Color.b /= InTex;
	
	return Color;
	
}



Last edited by the_clown; 03/05/11 16:37.
Re: Blending detail mapping [Re: the_clown] #361890
03/05/11 18:08
03/05/11 18:08
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
The following code should answer your first question (I did not test it, though):

float detailBlend = 0.5;
float4 Color = colorMapping*( 1-detailBlend + detailBlend*detailMapping);


What do you mean by average color? When you've got a texture that's mostly red, the shader should color the model completely in red? I don't think that's possible in a shader, you'd have to calculate it with pixel-instructions, I think.
Btw. calculateAverage wastes way too many resources, the following line should produce the same (monochrome) result:

float Color = tex2D(ColorMapSampler,InTex).rgb;


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Blending detail mapping [Re: Superku] #361900
03/05/11 18:41
03/05/11 18:41
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

Joined: Nov 2008
Posts: 946
Thanks for the answer, I'll test the blending approach, guess it will work, looks logical.

To my second question, I solved it, used this function to calculate the color:

Code:
// function to get average color of the bitmap

float4 getAverageColor()
{
	float4 Color = (0,0,0,1);
	for(int i = 0; i<= 1; i++)
	{
		Color.rgb += tex2D(DetailMapSampler,float2(i*fSampleRate,i*fSampleRate)).rgb;
		Color.rgb /= 2; 	
	}
	
	return Color;
}




I'm not sure though if it's too expensive.

What I'm using it for is a pretty interesting approach at detail mapping, inspired by this blog post:

http://blog.wolfire.com/2009/12/Detail-texture-color-matching


Moderated by  Blink, Hummel, Superku 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1