Create Normal map from grayscale map

来源:互联网 发布:企业站如何优化 编辑:程序博客网 时间:2024/06/11 21:27
desTex = new Texture2D(tex.width, tex.height, TextureFormat.RGB24, false);                for (int i = 0; i < tex.height; i++)        {             for(int j = 0; j < tex.width; j++)            {                Color col = tex.GetPixel(i, j);                                float grayscale = col.grayscale;                float xLeft = tex.GetPixel(i - 1, j).grayscale;                float xRight = tex.GetPixel(i + 1, j).grayscale;                float yUp = tex.GetPixel(i, j + 1).grayscale;                float yDown = tex.GetPixel(i, j - 1).grayscale;                // 方案1                //float xDelta = ((xLeft - xRight) + 1) * .5f;                //float yDelta = ((yUp - yDown) + 1) * .5f;                //desTex.SetPixel(i, j, new Color(xDelta, yDelta, 1f, 1f));                // 方案2, 更靠近Unity3D的 create from grayScale, Bumpiness = 0.01                Vector3 va = new Vector3(1, 0, xRight - xLeft).normalized;                Vector3 vb = new Vector3(0, 1, yUp - yDown).normalized;                Vector3 bump = Vector3.Cross(va, vb).normalized;                bump = (bump + Vector3.one) * 0.5f;                desTex.SetPixel(i, j, new Color(bump.x, bump.y, bump.z));                            }        }                desTex.Apply();        byte[] datas = desTex.EncodeToPNG();        System.IO.File.WriteAllBytes(Application.dataPath + "/aa.png", datas);        AssetDatabase.Refresh();


参考:

http://forum.unity3d.com/threads/bumpmap-grayscale-to-normalmap-rgb.5714/

http://stackoverflow.com/questions/5281261/generating-a-normal-map-from-a-height-map





0 0
原创粉丝点击