how to create shader outline for object (border)

Everything related to 3D programming
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

how to create shader outline for object (border)

Post 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();
    }
}
If translation=Error: reply="Sorry, Im Spanish": Endif
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

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

Post 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
bye,
Daniel
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

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

Post 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.
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

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

Post by minimy »

Solved!!! Thanks for help!
If translation=Error: reply="Sorry, Im Spanish": Endif
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post 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
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

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

Post 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
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

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

Post 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;
}
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

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

Post 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
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply