Esempio n. 1
0
        public GLTFMaterial(GLTFMaterial material, GLTFRoot gltfRoot) : base(material, gltfRoot)
        {
            if (material == null)
            {
                return;
            }

            if (material.PbrMetallicRoughness != null)
            {
                PbrMetallicRoughness = new PbrMetallicRoughness(material.PbrMetallicRoughness, gltfRoot);
            }

            if (material.NormalTexture != null)
            {
                NormalTexture = new NormalTextureInfo(material.NormalTexture, gltfRoot);
            }

            if (material.OcclusionTexture != null)
            {
                OcclusionTexture = new OcclusionTextureInfo(material.OcclusionTexture, gltfRoot);
            }

            if (material.EmissiveTexture != null)
            {
                EmissiveTexture = new TextureInfo(material.EmissiveTexture, gltfRoot);
            }

            EmissiveFactor = material.EmissiveFactor;
            AlphaMode      = material.AlphaMode;
            AlphaCutoff    = material.AlphaCutoff;
            DoubleSided    = material.DoubleSided;
        }
        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 = PbrMetallicRoughness.Deserialize(root, reader);
                    break;

                case "commonConstant":
                    material.CommonConstant = MaterialCommonConstant.Deserialize(root, reader);
                    break;

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

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

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

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

                case "alphaMode":
                    material.AlphaMode = reader.ReadStringEnum <AlphaMode>();
                    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);
        }
Esempio n. 3
0
        public static GLTFRoot Deserialize(TextReader textReader)
        {
            var jsonText = textReader.ReadToEnd();
            var sr       = textReader as StreamReader;

            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            sr.DiscardBufferedData();

            JObject jo = JObject.Parse(jsonText);

            var jsonReader = new JsonTextReader(textReader);
            var root       = new GLTFRoot();

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

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

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

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

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

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

                case "asset":
                    var asset = jo["asset"].ToString();
                    root.Asset = JsonUtility.FromJson <Asset>(asset);
                    // will be deleted
                    Asset.Deserialize(root, jsonReader);
                    break;

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

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

                case "cameras":
                    root.Cameras = new List <GLTFCamera>();
                    foreach (var camera in jo["cameras"])
                    {
                        GLTFCamera c = JsonUtility.FromJson <GLTFCamera>(camera.ToString());
                        root.Cameras.Append(c);
                    }
                    // will be deleted
                    jsonReader.ReadList(() => GLTFCamera.Deserialize(root, jsonReader));
                    break;

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

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

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

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

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

                case "scene":
                    root.Scene = SceneId.Deserialize(root, jsonReader);
                    break;

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

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

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

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

            return(root);
        }
        public static GLTFRoot Deserialize(TextReader textReader)
        {
            var jsonReader = new JsonTextReader(textReader);
            var root       = new GLTFRoot();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                case "scene":
                    root.Scene = SceneId.Deserialize(root, jsonReader);
                    break;

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

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

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

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

            return(root);
        }
Esempio n. 5
0
        private MaterialId ExportMaterial(UnityEngine.Material materialObj)
        {
            MaterialId id = GetMaterialId(_root, materialObj);

            if (id != null)
            {
                return(id);
            }

            var material = new GLTF.Schema.GLTFMaterial();

            if (ExportNames)
            {
                material.Name = materialObj.name;
            }

            if (materialObj.HasProperty("_Cutoff"))
            {
                material.AlphaCutoff = materialObj.GetFloat("_Cutoff");
            }

            switch (materialObj.GetTag("RenderType", false, ""))
            {
            case "TransparentCutout":
                material.AlphaMode = AlphaMode.MASK;
                break;

            case "Transparent":
                material.AlphaMode = AlphaMode.BLEND;
                break;

            default:
                material.AlphaMode = AlphaMode.OPAQUE;
                break;
            }

            material.DoubleSided = materialObj.HasProperty("_Cull") &&
                                   materialObj.GetInt("_Cull") == (float)UnityEngine.Rendering.CullMode.Off;

            if (materialObj.HasProperty("_EmissionColor"))
            {
                material.EmissiveFactor = materialObj.GetColor("_EmissionColor").ToNumericsColor();
            }

            if (materialObj.HasProperty("_EmissionMap"))
            {
                var emissionTex = materialObj.GetTexture("_EmissionMap");

                if (emissionTex != null)
                {
                    material.EmissiveTexture = ExportTextureInfo(emissionTex);

                    ExportTextureTransform(material.EmissiveTexture, materialObj, "_EmissionMap");
                }
            }

            if (materialObj.HasProperty("_BumpMap"))
            {
                var normalTex = materialObj.GetTexture("_BumpMap");

                if (normalTex != null)
                {
                    material.NormalTexture = ExportNormalTextureInfo(normalTex, materialObj);
                    ExportTextureTransform(material.NormalTexture, materialObj, "_BumpMap");
                }
            }

            if (materialObj.HasProperty("_OcclusionMap"))
            {
                var occTex = materialObj.GetTexture("_OcclusionMap");
                if (occTex != null)
                {
                    material.OcclusionTexture = ExportOcclusionTextureInfo(occTex, materialObj);
                    ExportTextureTransform(material.OcclusionTexture, materialObj, "_OcclusionMap");
                }
            }

            switch (materialObj.shader.name)
            {
            case "Standard":
            case "GLTF/GLTFStandard":
                material.PbrMetallicRoughness = ExportPBRMetallicRoughness(materialObj);
                break;

            case "GLTF/GLTFConstant":
                material.CommonConstant = ExportCommonConstant(materialObj);
                break;
            }

            _materials.Add(materialObj);

            id = new MaterialId {
                Id   = _root.Materials.Count,
                Root = _root
            };
            _root.Materials.Add(material);

            return(id);
        }