Using Diffuse + Normal shader with procedurally generated terrain

Hey everyone, I am working on generating a flat mesh using procedurally generated squares. That all works fine, but I need to apply a normal map to each tile to make the world look less flat. Since I am doing it from a script, I am having trouble figuring out how to apply it to each tile at the same time I pick apart the tile sheet to assign the UV texture. Here is the portion of the script that does that:

protected void GenSquare(int x, int y, Vector2 texture){
	newVertices.Add( new Vector3(worldScale * x, worldScale * y, 0));
	newVertices.Add( new Vector3(worldScale * (x + 1), worldScale * y, 0));
	newVertices.Add( new Vector3(worldScale * (x + 1), worldScale * (y - 1), 0));
	newVertices.Add( new Vector3(worldScale * x, worldScale * (y - 1), 0));
	
	newTriangles.Add(squareCount*4);
	newTriangles.Add((squareCount*4)+1);
	newTriangles.Add((squareCount*4)+3);
	newTriangles.Add((squareCount*4)+1);
	newTriangles.Add((squareCount*4)+2);
	newTriangles.Add((squareCount*4)+3);
	
	newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y + tUnit));
	newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y + tUnit));
	newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y));
	newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y));

	//Assign the normals here I think?

	squareCount++;
}

I have the object in the Inspector looking like this, I think this part is correct. I just need to make the script talk to it:

22286-tileset.jpg

For now I want to just make that entire normal map apply to each tile as a proof of concept, but eventually I will make a tile sheet of normal maps that corresponds to the ones our art team is going to replace the diffuse part with. For the record, newVertices, newTriangles, and newUV are Lists of Vector3/int/Vector2 respectively, and I have a List of Vector3’s named newNormals ready to go for this. I just need to figure out how to access that normal map in the image. Thank you in advance for all your help!

Example of the terrain I am getting. Notice it looks very flat. Please help me fix this :slight_smile:

The normal map is applied by the shader - but you need both the normals of the tiles and the tangents to be able to do bump mapping.

The normals should be easy, they should face perpendicular to whatever the plane you are creating is. So Vector3.up in your case I believe.

You have to solve the tangents - the answer by Ninja here should do it for you: Calculating Tangents (Vector4) - Unity Answers