Pages: [1]   Go Down
  Print  
Author Topic: Projection mapping  (Read 23112 times)
June 12, 2008, 12:10:01 pm
Hi Smiley

I've not been able to do a projection using HLSL similar to the standard projection we have as a Mapping option. I need it for mirror reflections using render to texture. This feature is apparently not supported by ShaderFX.
Does anyone know the HLSL code to do projection mapping? It must be possible since the water shader uses it.

Thanks Smiley


* projection.jpg (36.65 KB, 338x186 - viewed 2888 times.)
June 12, 2008, 01:00:39 pm
Hello,
here is a simple shader with a projection, using Vpos in the pixel shader.
I once made it for a very basic water. It has the Color texture as a projection (i used a mirrored camera for generating the "reflection" image) and a normal map for dissortion.

Code:

float xres : CHANNELVALUE0
<
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 10.0;
float UIStep = 0.1;
string UIName = "Texture Scale";
> = 0.015;
 
float yres : CHANNELVALUE1
<
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 10.0;
float UIStep = 0.1;
string UIName = "Texture Offset";
> = 0.5;

float normal_effect : CHANNELVALUE2
<
string UIName = "Normal Effect";
string UIWidget = "slider";
float UIMin = 0.0;
float UIStep = 0.05;
float UIMax = 1.0;
> = 0.5;


texture TextureMap_2273 : COLORMAP0
<
string ResourceName = "ELMLEAF.TGA";
string UIName = "Texture Map";
string ResourceType = "2D";
>;
 
sampler2D TextureMap_2273Sampler = sampler_state
{
Texture = <TextureMap_2273>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
 
texture NormalTexture : NormalMap : COLORTEXTURE1
<
string Name = "";
string UIName = "NormalTexture";
string ResourceType = "2D";
>;
 
sampler2D NormalTextureSampler = sampler_state
{
Texture = <NormalTexture>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
};

/************** light info **************/

float4 light1Pos[8] : LIGHTPOS ;

float4 light1Color[8] : LIGHTCOLOR ;

//----------------------------------
float4x4 wvp : WorldViewProjection < string UIWidget = "None"; >; 
float4x4 worldI : WorldInverse < string UIWidget = "None"; >; 
float4x4 worldIT : WorldInverseTranspose < string UIWidget = "None"; >; 
float4x4 viewInv : ViewInverse < string UIWidget = "None"; >; 
float4x4 world : World < string UIWidget = "None"; >; 


// input from application
struct a2v {
float4 position : POSITION;
float2 texCoord : TEXCOORD0;

float3 tangent : TANGENT;
float3 binormal : BINORMAL;
float3 normal : NORMAL;

};

// output to fragment program
struct v2f {
        float4 position    : POSITION;
float2 texCoord : TEXCOORD0;

float3 worldTangent  : TEXCOORD1;
        float3 worldBinormal : TEXCOORD2;
        float3 worldNormal    : TEXCOORD3;

};

//Ambient and Self-Illum Pass Vertex Shader
v2f av(a2v In, uniform float4 lightPosition)
{
v2f Out = (v2f)0;
    Out.position = mul(In.position, wvp); //transform vert position to homogeneous clip space

Out.worldNormal = mul(In.normal, worldIT).xyz; //compute world space normal
    Out.worldBinormal = mul(In.binormal, worldIT).xyz; //compute world space binormal
    Out.worldTangent = mul(In.tangent, worldIT).xyz; //compute world space tangent

Out.texCoord.xy = In.texCoord.xy;
    return Out;
}

//Ambient and Self-Illum Pass Pixel Shader
float4 af(v2f In, uniform float4 lightColor, float2 screenPos :VPOS) : COLOR
{


float3 flatNormal = float3(0.5f, 0.5f, 1.0f);
float3 NormalTexture = tex2D(NormalTextureSampler, In.texCoord.xy);
NormalTexture = lerp(flatNormal, NormalTexture, normal_effect);
NormalTexture = NormalTexture * 2 - 1; //expand to -1 to 1 range
NormalTexture.rgb = normalize(NormalTexture.rgb);
float3 Nn = normalize(In.worldNormal); //input the vectors required for tangent to world space transform
    float3 Tn = normalize(In.worldTangent);
    float3 Bn = normalize(In.worldBinormal);

//float3 NMV = ( Nn * NormalTexture.rgb.z ) + (NormalTexture.rgb.x * Tn ) + (NormalTexture.rgb.y * -Bn);

float3 NMV = (NormalTexture.rgb.y * -Bn);


//return screenPos.x /1280;
float2 coord = float2 (0,0);
coord.x = screenPos.x /xres;
coord.y = screenPos.y/yres;
float2 modCoord = coord;
// float4 TextureMap_2273 = tex2D(TextureMap_2273Sampler, coord.xy);
float4 TextureMap_2273 = tex2D(TextureMap_2273Sampler, modCoord.xy);
float3 input1 = TextureMap_2273.rgb*0.7;




float4 ret =  float4(0,0,0,1);
ret = float4(input1, 1);


return ret;
}

technique Complete

pass ambient 
    {
VertexShader = compile vs_3_0 av(light1Pos[0]);
ZEnable = true;
ZWriteEnable = true;
CullMode = ccw;
AlphaBlendEnable = false;
PixelShader = compile ps_3_0 af(light1Color[0]);




xres and y res do as input viewport width and viewport height.


cheerswerner
June 13, 2008, 09:49:08 am
Thank you very much! Smiley

Did you notice that the reflection is slightly darker using HLSL? I wonder why that happens...


* hlsl_rtt.jpg (27.64 KB, 800x330 - viewed 1081 times.)
June 13, 2008, 12:57:22 pm
Ah, I get it! You multiply by 0.7  Clown
June 13, 2008, 06:19:17 pm
Wink hehe as i said, i didi it long time ago =).
Btw. it looks better with 0.7  Tongue
cheers
werner
June 19, 2008, 09:25:33 pm
Someone feel like dropping by Ben and Kees' place and asking them if they'd like to support it in a future version of ShaderFX?

~Rhys
June 19, 2008, 09:45:47 pm
http://www.lumonix.biz/yabb/YaBB.pl?num=1213355316

I suppose you could make your own ShaderFX material that supports it, like Kees said in the thread Smiley
June 20, 2008, 02:20:23 pm
Excellent.  I was actually just wondering for their benefit.

~Rhys
June 20, 2008, 02:36:41 pm
Well, I think they're mostly interested in shaders that work inside 3ds Max. This one doesn't, as far as I know, so they probably won't make it an "official" material.
June 20, 2008, 02:46:51 pm
Makes sense.

Be nice to have our own thing.  I started making one a while back just using Merge Text operators within Quest.  It was quite nice but got tabled after I got a proof of concept running.  Even so, it doesn't seem so hard to do, at least in basic.

~Rhys
August 10, 2008, 02:54:10 pm
I've written some more on this topic here.
September 13, 2013, 02:39:58 pm
Hello!
yama has led a working code, but it works if the texture of mirror the same resolution as the viewport ..

who knows how to modify the code for any resolution textures?
Thank you!

edit:
Hurried to the question)
The problem is solved.
The problem was that I rebuilt the projection of the camera to fit the texture ..
Pages: [1]   Go Down
  Print  
 
Jump to: