Esempio n. 1
0
        public static void GenerateTexture(Texture2D texture, Surface surface, Rect space, Rect physicalSpace)
        {
            if (surface == null)
            {
                Debug.LogError("Null surface send to Generate Texture");
                return;
            }
            if (surface.previewTexture == null)
            {
                Debug.LogError(string.Format("Surface used ({0}) has no texture", surface.name));
                return;
            }

            int     colour32Width   = Mathf.RoundToInt(space.width);
            int     colour32Height  = Mathf.RoundToInt(space.height);
            Vector2 textureUnitSize = surface.textureUnitSize;

            Color32[] surfaceColor32 = surface.pixels;
            int       surfaceWidth   = surface.previewTexture.width;
            int       surfaceHeight  = surface.previewTexture.height;

            int pixelsPerMeter = Mathf.RoundToInt(space.width / physicalSpace.width);
            int resizeWidth    = Mathf.RoundToInt(textureUnitSize.x * pixelsPerMeter);
            int resizeHeight   = Mathf.RoundToInt(textureUnitSize.y * pixelsPerMeter);
            int tileX          = Mathf.CeilToInt(colour32Width / (float)resizeWidth);
            int tileY          = Mathf.CeilToInt(colour32Height / (float)resizeHeight);

            resizeWidth  = colour32Width / tileX;
            resizeHeight = colour32Height / tileY;
//            Debug.Log(surfaceWidth+" "+ surfaceHeight+" "+ resizeWidth+" "+ resizeHeight);
            Color32[] resizedSurface = TextureScale.NearestNeighbourSample(surfaceColor32, surfaceWidth, surfaceHeight, resizeWidth, resizeHeight);

            int offsetX = Mathf.RoundToInt(space.x);
            int offsetY = Mathf.RoundToInt(space.y);

//            Debug.Log(tileX+" "+colour32Width+" "+surfaceWidth);

            for (int tx = 0; tx < tileX; tx++)
            {
                for (int ty = 0; ty < tileY; ty++)
                {
                    int xPaintPos = offsetX + tx * resizeWidth;
                    int yPaintPos = offsetY + ty * resizeHeight;
                    texture.SetPixels32(xPaintPos, yPaintPos, resizeWidth, resizeHeight, resizedSurface);
                }
            }
        }
Esempio n. 2
0
        public void UpdateAtlas()
        {
            int wallSectionCount = wallSections.Length;
            int surfaceCount     = surfaces.Length;
            int totalEntries     = wallSectionCount + surfaceCount;

            if (totalEntries == 0)
            {
                return;
            }

            if (material == null)
            {
                material = new Material(Shader.Find("Standard"));
            }

            Texture2D[] textures   = new Texture2D[totalEntries];
            int         pixelWidth = (int)tileSize - padding;
            int         atlasWidth = (int)atlasSize;

            for (int w = 0; w < wallSectionCount; w++)
            {
                Texture2D useTexture = wallSections[w].previewTexture;
                if (useTexture == null)
                {
                    continue;
                }
                if (useTexture.width != pixelWidth)
                {
                    Color32[]  textureArray;
                    Vector2Int pixelSize = new Vector2Int(pixelWidth, pixelWidth);
                    WallSectionGenerator.Texture(out textureArray, wallSections[w], pixelSize, Vector2.one * 2, true);
                    useTexture = new Texture2D(pixelWidth, pixelWidth, atlasTextureFormat, true);
                    useTexture.SetPixels32(textureArray);
                    useTexture.Apply(true, false);
                }
                textures[w] = useTexture;
            }

            for (int s = 0; s < surfaceCount; s++)
            {
                Texture2D useTexture = surfaces[s].previewTexture as Texture2D;
                if (useTexture == null)
                {
                    continue;
                }
                int textureWidth  = useTexture.width;
                int textureHeight = useTexture.height;
                if (textureWidth != pixelWidth)
                {
                    Color32[] resizeColorArray = TextureScale.NearestNeighbourSample(useTexture.GetPixels32(), textureWidth, textureHeight, pixelWidth, pixelWidth);
                    useTexture.Resize(pixelWidth, pixelWidth, atlasTextureFormat, true);
                    useTexture.SetPixels32(resizeColorArray);
                    useTexture.Apply(true, false);
                }
                textures[wallSectionCount + s] = useTexture;
            }
            texture     = new Texture2D(1, 1, atlasTextureFormat, true);
            packedRects = texture.PackTextures(textures, padding, atlasWidth, false);
            texture.Apply(true, false);
            material.mainTexture = texture;
        }