How to dynamically change each vertex's color in NeoAxis?



Users browsing this forum: No registered users and 1 guest

Next topic
Previous topic
Post new topic Reply to topic  NeoAxis Forum Index » General » Help
Search for:
Author Message

Joined: Fri Dec 18, 2015
Posts: 15
PostPosted: Tue Nov 15, 2016 6:15 am Post subject: How to dynamically change each vertex's color in NeoAxis?
Reply with quote Bottom of Page Back to top
I managed to generate an object dynamically, by using code as below:
Mesh mesh = MeshManager.Instance.CreateManual(meshName);
SubMesh subMesh = mesh.CreateSubMesh();

Now what I want to do is to set the vertex colors each frame, so that the vertex color could represent its temperature. What is the best way to do this? Should I implement it by using C# code (such as dynamic vertex buffer) or by using shaders? Is there a similar example in Neoaxis?

Thanks in advance.

 
 Profile  

Joined: Sat Feb 21, 2015
Posts: 11
PostPosted: Tue Nov 15, 2016 8:13 am 
Reply with quote Bottom of Page Back to top
See DynamicSinusoidSurface.cs.
Create new class, for example, TemperatureFieldSurface.cs.
You need change code
Code: Select all   Expand view
      [StructLayout( LayoutKind.Sequential )]
      struct Vertex
      {
         public Vec3 position;
         public Vec3 normal;
         public Vec2 texCoord;
         //public Vec4 tangents;
      }

by next code
Code: Select all   Expand view

      [StructLayout( LayoutKind.Sequential )]
      struct Vertex
      {
         public Vec3 position;
         public Vec3 normal;
         public ColorValue color;
         //public UInt32 color;
         //public Vec4 tangents;
      }

and change vertex.texCoord by vertex.color in UpdateGeometry(), GenerateGeometry() and so on.
In CreateMesh():
Code: Select all   Expand view
declaration.AddElement( 0, 24, VertexElementType.Float2, VertexElementSemantic.TextureCoordinates, 0 );
//...
HardwareIndexBuffer indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer(
            HardwareIndexBuffer.IndexType._16Bit, maxIndexCount, usage );
//...
subMesh.MaterialName = "DynamicSinusoidSurface";

change by
Code: Select all   Expand view
declaration.AddElement(0, 24, VertexElementType.Float4, VertexElementSemantic.Diffuse);
//...
HardwareIndexBuffer indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer(
            HardwareIndexBuffer.IndexType._32Bit, maxIndexCount, usage );
//...
subMesh.MaterialName = "You Material Name"; //You Material Name


Example of Material in attached file.


TemperatureFieldSurface.7z [327 Bytes]
Downloaded 13 times
 
 Profile  

Joined: Fri Dec 18, 2015
Posts: 15
PostPosted: Wed Nov 16, 2016 1:44 am 
Reply with quote Bottom of Page Back to top
@vsufiy Thank you! I'm going to try this. Will there be a performance bottleneck while dynamically changing vertex attributes by this way?

 
 Profile  

Joined: Sat Feb 21, 2015
Posts: 11
PostPosted: Wed Nov 16, 2016 4:15 am 
Reply with quote Bottom of Page Back to top
For very big dynamic mesh it's will be a perfomance bottleneck.
If you need change only color, but not recalculate vertex position, then solution is next:
1) variables
Vertex[] vertices = null;
uint[] indices = null; // instead ushort[] !!!
must be out of function "UpdateGeometry".
Create its array only first time in function "CreateMesh".
2) create "function unsafe void UpdateColor( float time )" (see UpdateGeometry() without create "vertices" and "indeces"),
where change only color by time. Call copy data to vertex buffer and copy data to index buffer.
3) create new boolean variable "needUpdateColor",
check it instead "needUpdateGeometry" in OnRenderFrame().
if checked call "UpdateColor()".
Thats all.

 
 Profile  

Joined: Fri Dec 18, 2015
Posts: 15
PostPosted: Thu Nov 17, 2016 2:07 am 
Reply with quote Bottom of Page Back to top
@vsufiy Thank you! It works now by your way.

Now I'm only updating vertex buffer, without updating index buffer, in updateColor() function.
Is there a way I only update color things, not the whole vertex buffer(including position, normal, etc.)?

Since the vertices array is created beforehand and stay in the memory, does that means I'm using more memory?

 
 Profile  

Joined: Sat Feb 21, 2015
Posts: 11
PostPosted: Thu Nov 17, 2016 2:54 am 
Reply with quote Bottom of Page Back to top
See NativeUtils.CopyMemory. It's native Neoaxis utilite, that work with Hardware.
You try copy color by pointer in cycle, but i not sure it will be faster. Ask this Ivan Efimov.
Yes, you use more memory, but for modern computer it's not problem. IMHO.

 
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  NeoAxis Forum Index » General » Help

Jump to:  

Next topic
Previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum


All times are UTC




Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group