コード例 #1
0
 public static (SubAssetKey, TextureDescriptor) NormalTexture(GltfData data, glTFMaterial src)
 {
     var(offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.normalTexture);
     return(GltfTextureImporter.CreateNormal(data, src.normalTexture.index, offset, scale));
 }
コード例 #2
0
        public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc)
        {
            if (i < 0 || i >= data.GLTF.materials.Count)
            {
                matDesc = default;
                return(false);
            }

            var src = data.GLTF.materials[i];

            if (!glTF_KHR_materials_unlit.IsEnable(src))
            {
                matDesc = default;
                return(false);
            }

            matDesc = new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, src), ShaderName);

            // texture
            if (src.pbrMetallicRoughness.baseColorTexture != null)
            {
                var(offset, scale)     = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
                var(key, textureParam) = GltfTextureImporter.CreateSRGB(data, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale);
                matDesc.TextureSlots.Add("_MainTex", textureParam);
            }

            // color
            if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4)
            {
                matDesc.Colors.Add("_Color",
                                   src.pbrMetallicRoughness.baseColorFactor.ToColor4(ColorSpace.Linear, ColorSpace.sRGB)
                                   );
            }

            //renderMode
            matDesc.Actions.Add(material =>
            {
                if (src.alphaMode == "OPAQUE")
                {
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
                }
                else if (src.alphaMode == "BLEND")
                {
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent);
                }
                else if (src.alphaMode == "MASK")
                {
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout);
                    material.SetFloat("_Cutoff", src.alphaCutoff);
                }
                else
                {
                    // default OPAQUE
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
                }

                // culling
                if (src.doubleSided)
                {
                    UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off);
                }
                else
                {
                    UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Back);
                }

                // VColor
                var hasVertexColor = data.GLTF.MaterialHasVertexColor(i);
                if (hasVertexColor)
                {
                    UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply);
                }

                UniUnlit.Utils.ValidateProperties(material, true);
            });

            return(true);
        }
コード例 #3
0
 public static (SubAssetKey, TextureDescriptor) BaseColorTexture(GltfData data, glTFMaterial src)
 {
     var(offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
     return(GltfTextureImporter.CreateSrgb(data, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale));
 }
コード例 #4
0
        public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc)
        {
            if (i < 0 || i >= data.GLTF.materials.Count)
            {
                matDesc = default;
                return(false);
            }

            var src = data.GLTF.materials[i];

            if (!glTF_KHR_materials_unlit.IsEnable(src))
            {
                matDesc = default;
                return(false);
            }

            var textureSlots = new Dictionary <string, TextureDescriptor>();
            var colors       =
                src.pbrMetallicRoughness.baseColorFactor != null &&
                src.pbrMetallicRoughness.baseColorFactor.Length == 4
                    ? new Dictionary <string, Color>
            {
                {
                    "_Color",
                    src.pbrMetallicRoughness.baseColorFactor.ToColor4(ColorSpace.Linear, ColorSpace.sRGB)
                }
            }
                    : new Dictionary <string, Color>();

            // texture
            if (src.pbrMetallicRoughness.baseColorTexture != null)
            {
                var(offset, scale) =
                    GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
                var(key, textureParam) = GltfTextureImporter.CreateSrgb(data,
                                                                        src.pbrMetallicRoughness.baseColorTexture.index, offset, scale);
                textureSlots.Add("_MainTex", textureParam);
            }

            matDesc = new MaterialDescriptor(
                GltfMaterialDescriptorGenerator.GetMaterialName(i, src),
                UniUnlitUtil.ShaderName,
                null,
                textureSlots,
                new Dictionary <string, float>(),
                colors,
                new Dictionary <string, Vector4>(),
                new Action <Material>[]
            {
                //renderMode
                material =>
                {
                    switch (src.alphaMode)
                    {
                    case "OPAQUE":
                        UniUnlitUtil.SetRenderMode(material, UniUnlitRenderMode.Opaque);
                        break;

                    case "BLEND":
                        UniUnlitUtil.SetRenderMode(material, UniUnlitRenderMode.Transparent);
                        break;

                    case "MASK":
                        UniUnlitUtil.SetRenderMode(material, UniUnlitRenderMode.Cutout);
                        material.SetFloat(Cutoff, src.alphaCutoff);
                        break;

                    default:
                        // default OPAQUE
                        UniUnlitUtil.SetRenderMode(material, UniUnlitRenderMode.Opaque);
                        break;
                    }

                    // culling
                    if (src.doubleSided)
                    {
                        UniUnlitUtil.SetCullMode(material, UniUnlitCullMode.Off);
                    }
                    else
                    {
                        UniUnlitUtil.SetCullMode(material, UniUnlitCullMode.Back);
                    }

                    // VColor
                    var hasVertexColor = data.MaterialHasVertexColor(i);
                    if (hasVertexColor)
                    {
                        UniUnlitUtil.SetVColBlendMode(material, UniUnlitVertexColorBlendOp.Multiply);
                    }

                    UniUnlitUtil.ValidateProperties(material, true);
                }
            }
                );

            return(true);
        }