コード例 #1
0
        public static GLTFMaterial Deserialize(GLTFRoot root, JsonReader reader)
        {
            var material = new GLTFMaterial();

            while (reader.Read() && reader.TokenType == JsonToken.PropertyName)
            {
                var curProp = reader.Value.ToString();

                switch (curProp)
                {
                case "pbrMetallicRoughness":
                    material.PbrMetallicRoughness = GLTFPBRMetallicRoughness.Deserialize(root, reader);
                    break;

                case "normalTexture":
                    material.NormalTexture = GLTFNormalTextureInfo.Deserialize(root, reader);
                    break;

                case "occlusionTexture":
                    material.OcclusionTexture = GLTFOcclusionTextureInfo.Deserialize(root, reader);
                    break;

                case "emissiveTexture":
                    material.EmissiveTexture = GLTFTextureInfo.Deserialize(root, reader);
                    break;

                case "emissiveFactor":
                    material.EmissiveFactor = reader.ReadAsRGBColor();
                    break;

                case "alphaMode":
                    material.AlphaMode = reader.ReadStringEnum <GLTFAlphaMode>();
                    break;

                case "alphaCutoff":
                    material.AlphaCutoff = reader.ReadAsDouble().Value;
                    break;

                case "doubleSided":
                    material.DoubleSided = reader.ReadAsBoolean().Value;
                    break;

                default:
                    material.DefaultPropertyDeserializer(root, reader);
                    break;
                }
            }

            return(material);
        }
コード例 #2
0
        private static void MergeMaterialsImagesTexturesAndSamplers(GLTFRoot mergeToRoot, GLTFRoot mergeFromRoot, PreviousGLTFSizes previousGLTFSizes)
        {
            if (mergeFromRoot.Samplers != null)
            {
                if (mergeToRoot.Samplers == null)
                {
                    mergeToRoot.Samplers = new List <Sampler>(mergeFromRoot.Samplers.Count);
                }

                mergeToRoot.Samplers.AddRange(mergeFromRoot.Samplers);
            }

            if (mergeFromRoot.Images != null)
            {
                if (mergeToRoot.Images == null)
                {
                    mergeToRoot.Images = new List <GLTFImage>(mergeFromRoot.Images.Count);
                }

                mergeToRoot.Images.AddRange(mergeFromRoot.Images);
                for (int i = previousGLTFSizes.PreviousImageCount; i < mergeToRoot.Images.Count; ++i)
                {
                    GLTFImage image = mergeToRoot.Images[i];
                    if (image.BufferView != null)
                    {
                        BufferViewId bufferViewId = image.BufferView;
                        bufferViewId.Id  += previousGLTFSizes.PreviousBufferViewCount;
                        bufferViewId.Root = mergeToRoot;
                    }
                }
            }

            if (mergeFromRoot.Textures != null)
            {
                if (mergeToRoot.Textures == null)
                {
                    mergeToRoot.Textures = new List <GLTFTexture>(mergeFromRoot.Textures.Count);
                }

                mergeToRoot.Textures.AddRange(mergeFromRoot.Textures);
                for (int i = previousGLTFSizes.PreviousTextureCount; i < mergeToRoot.Textures.Count; ++i)
                {
                    GLTFTexture texture = mergeToRoot.Textures[i];

                    if (texture.Sampler != null)
                    {
                        SamplerId samplerId = texture.Sampler;
                        samplerId.Id  += previousGLTFSizes.PreviousSamplerCount;
                        samplerId.Root = mergeToRoot;
                    }

                    if (texture.Source != null)
                    {
                        ImageId samplerId = texture.Source;
                        samplerId.Id  += previousGLTFSizes.PreviousImageCount;
                        samplerId.Root = mergeToRoot;
                    }
                }
            }

            if (mergeFromRoot.Materials != null)
            {
                if (mergeToRoot.Materials == null)
                {
                    mergeToRoot.Materials = new List <GLTFMaterial>(mergeFromRoot.Materials.Count);
                }

                mergeToRoot.Materials.AddRange(mergeFromRoot.Materials);
                for (int i = previousGLTFSizes.PreviousMaterialCount; i < mergeToRoot.Materials.Count; ++i)
                {
                    GLTFMaterial material = mergeToRoot.Materials[i];

                    PbrMetallicRoughness pbrMetallicRoughness = material.PbrMetallicRoughness;
                    if (pbrMetallicRoughness != null)
                    {
                        if (pbrMetallicRoughness.BaseColorTexture != null)
                        {
                            TextureId textureId = pbrMetallicRoughness.BaseColorTexture.Index;
                            textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                            textureId.Root = mergeToRoot;
                        }
                        if (pbrMetallicRoughness.MetallicRoughnessTexture != null)
                        {
                            TextureId textureId = pbrMetallicRoughness.MetallicRoughnessTexture.Index;
                            textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                            textureId.Root = mergeToRoot;
                        }
                    }

                    MaterialCommonConstant commonConstant = material.CommonConstant;
                    if (commonConstant?.LightmapTexture != null)
                    {
                        TextureId textureId = material.CommonConstant.LightmapTexture.Index;
                        textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                        textureId.Root = mergeToRoot;
                    }

                    if (material.EmissiveTexture != null)
                    {
                        TextureId textureId = material.EmissiveTexture.Index;
                        material.EmissiveTexture.Index.Id += previousGLTFSizes.PreviousTextureCount;
                        textureId.Root = mergeToRoot;
                    }

                    if (material.NormalTexture != null)
                    {
                        TextureId textureId = material.NormalTexture.Index;
                        textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                        textureId.Root = mergeToRoot;
                    }

                    if (material.OcclusionTexture != null)
                    {
                        TextureId textureId = material.OcclusionTexture.Index;
                        textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                        textureId.Root = mergeToRoot;
                    }
                }
            }
        }
コード例 #3
0
        private Material CreateMaterial(GLTFMaterial def, bool useVertexColors)
        {
            Shader shader = _standardShader;

            shader.maximumLOD = MaximumLod;

            var material = new Material(shader);

            if (def.AlphaMode == GLTFAlphaMode.MASK)
            {
                material.SetOverrideTag("RenderType", "TransparentCutout");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.EnableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
                material.SetFloat("_Cutoff", (float)def.AlphaCutoff);
            }
            else if (def.AlphaMode == GLTFAlphaMode.BLEND)
            {
                material.SetOverrideTag("RenderType", "Transparent");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                material.SetInt("_ZWrite", 0);
                material.DisableKeyword("_ALPHATEST_ON");
                material.EnableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
            }
            else
            {
                material.SetOverrideTag("RenderType", "");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.DisableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = -1;
            }

            if (def.DoubleSided)
            {
                material.SetInt("_Cull", (int)CullMode.Off);
            }
            else
            {
                material.SetInt("_Cull", (int)CullMode.Back);
            }



            if (useVertexColors)
            {
                material.EnableKeyword("VERTEX_COLOR_ON");
            }

            if (def.PbrMetallicRoughness != null)
            {
                var pbr = def.PbrMetallicRoughness;

                material.SetColor("_Color", pbr.BaseColorFactor);

                if (pbr.BaseColorTexture != null)
                {
                    var texture = pbr.BaseColorTexture.Index.Value;
                    material.SetTexture("_MainTex", _imageCache[texture.Source.Value]);
                }

                material.SetFloat("_Metallic", (float)pbr.MetallicFactor);

                if (pbr.MetallicRoughnessTexture != null)
                {
                    var texture = pbr.MetallicRoughnessTexture.Index.Value;
                    material.SetTexture("_MetallicRoughnessMap", _imageCache[texture.Source.Value]);
                }

                material.SetFloat("_Roughness", (float)pbr.RoughnessFactor);
            }

            if (def.NormalTexture != null)
            {
                var texture = def.NormalTexture.Index.Value;
                material.SetTexture("_BumpMap", _imageCache[texture.Source.Value]);
                material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale);
            }

            if (def.OcclusionTexture != null)
            {
                var texture = def.OcclusionTexture.Index;

                material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength);

                if (def.PbrMetallicRoughness != null &&
                    def.PbrMetallicRoughness.MetallicRoughnessTexture.Index.Id == texture.Id)
                {
                    material.EnableKeyword("OCC_METAL_ROUGH_ON");
                }
                else
                {
                    material.SetTexture("_OcclusionMap", _imageCache[texture.Value.Source.Value]);
                }
            }

            if (def.EmissiveTexture != null)
            {
                var texture = def.EmissiveTexture.Index.Value;
                material.SetTexture("_EmissionMap", _imageCache[texture.Source.Value]);
            }

            material.SetColor("_EmissionColor", def.EmissiveFactor);

            return(material);
        }
コード例 #4
0
ファイル: GLTFRoot.cs プロジェクト: seacloud9/UnityGLTFLoader
        public static GLTFRoot Deserialize(JsonReader reader)
        {
            var root = new GLTFRoot();

            if (reader.Read() && reader.TokenType != JsonToken.StartObject)
            {
                throw new Exception("gltf json must be an object");
            }

            while (reader.Read() && reader.TokenType == JsonToken.PropertyName)
            {
                var curProp = reader.Value.ToString();

                switch (curProp)
                {
                case "extensionsUsed":
                    root.ExtensionsUsed = reader.ReadStringList();
                    break;

                case "extensionsRequired":
                    root.ExtensionsRequired = reader.ReadStringList();
                    break;

                case "accessors":
                    root.Accessors = reader.ReadList(() => GLTFAccessor.Deserialize(root, reader));
                    break;

                case "animations":
                    root.Animations = reader.ReadList(() => GLTFAnimation.Deserialize(root, reader));
                    break;

                case "asset":
                    root.Asset = GLTFAsset.Deserialize(root, reader);
                    break;

                case "buffers":
                    root.Buffers = reader.ReadList(() => GLTFBuffer.Deserialize(root, reader));
                    break;

                case "bufferViews":
                    root.BufferViews = reader.ReadList(() => GLTFBufferView.Deserialize(root, reader));
                    break;

                case "cameras":
                    root.Cameras = reader.ReadList(() => GLTFCamera.Deserialize(root, reader));
                    break;

                case "images":
                    root.Images = reader.ReadList(() => GLTFImage.Deserialize(root, reader));
                    break;

                case "materials":
                    root.Materials = reader.ReadList(() => GLTFMaterial.Deserialize(root, reader));
                    break;

                case "meshes":
                    root.Meshes = reader.ReadList(() => GLTFMesh.Deserialize(root, reader));
                    break;

                case "nodes":
                    root.Nodes = reader.ReadList(() => GLTFNode.Deserialize(root, reader));
                    break;

                case "samplers":
                    root.Samplers = reader.ReadList(() => GLTFSampler.Deserialize(root, reader));
                    break;

                case "scene":
                    root.Scene = GLTFSceneId.Deserialize(root, reader);
                    break;

                case "scenes":
                    root.Scenes = reader.ReadList(() => GLTFScene.Deserialize(root, reader));
                    break;

                case "skins":
                    root.Skins = reader.ReadList(() => GLTFSkin.Deserialize(root, reader));
                    break;

                case "textures":
                    root.Textures = reader.ReadList(() => GLTFTexture.Deserialize(root, reader));
                    break;

                default:
                    root.DefaultPropertyDeserializer(root, reader);
                    break;
                }
            }

            return(root);
        }