Continuing with runtime image manipulation I stumbled across some code on the Unity forums found here, all I had to add was a strength modifier to gain a little more control of the function. The function below “NormalMap” is written in c#, it takes 2 arguments and should be used as such:-
Returned Image(Texture 2d)= NormalMap(Source Image(Texture2D),Strength of Bump(Float));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private Texture2D NormalMap(Texture2D source,float strength) { strength=Mathf.Clamp(strength,0.0F,10.0F); Texture2D result; float xLeft; float xRight; float yUp; float yDown; float yDelta; float xDelta; result = new Texture2D (source.width, source.height, TextureFormat.ARGB32, true); for (int by=0; by<result.height; by++) { for (int bx=0; bx<result.width; bx++) { xLeft = source.GetPixel(bx-1,by).grayscale*strength; xRight = source.GetPixel(bx+1,by).grayscale*strength; yUp = source.GetPixel(bx,by-1).grayscale*strength; yDown = source.GetPixel(bx,by+1).grayscale*strength; xDelta = ((xLeft-xRight)+1)*0.5f; yDelta = ((yUp-yDown)+1)*0.5f; result.SetPixel(bx,by,new Color(xDelta,yDelta,1.0f,yDelta)); } } result.Apply(); return result; } |
14438 Total Views 1 Views Today
Excellent! Always wanted to create normal maps in real time. Great blog btw, very promising start. Image and geometry manipulation are great subjects. Look forward to what you come up with next.
Many thanks, apologies for the late response, much more content to come.
Thanks for this! This was just what I’d been looking for. However! I found that the left/right bump mapping didn’t quite work properly for me until I changed the line
result.SetPixel(bx,by,new Color(xDelta,yDelta,1.0f,yDelta));
to instead end in xDelta like this:
result.SetPixel(bx,by,new Color(xDelta,yDelta,1.0f,xDelta));
Aha! *Copy–>*paste*
thanks!