Baking vertices down to flat texture from shader

I’m trying to bake from a shader material down to a flat texture, but getting results that seem rather sparse…

Wanted result:
[58076-316d33bcfab36f18dbc18500054932b4.png*_|58076]

What my below attempts are getting:
[58075-d48a169e60140110152bfb1843a47641.png*_|58075]

Actually - I am not sure if I am looping through the constructs right to be able to get the color of each UV point to bake to texture?

		texBaked = new Texture2D (512,512);

		MeshFilter mf = goBakeMe.GetComponent ();
		Mesh mesh = mf.mesh;
		/*for(int i=0;i<mesh.vertices.Length;i++){
			Vector2 uv = mesh.uv*;*
_			Color color = mesh.colors*;*_
_			int minx = Mathf.FloorToInt (uv.x * 512); int maxx = Mathf.CeilToInt (uv.x*512);_
_			int miny = Mathf.FloorToInt (uv.y * 512); int maxy = Mathf.CeilToInt (uv.y*512);_
_*			for(int a=minx;a<=maxx;a++){*_
_*				for(int b=miny;b<=maxy;b++){*_
_*					texBaked.SetPixel (a,b,color);*_
_*				}*_
_*			}*_
_		}*/_
_*		for(int i=0;i<mesh.triangles.Length;i++){*_
_			Vector2 uv = mesh.uv[mesh.triangles*];*_
_			Color color = mesh.colors[mesh.triangles*];*_ 
_			int minx = Mathf.FloorToInt (uv.x * 512); int maxx = Mathf.CeilToInt (uv.x*512);_
_			int miny = Mathf.FloorToInt (uv.y * 512); int maxy = Mathf.CeilToInt (uv.y*512);_
_*			for(int a=minx;a<=maxx;a++){*_
_*				for(int b=miny;b<=maxy;b++){*_
_*					texBaked.SetPixel (a,b,color);*_
_*				}*_
_*			}*_
_*		}*_
_*		texBaked.Apply();*_
_*
*_

*
*

So if i got you right you want to bake vertex colors into a texture. This does only work when your mesh is already unwrapped, so each vertex already has a uv coordinate. Next thing is the same area of the texture could be mapped to several different triangles. So you would either choose one triange and pick the colors of that or interpolate between all triangles that are mapped to that area.

The best way to sample the pixels for the texture is to do the reverse of what you’re doing at the moment. You don’t pick a vertex and try to find out which pixels might be affected by that vertex, but instead you start by picking a pixel and then figure out which vertices affect that pixel.

All you need is what i posted over here on this question. You basically loop through all pixels in your texture, convert that pixel coordinate to uv coordinates and then for each triangle in the mesh you calculate the barycentric coordinate of the uv point in relation to each triangle. That allows you to determine if the pixel is inside one of those triangles and furthermore it allows you to sample the interpolated color of the 3 vertices of a triangle.