Jump to content

[SOLVED] Drawing ObjectDynamic in lines with simple shader


photo

Recommended Posts

Hi !

I've got a problem with drawing ObjectDynamic in lines with simple shader. My goal is to connect several points in world with solid lines. The good practice for this issue is ObjectDynamic class. It can de drawn in lines.

I've create a new empty project and made a couple chages (remove material ball and switch sun scaterring mode to "moon"). ObjectDynamic is constructed in AppWorldLogic. There are only 3 points added to ObjectDynamic to make a triangle. Surface mode is set to MODE_LINES.

AppWorldLogic.h

#include <UnigineObjects.h>
using namespace Unigine;

class AppWorldLogic : public Unigine::WorldLogic
{
public:
  ...
private:
    ObjectDynamicPtr pOD;
};
AppWorldLogic.cpp

int AppWorldLogic::init()
{
    pOD = ObjectDynamic::create ();
    const ObjectDynamic::Attribute attributes[] = { { 0, ObjectDynamic::TYPE_FLOAT, 3 } };
    pOD->setVertexFormat ( attributes, 1 );
    pOD->setSurfaceMode ( ObjectDynamic::MODE_LINES, 0 );
    pOD->addLineStrip ( 4 );
    pOD->addVertexFloat ( 0, Math::vec3 ( -2.0, 2.0, 1.0 ).get (), 3 );
    pOD->addVertexFloat ( 0, Math::vec3 (  2.0, 2.0, 1.0 ).get (), 3 );
    pOD->addVertexFloat ( 0, Math::vec3 (  0.0, 2.0, 3.0 ).get (), 3 );
    pOD->addVertexFloat ( 0, Math::vec3 ( -2.0, 2.0, 1.0 ).get (), 3 );
    pOD->setMaterial ( "lines", "*" );
    pOD->flushVertex ();

    return 1;
}

"lines" is a basic material with very simple shaders:

lines.mat

<?xml version="1.0" encoding="utf-8"?>
<base_material name="lines" version="2.0" parameters_prefix="m" editable="0">

<supported node="object_dynamic"/>

	<shader pass="deferred"
                node="object_dynamic"
                vertex="lines.vert"
                fragment="lines.frag"/>

</base_material>
lines.vert

#include <core/shaders/common/common.h>

STRUCT(VERTEX_IN)
 INIT_ATTRIBUTE(float4,0,POSITION)
END

STRUCT(VERTEX_OUT)
 INIT_POSITION
END

MAIN_BEGIN(VERTEX_OUT,VERTEX_IN)

 float4 row_0 = s_transform[0];
 float4 row_1 = s_transform[1];
 float4 row_2 = s_transform[2];
	
 float4 in_vertex = float4(IN_ATTRIBUTE(0).xyz,1.0f);
 float4 position = mul4(row_0,row_1,row_2,in_vertex);
	
 OUT_POSITION = getPosition(position);

MAIN_END
lines.fraq

#include <core/shaders/common/fragment.h>

STRUCT(FRAGMENT_IN)
 INIT_POSITION
END

MAIN_BEGIN(FRAGMENT_OUT,FRAGMENT_IN)

 OUT_COLOR.r = 1.0f;
 OUT_COLOR.g = 1.0f;
 OUT_COLOR.b = 0.0f;
 OUT_COLOR.a = 0.0f;
	
MAIN_END

It works. The triangle is visible. But I've got a problem. Right after start the triangle is partialy visible. It looks like the triangle is clipped by some plane. This "cut-off line" is moving if I change the camera elevation. But if I had once change camera elevation totaly down the triangle becomes completely visible. I can't understand there is my mistake.

Additional questions:

1) How can I increase lines width? Can it be done from fragment shader?

2) Can lines be drawing using some antialiasing?

Start.png

CameraElevation.png

AfterCameraMovement.png

Link to comment

Hi Corvax,

You have used deferred pass in your material. In deferred pass you should fill all G-buffer textures. Please, check Render Sequence article for the refrence. We recommend to use ambient pass to draw lines. In that case you should set following parameters in material:

<options transparent="2"/> and <blend src="src_alpha" dest="one_minus_src_alpha"/>

and write non-zero alpha (1.0 for example).

You can also check our Orbits demo. We use custom lines shader here with orbitrary width and velocity for TAA.

Thanks!

Link to comment

Thanks!

Switching render pass to "ambient" makes triangle visible. But I've had to remove

<blend src="src_alpha" dest="one_minus_src_alpha"/>
Edited by Corvax
Link to comment
38 minutes ago, Corvax said:

Thanks!

Switching render pass to "ambient" makes triangle visible. But I've had to remove


<blend src="src_alpha" dest="one_minus_src_alpha"/>

Sorry, my fault. This line should be in material file.

Link to comment
  • silent changed the title to [SOLVED] Drawing ObjectDynamic in lines with simple shader

When squareddot says "write non-zero alpha (1.0 for example)", they're talking about this line:

OUT_COLOR.a = 0.0f;

which writes the alpha value used as src_alpha when blending in transparent="2" mode. If you set it to 0.0, you'll blend in 0% of your line and 100% background. You probably want 1.0.

Link to comment
15 hours ago, Greg.Mildenhall said:

When squareddot says "write non-zero alpha (1.0 for example)", they're talking about this line:


OUT_COLOR.a = 0.0f;

which writes the alpha value used as src_alpha when blending in transparent="2" mode. If you set it to 0.0, you'll blend in 0% of your line and 100% background. You probably want 1.0.

Thanks, Greg!

I've got it. Now I'm thinking about applying some AA to lines.

Link to comment
×
×
  • Create New...