Page 1 of 1

Flat Shader according to World Position (Barycentric?)

PostPosted: Wed Mar 20, 2019 8:33 pm
by AiSard
Hi,

I am currently trying to do a flat shader (low poly) for procgen meshes (no UV mapping so far) where the colour changes according to the height in World Space.

So far I've referenced the Low Poly Water example to achieve the low poly effect.

But there doesn't seem to be an easy way to interact with the World on a per-Triangle basis instead of per-Vertex.

I was looking in to Barycentric Co-ordinates, but I'm unsure how to implement it in ASE. Not a good enough coder to interpret the one thread I found that touched upon it either. (If I did, would each vertex on a triangle be able to reference the center?)

Is there a simpler way or a hack for my specific usage? If not, how would I go about implementing Barycentric co-ordinates.

current flat shader 'spilling' over in to other triangles
https://imgur.com/a/nCDFypG

Re: Flat Shader according to World Position (Barycentric?)

PostPosted: Thu Mar 21, 2019 10:51 am
by Amplify_Borba
Hey there, thank you for getting in touch and your support!

This matter has already been addressed in this post.

In short, I don't think that this is achievable through ASE without us supporting geometry shaders ( no ETA for this ), you'll likely have to research alternative means of achieving the effect you intend.

Re: Flat Shader according to World Position (Barycentric?)

PostPosted: Thu Mar 21, 2019 3:53 pm
by AiSard
Hmm, that is a shame :(

I shall try to bake the triangle center in to mesh.color instead (unshared vertices, identical value for all 3 corners).
Which I should be able to access via Vertex Color instead.

However, as the height is currently generated by the Noise Generator (Simplex2D) feeding in to Local Vertex Offset, I don't actually know the Z value and am struggling to emulate Amplify's Simplex2D in C#.

Is there a readable Simplex2d code example somewhere who's implementation is similar enough to Amplify's that I could then just tweak to get the same values from?

Re: Flat Shader according to World Position (Barycentric?)

PostPosted: Thu Mar 21, 2019 4:34 pm
by Amplify_Borba
I believe our implementation is based on this one.

You may also check out our nodes' code by opening their scripts, the SimplexNoiseNode.cs can be found within the Assets\AmplifyShaderEditor\Plugins\Editor\Nodes\ImageEffects folder, for example.

Re: Flat Shader according to World Position (Barycentric?)

PostPosted: Thu Mar 21, 2019 7:46 pm
by AiSard
Not knowing the first thing about coding shaders, quickly dropped that avenue.

That said, found a way to access triangle centers. Posting for posterity's sake.

Calculated the center of each triangle in world space (made sure to unshare the vertices) and for each triangle corner assigning Mesh.SetUV identical float3 UVs of said triangle center.
Vertex TexCoord pretty much then replaces the need for World Position, returning the triangle center for all points inside a triangle.