static void TrySetTextureTransform(
     Schema.TextureInfo textureInfo,
     UnityEngine.Material material,
     int propertyId
     )
 {
     if (textureInfo.extensions != null && textureInfo.extensions.KHR_texture_transform != null)
     {
         var tt = textureInfo.extensions.KHR_texture_transform;
         if (tt.texCoord != 0)
         {
             Debug.LogError("Multiple UV sets are not supported!");
         }
         if (tt.offset != null)
         {
             material.SetTextureOffset(propertyId, new Vector2(tt.offset[0], 1 - tt.offset[1]));
         }
         if (tt.rotation != 0)
         {
             float cos = Mathf.Cos(tt.rotation);
             float sin = Mathf.Sin(tt.rotation);
             material.SetVector(StandardShaderHelper.mainTexRotatePropId, new Vector4(cos, -sin, sin, cos));
             material.EnableKeyword(StandardShaderHelper.KW_UV_ROTATION);
         }
         if (tt.scale != null)
         {
             material.SetTextureScale(propertyId, new Vector2(tt.scale[0], -tt.scale[1]));
         }
     }
 }
 static bool TrySetTexture(
     Schema.TextureInfo textureInfo,
     UnityEngine.Material material,
     int propertyId,
     Schema.Texture[] textures,
     Texture2D[] images
     )
 {
     if (textureInfo != null && textureInfo.index >= 0)
     {
         int bcTextureIndex = textureInfo.index;
         if (textures != null && textures.Length > bcTextureIndex)
         {
             var txt = textures[bcTextureIndex];
             if (images != null && images.Length > txt.source)
             {
                 material.SetTexture(propertyId, images[txt.source]);
                 TrySetTextureTransform(textureInfo, material, propertyId);
                 return(true);
             }
             else
             {
                 Debug.LogErrorFormat("Image #{0} not found", txt.source);
             }
         }
         else
         {
             Debug.LogErrorFormat("Texture #{0} not found", bcTextureIndex);
         }
     }
     return(false);
 }
        static void TrySetTextureTransform(
            Schema.TextureInfo textureInfo,
            UnityEngine.Material material,
            int propertyId,
            bool flipY = false
            )
        {
            Vector2 offset = Vector2.zero;
            Vector2 scale  = Vector2.one;

            if (textureInfo.extensions != null && textureInfo.extensions.KHR_texture_transform != null)
            {
                var tt = textureInfo.extensions.KHR_texture_transform;
                if (tt.texCoord != 0)
                {
                    Debug.LogError("Multiple UV sets are not supported!");
                }

                float cos = 1;
                float sin = 0;

                if (tt.offset != null)
                {
                    offset.x = tt.offset[0];
                    offset.y = 1 - tt.offset[1];
                }
                if (tt.scale != null)
                {
                    scale.x = tt.scale[0];
                    scale.y = tt.scale[1];
                    material.SetTextureScale(propertyId, scale);
                }
                if (tt.rotation != 0)
                {
                    cos = Mathf.Cos(tt.rotation);
                    sin = Mathf.Sin(tt.rotation);
                    material.SetVector(StandardShaderHelper.mainTexRotatePropId, new Vector4(cos, sin, -sin, cos));
                    material.EnableKeyword(StandardShaderHelper.KW_UV_ROTATION);
                    offset.x += scale.y * sin;
                }
                offset.y -= scale.y * cos;
                material.SetTextureOffset(propertyId, offset);
            }

            if (flipY)
            {
                offset.y = 1 - offset.y;
                scale.y  = -scale.y;
            }

            material.SetTextureOffset(propertyId, offset);
            material.SetTextureScale(propertyId, scale);
        }
        static bool TrySetTexture(
            Schema.TextureInfo textureInfo,
            UnityEngine.Material material,
            int propertyId,
            ref Schema.Texture[] textures,
            ref Schema.Image[] schemaImages,
            ref Dictionary <int, Texture2D>[] imageVariants
            )
        {
            if (textureInfo != null && textureInfo.index >= 0)
            {
                int bcTextureIndex = textureInfo.index;
                if (textures != null && textures.Length > bcTextureIndex)
                {
                    var txt        = textures[bcTextureIndex];
                    var imageIndex = txt.GetImageIndex();

                    Texture2D img = null;
                    if (imageVariants != null &&
                        imageIndex >= 0 &&
                        imageVariants.Length > imageIndex &&
                        imageVariants[imageIndex] != null &&
                        imageVariants[imageIndex].TryGetValue(txt.sampler, out img)
                        )
                    {
                        material.SetTexture(propertyId, img);
                        var isKtx = txt.isKtx;
                        TrySetTextureTransform(textureInfo, material, propertyId, isKtx);
                        return(true);
                    }
                    else
                    {
                        Debug.LogErrorFormat("Image #{0} not found", imageIndex);
                    }
                }
                else
                {
                    Debug.LogErrorFormat("Texture #{0} not found", bcTextureIndex);
                }
            }
            return(false);
        }
 static Texture2D GetTexture(Schema.TextureInfo textureInfo, Schema.Texture[] textures, Texture2D[] images)
 {
     if (textureInfo != null && textureInfo.index >= 0)
     {
         int bcTextureIndex = textureInfo.index;
         if (textures != null && textures.Length > bcTextureIndex)
         {
             var txt = textures[bcTextureIndex];
             if (images != null && images.Length > txt.source)
             {
                 return(images[txt.source]);
             }
             else
             {
                 Debug.LogErrorFormat("Image #{0} not found", txt.source);
             }
         }
         else
         {
             Debug.LogErrorFormat("Texture #{0} not found", bcTextureIndex);
         }
     }
     return(null);
 }
Example #6
0
        static bool TrySetTexture(
            Schema.TextureInfo textureInfo,
            UnityEngine.Material material,
            int propertyId,
            ref Schema.Texture[] textures,
            ref Schema.Image[] schemaImages,
            ref Texture2D[] images
            )
        {
            if (textureInfo != null && textureInfo.index >= 0)
            {
                int bcTextureIndex = textureInfo.index;
                if (textures != null && textures.Length > bcTextureIndex)
                {
                    var txt        = textures[bcTextureIndex];
                    var imageIndex = txt.GetImageIndex();

                    if (images != null && imageIndex >= 0 && images.Length > imageIndex)
                    {
                        material.SetTexture(propertyId, images[imageIndex]);
                        var isKtx = txt.isKtx;
                        TrySetTextureTransform(textureInfo, material, propertyId, isKtx);
                        return(true);
                    }
                    else
                    {
                        Debug.LogErrorFormat("Image #{0} not found", imageIndex);
                    }
                }
                else
                {
                    Debug.LogErrorFormat("Texture #{0} not found", bcTextureIndex);
                }
            }
            return(false);
        }