Page 1 of 1

how to create shader outline for object (border)

Posted: Sat Apr 27, 2024 12:00 pm
by minimy
Hello, im triying to make a border shader but no work.
Im using this code from example in the web.

What im doing bad?. Thanks!

Material

Code: Select all

material BlackBorderMaterial
{
    technique
    {
        pass
        {
            ambient 1 0 0
            diffuse 1 0 0
            specular 1.0 1.0 0.0
            vertex_program_ref BlackBorderVertexShader
            {
                param_named_auto worldViewProj worldviewproj_matrix
            }

            fragment_program_ref BlackBorderFragmentShader
            {
            }
        }
    }
}

Code: Select all

float4x4 worldViewProj : WORLDVIEWPROJ;

struct VS_INPUT
{
    float4 position : POSITION;
};

struct PS_INPUT
{
    float4 position : POSITION;
};

VS_INPUT vs_main(VS_INPUT input)
{
    VS_INPUT output = (VS_INPUT)0;
    output.position = mul(worldViewProj, input.position);
    return output;
}

float4 ps_main(PS_INPUT input) : COLOR
{
    float2 screenPos = input.position.xy / input.position.w;
    float thickness = 10.0 / 800.0; // Grosor del borde en píxeles, ajustado por la resolución de la pantalla
    float border = step(thickness, length(screenPos - 0.5));
    return float4(0, 0, 0, 1) * border; // Color del borde (negro)
}

technique BlackBorder
{
    pass P0
    {
        VertexShader = compile vs_2_0 vs_main();
        PixelShader = compile ps_2_0 ps_main();
    }
}

Re: how to create shader outline for object (border)

Posted: Sat Apr 27, 2024 1:26 pm
by DarkDragon
You are mixing up some things into a single file.

1. The technique is part of the OGRE material script, not HLSL, why is it in the HLSL script?
2. Better split the HLSL into vert.hlsl and frag.hlsl.
3. The program_refs aren't declared anywhere?! You have "vertex_program_ref BlackBorderVertexShader" but no block named "vertex_program BlackBorderVertexShader" anywhere.

Here is some introduction to material scripts, I hope it helps:
https://ogrecave.github.io/ogre/api/lat ... ripts.html
https://ogrecave.github.io/ogre/api/lat ... grams.html

Re: how to create shader outline for object (border)

Posted: Sat Apr 27, 2024 2:08 pm
by minimy
Hi DarkDragon, i dont know why technique is in hlsl script, the example was taked from the web.
I need read more about hlsl and materials. I know make simple materials with many pass or techniques but no have control over hlsl.
Please can you put a litle example to understand how work it?
Thanks for you help and time.

Re: how to create shader outline for object (border)

Posted: Tue Apr 30, 2024 5:15 pm
by minimy
Solved!!! Thanks for help!

Re: how to create shader outline for object (border)

Posted: Tue Apr 30, 2024 10:29 pm
by Fred
Please always post your solution if you find it yourself, it's not great for other people looking for the same stuff at later time

Re: how to create shader outline for object (border)

Posted: Wed May 01, 2024 11:11 am
by minimy
Sorry Fred, my solution was very primitive, using a plane with the mask of static objects. Is not the best but work for my project. For menu or inventary is not bad

Re: how to create shader outline for object (border)

Posted: Wed May 15, 2024 7:32 pm
by minimy
Greetings! I found this code to make the edges of the objects, but for many tries I have not been able to add it to my material.
The truth is that I'm still very green with the shaders hehe. :mrgreen:
But all in good time.
If anyone dares to set an example, I appreciate it.

VertexShader

Code: Select all

cbuffer cbPerFrame : register(b0)
{
	float4x4 matVP;
	float4x4 matGeo;
};

struct VSInput
{
	float3 Position : POSITION;
	float3 Normal : NORMAL;
};

struct VSOutput
{
	float4 Position : SV_POSITION;
	float4 Color : COLOR;
	float3 Normal : NORMAL;
};
		
VSOutput main(VSInput vin)
{
	VSOutput vout = (VSOutput)0;
	
	vout.Position = mul(mul(float4(vin.Position, 1), matGeo), matVP);
	vout.Color = float4(1,0,0, 1);
	vout.Normal = vin.Normal;
	
	return vout;
}
FragmentShader

Code: Select all

struct PSInput
{
	float4 Position : SV_POSITION;
	float4 Color : COLOR;
	float3 Normal : NORMAL;
};

struct PSOutput
{
	float4 Color : SV_TARGET0;
	float4 Normal : SV_TARGET1;
};

PSOutput main(PSInput pin)
{
	PSOutput pout;
	
	pout.Color = pin.Color;
	pout.Normal = float4(pin.Normal,1);
	
	return pout;
}

Re: how to create shader outline for object (border)

Posted: Tue Jul 02, 2024 11:49 pm
by minimy
I did a solution with no shaders, only using objects.
You can found it here:

https://www.purebasic.fr/english/viewtopic.php?t=84649