コード例 #1
0
        public int CopyAndGetIndex(Texture texture, RenderTextureReadWrite readWrite)
        {
            if (texture == null)
            {
                return(-1);
            }

            var index = m_textures.IndexOf(texture);

            if (index == -1)
            {
                // ありえない?
                return(-1);
            }

#if UNITY_EDITOR
            if (!string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture)))
            {
                m_exportTextures[index] = texture;
                return(index);
            }
#endif

            // ToDo: may already exists
            m_exportTextures[index] = TextureItem.CopyTexture(texture, readWrite, null);

            return(index);
        }
コード例 #2
0
        public void CreateTextureItems(UnityPath imageBaseDir = default(UnityPath))
        {
            if (m_textures.Any())
            {
                return;
            }

            for (int i = 0; i < GLTF.textures.Count; ++i)
            {
                TextureItem item = null;
#if UNITY_EDITOR
                var image = GLTF.GetImageFromTextureIndex(i);
                if (imageBaseDir.IsUnderAssetsFolder &&
                    !string.IsNullOrEmpty(image.uri) &&
                    !image.uri.StartsWith("data:")
                    )
                {
                    ///
                    /// required SaveTexturesAsPng or SetTextureBaseDir
                    ///
                    var assetPath   = imageBaseDir.Child(image.uri);
                    var textureName = !string.IsNullOrEmpty(image.name) ? image.name : Path.GetFileNameWithoutExtension(image.uri);
                    item = new TextureItem(i, assetPath, textureName);
                }
                else
#endif
                {
                    item = new TextureItem(i);
                }

                AddTexture(item);
            }
        }
コード例 #3
0
        static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
        {
#if UNITY_EDITOR
            var path = UnityPath.FromAsset(texture);
            if (path.IsUnderAssetsFolder)
            {
                if (path.Extension == ".png")
                {
                    return(new BytesWithMime
                    {
                        Bytes = System.IO.File.ReadAllBytes(path.FullPath),
                        Mime = "image/png",
                    });
                }
                if (path.Extension == ".jpg")
                {
                    return(new BytesWithMime
                    {
                        Bytes = System.IO.File.ReadAllBytes(path.FullPath),
                        Mime = "image/jpeg",
                    });
                }
            }
#endif

            return(new BytesWithMime
            {
                Bytes = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
                Mime = "image/png",
            });
        }
コード例 #4
0
        public virtual (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
        {
#if UNITY_EDITOR
            var path = UnityPath.FromAsset(texture);
            if (path.IsUnderAssetsFolder)
            {
                var textureImporter = AssetImporter.GetAtPath(path.Value) as TextureImporter;
                var getSizeMethod   = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
                if (textureImporter != null && getSizeMethod != null)
                {
                    var args = new object[2] {
                        0, 0
                    };
                    getSizeMethod.Invoke(textureImporter, args);
                    var originalWidth  = (int)args[0];
                    var originalHeight = (int)args[1];

                    var originalSize    = Mathf.Max(originalWidth, originalHeight);
                    var requiredMaxSize = textureImporter.maxTextureSize;

                    // Resized exporting if MaxSize setting value is smaller than original image size.
                    if (originalSize > requiredMaxSize)
                    {
                        return
                            (
                            TextureItem.CopyTexture(texture, GetColorSpace(textureType), null).EncodeToPNG(),
                            "image/png"
                            );
                    }
                }

                if (path.Extension == ".png")
                {
                    return
                        (
                        System.IO.File.ReadAllBytes(path.FullPath),
                        "image/png"
                        );
                }
                if (path.Extension == ".jpg")
                {
                    return
                        (
                        System.IO.File.ReadAllBytes(path.FullPath),
                        "image/jpeg"
                        );
                }
            }
#endif

            return
                (
                TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
                "image/png"
                );
        }
コード例 #5
0
        public static Texture2D Convert(Texture2D texture, glTFTextureTypes textureType, ColorConversion colorConversion, Material convertMaterial)
        {
            var copyTexture = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), convertMaterial);

            if (colorConversion != null)
            {
                copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => colorConversion(x)).ToArray());
                copyTexture.Apply();
            }
            copyTexture.name = texture.name;
            return(copyTexture);
        }
コード例 #6
0
        public int CopyAndGetIndex(Texture texture, RenderTextureReadWrite readWrite)
        {
            if (texture == null)
            {
                return(-1);
            }

            var index = m_textures.IndexOf(texture);

            if (index == -1)
            {
                // ありえない?
                return(-1);
            }

            // ToDo: may already exists
            m_exportTextures[index] = TextureItem.CopyTexture(texture, readWrite, null);

            return(index);
        }
コード例 #7
0
        static void Export_Color(Material m, List <Texture> textures, List <Texture> exportTextures, glTFMaterial material)
        {
            if (m.HasProperty("_Color"))
            {
                material.pbrMetallicRoughness.baseColorFactor = m.color.ToArray();
            }

            if (m.HasProperty("_MainTex"))
            {
                var index = textures.IndexOf(m.GetTexture("_MainTex"));
                if (index != -1 && m.mainTexture != null)
                {
                    exportTextures[index] = TextureItem.CopyTexture(m.mainTexture, RenderTextureReadWrite.sRGB, null);
                    material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo()
                    {
                        index = index,
                    };
                }
            }
        }
コード例 #8
0
        static void Export_Emission(Material m, List <Texture> textures, List <Texture> exportTextures, glTFMaterial material)
        {
            if (m.HasProperty("_EmissionColor"))
            {
                var color = m.GetColor("_EmissionColor");
                material.emissiveFactor = new float[] { color.r, color.g, color.b };
            }

            if (m.HasProperty("_EmissionMap"))
            {
                var index = textures.IndexOf(m.GetTexture("_EmissionMap"));
                if (index != -1 && m.HasProperty("_EmissionMap"))
                {
                    exportTextures[index]    = TextureItem.CopyTexture(textures[index], RenderTextureReadWrite.sRGB, null);
                    material.emissiveTexture = new glTFMaterialEmissiveTextureInfo()
                    {
                        index = index,
                    };
                }
            }
        }
コード例 #9
0
ファイル: TextureIO.cs プロジェクト: artv93/UniVRM
 static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
 {
     /*
      #if UNITY_EDITOR
      * var path = UnityPath.FromAsset(texture);
      * if (path.IsUnderAssetsFolder)
      * {
      *  if (path.Extension == ".png")
      *  {
      *      return new BytesWithMime
      *      {
      *          Bytes = System.IO.File.ReadAllBytes(path.FullPath),
      *          Mime = "image/png",
      *      };
      *  }
      * }
      #endif
      */
     return(new BytesWithMime
     {
         Bytes = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
         Mime = "image/png",
     });
 }
コード例 #10
0
 public void AddTexture(TextureItem item)
 {
     m_textures.Add(item);
 }
コード例 #11
0
ファイル: gltfExporter.cs プロジェクト: Phantom100/UniGLTF
 public BytesWithPath(Texture texture)
 {
     Path  = "";
     Bytes = TextureItem.CopyTexture(texture).EncodeToPNG();
     Mime  = "image/png";
 }
コード例 #12
0
        public static void Load(ImporterContext ctx)
        {
            // textures
            if (ctx.GLTF.textures != null)
            {
                for (int i = 0; i < ctx.GLTF.textures.Count; ++i)
                {
                    var item = new TextureItem(ctx.GLTF, i, ctx.TextureBaseDir);
                    ctx.AddTexture(item);
                }
            }
            foreach (var x in ctx.GetTextures())
            {
                x.Process(ctx.GLTF, ctx.Storage);
            }

            // materials
            if (ctx.MaterialImporter == null)
            {
                ctx.MaterialImporter = new MaterialImporter(new ShaderStore(ctx), ctx);
            }

            if (ctx.GLTF.materials == null || !ctx.GLTF.materials.Any())
            {
                // no material
                ctx.AddMaterial(ctx.MaterialImporter.CreateMaterial(0, null));
            }
            else
            {
                for (int i = 0; i < ctx.GLTF.materials.Count; ++i)
                {
                    var index    = i;
                    var material = ctx.MaterialImporter.CreateMaterial(index, ctx.GLTF.materials[i]);
                    ctx.AddMaterial(material);
                }
            }

            // meshes
            if (ctx.GLTF.meshes
                .SelectMany(x => x.primitives)
                .Any(x => x.extensions.KHR_draco_mesh_compression != null))
            {
                throw new UniGLTFNotSupportedException("draco is not supported");
            }

            var meshImporter = new MeshImporter();

            for (int i = 0; i < ctx.GLTF.meshes.Count; ++i)
            {
                var meshContext       = meshImporter.ReadMesh(ctx, i);
                var meshWithMaterials = BuildMesh(ctx, meshContext);

                var mesh = meshWithMaterials.Mesh;

                // mesh name
                if (string.IsNullOrEmpty(mesh.name))
                {
                    mesh.name = string.Format("UniGLTF import#{0}", i);
                }
                var originalName = mesh.name;
                for (int j = 1; ctx.Meshes.Any(x => x.Mesh.name == mesh.name); ++j)
                {
                    mesh.name = string.Format("{0}({1})", originalName, j);
                }

                ctx.Meshes.Add(meshWithMaterials);
            }

            // nodes
            ctx.Nodes.AddRange(ctx.GLTF.nodes.Select(x => ImportNode(x).transform));

            var nodes = ctx.Nodes.Select((x, i) => BuildHierarchy(ctx, i)).ToList();

            gltfImporter.FixCoordinate(ctx, nodes);

            // skinning
            for (int i = 0; i < nodes.Count; ++i)
            {
                gltfImporter.SetupSkinning(ctx, nodes, i);
            }

            // connect root
            ctx.Root = new GameObject("_root_");
            foreach (var x in ctx.GLTF.rootnodes)
            {
                var t = nodes[x].Transform;
                t.SetParent(ctx.Root.transform, false);
            }

            ImportAnimation(ctx);

            //Debug.LogFormat("Import {0}", ctx.Path);
        }
コード例 #13
0
ファイル: TextureIO.cs プロジェクト: heroamir/AvatarResidency
 public BytesWithPath(Texture texture, bool isNormalMap)
 {
     //Path = "";
     Bytes = TextureItem.CopyTexture(texture, isNormalMap).EncodeToPNG();
     Mime  = "image/png";
 }