Transparent Black Pixel Shader
Posted: Sun Nov 22, 2015 7:09 am
Hello everyone,
Well, I haven't posted any shader examples in a while. I moved on to other 3D options in Purebasic almost a year ago so I'm a little rusty when it comes to writing material scripts. Hopefully everything will work smoothly for those interested in it.
First off here's what the shader looks like. The left entities are using the shader and the right ones are not.
Nothing fancy all it does is take the pure black pixels and then discards them. It does leave jagged edges, and sadly there's not much you can do to get rid of them.
Well, technically you can get rid of them, but then you have to deal with transparent triangles being out of order. And reordering the triangles in Purebasic is simply not possible and I don't see a good enough reason why it ever would be.
In other words you have to choose either jagged edges with correct ordering or smooth edges which have the possibility of incorrect ordering.
Now a simple explanation on how to use this shader with the textures you want. If you open TransparentMaterial.MATERIAL in notepad (or some other text editor) and scroll to the bottom you should find
I'll break down what these lines mean. The Transparent01 and Transparent02 are the Name$ that you call from Purebasic with GetScriptMaterial(#Material, Name$).
Image01.png and Image02.png are the actual image that the shader will use. In other words you need to write one of these out for every material that has a unique image.
For example if I were to add another one to the list it might look something like this.
And I'd call the new material in Purebasic like so.
That's about all you need to do as the end user of this shader.
This script includes both GLSL and HLSL shaders so you don't need to worry about it not working on your system. Just include all the files and the script will pick whichever one is currently in use by your program.
Now then here's the link to all the files and the example code to run them. Just start up the pb file and it should run without an issue. If there are problems let me know and I'll see what I can do to fix them.
Shader Transparent.zip
I'll also post the script and shaders below, but you can ignore them if you downloaded the files from the link above.
Enjoy!
If you copy and paste them into notepad just make sure you save them exactly as labeled below.
TransparentMaterial.MATERIAL
TransparentHLSL.HLSL
TransparentGLSL.VERT
TransparentGLSL.FRAG
Well, I haven't posted any shader examples in a while. I moved on to other 3D options in Purebasic almost a year ago so I'm a little rusty when it comes to writing material scripts. Hopefully everything will work smoothly for those interested in it.
First off here's what the shader looks like. The left entities are using the shader and the right ones are not.
Nothing fancy all it does is take the pure black pixels and then discards them. It does leave jagged edges, and sadly there's not much you can do to get rid of them.
Well, technically you can get rid of them, but then you have to deal with transparent triangles being out of order. And reordering the triangles in Purebasic is simply not possible and I don't see a good enough reason why it ever would be.
In other words you have to choose either jagged edges with correct ordering or smooth edges which have the possibility of incorrect ordering.
Now a simple explanation on how to use this shader with the textures you want. If you open TransparentMaterial.MATERIAL in notepad (or some other text editor) and scroll to the bottom you should find
Code: Select all
material Transparent01 : TransparentTexture
{
set $Texture Image01.png
}
material Transparent02 : TransparentTexture
{
set $Texture Image02.png
}
Image01.png and Image02.png are the actual image that the shader will use. In other words you need to write one of these out for every material that has a unique image.
For example if I were to add another one to the list it might look something like this.
Code: Select all
material Transparent01 : TransparentTexture
{
set $Texture Image01.png
}
material Transparent02 : TransparentTexture
{
set $Texture Image02.png
}
material Stars : TransparentTexture
{
set $Texture NightSky.png
}
Code: Select all
MaterialHandle = GetScriptMaterial(#PB_Any, "Stars")
This script includes both GLSL and HLSL shaders so you don't need to worry about it not working on your system. Just include all the files and the script will pick whichever one is currently in use by your program.
Now then here's the link to all the files and the example code to run them. Just start up the pb file and it should run without an issue. If there are problems let me know and I'll see what I can do to fix them.
Shader Transparent.zip
I'll also post the script and shaders below, but you can ignore them if you downloaded the files from the link above.
Enjoy!
If you copy and paste them into notepad just make sure you save them exactly as labeled below.
TransparentMaterial.MATERIAL
Code: Select all
// GLSL simple transparency
vertex_program transparencyVSGLSL glsl
{
source TransparentGLSL.vert
}
fragment_program transparencyFSGLSL glsl
{
source TransparentGLSL.frag
default_params
{
param_named TexTU int 0
}
}
// HLSL simple transparency
vertex_program transparencyVSHLSL hlsl
{
source TransparentHLSL.hlsl
entry_point AlphaTexture_Vert
target vs_1_1
default_params
{
param_named_auto worldViewProj worldviewproj_matrix
}
}
fragment_program transparencyFSHLSL hlsl
{
source TransparentHLSL.hlsl
entry_point AlphaTexture_Frag
target ps_2_0
}
// Delegates
vertex_program transparencyVp unified
{
delegate transparencyVSGLSL
delegate transparencyVSHLSL
}
fragment_program transparencyFp unified
{
delegate transparencyFSGLSL
delegate transparencyFSHLSL
}
abstract material TransparentTexture
{
technique
{
pass
{
vertex_program_ref transparencyVp
{
}
fragment_program_ref transparencyFp
{
}
texture_unit
{
texture $Texture
tex_coord_set 0
}
}
}
technique
{
pass
{
texture_unit
{
texture $Texture
tex_coord_set 0
}
}
}
}
material Transparent01 : TransparentTexture
{
set $Texture Image01.png
}
material Transparent02 : TransparentTexture
{
set $Texture Image02.png
}
Code: Select all
void AlphaTexture_Vert(
float4 position : POSITION,
float2 uv : TEXCOORD0,
uniform float4x4 worldViewProj,
out float4 oPosition : POSITION,
out float2 oUv : TEXCOORD0)
{
position = mul(worldViewProj, position);
oPosition = position;
oUv = uv;
}
void AlphaTexture_Frag(
float4 position : POSITION,
float2 uv : TEXCOORD0,
out float4 oColour : COLOR,
uniform sampler2D TextureMap : register(s0))
{
float4 Color = tex2D(TextureMap, uv.xy);
float Black = Color.r + Color.g + Color.b;
if(Black == 0)
discard;
oColour = Color;
}
TransparentGLSL.VERT
Code: Select all
varying vec2 TexCoord;
void main(void)
{
TexCoord = gl_MultiTexCoord0.xy;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
TransparentGLSL.FRAG
Code: Select all
varying vec2 TexCoord;
uniform sampler2D TexTU;
void main(void)
{
vec4 Color = texture2D(TexTU, TexCoord);
if (Color.rgb == vec3(0.0,0.0,0.0))
discard;
gl_FragColor = Color;
}