Пример #1
0
        public static IEnumerable <(string key, Color value)> TryGetAllColors(glTFMaterial material, VRMC_materials_mtoon mToon)
        {
            const ColorSpace gltfColorSpace = ColorSpace.Linear;

            var baseColor = material?.pbrMetallicRoughness?.baseColorFactor?.ToColor4(gltfColorSpace, ColorSpace.sRGB);

            if (baseColor.HasValue)
            {
                yield return(MToon.Utils.PropColor, baseColor.Value);
            }

            var emissionColor = material?.emissiveFactor?.ToColor3(gltfColorSpace, ColorSpace.Linear);

            if (emissionColor.HasValue)
            {
                yield return(MToon.Utils.PropEmissionColor, emissionColor.Value);
            }

            var shadeColor = mToon?.ShadeColorFactor?.ToColor3(gltfColorSpace, ColorSpace.sRGB);

            if (shadeColor.HasValue)
            {
                yield return(MToon.Utils.PropShadeColor, shadeColor.Value);
            }

            var rimColor = mToon?.ParametricRimColorFactor?.ToColor3(gltfColorSpace, ColorSpace.Linear);

            if (rimColor.HasValue)
            {
                yield return(MToon.Utils.PropRimColor, rimColor.Value);
            }

            var outlineColor = mToon?.OutlineColorFactor?.ToColor3(gltfColorSpace, ColorSpace.sRGB);

            if (outlineColor.HasValue)
            {
                yield return(MToon.Utils.PropOutlineColor, outlineColor.Value);
            }
        }
Пример #2
0
 private static Color ConvertColorSpace(this Color srcColor, ColorSpace srcColorSpace, ColorSpace dstColorSpace)
 {
     // Need pattern matching :(
     if (srcColorSpace == ColorSpace.sRGB && dstColorSpace == ColorSpace.sRGB)
     {
         return(srcColor);
     }
     else if (srcColorSpace == ColorSpace.sRGB && dstColorSpace == ColorSpace.Linear)
     {
         return(srcColor.linear);
     }
     else if (srcColorSpace == ColorSpace.Linear && dstColorSpace == ColorSpace.sRGB)
     {
         return(srcColor.gamma);
     }
     else if (srcColorSpace == ColorSpace.Linear && dstColorSpace == ColorSpace.Linear)
     {
         return(srcColor);
     }
     else
     {
         throw new ArgumentException();
     }
 }
Пример #3
0
 /// <summary>
 /// Texture2D から実際のバイト列を取得するデリゲート。
 ///
 /// textureColorSpace は Texture2D をコピーする際に用いる。
 /// Texture2D 単体では、色空間を知ることができないため。
 /// 一般には、その Texture2D がアサインされる glTF のプロパティの仕様が定める色空間と一致する。
 /// </summary>
 public delegate(byte[] bytes, string mime) GetBytesWithMimeFromTexture2D(Texture2D texture, ColorSpace textureColorSpace);
Пример #4
0
 public (byte[] bytes, string mime) ExportBytesWithMime(Texture2D texture, ColorSpace textureColorSpace)
 {
     if (CanExportAsEditorAssetFile(texture) && TryGetBytesWithMime(texture, out byte[] bytes, out string mime))
Пример #5
0
 static Color ToColor(JsonNode node, ColorSpace srcColorSpace, ColorSpace dstColorSpace)
 {
     return(node.ArrayItems().Select(x => x.GetSingle()).ToArray().ToColor4(srcColorSpace, dstColorSpace));
 }
Пример #6
0
        /// <summary>
        /// gltf に texture を足す
        ///
        /// * textures
        /// * samplers
        /// * images
        /// * bufferViews
        ///
        /// を更新し、textures の index を返す
        ///
        /// </summary>
        /// <param name="gltf"></param>
        /// <param name="bufferIndex"></param>
        /// <param name="texture"></param>
        /// <returns>gltf texture index</returns>
        public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture, ColorSpace textureColorSpace, ITextureSerializer textureSerializer)
        {
            var bytesWithMime = textureSerializer.ExportBytesWithMime(texture, textureColorSpace);

            // add view
            var view      = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE);
            var viewIndex = gltf.AddBufferView(view);

            // add image
            var imageIndex = gltf.images.Count;

            gltf.images.Add(new glTFImage
            {
                name       = TextureImportName.RemoveSuffix(texture.name),
                bufferView = viewIndex,
                mimeType   = bytesWithMime.mime,
            });

            // add sampler
            var samplerIndex = gltf.samplers.Count;
            var sampler      = TextureSamplerUtil.Export(texture);

            gltf.samplers.Add(sampler);

            // add texture
            var textureIndex = gltf.textures.Count;

            gltf.textures.Add(new glTFTexture
            {
                sampler = samplerIndex,
                source  = imageIndex,
            });

            return(textureIndex);
        }
Пример #7
0
        public static float[] ToFloat4(this Color src, ColorSpace srcColorSpace, ColorSpace dstColorSpace)
        {
            var dst = src.ConvertColorSpace(srcColorSpace, dstColorSpace);

            return(new float[] { dst.r, dst.g, dst.b, dst.a });
        }
Пример #8
0
        private static (byte[] bytes, string mime) CopyTextureAndGetBytesWithMime(Texture2D texture, ColorSpace colorSpace)
        {
            var copiedTex = TextureConverter.CopyTexture(texture, colorSpace, null);
            var bytes     = copiedTex.EncodeToPNG();

            if (Application.isPlaying)
            {
                UnityEngine.Object.Destroy(copiedTex);
            }
            else
            {
                UnityEngine.Object.DestroyImmediate(copiedTex);
            }

            return(bytes, "image/png");
        }