Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (AndrewAMD, Nymphodora, Quad, TipmyPip, Imhotep), 852 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 4 1 2 3 4
Specular Shader #477136
05/22/19 16:17
05/22/19 16:17
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
The specular shader I am using uses the alpha channel of the main diffuse texture for the specular map. I believe this is the default "mtl_specBump" that comes with game studio. I am using A7.

I'm wondering if there is another specular/bump material shader that might use, for instance, the alpha channel of the normal map, or maybe a separate skin for the specular map. That way, in theory, a transparent texture, like for grass or leaves, could also have a specular component.

Is that just the way the shaders were written, or is that some kind of limitation of the engine itself?

Re: Specular Shader [Re: Dooley] #477138
05/23/19 15:22
05/23/19 15:22
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
That is just the way the shaders were written.

Re: Specular Shader [Re: txesmi] #477139
05/23/19 17:36
05/23/19 17:36
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
Interesting! So in theory, a new shader could be written to allow specular mapping and transparency.

Re: Specular Shader [Re: Dooley] #477140
05/23/19 17:41
05/23/19 17:41
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Yes. Post your shader code and I will try to explain how to modify it.

Re: Specular Shader [Re: txesmi] #477141
05/23/19 20:01
05/23/19 20:01
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
It is the standard specBump.fx that comes with A7

Code:
// Blinn / Phong bump mapping
// (c) oP group 2009  Version 2.2

#include <bump_vs>
#include <phong>

bool REQUIRE_NORMAL;

texture entSkin1;	// texture
texture entSkin2;	// normal map or lightmap
texture entSkin3;	// normal map on blocks

sampler sBaseTex = sampler_state { Texture = <entSkin1>; MipFilter = Linear;	};
sampler sSkin2 = sampler_state { Texture = <entSkin2>; MipFilter = None;	};
sampler sSkin3 = sampler_state { Texture = <entSkin3>; MipFilter = None;	};

float3 DoSpecular(bumpOut In,float3 Normal,float fSpecular)
{
#ifdef BLINN
	float3 viewDir = normalize(In.PosView);	
			
	float fLight = DoShine(In.Light1.xyz,Normal);
	float fHalfway = DoShine(In.Light1.xyz+viewDir,Normal);
	float3 Diffuse = DoPhong(In.Diffuse1,fLight,fHalfway,fSpecular);

	fLight = DoShine(In.Light2.xyz,Normal);
	fHalfway = DoShine(In.Light2.xyz+viewDir,Normal);
	Diffuse += DoPhong(In.Diffuse2,fLight,fHalfway,fSpecular);

	fLight = DoShine(In.Light3.xyz,Normal);
	fHalfway = DoShine(In.Light3.xyz+viewDir,Normal);
	Diffuse += DoPhong(In.Diffuse3,fLight,fHalfway,fSpecular);
#else // PHONG			
	float fLight = DoShine(In.Light1.xyz,Normal);
	float3 Diffuse = DoPhong(In.Diffuse1,fLight,fLight,fSpecular);		

	fLight = DoShine(In.Light2.xyz,Normal);
	Diffuse += DoPhong(In.Diffuse2,fLight,fLight,fSpecular);		

	fLight = DoShine(In.Light3.xyz,Normal);
	Diffuse += DoPhong(In.Diffuse3,fLight,fLight,fSpecular);
#endif

	return Diffuse;
}

float4 specBump_PS(bumpOut In): COLOR
{
	float4 Base = tex2D(sBaseTex,In.Tex12.xy);
	float3 Normalmap = tex2D(sSkin2,In.Tex12.xy)*2-1;
   float3 Diffuse = DoSpecular(In,Normalmap,Base.w);	
	return Base * DoColor(Diffuse,In.Ambient);
}

float4 specBumpLM_PS(bumpOut In): COLOR
{
	float4 Base = tex2D(sBaseTex,In.Tex12.xy);
	float4 Lightmap = tex2D(sSkin2,In.Tex12.zw);
	float3 Normalmap = tex2D(sSkin3,In.Tex12.xy)*2-1;
   float3 Diffuse = DoSpecular(In,Normalmap,Base.w);	
	return Base * DoLightmap(Diffuse,Lightmap,In.Ambient);
}


technique spec
{
	pass one
	{		
      ZWriteEnable = True;
      AlphaBlendEnable = False;

		VertexShader = compile vs_2_0 bump_VS();
		PixelShader = compile ps_2_0 specBump_PS();
	}
}

technique spec_lm
{
	pass one
	{		
      ZWriteEnable = True;
      AlphaBlendEnable = False;

		VertexShader = compile vs_2_0 bump_VS();
		PixelShader = compile ps_2_0 specBumpLM_PS();
	}
}

technique fallback { pass one { } }


Re: Specular Shader [Re: Dooley] #477148
05/24/19 12:58
05/24/19 12:58
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Ok. I would start by setting up a new technique to draw alpha channeled entities. Copy the 'spec' techinque, rename it, change 'AlphaBlendEnable' state to 'True' and set a new function for the pixel shader.

Code:
technique spec_trans // a new name for the new shader 
{
	pass one
	{		
		ZWriteEnable = True;
		AlphaBlendEnable = True; // Enable alpha blending

		VertexShader = compile vs_2_0 bump_VS();
		PixelShader = compile ps_2_0 specBumpTrans_PS(); // A new pixel shader function
	}
}



I copied the 'spec' technique, renamed it, changed 'AlphaBlendEnable' state to 'True' and set a new function for pixel shading.

'AlphaBlendEnable' tells to the shader compiler how to threat the returned color of the pixel shader, it merges the computed color with the target pixel color when is set to true. This little change makes the shader far slower because it has to retrieve the render targets pixel color and merge it with the return of the pixel shader, so be sure of assigning this new technique to actual alpha channeled entities only. Rendering solid entities with activated alpha blending is a big waste of resources that can ruin the whole rendering. This new technique has to be especified inside a new MATERIAL* struct.

Code:
MATERIAL *mtlSpecBumpTrans = {
   effect = "specBump.fx"; // the shader file
   technique = "spec_trans"; // the new technique
   flags = TRANSLUCENT; // not neccesary but you can ensure the entity is rendered by the translucent pass
}



The only thing left is to write the new pixel shader specified inside the new technique.

Code:
float4 specBumpTrans_PS(bumpOut In): COLOR
{
	float4 Base = tex2D(sBaseTex,In.Tex12.xy);
	float4 Normalmap = tex2D(sSkin2,In.Tex12.xy);
	Normalmap.xyz = Normalmap.xyz*2-1;
	float3 Diffuse = DoSpecular(In,Normalmap.xyz,Normalmap.w);	
	Base.xyz *= DoColor(Diffuse,In.Ambient);
	return Base;
}



I copied the 'specBump_PS' function and renamed it. I converted the 'NormalMap' vector to a 4 members vector (float4) so it also holds the alpha channels value. Since the specular value saved into the normal maps alpha channel is not compressed, I needed to uncompress the other three channels in a separate line of code while the alpha channel remains unchanged.

Code:
Normalmap.xyz = Normalmap.xyz*2-1;



The next code line computes the specular term. 'DoSpecular' function spects a three member vector as second parameter, so it needs the channels of 'NormalMap' (previously transformed to a 4 members vector) be specified. The third parameter of 'DoSpecular' is the specular factor which I modified to be the alpha channel of the 'NormalMap' vector.

Code:
float3 Diffuse = DoSpecular(In,Normalmap.xyz,Normalmap.w);



If you want to use a different bitmap to hold the specular term, you will only need to set up a new 'texture' and its 'sampler_state', retrieve its value in a new code line, same as the other two textures, and use it inside 'DoSpecular' parameters.

And that's all.
Salud!

Re: Specular Shader [Re: txesmi] #477152
05/24/19 14:12
05/24/19 14:12
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
Oh wow! I will try to implement this in the next couple days. Thank you so much laugh

I will report back here with results.

Re: Specular Shader [Re: Dooley] #477156
05/25/19 09:34
05/25/19 09:34
Joined: Jan 2006
Posts: 168
Germany, Hannover
DexLoomer Offline
Member
DexLoomer  Offline
Member

Joined: Jan 2006
Posts: 168
Germany, Hannover
Hey guys,
I've been sitting on a project for some time trying to use shaders. I urgently need objects with ColorMap, NormalMap and SpecularMap like 'Dooley'.

Since the standard shaders of mtlFX.c of 3DGS do not fulfill this to my knowledge, I have experimented with shade-c 0.91 and Shadec EVO.
With Shade-c I have big problems with shadows. With Shadec EVO, I get error messages such as "ìnvalid name: sc_lights_shadowmapBlur.fx" while loading the scene.

Therefore, I would also find it great if there is a solution to modify the standard shader of the mtlFX.c of 3DGS so that they represent everything necessary.

So I tried to follow the instructions of 'txesmi':
1. I inserted the part of the code with "technique spec_trans" and with "float4 specBumpTrans_PS" into the code of the file "specBump.fx".
2. I inserted the code with "MATERIAL * mtlSpecBumpTrans" in the "mtlFX.c" file.

When I start the scene but I get error messages:
"Malfunction W1550 Cant't compile effect: specBump.fx(20,30): errorX3018: invalid subscript'postView' .... "


Have I misunderstood some of the instructions from 'txesmi' or is there any other trick I overlooked?
Can someone tell me how I can make this whole to work?


A8-Com-v 8.47.1
Re: Specular Shader [Re: DexLoomer] #477157
05/25/19 12:57
05/25/19 12:57
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
@DexLoomer
I has been a decade from last time I used the engines default shaders and I needed to test it. I had no error report when set the new technique. Anyway, there is nothing called 'postView' into 'specBump.fx'. I guess it has to come from anywhere else.

Moreover, it seems 'DoSpecular' does not compute the specular reflection. It looks flat for me. I don't know what happens.

Also, there is another state I missed to include into the pass states inside the technique description:

Code:
AlphaTestEnable = True;



This state makes the shader avoid witting into depth stencil map on transparent pixels.

Here goes a full diffuse+specular shader out of the 3dgs default shaders

Code:
float3x3 matTangent;

float4x4 matWorldViewProj;
float4x4 matWorld;
float4 vecViewDir;
float4 vecLight;

int iLights;
float4 vecLightPos[8];
float4 vecLightColor[8];

texture entSkin1;
sampler TexSampler = sampler_state { Texture = <entSkin1>; Mipfilter = Linear; Minfilter = Linear; Magfilter = Linear; };
texture entSkin2;
sampler TNSampler = sampler_state { Texture = <entSkin2>; Mipfilter = Linear; Minfilter = Linear; Magfilter = Linear; };

void VS (
	in float4 inPos: POSITION,
	in float3 inNormal: NORMAL,
	in float2 inTex: TEXCOORD0,
	in float3 inTangent: TEXCOORD2,
	out float4 outPos: POSITION,
	out float3 outNormal: TEXCOORD0,
	out float3 outWorld: TEXCOORD1,
	out float2 outTex: TEXCOORD2,
	out float3 outTangent: TEXCOORD3) {
		outPos = mul(inPos, matWorldViewProj);
		outNormal = mul(inNormal, (float3x3)matWorld);
		outWorld = mul(inPos, matWorld).xyz;
		outTex.xy = inTex.xy;
		outTangent = mul( inTangent.xyz, (float3x3)matWorld );
	}
	
float4 PS (
	in float3 inNormal: TEXCOORD0,
	in float3 inWorld: TEXCOORD1,
	in float2 inTex: TEXCOORD2,
	in float3 inTangent: TEXCOORD3) : COLOR0 {
		float4 Color = tex2D(TexSampler, inTex);
		float4 TNormalTex = tex2D(TNSampler, inTex);
		float3 bumpNormal = (TNormalTex.xyz * 2.0f) - 1.0f;
		inTangent = normalize(inTangent);
		inNormal = normalize(inNormal);
		float3 binormal = cross(inNormal, inTangent);
		float3 worldNormal = bumpNormal.x * inTangent + bumpNormal.y * binormal + bumpNormal.z * inNormal;
		
		float3 fDiffuse = vecLight.rgb;
		float3 fSpecular = 0;
		for(float i=0; i<iLights; i+=1) {
			float3 fDir = inWorld.xyz - vecLightPos[i].xyz;
			float3 fReflect = normalize((2.0f * dot(worldNormal, fDir) * worldNormal) - fDir);
			float fRadiance = saturate((vecLightPos[i].w - length(fDir)) / vecLightPos[i].w);
		   fSpecular += vecLightColor[i].rgb * pow(saturate(dot(fReflect, vecViewDir.xyz)), 8.0f) * TNormalTex.a;
			fDiffuse += vecLightColor[i].rgb * saturate(dot(normalize(-fDir), worldNormal.xyz)) * fRadiance;
		}
		
		float4 FinalColor;
		FinalColor.rgb = Color.rgb * fDiffuse;
		FinalColor.rgb += fSpecular;
		FinalColor.a = Color.a;
		
		return FinalColor;
	}

technique SpecTrans
{
	pass p0
	{
		ZWriteEnable = True;
		AlphaBlendEnable = True;
		AlphaTestEnable = True;
		
		VertexShader = compile vs_3_0 VS();
		PixelShader  = compile ps_3_0 PS();
	}
}



It computes the lights effect pixel side instead of vertex side as the defult 3dgs shader does. It looks much better but it is also much slower.

Salud!

Re: Specular Shader [Re: txesmi] #477158
05/25/19 13:55
05/25/19 13:55
Joined: Jan 2006
Posts: 168
Germany, Hannover
DexLoomer Offline
Member
DexLoomer  Offline
Member

Joined: Jan 2006
Posts: 168
Germany, Hannover
Hi,'txesmi' and thanks for the quick reply! I tried to install the code.
Unfortunately I have no idea about shader programming. I'm 3d graphic artist with little programming knowledge. wink

That's why I'm reaching my limits. In between I kept looking for a 'color_map + normal_map + specular_map ObjektShader' for 3DGS, but apart from shade-c and shadeC-EVO, I did not find anything. These did not go with me.

I am also afraid that with your tips my proficiency in programming will not be enough to modify the standard 3dgs shader. I do not know which files have to be changed at what point.


A8-Com-v 8.47.1
Page 1 of 4 1 2 3 4

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