Jump to content
Unity Insider Forum

Texturen Scalieren/ (Resize)


frkx316

Recommended Posts

hallo erstmal,

ich komm irgendwie nicht weiter.

Ich habe im Internet ein Script gefunden, wobei ich versucht habe es für Unity umzuschreiben. 

Bei dem Script handelt es sich um ein Programm mit dem man die Größe einer Textur ändern kann.

Das Problem ist das es mir keine Textur zurückgibt und dabei trotzdem keine Fehlermeldung ausspuckt.

Deswegen wäre es super von euch wenn ihr vielleicht ein Auge darauf werfen könntet!!!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Resize : MonoBehaviour {

    public Texture2D temp;

    public int newWidth;
    public int newHeight;
    private Texture bmap;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            
            

           
                if (newWidth != 0 && newHeight != 0)
                {
                    
                    Texture2D bmap = new Texture2D(newWidth, newHeight);

                    double nWidthFactor = (double)temp.width / (double)newWidth;
                    double nHeightFactor = (double)temp.height / (double)newHeight;

                    double fx, fy, nx, ny;
                    int cx, cy, fr_x, fr_y;
                    Color color1 = new Color();
                    Color color2 = new Color();
                    Color color3 = new Color();
                    Color color4 = new Color();
                    Color z;
                    

                    byte bp1, bp2;

                    for (int x = 0; x < bmap.width; ++x)
                    {
                        for (int y = 0; y < bmap.height; ++y)
                        {

                            fr_x = (int)(x * nWidthFactor);
                            fr_y = (int)(y * nHeightFactor);
                            cx = fr_x + 1;
                            if (cx >= temp.width) cx = fr_x;
                            cy = fr_y + 1;
                            if (cy >= temp.height) cy = fr_y;
                            fx = x * nWidthFactor - fr_x;
                            fy = y * nHeightFactor - fr_y;
                            nx = 1.0 - fx;
                            ny = 1.0 - fy;

                            color1 = temp.GetPixel(fr_x, fr_y);
                            color2 = temp.GetPixel(cx, fr_y);
                            color3 = temp.GetPixel(fr_x, cy);
                            color4 = temp.GetPixel(cx, cy);

                            // Blue
                            bp1 = (byte)(nx * color1.b + fx * color2.b);

                            bp2 = (byte)(nx * color3.b + fx * color4.b);

                            z.b = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                            // Green
                            bp1 = (byte)(nx * color1.g + fx * color2.g);

                            bp2 = (byte)(nx * color3.g + fx * color4.g);

                            z.g = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                            // Red
                            bp1 = (byte)(nx * color1.r + fx * color2.r);

                            bp2 = (byte)(nx * color3.r + fx * color4.r);

                            z.r= (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                            z.a = 1.0f;

                            bmap.SetPixel(x, y, z);
                    
                        }
                    }
                    bmap.Apply();
                }
           


            

            GetComponent<Renderer>().material.mainTexture = bmap;
        }
    }
}

Da im vorgeschriebenem Script unter anderem Ziemlich wirre namen verwendet werden ist es leider schwierig der Logic zu folgen.

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...