Refraction/Distortion?

Materials, textures, lighting, postfx
27 posts Page 2 of 3
LoLJester
Posts: 37
Joined: Thu Aug 13, 2015 5:58 pm
 
by LoLJester » Thu Nov 10, 2016 6:48 pm
Thank you irei1as, I will see how this works.
Steve_Yorkshire
Posts: 182
Joined: Tue Feb 03, 2015 10:30 pm
 
  by Steve_Yorkshire » Mon Jan 16, 2017 5:56 pm
Gave this a bash. After first test I found it gives more distortion if the material has translucent set to false.

Steve_Yorkshire
Posts: 182
Joined: Tue Feb 03, 2015 10:30 pm
 
by Steve_Yorkshire » Tue Jan 17, 2017 2:49 pm
Anyone know how to disable a custom material/shader from casting shadows?

I've looked through the stock hlsl files for reference and can find how to make it accept shadowing but aren't seeing anything it not casting shadows ...
Steve_Yorkshire
Posts: 182
Joined: Tue Feb 03, 2015 10:30 pm
 
by Steve_Yorkshire » Tue Jan 17, 2017 4:41 pm
After much searching for shadows in customMaterials and shaders ... turns out I should have been looking for customMaterials in shadows ... :roll:

lighting/shadowMap/shadowMatHook.cpp

void ShadowMaterialHook::init( BaseMatInstance *inMat )
{
//...
// Do instancing in shadows if we can.
if ( inFeatures.hasFeature( MFT_UseInstancing ) )
features.addFeature( MFT_UseInstancing );

Material *shadowMat = (Material*)inMat->getMaterial();
if ( dynamic_cast<CustomMaterial*>( shadowMat ) )
{
// This is a custom material... who knows what it really does, but
// if it wasn't already filtered out of the shadow render then just
// give it some default depth out material.
//shadowMat = MATMGR->getMaterialDefinitionByName( "AL_DefaultShadowMaterial" );//<<<<<<<<<<yorks out!
}

// By default we want to disable some states
// that the material might enable for us.
//...


So ends up being a standard Torque concept of "who knows what the hell this for, let's just do something with it" :lol:
Steve_Yorkshire
Posts: 182
Joined: Tue Feb 03, 2015 10:30 pm
 
by Steve_Yorkshire » Tue Jan 17, 2017 6:27 pm
On @
User avatar
JeffR
advice I added:
mCastShadows = true;//yorks in (to customMaterialDefinition.cpp)
bool mCastShadows;//yorks in (to customMaterialDefinition.h)
virtual bool castsShadows() const { return mCastShadows; }//yorks in (to customMaterialDefinition.h)

Okay, so what's happening now is this:

// This is a custom material... who knows what it really does, but
// if it wasn't already filtered out of the shadow render then just
// give it some default depth out material.


So now translucent gets filtered out and no shadows appear, and non-translucent customMaterials cast shadows - regardless of what castShadows=bool setting the texture material actually has.

A more useful addition would be to get the status of castShadows and then force "shadowMat = MATMGR->getMaterialDefinitionByName( "AL_DefaultShadowMaterial" );" whether translucent or not. Though I'm not sure how to do that ... :|
Steve_Yorkshire
Posts: 182
Joined: Tue Feb 03, 2015 10:30 pm
 
by Steve_Yorkshire » Tue Jan 17, 2017 6:39 pm
And here's a little pic of what it looks like:
Player set to translucent=0;
Big Disk set to translucent=1;

Image
Azaezel
Posts: 353
Joined: Tue Feb 03, 2015 9:50 pm
 
by Azaezel » Tue Jan 17, 2017 6:57 pm
irei1as wrote:Let me share my try at this subject.
What I did is to use a PostFX to copy the backbuffer before the TransparentObj RenderBin into a NamedTexture and use that NamedTexture as a texture sampler for a Custom Material. That Custom material just distorted a bit so it's not really refraction.

should be able to also snag https://github.com/GarageGames/Torque3D ... ers.cs#L85
which will let you grab
https://github.com/GarageGames/Torque3D ... P.hlsl#L33
+ https://github.com/GarageGames/Torque3D ... #L216-L218
for the variables to use to determine which way to 'bend' the texture lookup if you're not using translucency itself.

edit: a shadergen result file with normals calcs to pull from

Code: Select all

//*****************************************************************************
// Torque -- HLSL procedural shader
//*****************************************************************************

// Dependencies:
#include "shaders/common/torque.hlsl"

// Features:
// Vert Position
// Render Target Output = 0.0, output mask b
// Base Texture
// Diffuse Color
// Deferred Shading: Specular Map
// Deferred Shading: Mat Info Flags
// Bumpmap [Deferred]
// Reflect Cube
// Eye Space Depth (Out)
// Visibility
// GBuffer Conditioner
// BC5nm
// Deferred Material

struct VertData
{
   float3 position        : POSITION;
   float tangentW        : TEXCOORD3;
   float3 normal          : NORMAL;
   float3 T               : TANGENT;
   float2 texCoord        : TEXCOORD0;
};


struct ConnectData
{
   float4 hpos            : SV_Position;
   float2 out_texCoord    : TEXCOORD0;
   float3x3 outViewToTangent : TEXCOORD1;
   float3 reflectVec      : TEXCOORD4;
   float4 wsEyeVec        : TEXCOORD5;
};


//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
ConnectData main( VertData IN,
                  uniform float4x4 modelview       : register(C0),
                  uniform float4x4 viewToObj       : register(C4),
                  uniform float4x4 objTrans        : register(C8),
                  uniform float3   eyePosWorld     : register(C12)
)
{
   ConnectData OUT;

   // Vert Position
   OUT.hpos = mul(modelview, float4(IN.position.xyz,1));
   
   // Render Target Output = 0.0, output mask b
   
   // Base Texture
   OUT.out_texCoord = (float2)IN.texCoord;
   
   // Diffuse Color
   
   // Deferred Shading: Specular Map
   
   // Deferred Shading: Mat Info Flags
   
   // Bumpmap [Deferred]
   float3x3 objToTangentSpace;
   objToTangentSpace[0] = IN.T;
   objToTangentSpace[1] = cross( IN.T, normalize(IN.normal) ) * IN.tangentW;
   objToTangentSpace[2] = normalize(IN.normal);
   float3x3 viewToTangent = mul( objToTangentSpace, (float3x3)viewToObj );
   OUT.outViewToTangent = viewToTangent;
   
   // Reflect Cube
   float3 cubeVertPos = mul(objTrans, float4(IN.position,1)).xyz;
   float3 cubeNormal = ( mul( (objTrans),  float4(IN.normal, 0) ) ).xyz;
   cubeNormal = bool(length(cubeNormal)) ? normalize(cubeNormal) : cubeNormal;
   float3 eyeToVert = cubeVertPos - eyePosWorld;
   OUT.reflectVec = reflect(eyeToVert, cubeNormal);
   
   // Eye Space Depth (Out)
   float3 depthPos = mul( objTrans, float4( IN.position.xyz, 1 ) ).xyz;
   OUT.wsEyeVec = float4( depthPos.xyz - eyePosWorld, 1 );
   
   // Visibility
   
   // GBuffer Conditioner
   
   // BC5nm
   
   // Deferred Material
   
   return OUT;
}


Code: Select all

//*****************************************************************************
// Torque -- HLSL procedural shader
//*****************************************************************************

// Dependencies:
#include "shaders/common/torque.hlsl"

// Features:
// Vert Position
// Render Target Output = 0.0, output mask b
// Base Texture
// Diffuse Color
// Deferred Shading: Specular Map
// Deferred Shading: Mat Info Flags
// Bumpmap [Deferred]
// Reflect Cube
// Eye Space Depth (Out)
// Visibility
// GBuffer Conditioner
// BC5nm
// Deferred Material

struct ConnectData
{
   float4 vpos            : SV_Position;
   float2 texCoord        : TEXCOORD0;
   float3x3 viewToTangent   : TEXCOORD1;
   float3 reflectVec      : TEXCOORD4;
   float4 wsEyeVec        : TEXCOORD5;
};


struct Fragout
{
   float4 col : SV_Target0;
   float4 col1 : SV_Target1;
   float4 col2 : SV_Target2;
   float4 col3 : SV_Target3;
};


//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
              uniform SamplerState diffuseMap      : register(S0),
              uniform Texture2D diffuseMapTex   : register(T0),
              uniform float4    diffuseMaterialColor : register(C0),
              uniform SamplerState specularMap     : register(S1),
              uniform Texture2D specularMapTex  : register(T1),
              uniform float     matInfoFlags    : register(C1),
              uniform SamplerState bumpMap         : register(S2),
              uniform Texture2D bumpMapTex      : register(T2),
              uniform SamplerState cubeMap         : register(S3),
              uniform TextureCube cubeMapTex      : register(T3),
              uniform float     cubeMips        : register(C2),
              uniform float3    vEye            : register(C4),
              uniform float4    oneOverFarplane : register(C5),
              uniform float     visibility      : register(C3)
)
{
   Fragout OUT;

   // Vert Position
   
   // Render Target Output = 0.0, output mask b
   OUT.col3 = 0.00001;
   
   // Base Texture
   float4 diffuseColor = diffuseMapTex.Sample(diffuseMap, IN.texCoord);
   OUT.col1 = diffuseColor;
   
   // Diffuse Color
   OUT.col1 *= diffuseMaterialColor;
   
   // Deferred Shading: Specular Map
   float smoothness =    specularMapTex.Sample(specularMap, IN.texCoord).r;
   float metalness =    specularMapTex.Sample(specularMap, IN.texCoord).b;
   float4 specularColor =    specularMapTex.Sample(specularMap, IN.texCoord).ggga;
   OUT.col2.bga = float3(smoothness,specularColor.g,metalness);
   
   // Deferred Shading: Mat Info Flags
   OUT.col2.r = matInfoFlags;
   
   // Bumpmap [Deferred]
   float4 bumpNormal = float4( bumpMapTex.Sample(bumpMap, IN.texCoord).rg * 2.0 - 1.0, 0.0, 0.0 ); // bc5nm
   bumpNormal.z = sqrt( 1.0 - dot( bumpNormal.xy, bumpNormal.xy ) ); // bc5nm
   half3 gbNormal = (half3)mul( bumpNormal.xyz, IN.viewToTangent );
   
   // Reflect Cube
   OUT.col3 = cubeMapTex.SampleLevel( cubeMap, float3(IN.reflectVec).rgb, min((1.0 - smoothness)*cubeMips + 1.0, cubeMips));
   
   // Eye Space Depth (Out)
#ifndef CUBE_SHADOW_MAP
   float eyeSpaceDepth = dot(vEye, (IN.wsEyeVec.xyz / IN.wsEyeVec.w));
#else
   float eyeSpaceDepth = length( IN.wsEyeVec.xyz / IN.wsEyeVec.w ) * oneOverFarplane.x;
#endif
   
   // Visibility
   fizzle( IN.vpos.xy, visibility );
   
   // GBuffer Conditioner
   float4 normal_depth = float4(normalize(gbNormal), eyeSpaceDepth);

   // output buffer format: GFXFormatR16G16B16A16F
   // g-buffer conditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)
   float4 _gbConditionedOutput = float4(sqrt(half(2.0/(1.0 - normal_depth.y))) * half2(normal_depth.xz), 0.0, normal_depth.a);
   
   // Encode depth into hi/lo
   float2 _tempDepth = frac(normal_depth.a * float2(1.0, 65535.0));
   _gbConditionedOutput.zw = _tempDepth.xy - _tempDepth.yy * float2(1.0/65535.0, 0.0);

   OUT.col = _gbConditionedOutput;
   
   // BC5nm
   
   // Deferred Material
   

   return OUT;
}
Steve_Yorkshire
Posts: 182
Joined: Tue Feb 03, 2015 10:30 pm
 
by Steve_Yorkshire » Wed Jan 18, 2017 12:34 am
One of the things I was wanting this for was to get the distortion shockwave effect that you see in some games. And here's what I've managed with an animatedShape in the explosions themselves.

Steve_Yorkshire
Posts: 182
Joined: Tue Feb 03, 2015 10:30 pm
 
by Steve_Yorkshire » Fri Jan 27, 2017 11:26 pm
Righty, had a bit of a look at shaders hlsl code ... and it's gibberish to me :roll: but having said that I still managed to get some cool improvements! :mrgreen:



Two types of distortion material in the video, the first has translucency the second doesn not - hence the shadow, and also this creates more distortion for reasons ...

Bascially I added a swirly distort effect to screenpos x and y using y and x and some offsets.

So the Pixel shader now looks like this:

struct datainput 
{
float4 screenCoord : TEXCOORD1;
};

//diffuseMap is defined in the CustomMaterial and its ShaderData
uniform sampler2D diffuseMap : register(S0);

//accumTime is an engine variable. It's kind of the time of the material.
//Use it to move things in time
uniform float accumTime;

float4 main( datainput IN ) : COLOR0
{

//texture to screen
float2 screenpos = 0.5+IN.screenCoord.xy/2*float2(1,-1)/IN.screenCoord.w;

float2 wobble;
float speed = 0.03;
float magnify = 1.0;

wobble.y = magnify + sin(IN.screenCoord.x + accumTime) * speed;
wobble.x = magnify + sin(IN.screenCoord.y + accumTime) * speed;

//yorks abs seems to give more rotation but splits it into visible cells
//wobble.y = magnify + abs(sin(IN.screenCoord.x + accumTime) * speed);
//wobble.x = magnify + abs(sin(IN.screenCoord.y + accumTime) * speed);

screenpos = screenpos * wobble;

float4 colour_exit = tex2D(diffuseMap, screenpos);

return colour_exit;
}


Initially I did abs(sin like the original wobble script but it dissected the area into noticeable cells which then rotated against each other. Dropping abs gives a more integrated flow.

And the Vertex Shader has a very minor change to increase the distortion somewhat:

struct VertToPixOut
{
float4 hpos : POSITION;
float4 screen : TEXCOORD1;
};

struct VertToPixIn
{
float4 hpos : POSITION;
};

uniform float4x4 modelview;

VertToPixOut main( VertToPixIn In)
{
VertToPixOut Out;
Out.hpos = mul( modelview, In.hpos);

//Out.screen = Out.hpos;
Out.screen = Out.hpos * 1.5;//yorks bump it up a bit but not too much

return Out;
}


I'd still like a couple of extra things ... though I'm not entirely sure what ... perhaps make the material slightly darker like introduce a color float of 0.9 or something. Really I'm trying for the old Doom pink demons which had invisible types but you could still see them if if you were looking. Having said that I haven't tested this with an enemy attack on the player so not sure how well it'd currently show up attacking/following in game.
irei1as
Posts: 59
Joined: Fri Feb 27, 2015 5:13 pm
by irei1as » Sat Jan 28, 2017 9:56 am
Those textures are looking really amazing.

I'm not sure you'll experience this but let me share some issues I had while playing with these kind of transparencies:

a) Using #prepass works only for Advance Lightning. But that's fine as very few users have the kind of ancient computers* that can't handle it.
b) Be careful with the limits of the screen. For me the texture wraps around so in these limits a wobble can show the texture of the other side of the screen. It's more noticeable if there is a huge contrast of colours between both sides.
Image



*just me, probably.
27 posts Page 2 of 3

Who is online

Users browsing this forum: No registered users and 1 guest