Esempio n. 1
0
File: Ground.cs Progetto: l-like/pot
    public void Explosion(Vector3 pos)
    {
        //Debug.Log(pos); //pos of attached bullet
        float bulletx = pos[0] * 100 + 960; //magic number (display size), 0-1920
        float bullety = pos[1] * 100;       //0-540

        Texture2D image = Resources.Load("Ground") as Texture2D;

        //Texture2D image = Resources.Load("test") as Texture2D;
        Color[] pixels;
        if (change_image == null)
        {
            pixels = image.GetPixels();
        }
        else
        {
            pixels = change_image.GetPixels();
        }
        Color[] change_pixels = new Color[pixels.Length];
        for (int i = 0; i < pixels.Length; i++)
        {
            int h = i / 1920 - 540;//ground:540, !=depth
            int w = i % 1920;

            Color pixel = pixels[i];
            //Color change_pixel = new Color(pixel.r, pixel.g, pixel.b, pixel.a);
            //change_pixels.SetValue(change_pixel, i);

            if ((bulletx - w) * (bulletx - w) + (bullety - h) * (bullety - h) < 10000)
            {
                Color change_pixel = new Color(pixel.r, pixel.g, pixel.b, 0);
                change_pixels.SetValue(change_pixel, i);
            }
            else
            {
                Color change_pixel = new Color(pixel.r, pixel.g, pixel.b, pixel.a);
                change_pixels.SetValue(change_pixel, i);
            }
        }

        change_image            = new Texture2D(image.width, image.height, TextureFormat.RGBA32, false);
        change_image.filterMode = FilterMode.Point;
        change_image.SetPixels(change_pixels);
        change_image.Apply();

        MainSpriteRenderer.sprite = Sprite.Create(change_image, new Rect(0, 0, image.width, image.height), new Vector2(0.5f, 0.5f));
        Object.Destroy(pc2d);
        pc2d = gameObject.AddComponent(typeof(PolygonCollider2D)) as PolygonCollider2D;
    }
Esempio n. 2
0
    void CreateTriangle()
    {
        //Pure rectangle
        int width  = 65;
        int height = 65;
        //Create rextangle texture
        Texture2D texture2D = new Texture2D(width, height);
        //Rectangle pixels
        int pixelAmount = width * height;
        //The part of clear
        int heightCount = 0;

        //Create array for color
        Color[] changedPixels = new Color[pixelAmount];
        bool    putColor      = false;

        //input color
        for (int i = 0; i < pixelAmount; i++)
        {
            if ((height * heightCount) == i)
            {
                heightCount += 1;
                putColor     = false;
            }
            else if ((height * heightCount) - heightCount == i)
            {
                putColor = true;
            }
            if (putColor)
            {
                changedPixels.SetValue(Color.white, i);
            }
            else
            {
                changedPixels.SetValue(Color.clear, i);
            }
        }
        //Create texture with pixels
        Texture2D newTexture = new Texture2D(texture2D.width, texture2D.height, TextureFormat.RGBA32, false);

        newTexture.filterMode = FilterMode.Point;
        newTexture.SetPixels(changedPixels);
        newTexture.Apply();
        //Create sprite of texture
        Sprite sprite = Sprite.Create(newTexture, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));

        testSprite.sprite = sprite;
    }
Esempio n. 3
0
 public void Fill(Color Color)
 {
     Color[] colors = new Color[texture.Width * texture.Height];
     for (int i = 0; i < colors.Length; i++)
     { colors.SetValue(Color, i); }
     texture.SetData<Color>(colors);
 }
Esempio n. 4
0
        //----------------------------------
        //  Textureの色を明るくする
        //----------------------------------
        #region ChangeTextureBright
        public void ChangeTextureBright(GameObject obj, float bright)
        {
            // エラーが出る場合はTexture設定をInspectorで[Read/Write Enabled]にする必要あり
            Texture2D mainTexture = obj.GetComponent <Renderer>().material.mainTexture as Texture2D;

            Color[] pixels = mainTexture.GetPixels();

            // 書き換え用テクスチャ用配列の作成
            Color[] change_pixels = new Color[pixels.Length];
            for (int i = 0; i < pixels.Length; i++)
            {
                Color pixel = pixels[i];

                // 書き換え用テクスチャのピクセル色を指定
                pixel = Bright(pixel, bright);
                Color change_pixel = new Color(pixel.r, pixel.g, pixel.b, pixel.a);
                change_pixels.SetValue(change_pixel, i);
            }

            // 書き換え用テクスチャの生成
            Texture2D changeTexture = new Texture2D(mainTexture.width, mainTexture.height, TextureFormat.RGBA32, false);

            changeTexture.filterMode = FilterMode.Point;
            changeTexture.SetPixels(change_pixels);
            changeTexture.Apply();

            // テクスチャを貼り替える
            obj.GetComponent <Renderer>().material.mainTexture = changeTexture;
        }
Esempio n. 5
0
    void Start()
    {
        // SpriteTextureの取得
        Texture2D tex = (Texture2D)GetComponent <Image>().sprite.texture;

        // 全てのpixcelを取得して入れる配列
        Color[] pixels = tex.GetPixels();

        // 変換後のpixcelを格納する配列
        Color[] draw_Pixcels = new Color[pixels.Length];
        for (int i = 0; i < pixels.Length; i++)
        {
            Color pixel = list[1];

            if (pixels[i].a == 0.0f)
            {
                Color tmp = new Color(0.0f, 0.0f, 0.0f, 0.0f);
                draw_Pixcels.SetValue(tmp, i);
            }
            else if (pixel == pixels[i])
            {
                // 書き換え用テクスチャのピクセル色を指定
                Color tmp = list[0];
                draw_Pixcels.SetValue(tmp, i);
            }
            else
            {
                Color tmp = list[2];
                draw_Pixcels.SetValue(tmp, i);
            }
        }

        // 書き換え用テクスチャの生成
        Texture2D change_Texture = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false);

        change_Texture.filterMode = FilterMode.Point;
        change_Texture.SetPixels(draw_Pixcels);
        change_Texture.Apply();

        // テクスチャを貼り替える
        GetComponent <Image>().sprite = Sprite.Create(change_Texture, new Rect(0, 0, change_Texture.width, change_Texture.height), Vector2.zero);
    }
Esempio n. 6
0
    void CreateNewTexture(int commandAmount)
    {
        //Pure rectangle
        int width  = 65 * commandAmount;
        int row    = 1 + commandAmount / 7;
        int height = 65 * row;
        //Create rextangle texture
        Texture2D texture2D = new Texture2D(width, height);
        //Rectangle pixels
        int pixelAmount = width * height;
        //Amount of colored pixels in a low
        int clearBaundary = 6 - (commandAmount % 6);

        //Create array for color
        Color[] changedPixels = new Color[pixelAmount];
        //input color
        for (int i = 0; i < pixelAmount; i++)
        {
            if (i >= pixelAmount / row)
            {
                changedPixels.SetValue(Color.white, i);
            }
            else if (width / clearBaundary <= i % width)
            {
                changedPixels.SetValue(Color.clear, i);
            }
            else
            {
                changedPixels.SetValue(Color.white, i);
            }
        }
        //Create texture with pixels
        Texture2D newTexture = new Texture2D(texture2D.width, texture2D.height, TextureFormat.RGBA32, false);

        newTexture.filterMode = FilterMode.Point;
        newTexture.SetPixels(changedPixels);
        newTexture.Apply();
        //Create sprite of texture
        Sprite sprite = Sprite.Create(newTexture, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));

        testSprite.sprite = sprite;
    }
Esempio n. 7
0
        private void AnalysisTexture()
        {
            if (null == texture)
            {
                Debug.Log("texture can't be null,<color=red>please assign texture asset on inspector window.</color>");
                return;
            }

            // TODO: 完善颜色取值的算法
            // TODO: 完善固定尺寸对于平铺、适应、拉伸的情况的算法
            if (fixedResolution)
            {
                int width  = (int)(resolution.x);
                int height = (int)(resolution.y);
                datas  = new float[width, height];
                colors = new Color[width, height];

                int offset       = Mathf.CeilToInt(texture.width / width);
                int offsetHeight = Mathf.CeilToInt(texture.height / height);
                if (offsetHeight > offset)
                {
                    offset = offsetHeight;
                }

                for (int x = 0, pixelX = 0; x < width; x++, pixelX += offset)
                {
                    for (int y = 0, pixelY = 0; y < height; y++, pixelY += offset)
                    {
                        Color pixel  = texture.GetPixel(pixelX, pixelY);
                        float weight = (new Vector3(1 - pixel.r, 1 - pixel.g, 1 - pixel.b) * 2 - Vector3.one).z * 2;
                        colors.SetValue(pixel, x, y);
                        datas.SetValue(weight, x, y);
                    }
                }
            }
            else
            {
                int width  = texture.width;
                int height = texture.height;
                datas  = new float[width, height];
                colors = new Color[width, height];

                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Color pixel  = texture.GetPixel(x, y);
                        float weight = (new Vector3(1 - pixel.r, 1 - pixel.g, 1 - pixel.b) * 2 - Vector3.one).z * 2;
                        colors.SetValue(pixel, x, y);
                        datas.SetValue(weight, x, y);
                    }
                }
            }
        }
Esempio n. 8
0
    // Create the blip textures
    void CreateBlipTexture(Texture2D tex, Color c)
    {
        int size = tex.height * tex.width;

        Color[] cols = new Color[size];
        for (int i = 0; i < size; i++)
        {
            cols.SetValue(c, i);
        }
        tex.SetPixels(cols, 0);
        tex.Apply();
    }
Esempio n. 9
0
    //Get an array of Colours
    public static Color[] GetColourArray( string key )
    {
        Color[] returns = new Color[PlayerPrefs.GetInt("PlayerPrefsArray:Colour:L:"+key)];

           	int i = 0;

        while(i < PlayerPrefs.GetInt("PlayerPrefsArray:Colour:L"+key)){
            returns.SetValue(PlayerPrefsPlus.GetColour("PlayerPrefsArray:Colour:"+key + i.ToString()), i);
            ++i;
        }
        return returns;
    }
Esempio n. 10
0
    //Get an array of Colours
    public static Color[] GetColourArray(string key)
    {
        Color[] returns = new Color[PlayerPrefs.GetInt("PlayerPrefsArray:Colour:L:" + key)];

        int i = 0;

        while (i < PlayerPrefs.GetInt("PlayerPrefsArray:Colour:L:" + key))
        {
            returns.SetValue(PlayerPrefsPlus.GetColour("PlayerPrefsArray:Colour:" + key + i.ToString()), i);
            ++i;
        }
        return(returns);
    }
Esempio n. 11
0
    private Color ColorFromXY(Vector2 v)
    {
        float halfHeight = texture.height / 2f;
        Color color      = Color.HSVToRGB(v.x / texture.width, 1f, 1f);

        if (v.y > halfHeight)
        {
            color.SetSaturation(2f - v.y / halfHeight);
        }
        else
        {
            color.SetValue(v.y / halfHeight);
        }
        return(color);
    }
Esempio n. 12
0
    void GetMaterialPixel()
    {
        Texture2D mainTexture = (Texture2D)GetComponent <Renderer>().material.mainTexture;

        Color[] pixels        = mainTexture.GetPixels();  //配列
        Color[] change_pixels = new Color[pixels.Length]; //書き換え用

        for (int i = 1 + (28 * 28) * n; i < 1 + (28 * 28) * (n + 1); i++)
        {
            i -= 28 * 28 * n;

            int y1 = (i - 1) / 28;
            int x1 = i - (28 * y1 + 1);

            // 18倍した座標
            y1 *= 18;
            x1 *= 18;

            arr = userData.text.Split(',');
            //Debug.Log(i);
            //Debug.Log(float.Parse(arr[t]));   // Debug.Log(arr)とすると、arrのデータ型を教えてくれるだけ(string)
            float c = float.Parse(arr[i - 1]) / 255.0f;
            //Debug.Log(c);

            for (int y = y1; y < y1 + 18; y++)
            {
                for (int x = x1; x < x1 + 18; x++)
                {
                    //512×512の座標 => インデックス
                    int   index        = (512 * y) + x;
                    Color change_pixel = new Color(c, c, c, 1.0f);
                    //Color change_pixel = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                    change_pixels.SetValue(change_pixel, index);
                    //Debug.Log(c);
                }
            }
        }
        // 書き換え用テクスチャの生成3
        Texture2D change_texture = new Texture2D(mainTexture.width, mainTexture.height, TextureFormat.RGBA32, false);

        change_texture.filterMode = FilterMode.Point;
        change_texture.SetPixels(change_pixels);
        change_texture.Apply();
        // テクスチャを貼り替える
        GetComponent <Renderer>().material.mainTexture = change_texture;
    }
Esempio n. 13
0
    void CreateSquare()
    {
        //(Width, height)
        Texture2D texture2D = new Texture2D(128, 128);

        //最初から色を付けたい場合はテクスチャの各ピクセルに色を入れていく
        //単色の場合はあとからカラーを指定して変更することもできる。その場合は以下Sprite.Createまで不要
        int pixelAmount = 128 * 128; //テクスチャの高さや幅はピクセル数を表すので総ピクセルは両辺の積

        Color[] changedPixels = new Color[pixelAmount];
        for (int i = 0; i < pixelAmount; i++)
        {
            changedPixels.SetValue(Color.white, i);
        }

        texture2D.SetPixels(changedPixels);
        texture2D.Apply();

        Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));

        testSprite.sprite = sprite;
    }
    static void objPrefabToAndroid(Object prefab, string androidFolderPath, string androidMatFolder, string androidTexFolder)
    {
        GameObject newObj = Object.Instantiate(prefab) as GameObject;
        newObj.name = newObj.name.Replace("(Clone)", "");

        string newPrePath = androidFolderPath + newObj.name+".prefab";
        newPrePath = newPrePath.Replace(Application.dataPath, "Assets");

        GameObject newPrefab = PrefabUtility.CreatePrefab(newPrePath, newObj);
        GameObject.DestroyImmediate(newObj);

        GameObject test = Object.Instantiate(newPrefab) as GameObject;

        //get all children transforms
        Transform[] TMs = test.GetComponentsInChildren<Transform>();

        Dictionary<string, Material> mats = new Dictionary<string, Material>();
        foreach (Transform t in TMs)
        {
            GameObject obj = t.gameObject;

            //-----------------ͨ¹ýRenderer²éÕÒ²ÄÖÊ
            Renderer ren = obj.GetComponent<MeshRenderer>();
            if (ren == null) ren = obj.GetComponent<SkinnedMeshRenderer>();

            if (ren != null)
            {
                Material[] submats = ren.sharedMaterials;
                foreach (Material mat in submats)
                {
                    if (mat.shader.name == "Unlit/Transparent" || mat.shader.name == "Unlit/Premultiplied Colored" || mat.shader.name == "Transparent/colorTint" || mat.shader.name == "FXMaker/Mask Alpha Blended Tint")
                    {
                        //Debug.Log("obj:" + obj.name);
                        if (!mats.ContainsKey(mat.name) && !File.Exists(androidMatFolder+mat.name+".mat"))
                            mats[mat.name] = Material.Instantiate(mat) as Material;
                    }
                }
            }
        }

        Dictionary<string, Texture2D> texs = new Dictionary<string, Texture2D>();
        string matPath = androidMatFolder.Replace(Application.dataPath, "Assets");
        foreach (Material mat in mats.Values)
        {
            mat.name = mat.name.Replace("(Clone)", "");

            AssetDatabase.CreateAsset(mat, matPath + mat.name + ".mat");
            texs[mat.mainTexture.name] = mat.mainTexture as Texture2D;

            if (mat.shader.name == "FXMaker/Mask Alpha Blended Tint")
            {
                Texture2D maskTex = mat.GetTexture("_Mask") as Texture2D;
                texs[maskTex.name] = maskTex;

                mat.shader = Shader.Find("Transparent/extraAlphaMask");
                mat.SetTexture("_Mask", maskTex);
            }
            else if (mat.shader.name == "FXMaker/Mask Alpha Blended Tint")
            {
                mat.shader = Shader.Find("Transparent/extraAlphaTint");
            }
            else
            {
                mat.shader = Shader.Find("Transparent/extraAlpha");
            }
        }

        foreach (Transform t in TMs)
        {
            GameObject obj = t.gameObject;
			
			Debug.Log("start reset mat:"+obj.name);
			
            Renderer ren = obj.GetComponent<MeshRenderer>();
            if (ren == null) ren = obj.GetComponent<SkinnedMeshRenderer>();

            if (ren != null)
            {
                Material[] submats = ren.sharedMaterials;

                for (int i = 0; i < submats.Length; i++)
                {
                    if (mats.ContainsKey(submats[i].name))
					{
							submats[i] = mats[submats[i].name];
					}else
					{
						if(File.Exists(androidMatFolder+submats[i].name+".mat"))
						{
							string localMatPath = androidMatFolder+submats[i].name+".mat";
							localMatPath = localMatPath.Replace(Application.dataPath,"Assets");
							
							Debug.Log(obj.name+" loadMatAtPath:"+localMatPath);
							submats[i] = AssetDatabase.LoadAssetAtPath(localMatPath,typeof(Material)) as Material;
						}
					}
                }
				
                ren.sharedMaterials = submats;
            }
        }

        newPrefab = PrefabUtility.CreatePrefab(newPrePath, test);
        GameObject.DestroyImmediate(test);

        foreach (Texture2D t in texs.Values)
        {
            //Debug.Log("texture:" + t.name);
			
			string cTexPath = androidTexFolder + t.name + "_c.png";
            string aTexPath = androidTexFolder + t.name + "_a.png";
			
			if(!File.Exists(cTexPath) && !File.Exists(aTexPath))
			{
				string texturePath = AssetDatabase.GetAssetPath(t);
	            TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(texturePath);
	
	            textureImporter.textureType = TextureImporterType.Advanced;
	            textureImporter.isReadable = true;
	            textureImporter.mipmapEnabled = false;
	            AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);
	
	            //Debug.Log("propertives updated");
	
	            Color32[] cols = t.GetPixels32();
	            Color[] cCols = new Color[cols.Length];
	            Color[] aCols = new Color[cols.Length];
	            int id = -1;
	            foreach (Color32 c in cols)
	            {
	                id++;
	
	                cCols.SetValue(new Color((float)c.r / 255, (float)c.g / 255, (float)c.b / 255), id);
	                aCols.SetValue(new Color((float)c.a / 255, (float)c.a / 255, (float)c.a / 255), id);
	            }
	
	            Texture2D colorTexture = new Texture2D(t.width, t.height);
	            colorTexture.SetPixels(cCols);
	            colorTexture.Apply();
	
	            Texture2D maskTexture = new Texture2D(t.width, t.height);
	            maskTexture.SetPixels(aCols);
	            maskTexture.Apply();
	
	            // Debug.Log("pixels done");
	
	            byte[] byt = colorTexture.EncodeToPNG();
	            File.WriteAllBytes(cTexPath, byt);
	
	            byt = maskTexture.EncodeToPNG();
	            File.WriteAllBytes(aTexPath, byt);
	
	            textureImporter.isReadable = false;
	            //Debug.Log("-----------------------texture saved");
	            cTexPath = cTexPath.Replace(Application.dataPath, "Assets");
	            aTexPath = aTexPath.Replace(Application.dataPath, "Assets");
	
	            AssetDatabase.ImportAsset(cTexPath);
	            AssetDatabase.ImportAsset(aTexPath);
			}
            
        }

        foreach (Material mat in mats.Values)
        {
            string oldTexName = mat.mainTexture.name;

            string cTexPath = androidTexFolder + oldTexName + "_c.png";
            string aTexPath = androidTexFolder + oldTexName + "_a.png";

            cTexPath = cTexPath.Replace(Application.dataPath, "Assets");
            aTexPath = aTexPath.Replace(Application.dataPath, "Assets");

            Texture cTex = AssetDatabase.LoadAssetAtPath(cTexPath, typeof(Texture)) as Texture;
            Texture aTex = AssetDatabase.LoadAssetAtPath(aTexPath, typeof(Texture)) as Texture;
            //----------------

            mat.mainTexture = cTex;
            mat.SetTexture("_AlphaMap", aTex);
            //----------------

            List<string> paths = new List<string>();
            paths.Add(cTexPath);
            paths.Add(aTexPath);

            if (mat.shader.name == "Transparent/extraAlphaMask")
            {
                string maskTexName = mat.GetTexture("_Mask").name;

                string dTexPath = androidTexFolder + maskTexName + "_c.png";
                File.Delete(dTexPath);
                string metaPath = dTexPath + ".meta";
                if (File.Exists(metaPath)) File.Delete(metaPath);

                string mTexPath = androidTexFolder + maskTexName + "_a.png";
                mTexPath = mTexPath.Replace(Application.dataPath, "Assets");

                Texture mTex = AssetDatabase.LoadAssetAtPath(mTexPath, typeof(Texture)) as Texture;
                mat.SetTexture("_Mask", mTex);

                paths.Add(mTexPath);
            }

            foreach (string p in paths)
            {
                TextureImporter tImporter = AssetImporter.GetAtPath(p) as TextureImporter;
                tImporter.textureType = TextureImporterType.Advanced;
                tImporter.mipmapEnabled = false;
            }
        }
        System.GC.Collect();
    }
Esempio n. 15
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Size             s;
            GH_ObjectWrapper gobj = null;

            DA.GetData(0, ref gobj);
            DA.GetDataTree(1, out GH_Structure <GH_Number> data);
            DA.GetDataTree(2, out GH_Structure <GH_String> keys);
            DA.GetDataTree(3, out GH_Structure <GH_Colour> clrs);

            #region get size
            if (gobj.Value is GH_Rectangle grec)
            {
                s = new Size((int)grec.Value.X.Length, (int)grec.Value.Y.Length);
            }
            else if (gobj.Value is GH_Vector gvec)
            {
                s = new Size((int)gvec.Value.X, (int)gvec.Value.Y);
            }
            else if (gobj.Value is GH_ComplexNumber gcomp)
            {
                s = new Size((int)gcomp.Value.Real, (int)gcomp.Value.Imaginary);
            }
            else if (gobj.Value is GH_Point gpt)
            {
                s = new Size((int)gpt.Value.X, (int)gpt.Value.Y);
            }
            else if (gobj.Value is GH_Integer gint)
            {
                s = new Size(gint.Value, gint.Value);
            }
            else if (gobj.Value is GH_Number gn)
            {
                s = new Size((int)gn.Value, (int)gn.Value);
            }
            else if (gobj.Value is GH_String gstr)
            {
                string str = gstr.Value;
                if (str.Contains(","))
                {
                    string[] split = str.Split(',');
                    if (split.Length != 2)
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, " cannot parse size input string");
                        return;
                    }
                    else
                    {
                        bool a = int.TryParse(split[0], out int xi);
                        bool b = int.TryParse(split[1], out int yi);
                        if (a && b)
                        {
                            s = new Size(xi, yi);
                        }
                        else
                        {
                            AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, " cannot parse size input string");
                            return;
                        }
                    }
                }
                else if (int.TryParse(str, out int i))
                {
                    s = new Size(i, i);
                }
                else
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, " cannot parse size input string");
                    return;
                }
            }
            else if (gobj.Value is Size etosize)
            {
                s = etosize;
            }
            else
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, " size object not valid\n use a point, integer, vector, complex number or rectangle\n an actual Eto.Drawing.Size object would be better!");
                return;
            }
            #endregion

            Bitmap    bitmap   = new Bitmap(s, PixelFormat.Format32bppRgba);
            Graphics  graphics = new Graphics(bitmap);
            ChartAxis axis     = new ChartAxis(new RectangleF(s), graphics);
            if (s.Height <= 10 || s.Width <= 10)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, " size too small to draw");
                return;
            }

            IEnumerable <double> flatdata = data.FlattenData().Select(i => i.Value);
            double   h_incr  = (s.Height - 10) / flatdata.Max(); // -10 is leaving space for axes
            double[] avg     = new double[data.Branches.Count];
            Color[]  etoclrs = new Color[data.Branches.Count];
            string[] txtkeys = new string[data.Branches.Count];
            for (int bi = 0; bi < data.Branches.Count; bi++)
            {
                try
                {
                    etoclrs.SetValue(clrs.Branches[bi][0].Value.ToEto(), bi);
                    txtkeys.SetValue(keys.Branches[bi][0].Value, bi);
                }
                catch (ArgumentOutOfRangeException)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, " mismatch of data length\n if N has 3 branches each with 5 numbers, K and C must be 3 branches each has one item\n did you forget to graft or duplicate data as necessary?");
                    return;
                }

                double[] nums = data.Branches[bi].Select(i => i.Value).ToArray();
                avg.SetValue(nums.Average(), bi);
                double barwidth = (s.Width - 10) / (double)nums.Length; // -10 leaving left for axis

                PointF[] nodes = new PointF[nums.Length];
                Pen      pen   = new Pen(etoclrs[bi], 2f);
                for (int i = 0; i < nums.Length; i++)
                {
                    double x = (i + 0.5) * barwidth + 10; // +10 moving right, leaving left for axis
                    double h = nums[i] * h_incr;
                    double y = s.Height - 10 + 4 - h;     // -10 moving up leaving space for x axis, additional +4 moving down to avoid top chop off
                    nodes.SetValue(new PointF((float)x, (float)y), i);
                }
                graphics.DrawLines(pen, nodes);
                foreach (PointF p in nodes)
                {
                    graphics.DrawArc(pen, p.X - 4, p.Y - 4, 8f, 8f, 0f, 360f);
                }
            }
            axis.Draw();
            graphics.Flush();

            ImageView graph = new ImageView()
            {
                Image = bitmap,
            };
            ChartData bardata = new ChartData(txtkeys, ChartType.Trend)
            {
                AppdVals = avg, Colors = etoclrs,
            };
            DA.SetData(0, new GH_ObjectWrapper(graph));
            DA.SetData(1, new GH_ObjectWrapper(bardata));
        }
Esempio n. 16
0
    static void objPrefabToAndroid(Object prefab, string androidFolderPath, string androidMatFolder, string androidTexFolder)
    {
        GameObject newObj = Object.Instantiate(prefab) as GameObject;

        newObj.name = newObj.name.Replace("(Clone)", "");

        string newPrePath = androidFolderPath + newObj.name + ".prefab";

        newPrePath = newPrePath.Replace(Application.dataPath, "Assets");

        GameObject newPrefab = PrefabUtility.CreatePrefab(newPrePath, newObj);

        GameObject.DestroyImmediate(newObj);

        GameObject test = Object.Instantiate(newPrefab) as GameObject;

        //get all children transforms
        Transform[] TMs = test.GetComponentsInChildren <Transform>();

        Dictionary <string, Material> mats = new Dictionary <string, Material>();

        foreach (Transform t in TMs)
        {
            GameObject obj = t.gameObject;

            //-----------------ͨ¹ýRenderer²éÕÒ²ÄÖÊ
            Renderer ren = obj.GetComponent <MeshRenderer>();
            if (ren == null)
            {
                ren = obj.GetComponent <SkinnedMeshRenderer>();
            }

            if (ren != null)
            {
                Material[] submats = ren.sharedMaterials;
                foreach (Material mat in submats)
                {
                    if (mat.shader.name == "Unlit/Transparent" || mat.shader.name == "Unlit/Premultiplied Colored" || mat.shader.name == "Transparent/colorTint" || mat.shader.name == "FXMaker/Mask Alpha Blended Tint")
                    {
                        //Debug.Log("obj:" + obj.name);
                        if (!mats.ContainsKey(mat.name) && !File.Exists(androidMatFolder + mat.name + ".mat"))
                        {
                            mats[mat.name] = Material.Instantiate(mat) as Material;
                        }
                    }
                }
            }
        }

        Dictionary <string, Texture2D> texs = new Dictionary <string, Texture2D>();
        string matPath = androidMatFolder.Replace(Application.dataPath, "Assets");

        foreach (Material mat in mats.Values)
        {
            mat.name = mat.name.Replace("(Clone)", "");

            AssetDatabase.CreateAsset(mat, matPath + mat.name + ".mat");
            texs[mat.mainTexture.name] = mat.mainTexture as Texture2D;

            if (mat.shader.name == "FXMaker/Mask Alpha Blended Tint")
            {
                Texture2D maskTex = mat.GetTexture("_Mask") as Texture2D;
                texs[maskTex.name] = maskTex;

                mat.shader = Shader.Find("Transparent/extraAlphaMask");
                mat.SetTexture("_Mask", maskTex);
            }
            else if (mat.shader.name == "FXMaker/Mask Alpha Blended Tint")
            {
                mat.shader = Shader.Find("Transparent/extraAlphaTint");
            }
            else
            {
                mat.shader = Shader.Find("Transparent/extraAlpha");
            }
        }

        foreach (Transform t in TMs)
        {
            GameObject obj = t.gameObject;

            Debug.Log("start reset mat:" + obj.name);

            Renderer ren = obj.GetComponent <MeshRenderer>();
            if (ren == null)
            {
                ren = obj.GetComponent <SkinnedMeshRenderer>();
            }

            if (ren != null)
            {
                Material[] submats = ren.sharedMaterials;

                for (int i = 0; i < submats.Length; i++)
                {
                    if (mats.ContainsKey(submats[i].name))
                    {
                        submats[i] = mats[submats[i].name];
                    }
                    else
                    {
                        if (File.Exists(androidMatFolder + submats[i].name + ".mat"))
                        {
                            string localMatPath = androidMatFolder + submats[i].name + ".mat";
                            localMatPath = localMatPath.Replace(Application.dataPath, "Assets");

                            Debug.Log(obj.name + " loadMatAtPath:" + localMatPath);
                            submats[i] = AssetDatabase.LoadAssetAtPath(localMatPath, typeof(Material)) as Material;
                        }
                    }
                }

                ren.sharedMaterials = submats;
            }
        }

        newPrefab = PrefabUtility.CreatePrefab(newPrePath, test);
        GameObject.DestroyImmediate(test);

        foreach (Texture2D t in texs.Values)
        {
            //Debug.Log("texture:" + t.name);

            string cTexPath = androidTexFolder + t.name + "_c.png";
            string aTexPath = androidTexFolder + t.name + "_a.png";

            if (!File.Exists(cTexPath) && !File.Exists(aTexPath))
            {
                string          texturePath     = AssetDatabase.GetAssetPath(t);
                TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(texturePath);

                textureImporter.textureType   = TextureImporterType.Advanced;
                textureImporter.isReadable    = true;
                textureImporter.mipmapEnabled = false;
                AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);

                //Debug.Log("propertives updated");

                Color32[] cols  = t.GetPixels32();
                Color[]   cCols = new Color[cols.Length];
                Color[]   aCols = new Color[cols.Length];
                int       id    = -1;
                foreach (Color32 c in cols)
                {
                    id++;

                    cCols.SetValue(new Color((float)c.r / 255, (float)c.g / 255, (float)c.b / 255), id);
                    aCols.SetValue(new Color((float)c.a / 255, (float)c.a / 255, (float)c.a / 255), id);
                }

                Texture2D colorTexture = new Texture2D(t.width, t.height);
                colorTexture.SetPixels(cCols);
                colorTexture.Apply();

                Texture2D maskTexture = new Texture2D(t.width, t.height);
                maskTexture.SetPixels(aCols);
                maskTexture.Apply();

                // Debug.Log("pixels done");

                byte[] byt = colorTexture.EncodeToPNG();
                File.WriteAllBytes(cTexPath, byt);

                byt = maskTexture.EncodeToPNG();
                File.WriteAllBytes(aTexPath, byt);

                textureImporter.isReadable = false;
                //Debug.Log("-----------------------texture saved");
                cTexPath = cTexPath.Replace(Application.dataPath, "Assets");
                aTexPath = aTexPath.Replace(Application.dataPath, "Assets");

                AssetDatabase.ImportAsset(cTexPath);
                AssetDatabase.ImportAsset(aTexPath);
            }
        }

        foreach (Material mat in mats.Values)
        {
            string oldTexName = mat.mainTexture.name;

            string cTexPath = androidTexFolder + oldTexName + "_c.png";
            string aTexPath = androidTexFolder + oldTexName + "_a.png";

            cTexPath = cTexPath.Replace(Application.dataPath, "Assets");
            aTexPath = aTexPath.Replace(Application.dataPath, "Assets");

            Texture cTex = AssetDatabase.LoadAssetAtPath(cTexPath, typeof(Texture)) as Texture;
            Texture aTex = AssetDatabase.LoadAssetAtPath(aTexPath, typeof(Texture)) as Texture;
            //----------------

            mat.mainTexture = cTex;
            mat.SetTexture("_AlphaMap", aTex);
            //----------------

            List <string> paths = new List <string>();
            paths.Add(cTexPath);
            paths.Add(aTexPath);

            if (mat.shader.name == "Transparent/extraAlphaMask")
            {
                string maskTexName = mat.GetTexture("_Mask").name;

                string dTexPath = androidTexFolder + maskTexName + "_c.png";
                File.Delete(dTexPath);
                string metaPath = dTexPath + ".meta";
                if (File.Exists(metaPath))
                {
                    File.Delete(metaPath);
                }

                string mTexPath = androidTexFolder + maskTexName + "_a.png";
                mTexPath = mTexPath.Replace(Application.dataPath, "Assets");

                Texture mTex = AssetDatabase.LoadAssetAtPath(mTexPath, typeof(Texture)) as Texture;
                mat.SetTexture("_Mask", mTex);

                paths.Add(mTexPath);
            }

            foreach (string p in paths)
            {
                TextureImporter tImporter = AssetImporter.GetAtPath(p) as TextureImporter;
                tImporter.textureType   = TextureImporterType.Advanced;
                tImporter.mipmapEnabled = false;
            }
        }
        System.GC.Collect();
    }
    void Start()
    {
        // resourcesフォルダの中にあるファイルを取得
        TextAsset text_asset = (TextAsset)Resources.Load("t10k-images-idx3-ubyte");

        // 取得したファイルを元に配列を作成
        byte[] file_data = text_asset.bytes;

        //Debug.Log("file_size_of_images : " + file_data.Length);

        //Debug.Log("IsLittleEndian_of_labels : " + BitConverter.IsLittleEndian);

        // new はインスタンス作成
        byte[] magic_num_byte_array = new byte[4];
        // 新規インスタンスに元となるデータを4つ格納
        Array.Copy(file_data, magic_num_byte_array, 4);

        //Debug.Log("magic_Num_with_bytes" + magic_num_byte_array);

        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(magic_num_byte_array);
        }


        // 4バイトから変換された32ビット符号なし整数を返す
        uint magic_num = BitConverter.ToUInt32(magic_num_byte_array, 0);

        //Debug.Log("magic_number_of_images : " + magic_num);

        byte[] num_of_items_byte_array = new byte[4];
        Array.Copy(file_data, 4, num_of_items_byte_array, 0, 4);

        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(num_of_items_byte_array);
        }

        uint num_of_images = BitConverter.ToUInt32(num_of_items_byte_array, 0);

        //Debug.Log("num_of_images : " + num_of_images);

        //
        //  28×28のとこ
        //
        byte[] num_of_rows_byte_array = new byte[4];
        Array.Copy(file_data, 8, num_of_rows_byte_array, 0, 4);

        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(num_of_rows_byte_array);
        }

        uint num_of_rows = BitConverter.ToUInt32(num_of_rows_byte_array, 0);

        //Debug.Log("num_of_rows : " + num_of_rows);

        byte[] num_of_columns_byte_array = new byte[4];
        Array.Copy(file_data, 12, num_of_columns_byte_array, 0, 4);

        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(num_of_columns_byte_array);
        }

        uint num_of_columns = BitConverter.ToUInt32(num_of_columns_byte_array, 0);
        //Debug.Log("num_of_columns : " + num_of_columns);

        //
        // 画像データ(欲しいやつ)
        //
        string image_str = "";

        for (int i = 16; i < 16 + 28 * 28; ++i)
        {
            image_str += file_data[i].ToString() + ",";
        }

        //Debug.Log("image_str: " + image_str);

        Texture2D mainTexture = (Texture2D)GetComponent <Renderer>().material.mainTexture;

        Color[] pixels = mainTexture.GetPixels();



        //やりたいこと
        Color[] change_pixels = new Color[pixels.Length];

        // 28×28の座標

        int n = 10; // 0なら1枚目、1なら2枚目、2なら3枚目......


        for (int i = 16 + (28 * 28) * n; i < 16 + (28 * 28) * (n + 1); i++)
        {
            int j = i - 15;
            j -= 28 * 28 * n;

            // 座標=>インデックス (28* y1 + 1) + (x)

            // j:1 ~ 1+(28*28)
            // j:1+(28*28) ~ 1+(28*28*2)
            // j:1+(28*28*2) ~ 1+(28*28*3)

            int y1 = (j - 1) / 28;
            int x1 = j - (28 * y1 + 1);

            // 18倍した座標
            y1 *= 18;
            x1 *= 18;

            //Debug.Log("x1,y1,f:" + x1 + "," + y1 + "," + file_data[i]);

            for (int y = y1; y < y1 + 18; y++)
            {
                for (int x = x1; x < x1 + 18; x++)
                {
                    //Debug.Log("(x,y):" + x + "," + y);

                    // 512×512の座標=>インデックス
                    int   index        = (512 * y) + x;
                    float c            = file_data[i] / 255.0f;
                    Color change_pixel = new Color(c, c, c, 1.0f);
                    //Color change_pixel = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                    change_pixels.SetValue(change_pixel, index);
                }
            }
        }

        // 書き換え用テクスチャの生成
        Texture2D change_texture = new Texture2D(mainTexture.width, mainTexture.height, TextureFormat.RGBA32, false);

        change_texture.filterMode = FilterMode.Point;
        change_texture.SetPixels(change_pixels);
        change_texture.Apply();
        // テクスチャを貼り替える
        GetComponent <Renderer>().material.mainTexture = change_texture;
    }
Esempio n. 18
0
        public void Generate()
        {
            // Highly experimental
            // Componer paleta
            Color[] tpalette = new Color[paleta_extra.Length];//[256];
            int i = 0;
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Color color = bitmap.GetPixel(x, y);
                    if (!tpalette.Contains(color))
                    {
                        tpalette.SetValue(color, i);
                        i++;
                    }
                }
            }
            // Pasar la paleta a 555Color
            palette = new _555Color[tpalette.Length];
            for (int j = 0; j < tpalette.Length; j++)
            {
                //palette.SetValue(new _555Color(tpalette[j]), j);
                palette.SetValue(paleta_extra[j], j);
            }

            // Generar los tiles numericos
            int total = width * height;
            total_tiles = (ushort)(total / 64);
            int current = 0;
            int extra_x = 0;
            int extra_y = 0;
            // array con todos los bytes de los tiles
            tiles = new byte[width * height];
            while (current < total)
            {
                for (int y = 0; y < 8; y++)
                {
                    for (int x = 0; x < 8; x++)
                    {
                        Color color = bitmap.GetPixel(x + extra_x, y + extra_y);
                        _555Color color2 = new _555Color(color);
                        for (int j = 0; j < palette.Length; j++)
                        {
                            if (palette[j].Int16 == color2.Int16)
                            {
                                // Guardar la posicion del color
                                tiles.SetValue((byte)j, current);
                                break;
                            }
                            else { tiles.SetValue((byte)0, current); }
                        }
                        current++;
                    }
                }
                extra_x += 8;
                if (extra_x >= width)
                {
                    extra_x = 0;
                    extra_y += 8;
                }
            }
        }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (space_press < 7)
            {
                vertices = mesh.vertices;

                for (int i = 0; i < rise_ver [space_press].Length; i++)
                {
                    vertices [rise_ver [space_press] [i]].z = 1;
                }
                mesh.vertices = vertices;
                space_press++;
            }
            else
            {
                vertices2 = vertices;
                //the first four cornors
                for (int t = 0; t < rise_ver [0].Length; t++)
                {
                    vertices2 [rise_ver [0] [t]].z = Random.Range(-1f, 1f);
                }
                //diamond step

                vertices2[rise_ver[1][0]].z = (vertices2[0].z + vertices2[8].z + vertices2[72].z + vertices2[80].z) / 4f + Random.Range(-1f, 1f) / 2f;
                //square step
                for (int t = 0; t < rise_ver [2].Length; t++)
                {
                    int id = rise_ver [2] [t];
                    if (t % 3f == 0)
                    {
                        vertices2 [id].z = (vertices2 [id - 4].z + vertices2 [id + 4].z) / 2f + Random.Range(-1f, 1f) / 2f;
                    }
                    else
                    {
                        vertices2 [id].z = (vertices2 [id - 36].z + vertices2 [id + 36].z) / 2f + Random.Range(-1f, 1f) / 2f;
                    }
                }

                //diomond step
                for (int t = 0; t < rise_ver [3].Length; t++)
                {
                    int id = rise_ver [3] [t];
                    vertices2 [id].z = (vertices2 [id - 20].z + vertices2 [id - 16].z + vertices2 [id + 16].z + vertices2 [id + 20].z) / 4f + Random.Range(-1f, 1f) / 4f;
                }
                //square step
                for (int t = 0; t < rise_ver [4].Length; t++)
                {
                    int id = rise_ver [4] [t];
                    if (t % 5f < 2f)
                    {
                        vertices2 [id].z = (vertices2 [id - 2].z + vertices2 [id + 2].z) / 2f + Random.Range(-1f, 1f) / 4f;
                    }
                    else
                    {
                        vertices2 [id].z = (vertices2 [id - 18].z + vertices2 [id + 18].z) / 2f + Random.Range(-1f, 1f) / 4f;
                    }
                }

                //diamond step
                for (int t = 0; t < rise_ver [5].Length; t++)
                {
                    int id = rise_ver [5] [t];
                    vertices2 [id].z = (vertices2 [id - 10].z + vertices2 [id - 8].z + vertices2 [id + 8].z + vertices2 [id + 10].z) / 4f + Random.Range(-1f, 1f) / 8f;
                }
                //square step
                for (int t = 0; t < rise_ver [6].Length; t++)
                {
                    int id = rise_ver [6] [t];
                    if (t % 9f < 4)
                    {
                        vertices2 [id].z = (vertices2 [id - 1].z + vertices2 [id + 1].z) / 2f + Random.Range(-1f, 1f) / 8f;
                    }
                    else
                    {
                        vertices2 [id].z = (vertices2 [id - 9].z + vertices2 [id + 9].z) / 2f + Random.Range(-1f, 1f) / 8f;
                    }
                }

                mesh.vertices = vertices2;

                Color[] colors = new Color[vertices2.Length];

                for (int t = 0; t < vertices2.Length; t++)
                {
                    if (vertices2 [t].z < 0.4f)
                    {
                        colors.SetValue(new Color(0, 0.8f + 0.2f * vertices2 [t].z / 0.4f, 0), t);
                    }
                    else if (vertices2 [t].z < 0.6f)
                    {
                        colors.SetValue(new Color(0.56f * (vertices2 [t].z - 0.4f) / 0.2f, 0.27f + (0.6f - vertices2 [t].z) / 0.2f, 0.16f * (vertices2 [t].z - 0.4f) / 0.2f), t);
                    }
                    else
                    {
                        colors.SetValue(new Color(0.56f + 0.44f * (vertices2 [t].z - 0.6f), 0.27f + 0.73f * (vertices2 [t].z - 0.6f), 0.16f + 0.84f * (vertices2 [t].z - 0.6f)), t);
                    }
                }

                mesh.colors = colors;
            }
        }
    }
    public void DrawMNIST()
    {
        // data import
        byte[] file_data = DataImport();

        Texture2D mainTexture = (Texture2D)GetComponent <Renderer>().material.mainTexture;

        Color[] pixels = mainTexture.GetPixels();

        Color[] change_pixels = new Color[pixels.Length];

        // 28×28の座標

        //int n = 10; // 0なら1枚目、1なら2枚目、2なら3枚目......


        for (int i = 16 + (28 * 28) * n; i < 16 + (28 * 28) * (n + 1); i++)
        {
            int j = i - 15;
            j -= 28 * 28 * n;

            // 座標=>インデックス (28* y1 + 1) + (x)

            // j:1 ~ 1+(28*28)
            // j:1+(28*28) ~ 1+(28*28*2)
            // j:1+(28*28*2) ~ 1+(28*28*3)

            int y1 = (j - 1) / 28;
            int x1 = j - (28 * y1 + 1);

            // 18倍した座標
            y1 *= 18;
            x1 *= 18;

            //Debug.Log("x1,y1,f:" + x1 + "," + y1 + "," + file_data[i]);

            for (int y = y1; y < y1 + 18; y++)
            {
                for (int x = x1; x < x1 + 18; x++)
                {
                    //Debug.Log("(x,y):" + x + "," + y);

                    // 512×512の座標=>インデックス
                    int   index        = (512 * y) + x;
                    float c            = file_data[i] / 255.0f;
                    Color change_pixel = new Color(c, c, c, 1.0f);
                    //Color change_pixel = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                    change_pixels.SetValue(change_pixel, index);
                }
            }
        }
        // 書き換え用テクスチャの生成
        Texture2D change_texture = new Texture2D(mainTexture.width, mainTexture.height, TextureFormat.RGBA32, false);

        change_texture.filterMode = FilterMode.Point;
        change_texture.SetPixels(change_pixels);
        change_texture.Apply();
        // テクスチャを貼り替える
        GetComponent <Renderer>().material.mainTexture = change_texture;

        //// 子供のGameObjectを取得
        //for (int ch = 0; )
        //{
        //    // テクスチャを貼り替える
        //    GameObject c;

        //    c.GetComponent<Renderer>().material.mainTexture = change_texture;


        //}
    }