Procedural Mesh and Lighting

Its took me a while but i’ve finally got my head aronud creating meshes procedurally and it looks great :slight_smile:

I’ve been able to apply a texture to my mesh and shape it as a cube, similar to minecraft :slight_smile: Ill be changing everything soon

But the only problem I have is I can’t seem to get light to shine on the mesh, it just looks plain.

Anyone know how to do this?

Did you add Normals?

Also did you check your Material settings?

Thanks for the reply :slight_smile:

I didn’t really understand the normals and just left them at Vector3.up.

How would I go about setting the normals for each vertex in certain directions such as up left right etc.

Just think about the Normal as the direction the surface is viewing, its harder to understand when applying this to a vertex, so just a simple example:

If you want a cube with smooth appearing corners (lighting wise) all your normals should point away from the center of this cube.
This gives you smooth corners (lighting wise) and you only need 8 Vertices for this cube.
This is a cube with smooth corners:

If you want hard corners, you need for each face of the cube separate vertices (4) and 24 Vertices for the whole cube. Each Normal of an Facevertex should point at the direction the face surface is looking/facing.
This is a cube with hard corners: http://www.cs.berkeley.edu/~ug/slide/docs/slide/spec/images/normals_shared_flat.gif

(Images are only a approximation)

Thankyou very much :smile:, its looking good!

You can use RecalculateNormals, however it’s faster to supply them. For cubes it’s very easy; a surface facing up would have the normals be (0, 1, 0), a surface facing to the right along the x axis would be (1, 0, 0), a surface facing forward along the z axis would be (0, 0, 1), etc.

–Eric

Some shaders make use of tangents, so you might need them as well. Here is the code I use. Don’t remember where I got it from, Unity Answers probably.

private Vector4[] CalculateTangents()
{
	int triangleCount = _triangles.Count;
	int vertexCount = _vertices.Count;

	var tan1 = new Vector3[vertexCount];
	var tan2 = new Vector3[vertexCount];

	var tangents = new Vector4[vertexCount];

	for (int a = 0; a < triangleCount; a += 3)
	{
		int i1 = _triangles[a + 0];
		int i2 = _triangles[a + 1];
		int i3 = _triangles[a + 2];

		var v1 = _vertices[i1];
		var v2 = _vertices[i2];
		var v3 = _vertices[i3];

		var w1 = _uvs[i1];
		var w2 = _uvs[i2];
		var w3 = _uvs[i3];

		float x1 = v2.x - v1.x;
		float x2 = v3.x - v1.x;
		float y1 = v2.y - v1.y;
		float y2 = v3.y - v1.y;
		float z1 = v2.z - v1.z;
		float z2 = v3.z - v1.z;

		float s1 = w2.x - w1.x;
		float s2 = w3.x - w1.x;
		float t1 = w2.y - w1.y;
		float t2 = w3.y - w1.y;

		float r = 1.0f / (s1 * t2 - s2 * t1);

		var sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
		var tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);

		tan1[i1] += sdir;
		tan1[i2] += sdir;
		tan1[i3] += sdir;

		tan2[i1] += tdir;
		tan2[i2] += tdir;
		tan2[i3] += tdir;
	}

	for (int a = 0; a < vertexCount; ++a)
	{
		var n = _normals[a];
		var t = tan1[a];

		var tmp = (t - n * Vector3.Dot(n, t)).normalized;
		tangents[a] = new Vector4(tmp.x, tmp.y, tmp.z);
		tangents[a].w = Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f ? -1.0f : 1.0f;
	}

	return tangents;
}

Sorry for taking a while to reply.

I use 8 vertices for my cube and i’ve used the recalculate normals which leaves me with this effect.

I’ve also tried setting them ,with recalculate off, and end up with a very similar effect

What im after is an effect simlar to this

Is that as easy as setting normals etc. or will it take a lot more learning?

The problem is you are calculating normals per vert as though each is a cube. If you visualise them, your cube looks like it has one set of normals per vertex. Therefore they will all averagely be pointing away from the center of the cube.

To get lighting like you want, most of the normals would actually be pointing directly up when they are not at the edges of the area.

Here is an image which describes the normals on a curved mesh. Notice how important their directions are for the mesh to look proper when lit:

The following unity docs link explains more: http://unity3d.com/support/documentation/Manual/Anatomy%20of%20a%20Mesh.html - in particular why you aren’t getting the shading you’re expecting. Automatic normals generation would have to take into account smoothing angles you desire.

A cube with faceted normals needs to be made of 24 vertices, not 8.

As, although the verts at each corner of the cube share the same spatial coordinates, they don’t share the same normal vectors and so must be split apart into 3 different vertices, each with it’s own normal direction.

So I would pretty much need to seperate each face, 4 for each face and point the normals in the required direction eg top face pointing up, left left etc.?

Just checking before I put the work in :slight_smile:

Yes for a hard-edged look, you would need 6 quads within the same mesh :slight_smile: