private void InitializePenSize() //Pen texture rescaling depending on the images resolution
 {
     pencil = penTexture.GetPixels();
     eraser = new Texture2D(penTexture.width, penTexture.height, TextureFormat.ARGB32, false);
     eraser.SetPixels(pencil);
     TextureScaler.Bilinear(eraser, (int)(eraser.width / (960f / imageWidth)), (int)(eraser.height / (960f / imageHeight)));
     pencil = null;
     pencil = eraser.GetPixels();
 }
Example #2
0
    public void ReadImageFromImageFileToCurrentCardImage(string directory)
    {
        byte[]    fileData = System.IO.File.ReadAllBytes(directory);
        Texture2D texture  = new Texture2D(2, 2);

        texture.LoadImage(fileData);

        TextureScaler.Bilinear(texture, (int)CardBeingEditedImage.preferredWidth, (int)CardBeingEditedImage.preferredHeight);

        Sprite sprite = new Sprite();

        sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
        CardBeingEditedImage.overrideSprite = sprite;
    }
Example #3
0
        /// <summary>
        /// Imports a raster texture
        /// </summary>
        /// <param name="file">The file to import from</param>
        /// <param name="size">The size of the texture</param>
        /// <returns>The raster texture imported as a Sprite</returns>
        public static Sprite ImportRaster(FileInfo file, int size)
        {
            // WWW is obsolete, needs replaced eventually
            WWW       www = new WWW(file.FullName);
            Texture2D tex = www.texture;
            //tex.alphaIsTransparency = true;

            Texture2D newTex = MonoBehaviour.Instantiate(tex);

            TextureScaler.Bilinear(newTex, size, size);

            Sprite sprite = Sprite.Create(newTex, new Rect(Vector2.zero, new Vector2(size, size)), new Vector2(0.5f, 0.5f), size * 2);

            return(sprite);
        }
    private void SetupStyleTransfer()
    {
        setStyle = StyleImage;

        if (input != null)
        {
            input.Dispose();
        }
        if (styleInput != null)
        {
            styleInput.Dispose();
        }
        if (styleOutput != null)
        {
            styleOutput.Release();
        }

        styleImageSrgb = new Texture2D(StyleImage.width, StyleImage.height, TextureFormat.RGBA32, false, false);
        Color[] original       = StyleImage.GetPixels();
        Color[] modifiedColors = new Color[original.Length];
        for (int i = 0; i < original.Length; i++)
        {
            modifiedColors[i] = original[i].linear;
        }
        styleImageSrgb.SetPixels(modifiedColors);
        styleImageSrgb.Apply();
        TextureScaler.Bilinear(styleImageSrgb, styleInsetSize - styleInsetBorderWidth * 2, styleInsetSize - styleInsetBorderWidth * 2);

        ComputeInfo.channelsOrder = ComputeInfo.ChannelsOrder.NCHW;

        PrepareStylePrediction();
        PatchRuntimeWorkerWithStylePrediction();

        whiteBorder = new Texture2D(styleInsetSize, styleInsetSize);
        Color[] colors = new Color[styleInsetSize * styleInsetSize];
        for (int i = 0; i < colors.Length; i++)
        {
            colors[i] = Color.white;
        }
        whiteBorder.SetPixels(colors);
        whiteBorder.Apply();

        styleOutput            = new RenderTexture(setBidirWidth, setBidirHeight, 0, RenderTextureFormat.ARGB32);
        styleOutput.wrapMode   = TextureWrapMode.Clamp;
        styleOutput.filterMode = FilterMode.Bilinear;
    }
Example #5
0
        private void SaveAsPNG()
        {
            // Show save file dialog
                        #if UNITY_EDITOR
            path = UnityEditor.EditorUtility.SaveFilePanel("Export Single Image", "Assets/", fileName, "png");
                        #endif

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            // Pause character animation
            character.speed = 0;
            playToggle.isOn = false;

            // Render and save to file
            Texture2D tex2D = new Texture2D(width * superSampling, height * superSampling, TextureFormat.RGBA32, false);
            RenderTexture.active = renderTexture;
            previewCam.Render();
            tex2D.ReadPixels(new Rect(0, 0, width * superSampling, height * superSampling), 0, 0, false);
            TextureScaler.Bilinear(tex2D, width, height);
            tex2D.Apply();
            byte[] bytes = tex2D.EncodeToPNG();
            File.WriteAllBytes(path, bytes);

            RenderTexture.active = null;
            Object.Destroy(tex2D);

            // Show success dialog box
            Debug.Log("Saved to " + path);
                        #if UNITY_EDITOR
            UnityEditor.EditorUtility.DisplayDialog("Save Successful", "Saved to " + path, "OK");
                        #else
            runtimeDialog.DisplayDialog("Save Successful", "PNG saved to <color=#178294>" + path + "</color> successfully.");
                        #endif
        }
Example #6
0
        private IEnumerator SaveSequence()
        {
            // Show save dialog
                        #if UNITY_EDITOR
            path = UnityEditor.EditorUtility.SaveFilePanel("Export Sequence", "Assets/", fileName, "png");
                        #endif

            if (string.IsNullOrEmpty(path))
            {
                yield break;
            }

            // Split path into base (without extension) and directory path to use when saving sequence
            string basePath = path.Replace(".png", "_");

            // Get animation length
            GetAnimationLength();
            float frameDelay  = 1 / (float)targetFPS;
            float elapsedTime = 0;

            // Pause character animation and reset aim layer to make sure muzzle flash particle is off
            playToggle.isOn = false;
            character.Play("None", 1, 0);
            yield return(null);

            // Iterate and render through each frame of the sequence
            for (int i = 0; i < frameCount; i++)
            {
                yield return(new WaitForSeconds(frameDelay));

                // update progress bar
                float progress = ((float)i + 1) / frameCount;
                                #if UNITY_EDITOR
                UnityEditor.EditorUtility.DisplayProgressBar("Export Sequence", "Exporting frame " + i.ToString() + "/" + frameCount.ToString(), progress);
                                #else
                runtimeDialog.DisplayProgressBar("Export Sequence", "Exporting frame " + i.ToString() + "/" + frameCount.ToString(), progress);
                                #endif

                // Set the character animation and convert animation length into normalized time
                float baseTime = (float)i * frameDelay / baseLength;
                character.Play(selectedAnimation, 0, baseTime);
                character.speed = 0;
                // Only play aim layer once and clamp the animation at the end
                // or keep looping if loop aim layer is set to true
                if (elapsedTime <= aimLength || loopAimLayer)
                {
                    float aimTIme = (float)i * frameDelay / aimLength;
                    character.Play(selectedAim, 1, aimTIme);
                    character.speed = 0;
                }

                // Wait til animator is updated properly
                elapsedTime += frameDelay;
                yield return(null);

                // Render and save the frame
                Texture2D tex2D = new Texture2D(width * superSampling, height * superSampling, TextureFormat.RGBA32, false);
                RenderTexture.active = renderTexture;
                previewCam.Render();
                tex2D.ReadPixels(new Rect(0, 0, width * superSampling, height * superSampling), 0, 0, false);
                TextureScaler.Bilinear(tex2D, width, height);
                tex2D.Apply();
                byte[] bytes = tex2D.EncodeToPNG();

                path = basePath + i.ToString("000") + ".png";
                File.WriteAllBytes(path, bytes);

                RenderTexture.active = null;
                Object.Destroy(tex2D);
            }

            // Show success dialog box
            Debug.Log("PNG sequences saved to " + path);
                        #if UNITY_EDITOR
            UnityEditor.EditorUtility.ClearProgressBar();
            UnityEditor.EditorUtility.DisplayDialog("Save Successful", "PNG sequence saved to " + path, "OK");
                        #else
            runtimeDialog.DisplayDialog("Save Successful", "PNG sequence saved to <color=#178294>" + path + "</color> successfully.");
                        #endif
        }
Example #7
0
        private static void Circle(Texture2D texture, int x, int y, int radius, Color color, bool filled = false, int dashWidth = int.MaxValue, int gapWidth = 0, Texture2D tex = null)
        {
            int       cx          = radius;
            int       cy          = 0;
            int       radiusError = 1 - cx;
            int       dashCnt     = 0;
            Texture2D resizedTex  = null;

            if (tex != null)
            {
#if false
                resizedTex = TextureScaler.scaled(tex, radius * 2, radius * 2);
#endif
#if false
                resizedTex = new Texture2D(tex.width, tex.height);
                Graphics.CopyTexture(tex, resizedTex);
                TextureScaler.Bilinear(resizedTex, radius * 2, radius * 2);
#endif
#if true
                resizedTex = TextureScaler.Scale(tex, radius * 2, radius * 2);
#endif
            }
            while (cx >= cy)
            {
                if (dashCnt >= 0 && dashCnt < dashWidth)
                {
                    if (!filled)
                    {
                        PlotCircle(texture, cx, x, cy, y, color);
                    }
                    else
                    {
                        if (tex == null)
                        {
                            ScanLineCircle(texture, cx, x, cy, y, color);
                        }
                        else
                        {
                            FillLineCircle(texture, cx, x, cy, y, resizedTex);
                        }
                    }
                }
                dashCnt++;
                if (dashCnt == dashWidth)
                {
                    dashCnt = -1 * gapWidth;
                }

                cy++;

                if (radiusError < 0)
                {
                    radiusError += 2 * cy + 1;
                }
                else
                {
                    cx--;
                    radiusError += 2 * (cy - cx + 1);
                }
            }
            if (tex != null)
            {
                UnityEngine.Object.Destroy(resizedTex);
            }
        }