I had a go at combining DBP's bubble shader with Green Gandalfs reflection shader in an attempt to produce what looks like a chrome bubble.
It's a very difficult task when you don't know how a shader file works and my attempts involved cutting and pasting seemingly related parts of each file into a new file. In the blind hope that the end result might actually do something. Similar in many ways to throwing a jigsaw puzzle in the air and hoping that it will all fall in to place when it lands.
Needless to say it failed!
I would really like to have a shader that performed like this but I would like it so much more if I could make it myself. So, some questions to the shader experts:
Is it possible to combine these two shaders?
Would it be quicker and easier to make a new shader from scratch?
Presumably some of the combined code that I have is conflicting and perhaps canceling out any results but I don't know which.
Actually ... the more I sit here composing questions, the more I realise I haven't a clue what I am doing. So, instead of answering my questions, if someone has time would it be possible to take the two shaders and combine the result to produce a reflective bubble for me? Perhaps with comments so that i can try to work out what is happening?
Thank you
*Skulks off to a corner to lick wounds*
Bubble shader:
//
// Bubble
//
matrix worldi : WorldIT;
matrix wvp : WorldViewProjection;
matrix mworld : World;
matrix viewInv : ViewIT;
float4 lightPos : Position < string UIPosition = "Light Position"; >;
float ticks : Time;
string XFile = "sphere.x";
texture colorTexture < string Name = "images/water_001.bmp"; >;
pixelshader pNIL;
struct VS_INPUT
{
float3 Position : POSITION;
float4 UV : TEXCOORD0;
float3 Normal : NORMAL;
};
struct VS_OUTPUT
{
float4 HPosition : POSITION;
float4 TexCoord0 : TEXCOORD0;
float4 diffCol : COLOR0;
float4 specCol : COLOR1;
};
float4 ambiColor={0.2f, 0.2f, 0.2f, 1.0f};
float4 surfColor={0.8f, 0.8f, 0.8f, 0.5f};
float4 liteColor={1.0f, 1.0f, 1.0f, 1.0f};
float specExpon=22.0;
float horizontal=0.2;
float vertical=3.0;
float timeScale=0.005;
VS_OUTPUT VShade(VS_INPUT IN)
{
VS_OUTPUT OUT;
// world normal
float3 worldNormal = mul(IN.Normal, mworld).xyz;
worldNormal = normalize(worldNormal);
float timeNow = ((ticks/100.0)/timeScale);
//build float4
float4 tempPos;
tempPos.xyz = IN.Position.xyz;
tempPos.w = 1.0;
float iny = tempPos.y * vertical + timeNow;
float wiggleX = sin(iny) * horizontal;
float wiggleY = cos(iny) * horizontal; // deriv
worldNormal.y = worldNormal.y + wiggleY;
worldNormal = normalize(worldNormal);
tempPos.x = tempPos.x + wiggleX;
//compute worldspace position
float3 worldSpacePos = mul(tempPos,mworld).xyz;
float3 LightVec = normalize(lightPos - worldSpacePos);
float ldn = dot(LightVec,worldNormal);
float diffComp = max(0,ldn);
float4 diffContrib = surfColor * ( diffComp * liteColor + ambiColor);
OUT.diffCol = diffContrib;
OUT.diffCol.w = 1.0;
OUT.TexCoord0 = IN.UV;
float3 EyePos = viewInv[3].xyz;
float3 vertToEye = normalize(EyePos - worldSpacePos);
float3 halfAngle = normalize(vertToEye + LightVec);
float hdn = pow(max(0,dot(halfAngle,worldNormal)),specExpon);
OUT.specCol = hdn * liteColor;
// transform into homogeneous-clip space
OUT.HPosition = mul(tempPos, wvp);
return OUT;
}
uniform sampler sampler0 =
sampler_state
{
texture = <colorTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
};
struct PS_INPUT
{
float4 TexCoord0 : TEXCOORD0;
float4 diffCol : COLOR0;
float4 specCol : COLOR1;
};
struct pixelOutput
{
float4 col : COLOR;
};
pixelOutput MrWigglePS_t(PS_INPUT IN)
{
// Lookup the colorMap texture
pixelOutput OUT;
float4 result = IN.diffCol * tex2D( sampler0, IN.TexCoord0) + IN.specCol;
OUT.col = result;
return OUT;
}
//////////////////////////////////////
// Techniques specs follow
//////////////////////////////////////
technique t0
{
pass p0
{
VertexShader = compile vs_2_0 VShade();
PixelShader = compile ps_1_1 MrWigglePS_t();
// Z-buffering not to be used
ZEnable = false;
ZWriteEnable = false;
CullMode = CW;
// enable alpha blending
AlphaBlendEnable = TRUE;
SrcBlend = SRCALPHA;
DestBlend = INVSRCALPHA;
MinFilter[0] = Linear;
MagFilter[0] = Linear;
MipFilter[0] = Linear;
}
pass p1
{
VertexShader = compile vs_2_0 VShade();
PixelShader = compile ps_1_1 MrWigglePS_t();
// Z-buffering not to be used
ZEnable = false;
ZWriteEnable = false;
CullMode = CCW;
// enable alpha blending
AlphaBlendEnable = TRUE;
SrcBlend = SRCALPHA;
DestBlend = INVSRCALPHA;
MinFilter[0] = Linear;
MagFilter[0] = Linear;
MipFilter[0] = Linear;
}
}
Green Gandalfs Reflection Shader:
// Green Gandalf's simple reflection shader
// Created 25 October 2006.
matrix wvp : WorldViewProjection;
matrix mw : World;
float3 eyePos : CameraPosition;
float reflectRatio = 0.8;
texture BaseTexture < string ResourceName = "lead.jpg"; >;
sampler2D BaseSample = sampler_state
{ texture = <BaseTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
};
texture EnvironmentTexture
< string Type = "cube"; string ResourceName = "SnowGG.dds"; >;
sampler EnvironmentSample = sampler_state
{ Texture = (EnvironmentTexture);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
struct VSInput1
{ float4 Pos: POSITION;
float2 UV: TEXCOORD0;
float3 Normal: NORMAL;
};
struct VSInput2
{ float4 Pos: POSITION;
float2 UV: TEXCOORD0;
};
struct VSOutput1
{ float4 Pos: POSITION;
float2 UV: TEXCOORD0;
float3 Normal: TEXCOORD1;
float3 View: TEXCOORD2;
};
struct VSOutput2
{ float4 Pos: POSITION;
float2 UV: TEXCOORD0;
float3 Dir: TEXCOORD3;
};
struct PSInput1
{ float2 UV: TEXCOORD0;
float3 Normal: TEXCOORD1;
float3 View: TEXCOORD2;
};
struct PSInput2
{ float2 UV: TEXCOORD0;
float3 Dir: TEXCOORD3;
};
struct PSOutput { float4 Colour: COLOR; };
VSOutput1 VShader1(VSInput1 In, VSOutput1 Out)
{ Out.Pos = mul(In.Pos, wvp);
Out.UV = In.UV;
Out.Normal = normalize(mul(In.Normal, mw));
float4 temp = mul(In.Pos, mw);
Out.View = normalize(eyePos - temp.xyz);
return Out;
}
VSOutput2 VShader2(VSInput2 In, VSOutput2 Out)
{ Out.Pos = mul(In.Pos, wvp);
Out.UV = In.UV;
float4 temp = mul(In.Pos, mw);
Out.Dir = temp.xyz;
return Out;
}
PSOutput PShader1(PSInput1 In, PSOutput Out)
{ float3 reflectDir = reflect(-In.View, In.Normal);
float4 reflectionColour = texCUBE(EnvironmentSample, reflectDir);
float4 baseColour = tex2D(BaseSample, In.UV);
Out.Colour = lerp(baseColour, reflectionColour, reflectRatio);
return Out;
}
PSOutput PShader2(PSInput2 In, PSOutput Out)
{ Out.Colour = texCUBE(EnvironmentSample, In.Dir);
return Out;
}
technique Reflect
{ pass one
{ VertexShader = compile vs_2_0 VShader1();
PixelShader = compile ps_2_0 PShader1();
}
}
technique Environment
{ pass one
{ VertexShader = compile vs_2_0 VShader2();
PixelShader = compile ps_2_0 PShader2();
}
}
My pathetic attempt at combining them:
matrix worldi : WorldIT;
matrix wvp : WorldViewProjection;
matrix mworld : World;
matrix viewInv : ViewIT;
float3 eyePos : CameraPosition;
float4 lightPos : Position < string UIPosition = "Light Position"; >;
float ticks : Time;
float reflectRatio = 0.8;
string XFile = "sphere.x";
texture colorTexture < string Name = "images/water_001.bmp"; >;
texture BaseTexture < string ResourceName = "lead.jpg"; >;
sampler2D BaseSample = sampler_state
{ texture = <BaseTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
};
texture EnvironmentTexture
< string Type = "cube"; string ResourceName = "SnowGG.dds"; >;
sampler EnvironmentSample = sampler_state
{ Texture = (EnvironmentTexture);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
struct VSInput1
{ float4 Pos: POSITION;
float2 UV: TEXCOORD0;
float3 Normal: NORMAL;
};
struct VSInput2
{ float4 Pos: POSITION;
float2 UV: TEXCOORD0;
};
struct VSOutput1
{ float4 Pos: POSITION;
float2 UV: TEXCOORD0;
float3 Normal: TEXCOORD1;
float3 View: TEXCOORD2;
};
struct VSOutput2
{ float4 Pos: POSITION;
float2 UV: TEXCOORD0;
float3 Dir: TEXCOORD3;
};
struct VS_INPUT
{
float3 Position : POSITION;
float4 UV : TEXCOORD0;
float3 Normal : NORMAL;
};
struct VS_OUTPUT
{
float4 HPosition : POSITION;
float4 TexCoord0 : TEXCOORD0;
float4 diffCol : COLOR0;
float4 specCol : COLOR1;
};
float4 ambiColor={0.2f, 0.2f, 0.2f, 1.0f};
float4 surfColor={0.8f, 0.8f, 0.8f, 0.5f};
float4 liteColor={1.0f, 1.0f, 1.0f, 1.0f};
float specExpon=22.0;
float horizontal=0.2;
float vertical=3.0;
float timeScale=0.005;
VS_OUTPUT VShade(VS_INPUT IN)
{
VS_OUTPUT OUT;
// world normal
float3 worldNormal = mul(IN.Normal, mworld).xyz;
worldNormal = normalize(worldNormal);
float timeNow = ((ticks/100.0)/timeScale);
//build float4
float4 tempPos;
tempPos.xyz = IN.Position.xyz;
tempPos.w = 1.0;
float iny = tempPos.y * vertical + timeNow;
float wiggleX = sin(iny) * horizontal;
float wiggleY = cos(iny) * horizontal; // deriv
worldNormal.y = worldNormal.y + wiggleY;
worldNormal = normalize(worldNormal);
tempPos.x = tempPos.x + wiggleX;
//compute worldspace position
float3 worldSpacePos = mul(tempPos,mworld).xyz;
float3 LightVec = normalize(lightPos - worldSpacePos);
float ldn = dot(LightVec,worldNormal);
float diffComp = max(0,ldn);
float4 diffContrib = surfColor * ( diffComp * liteColor + ambiColor);
OUT.diffCol = diffContrib;
OUT.diffCol.w = 1.0;
OUT.TexCoord0 = IN.UV;
float3 EyePos = viewInv[3].xyz;
float3 vertToEye = normalize(EyePos - worldSpacePos);
float3 halfAngle = normalize(vertToEye + LightVec);
float hdn = pow(max(0,dot(halfAngle,worldNormal)),specExpon);
OUT.specCol = hdn * liteColor;
// transform into homogeneous-clip space
OUT.HPosition = mul(tempPos, wvp);
return OUT;
}
uniform sampler sampler0 =
sampler_state
{
texture = <colorTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
};
struct PSInput1
{ float2 UV: TEXCOORD0;
float3 Normal: TEXCOORD1;
float3 View: TEXCOORD2;
};
struct PSInput2
{ float2 UV: TEXCOORD0;
float3 Dir: TEXCOORD3;
};
struct PSOutput { float4 Colour: COLOR; };
VSOutput1 VShader1(VSInput1 In, VSOutput1 Out)
{ Out.Pos = mul(In.Pos, wvp);
Out.UV = In.UV;
Out.Normal = normalize(mul(In.Normal, mw));
float4 temp = mul(In.Pos, mw);
Out.View = normalize(eyePos - temp.xyz);
return Out;
}
VSOutput2 VShader2(VSInput2 In, VSOutput2 Out)
{ Out.Pos = mul(In.Pos, wvp);
Out.UV = In.UV;
float4 temp = mul(In.Pos, mw);
Out.Dir = temp.xyz;
return Out;
}
PSOutput PShader1(PSInput1 In, PSOutput Out)
{ float3 reflectDir = reflect(-In.View, In.Normal);
float4 reflectionColour = texCUBE(EnvironmentSample, reflectDir);
float4 baseColour = tex2D(BaseSample, In.UV);
Out.Colour = lerp(baseColour, reflectionColour, reflectRatio);
return Out;
}
PSOutput PShader2(PSInput2 In, PSOutput Out)
{ Out.Colour = texCUBE(EnvironmentSample, In.Dir);
return Out;
}
struct PS_INPUT
{
float4 TexCoord0 : TEXCOORD0;
float4 diffCol : COLOR0;
float4 specCol : COLOR1;
};
struct pixelOutput
{
float4 col : COLOR;
};
pixelOutput MrWigglePS_t(PS_INPUT IN)
{
// Lookup the colorMap texture
pixelOutput OUT;
float4 result = IN.diffCol * tex2D( sampler0, IN.TexCoord0) + IN.specCol;
OUT.col = result;
return OUT;
}
//////////////////////////////////////
// Techniques specs follow
//////////////////////////////////////
technique t0
{
pass p0
{
VertexShader = compile vs_2_0 VShade();
PixelShader = compile ps_1_1 MrWigglePS_t();
// Z-buffering not to be used
ZEnable = false;
ZWriteEnable = false;
CullMode = CW;
// enable alpha blending
AlphaBlendEnable = TRUE;
SrcBlend = SRCALPHA;
DestBlend = INVSRCALPHA;
MinFilter[0] = Linear;
MagFilter[0] = Linear;
MipFilter[0] = Linear;
}
pass p1
{
VertexShader = compile vs_2_0 VShade();
PixelShader = compile ps_1_1 MrWigglePS_t();
// Z-buffering not to be used
ZEnable = false;
ZWriteEnable = false;
CullMode = CCW;
// enable alpha blending
AlphaBlendEnable = TRUE;
SrcBlend = SRCALPHA;
DestBlend = INVSRCALPHA;
MinFilter[0] = Linear;
MagFilter[0] = Linear;
MipFilter[0] = Linear;
}
}
technique Reflect
{ pass one
{ VertexShader = compile vs_2_0 VShader1();
PixelShader = compile ps_2_0 PShader1();
}
}
technique Environment
{ pass one
{ VertexShader = compile vs_2_0 VShader2();
PixelShader = compile ps_2_0 PShader2();
}
}
Edit I have just re-read my post and realised what I have done ... I have just asked a very noob 'Gimme Teh Codez' question ... sorry