NVIDIA Shader Library Convertation start

Posted By: Slin

NVIDIA Shader Library Convertation start - 06/30/07 16:35

Yesterday I found this awsome collection and converted the first shader.
I think that IŽll go on converting them always if IŽm not motivated enough to go on with "Chopper Zone".

Normal, Parallax and Reliefmapping:
(IŽll post an example file later today or tomorrow)
The alphachannel of the normalmap gives the height.
The first Modelskin is the textur and the second the normalmap.
Only the Sunlight is supported



Code:

/*********************************************************************NVMH3****
File: $Id: //sw/devtools/ShaderLibrary/main/HLSL/relief_mapping.fx#1 $

Copyright NVIDIA Corporation 2007
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

% This material shows and compares results from four popular and
% advanced schemes for emulating displaement mapping. They are:
% Relief Mapping, Parallax Mapping, Normal Mapping, and Relief
% Mapping with Shadows. Original File by Fabio Policarpo.

keywords: material bumpmap
date: 070305

Note: Strong discontinuties in the model geometric normal (e.g., very sharp
differences from the normals in two or more parts of the same triangle)
can cause unusual overall light-attentuation errors. Re-normalizing the
rasterized normals in the pixel shader can correct this, but the case
was considered rare enough that these extra steps were eliminated for
code efficiency. If you see off lighting near sharp model edges, just
normalize "IN.normal" in the calculation of the varible "att" (shared
by all techniques).

******************************************************************************/

//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////

float4x4 matWorldViewProj;
float4x4 matView;
float4x4 matWorldView;

/////////////// Tweakables //////////

float4 vecSunPos;
float4 vecLight;
float3 DiffColor = {1,1,1};
float3 SpecColor = {0.75,0.75,0.75};

float PhongExp = 128.0;
float TileCount = 1;
float Depth = 0.1;

/*********** TEXTURES ***************/

texture entSkin1;

sampler2D ColorSampler = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MipFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

texture entSkin2;

sampler2D ReliefSampler = sampler_state
{
Texture = <entSkin2>;
MinFilter = Linear;
MipFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

/********** CONNECTOR STRUCTURES *****************/

struct AppVertexData
{
float4 pos : POSITION;
float4 color : COLOR0;
float3 normal : NORMAL; // expected to be normalized
float2 txcoord : TEXCOORD0;
float3 tangent : TEXCOORD2; // pre-normalized
};

struct VertexOutput
{
float4 hpos : POSITION;
float2 UV : TEXCOORD0;
float3 vpos : TEXCOORD1;
float3 tangent : TEXCOORD2;
float3 binormal : TEXCOORD3;
float3 normal : TEXCOORD4;
float4 lightpos : TEXCOORD5;
float4 color : COLOR0;
};

/*** SHADER FUNCTIONS **********************************************/

VertexOutput view_spaceVS(AppVertexData IN)
{
VertexOutput OUT = (VertexOutput)0;

float3 binormal = cross(IN.normal,IN.tangent);

// isolate matWorldView rotation-only part
float3x3 modelViewRotXf;
modelViewRotXf[0] = matWorldView[0].xyz;
modelViewRotXf[1] = matWorldView[1].xyz;
modelViewRotXf[2] = matWorldView[2].xyz;
float4 Po = float4(IN.pos.xyz,1.0);
OUT.hpos = mul(Po,matWorldViewProj);

// vertex position in view space (with model transformations)
OUT.vpos = mul(Po,matWorldView).xyz;

// light position in view space
float4 Lw = float4(vecSunPos.xyz,1); // this point in world space
OUT.lightpos = mul(Lw,matWorldView); // this point in view space

// tangent space vectors in view space (with model transformations)
OUT.tangent = mul(IN.tangent,modelViewRotXf);
OUT.binormal = mul(binormal,modelViewRotXf);
OUT.normal = mul(IN.normal,modelViewRotXf);

// copy color and texture coordinates
OUT.color = IN.color; // currently ignored by all techniques
OUT.UV = TileCount * IN.txcoord.xy;

return OUT;
}

/************ PIXEL SHADERS ******************/

float4 normal_mapPS(VertexOutput IN) : COLOR
{
float3 tNorm = tex2D(ReliefSampler,IN.UV).xyz - float3(0.5,0.5,0.5);
// transform tNorm to world space
tNorm = normalize(tNorm.x*IN.tangent -
tNorm.y*IN.binormal +
tNorm.z*IN.normal);
float3 texCol = tex2D(ColorSampler,IN.UV).xyz;
// view and light directions
float3 Vn = normalize(IN.vpos);
float3 Ln = normalize(IN.lightpos.xyz-IN.vpos);
// compute diffuse and specular terms
float att = saturate(dot(Ln,IN.normal));
float diff = saturate(dot(Ln,tNorm));
float spec = saturate(dot(normalize(Ln-Vn),tNorm));
spec = pow(spec,PhongExp);
// compute final color
float3 finalcolor = vecLight.xyz*texCol +
att*(texCol*DiffColor.xyz*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}

float4 parallax_mapPS(VertexOutput IN) : COLOR
{
// view and light directions
float3 Vn = normalize(IN.vpos);
float3 Ln = normalize(IN.lightpos.xyz-IN.vpos);
float2 uv = IN.UV;
// parallax code
float3x3 tbnXf = float3x3(IN.tangent,IN.binormal,IN.normal);
float4 reliefTex = tex2D(ReliefSampler,uv);
float height = reliefTex.w * 0.06 - 0.03;
uv += height * mul(tbnXf,Vn).xy;
// normal map
float3 tNorm = reliefTex.xyz - float3(0.5,0.5,0.5);
// transform tNorm to world space
tNorm = normalize(tNorm.x*IN.tangent -
tNorm.y*IN.binormal +
tNorm.z*IN.normal);
float3 texCol = tex2D(ColorSampler,uv).xyz;
// compute diffuse and specular terms
float att = saturate(dot(Ln,IN.normal));
float diff = saturate(dot(Ln,tNorm));
float spec = saturate(dot(normalize(Ln-Vn),tNorm));
spec = pow(spec,PhongExp);
// compute final color
float3 finalcolor = vecLight.xyz*texCol +
att*(texCol*DiffColor.xyz*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}

//// ray-intersect functions for relief mapping //////////

float ray_intersect_rm( // use linear and binary search
in sampler2D reliefmap,
in float2 dp,
in float2 ds)
{
const int linear_search_steps=15;

// current size of search window
float size = 1.0/linear_search_steps;
// current depth position
float depth = 0.0;
// search front to back for first point inside object
for( int i=0;i<linear_search_steps-1;i++ ) {
float4 t = tex2D(reliefmap,dp+ds*depth);
if (depth<t.w)
depth += size;
}
const int binary_search_steps=5;
// recurse around first point (depth) for closest match
for( int i=0;i<binary_search_steps;i++ ) {
size*=0.5;
float4 t = tex2D(reliefmap,dp+ds*depth);
if (depth<t.w)
depth += (2*size);
depth -= size;
}
return depth;
}

float ray_intersect_rm_lin( // only linear search for shadows
in sampler2D reliefmap,
in float2 dp,
in float2 ds)
{
const int linear_search_steps=15;
// current size of search window
float size = 1.0/linear_search_steps;
// current depth position
float depth = 0.0;
// search front to back for first point inside object
for( int i=0;i<linear_search_steps-1;i++ ) {
float4 t = tex2D(reliefmap,dp+ds*depth);
if (depth<t.w)
depth += size;
}
return depth;
}

////// relief mapping pixel shaders ////////

float4 relief_map_shadowsPS(VertexOutput IN) : COLOR
{
// ray intersect in view direction
float3 p = IN.vpos;
float3 Vn = normalize(p);
float a = dot(IN.normal,-Vn);
float3 s = float3(dot(Vn,IN.tangent.xyz), dot(Vn,IN.binormal.xyz), a);
s *= Depth/a;
float2 ds = s.xy;
float2 dp = IN.UV;
float d = ray_intersect_rm(ReliefSampler,dp,ds);

// get rm and color texture points
float2 uv = dp+ds*d;
float3 texCol = tex2D(ColorSampler,uv).xyz;
float3 tNorm = tex2D(ReliefSampler,uv).xyz - float3(0.5,0.5,0.5);
tNorm = normalize(tNorm.x*IN.tangent - tNorm.y*IN.binormal + tNorm.z*IN.normal);

// compute light direction
p += Vn*d/(a*Depth);
float3 Ln = normalize(p-IN.lightpos.xyz);

// compute diffuse and specular terms
float att = saturate(dot(-Ln,IN.normal));
float diff = saturate(dot(-Ln,tNorm));
float spec = saturate(dot(normalize(-Ln-Vn),tNorm));

// ray intersect in light direction
dp+= ds*d;
a = dot(IN.normal,-Ln);
s = float3(dot(Ln,IN.tangent.xyz),dot(Ln,IN.binormal.xyz),a);
s *= Depth/a;
ds = s.xy;
dp -= ds*d;
float dl = ray_intersect_rm_lin(ReliefSampler,dp,s.xy);
if (dl<d-0.05) // if pixel in shadow
{
diff *= dot(vecLight.xyz,float3(1.0,1.0,1.0))*0.133333;
spec = 0;
}
spec = pow(spec,PhongExp);

// compute final color
float3 finalcolor = vecLight.xyz*texCol + att*(texCol*DiffColor*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}

float4 relief_mapPS(VertexOutput IN) : COLOR
{
// ray intersect in view direction
float3 p = IN.vpos;
float3 Vn = normalize(p);
float a = dot(IN.normal,-Vn);
float3 s = float3(dot(Vn,IN.tangent.xyz), dot(Vn,IN.binormal.xyz), a);
s *= Depth/a;
float2 ds = s.xy;
float2 dp = IN.UV;
float d = ray_intersect_rm(ReliefSampler,dp,ds);

// get rm and color texture points
float2 uv = dp+ds*d;
float3 texCol = tex2D(ColorSampler,uv).xyz;
float3 tNorm = tex2D(ReliefSampler,uv).xyz - float3(0.5,0.5,0.5);
tNorm = normalize(tNorm.x*IN.tangent - tNorm.y*IN.binormal + tNorm.z*IN.normal);

// compute light direction
p += Vn*d/(a*Depth);
float3 Ln = normalize(p-IN.lightpos.xyz);

// compute diffuse and specular terms
float att = saturate(dot(-Ln,IN.normal));
float diff = saturate(dot(-Ln,tNorm));
float spec = saturate(dot(normalize(-Ln-Vn),tNorm));
spec = pow(spec,PhongExp);

// compute final color
float3 finalcolor = vecLight.xyz*texCol +
att*(texCol*DiffColor*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}

/********************* TECHNIQUES **************/
/*
technique normal_mapping {
pass p0 {
// CullMode = CW;
VertexShader = compile vs_2_0 view_spaceVS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a normal_mapPS();
}
}
*/
/*
technique parallax_mapping {
pass p0 {
// CullMode = CW;
VertexShader = compile vs_2_0 view_spaceVS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a parallax_mapPS();
}
}
*/

technique relief_mapping {
pass p0 {
// CullMode = CW;
VertexShader = compile vs_2_0 view_spaceVS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a relief_mapPS();
}
}

/*
technique relief_mapping_shadows {
pass p0 {
// CullMode = CW;
VertexShader = compile vs_2_0 view_spaceVS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a relief_map_shadowsPS();
}
}
*/
/****************************************** EOF ***/





I hope that this could become handy for someone.

Slin
Posted By: mpdeveloper_B

Re: NVIDIA Shader Library Convertation start - 06/30/07 18:01

nice to see parallax mapping finally coming to 3DGS, very good job
Posted By: Slin

Re: NVIDIA Shader Library Convertation start - 07/01/07 00:01

Now, you can download an example level here:
http://files.filefront.com/ShaderLibrary/;7928519;;/fileinfo.html

Move with the mouse and WASD.


Posted By: Frederick_Lim

Re: NVIDIA Shader Library Convertation start - 07/01/07 02:44

Thank you!
Posted By: Grafton

Re: NVIDIA Shader Library Convertation start - 07/01/07 06:34

Thanks Slin! Very Awesome 5 star contribution, especially the demo, very cool!
I really hope you are motivated to convert some more
Posted By: Machinery_Frank

Re: NVIDIA Shader Library Convertation start - 07/01/07 07:54

5 points from me as well.

Slin, youd did a very good job. The shaders work fine and produce a good fps rate for what they are doing.

If you continue this project then please try to add a few dynamic lights. If you need any textures and models for testing purposes then please let me know.

You are a good man!
Thanks!
Posted By: Dyc

Re: NVIDIA Shader Library Convertation start - 07/01/07 09:01

Thanks Slin, shame it doesn't really work very well on my ati x850xt.
Posted By: Loopix

Re: NVIDIA Shader Library Convertation start - 07/01/07 09:51

Yeahh...a new shader library for 3dgs Unfotunately the relief and relief/shadow don't work on my ATI x800...probable a NVIDIA buit in feature to boycott ATI
Posted By: Frederick_Lim

Re: NVIDIA Shader Library Convertation start - 07/01/07 12:26

The sample works fine with my 6800 GT, but I cannot get the shader work with WED level block.
Posted By: Slin

Re: NVIDIA Shader Library Convertation start - 07/01/07 13:33

Quote:


If you continue this project then please try to add a few dynamic lights. If you need any textures and models for testing purposes then please let me know.





Thanks for that offer, if want to, send me anything you want to see the shader on.
As a next step IŽll add some more lights. How many do you want?
I think I should add fog support either. And I should add something like lightranges.
My MSN adress is nils_daumann[at]online[point]de


Quote:


The sample works fine with my 6800 GT, but I cannot get the shader work with WED level block.





I the shader you have to change entSkin2 to mtlSkin1.
You have to set the flat flag in WED for that texture and you shouldnŽt forget to add the normalmap skin to the material wich has to have the same name than the leveltexture:

bmap BMAP_Name = <Normalmap.tga>;
material TextureName
{
skin1 = BMAP_Name;
//...
}


Is there anyone with an ati graphicscard for wich those effects are working?
Do you get any error messages or is the model just black or what happens?


Thanks for all your feedback

Slin
Posted By: frazzle

Re: NVIDIA Shader Library Convertation start - 07/01/07 15:18

Nice and very helpfull Slin !!
I get a decend framerate together with the faqt that those shaders look great during runtime, this is surely worth a nice rating too
Please keep on converting, if you need any help, please let me know

Cheers

Frazzle
Posted By: vlau

Re: NVIDIA Shader Library Convertation start - 07/01/07 15:30

Thanks for the shaders. Unfortunately, my uv texture was scrambbled,
I've tried to change AddressU and V to Clamp, but still no luck.
Is there any way to fix this?
Posted By: HeelX

Re: NVIDIA Shader Library Convertation start - 07/01/07 15:43

@Slin: kannst du bitte eine Version kompilieren (mit EXE!) und hochladen? Danke.
Posted By: Grafton

Re: NVIDIA Shader Library Convertation start - 07/01/07 16:25

Quote:

Thanks for the shaders. Unfortunately, my uv texture was scrambbled,
I've tried to change AddressU and V to Clamp, but still no luck.
Is there any way to fix this?




Same on my models too!
The normal mapping seems to work great with the box sample, but the UVs seem to
be a planer projection or something.
Posted By: Slin

Re: NVIDIA Shader Library Convertation start - 07/01/07 18:04

@HeelX:
Download Compiled Version

Quote:


Thanks for the shaders. Unfortunately, my uv texture was scrambbled,
I've tried to change AddressU and V to Clamp, but still no luck.
Is there any way to fix this?





You are right, I notice it too.
This doesnŽt happen if there is a frame around the heightmap wich has the highest value.
You can see that it doesnŽt happen here:


But I noticed this problem in fx composer as well. Wich means, that it is at least not my fault
In cases as the picture shows or if the texture is very repetative it isnŽt a problem but for everything else you may use an other shader...
Posted By: TSG_Christof

Re: NVIDIA Shader Library Convertation start - 07/01/07 20:05

How u have Create the Normal map for the Block.
Posted By: Frederick_Lim

Re: NVIDIA Shader Library Convertation start - 07/02/07 01:22

Quote:

I the shader you have to change entSkin2 to mtlSkin1.
You have to set the flat flag in WED for that texture and you shouldnŽt forget to add the normalmap skin to the material wich has to have the same name than the leveltexture:

bmap BMAP_Name = <Normalmap.tga>;
material TextureName
{
skin1 = BMAP_Name;
//...
}




I got it work now. Thanks!

How can I control the texture scale in WED?
Posted By: msl_manni

Re: NVIDIA Shader Library Convertation start - 07/02/07 04:41

I have a Intel on-board graphics card with vs-ps 2.0. Your shaders dont work on it. Is there something wrong.
Posted By: MrCode

Re: NVIDIA Shader Library Convertation start - 07/02/07 06:16

Really awesome looks...

But I can only get about 1-2 FPS with the parallax map demo on my GeForce FX 5200 (256MB).
Posted By: vlau

Re: NVIDIA Shader Library Convertation start - 07/02/07 06:32

Quote:


How can I control the texture scale in WED?





Select your level block, rightclick->Properties->surface.
Posted By: Machinery_Frank

Re: NVIDIA Shader Library Convertation start - 07/02/07 08:07

Quote:

"But I can only get about 1-2 FPS with the parallax map demo on my GeForce FX 5200 (256MB)."




For Parallax or Relief mapping you need a better GPU. Better you use simple Normalmapping instead.
Posted By: Frederick_Lim

Re: NVIDIA Shader Library Convertation start - 07/03/07 02:20

Slin:

How do you create the alpha channel in tga? Is that call relief map or heightmap or something? The nVidia normalmap plugin or crazybump are not create the alpha channel automatically.
Posted By: Slin

Re: NVIDIA Shader Library Convertation start - 07/03/07 11:48

The alphachannel is a heightmap. IŽm usually using an greyscale version of the texture.
Posted By: Slin

ShaderLibrary Update - 07/11/07 19:47



Update:
-2 dynamic lights and sun is supported.
-fog
-glossmap for the parallax, normal and reliefmapping shaders wich has to be in the alphachannel of the maintexture.
-one more shader

Important:
-For the reliefmapping is an nvidia fx, a newer nvidia graphicscard or an other card that supports shadermodel 3 needed.
-The BumpyGlossyMetal-Shader seems to have some small problems with ati x800 cards.


You can find the downloadlink and every new updates on my blog:
http://slindev.wordpress.com


I plan to add a small introduction about how to convert such a shader for the use with 3DGS to my blog. But IŽll let you know if itŽs done
I bought an update to A7 yesterday and hope to get it on friday. When I got it, IŽll try to convert some of the postprocessing effects and the others wich need render to texture.

Slin
Posted By: Machinery_Frank

Re: ShaderLibrary Update - 07/12/07 07:31

Hi Slin,

great to see that you are progressing. I am highly interested in your blog-comments regarding the conversion process.

I appreciate your great work.

Regards,
Frank
Posted By: EX Citer

Re: ShaderLibrary Update - 07/12/07 13:27

keep it up!
Posted By: Puppeteer

Re: ShaderLibrary Update - 07/12/07 18:52

Quote:

keep it up!



Signed
Posted By: Slin

Re: ShaderLibrary Update - 07/12/07 20:01



Okay, I started to write a small workshop about how to convert a shader for the use with 3DGS.
You can find the first part at my Blog.

Sorry that it is a bit messed up... At least I learned that I shouldnŽt use the Editor for such things
Posted By: Poison

Re: ShaderLibrary Update - 07/13/07 05:02

Thank you Slin.
© 2024 lite-C Forums