A neat idea, which could help from some rendering performance aspects. Who wants to use a separate material for each separate model part, especially when you’ve got flesh, cloth, metal, and glass all on the same model? I mean, when you’re doing diffuse only, it sure ain’t make sense to make a material for each.
So, I was searching the web in search [ omg a new concept, searching ] of how to generate some mesh tangents that won’t make my lighting look like it’s coming from the wrong direction - and I came across this page: Link.
More importantly, I saw this picture: Link.
Then I thought, hey, Unity can do that! So I spent about five minutes writing the following code.
Now, the code definitely will not work ‘out of the box,’ but it does work somewhat - it has been tested in chunks - and it is a pretty cool idea. I’m not exactly using it, but here it is. The idea is here. Not sure if this has been posted before, but what the heck.
// Returns one of the HitType enumerations, which is created by you.
public HitType GetHitEffect ( RaycastHit in_hitInfo )
{
// This works with no null reference exceptions because of short circuiting
if (( in_hitInfo.collider != null )( in_hitInfo.collider.gameObject != null )( in_hitInfo.collider.gameObject.renderer != null ))
{
if ( in_hitInfo.collider.gameObject.renderer.material != null )
{
// Obviously, the following line requires a sort of custom shader
Texture2D t_hit_texture = (Texture2D)( in_hitInfo.collider.gameObject.renderer.material.GetTexture( "_HitMat" ) );
if ( t_hit_texture != null )
{
Color hit_color = t_hit_texture.GetPixel( in_hitInfo.textureCoord.x * t_hit_texture.width, in_hitInfo.textureCoord.y * t_hit_texture.height );
// Another function created by you
return GetHitTypeFromColor( hit_color );
}
}
}
return HitType.Generic;
}
private HitType GetHitTypeFromColor ( Color in_color )
{
// Since Color is a struct, this is totally safe
switch ( in_color )
{
case (new IntegerColor( 255, 255, 255, 255 )):
return HitType.Glass;
default:
return HitType.Generic;
}
}
// This just converts a Color (basically a struct of floats) into a comparable version
private IntegerColor ToIntegerColor ( Color in_color )
{
return new IntegerColor( (int)(in_color.r * 255), (int)(in_color.g * 255), (int)(in_color.b * 255), (int)(in_color.a * 255));
}
public struct IntegerColor
{
public int r, g, b, a;
public IntegerColor( int c1, int c2, int c3, int c4 )
{
r = c1;
g = c2;
b = c3;
a = c4;
}
}
Now, to find some way to generate those tangents…