예제 #1
0
파일: MJImport.cs 프로젝트: WarrG3X/DobotVR
    // import textures
    private unsafe void ImportTextures(int ntexture)
    {
        // allocate array, find existing
        textures = new Texture2D[ntexture];
        Object[] alltextures = Resources.FindObjectsOfTypeAll(typeof(Texture2D));

        // process textures
        for (int i = 0; i < ntexture; i++)
        {
            // get texture name
            StringBuilder name = new StringBuilder(100);
            MJP.GetElementName(MJP.TElement.TEXTURE, i, name, 100);
            string texname = fileName + "_" + name.ToString();

            // get texture descriptor and save
            MJP.TTexture tex;
            MJP.GetTexture(i, &tex);

            // MuJoCo cube texture: use only top piece
            if (tex.cube > 0)
            {
                tex.height = tex.width;
            }

            // find existing texture
            foreach (Object texx in alltextures)
            {
                if (texx.name == texname)
                {
                    textures[i] = (Texture2D)texx;

                    // resize if different
                    if (textures[i].width != tex.width || textures[i].height != tex.height)
                    {
                        textures[i].Resize(tex.width, tex.height);
                    }

                    break;
                }
            }

            // not found: create new texture
            if (textures[i] == null)
            {
                textures[i] = new Texture2D(tex.width, tex.height);
            }

            // copy array
            Color32[] color = new Color32[tex.width * tex.height];
            for (int k = 0; k < tex.width * tex.height; k++)
            {
                color[k].r = tex.rgb[3 * k];
                color[k].g = tex.rgb[3 * k + 1];
                color[k].b = tex.rgb[3 * k + 2];
                color[k].a = 255;
            }

            // load data and apply
            textures[i].SetPixels32(color);
            textures[i].Apply();

            // create asset in database if not aleady there
            if (!AssetDatabase.Contains(textures[i]))
            {
                AssetDatabase.CreateAsset(textures[i], "Assets/Textures/" + texname + ".asset");
            }
        }

        AssetDatabase.Refresh();
    }