示例#1
0
            public static Texture2D NormalMapFrom(float strength, float diagonalPixelsCoef, Texture2D bump, Texture2D normalReady, Texture2D ambient, string name, Texture2D Result)
            {
                if (bump == null)
                {
                    Debug.Log("No bump texture");
                    return(null);
                }

                float xLeft;
                float xRight;
                float yUp;
                float yDown;

                float yDelta;
                float xDelta;

                width  = bump.width;
                height = bump.height;

                TextureImporter importer     = bump.GetTextureImporter();
                bool            needReimport = importer.WasNotReadable();

                needReimport |= importer.WasNotSingleChanel();
                if (needReimport)
                {
                    importer.SaveAndReimport();
                }

                if (normalReady != null)
                {
                    importer      = normalReady.GetTextureImporter();
                    needReimport  = importer.WasNotReadable();
                    needReimport |= importer.WasWrongIsColor(false);
                    needReimport |= importer.WasMarkedAsNormal();
                    if (needReimport)
                    {
                        importer.SaveAndReimport();
                    }
                }

                importer      = ambient.GetTextureImporter();
                needReimport  = importer.WasNotReadable();
                needReimport |= importer.WasWrongIsColor(false);
                needReimport |= importer.WasNotSingleChanel();
                if (needReimport)
                {
                    importer.SaveAndReimport();
                }

                try
                {
                    srcBmp     = (normalReady != null) ? normalReady.GetPixels(width, height) : bump.GetPixels();
                    srcSm      = bump.GetPixels(width, height);
                    srcAmbient = ambient.GetPixels(width, height);
                    dst        = new Color[height * width];
                }
                catch (UnityException e)
                {
                    Debug.Log("couldn't read one of the textures for  " + bump.name + " " + e.ToString());
                    return(null);
                }


                for (int by = 0; by < height; by++)
                {
                    for (int bx = 0; bx < width; bx++)
                    {
                        int dstIndex = IndexFrom(bx, by);

                        if (normalReady)
                        {
                            dst[dstIndex].r = (srcBmp[dstIndex].r - 0.5f) * strength + 0.5f;
                            dst[dstIndex].g = (srcBmp[dstIndex].g - 0.5f) * strength + 0.5f;
                        }
                        else
                        {
                            xLeft  = srcBmp[IndexFrom(bx - 1, by)].a;
                            xRight = srcBmp[IndexFrom(bx + 1, by)].a;
                            yUp    = srcBmp[IndexFrom(bx, by - 1)].a;
                            yDown  = srcBmp[IndexFrom(bx, by + 1)].a;

                            xDelta = (-xRight + xLeft) * strength;

                            yDelta = (-yDown + yUp) * strength;

                            dst[dstIndex].r = xDelta * Mathf.Abs(xDelta)
                                              + 0.5f;
                            dst[dstIndex].g = yDelta * Mathf.Abs(yDelta)
                                              + 0.5f;
                        }

                        dst[dstIndex].b = srcSm[dstIndex].a;
                        dst[dstIndex].a = srcAmbient[dstIndex].a;
                    }
                }


                if ((Result == null) || (Result.width != width) || (Result.height != height))
                {
                    Result = bump.CreatePngSameDirectory(name + "_MASKnMAPS");
                }

                TextureImporter resImp = Result.GetTextureImporter();

                needReimport  = resImp.WasClamped();
                needReimport |= resImp.WasWrongIsColor(false);
                needReimport |= resImp.WasNotReadable();
                needReimport |= resImp.HadNoMipmaps();


                if (needReimport)
                {
                    resImp.SaveAndReimport();
                }

                Result.SetPixels(dst);
                Result.Apply();
                Result.SaveTexture();

                return(Result);
            }
示例#2
0
            public static Texture2D GlossToAlpha(Texture2D gloss, Texture2D diffuse, string newName)
            {
                if (gloss == null)
                {
                    Debug.Log("No bump texture");
                    return(null);
                }

                TextureImporter ti           = gloss.GetTextureImporter();
                bool            needReimport = ti.WasNotSingleChanel();

                needReimport |= ti.WasNotReadable();
                if (needReimport)
                {
                    ti.SaveAndReimport();
                }


                ti            = diffuse.GetTextureImporter();
                needReimport  = ti.WasAlphaNotTransparency();
                needReimport |= ti.WasNotReadable();
                if (needReimport)
                {
                    ti.SaveAndReimport();
                }

                Texture2D product = diffuse.CreatePngSameDirectory(newName + "_COLOR");

                TextureImporter importer = product.GetTextureImporter();

                needReimport  = importer.WasNotReadable();
                needReimport |= importer.WasClamped();
                needReimport |= importer.HadNoMipmaps();
                if (needReimport)
                {
                    importer.SaveAndReimport();
                }


                width  = gloss.width;
                height = gloss.height;
                Color[] dstColor;

                try
                {
                    dstColor = diffuse.GetPixels();
                    srcBmp   = gloss.GetPixels(diffuse.width, diffuse.height);
                }
                catch (UnityException e)
                {
                    Debug.Log("couldn't read one of the textures for  " + gloss.name + " " + e.ToString());
                    return(null);
                }


                int dstIndex;

                for (int by = 0; by < height; by++)
                {
                    for (int bx = 0; bx < width; bx++)
                    {
                        dstIndex = IndexFrom(bx, by);
                        Color col;
                        col   = dstColor[dstIndex];
                        col.a = srcBmp[dstIndex].a;
                        dstColor[dstIndex] = col;
                    }
                }

                product.SetPixels(dstColor);
                product.Apply();
                product.SaveTexture();

                return(product);
            }