예제 #1
0
        public override Extension Deserialize(GLTFRoot root, JProperty extensionToken)
        {
            var extension = new KHR_lights_punctualExtension();

            if (extensionToken == null)
            {
                return(null);
            }

            extension.isGlobal = extensionToken.Value["lights"] != null;

            if (extension.isGlobal)
            {
                foreach (var light in extensionToken.Value["lights"])
                {
                    var l = new KHR_lights_punctualExtension.Light();

                    var type = light.Value <string>("type");
                    l.name      = light.Value <string>("name");
                    l.intensity = light.Value <float>("intensity");
                    var c = light.Value <JArray>("color");
                    l.color = new Color((float)c[0], (float)c[1], (float)c[2]);

                    if (type == "directional")
                    {
                        l.type = LightType.Directional;
                    }
                    else if (type == "point")
                    {
                        l.type  = LightType.Point;
                        l.range = light.Value <float>("range");
                    }
                    else if (type == "spot")
                    {
                        l.type           = LightType.Spot;
                        l.range          = light.Value <float>("range");
                        l.innerConeAngle = light.Value <float>("innerConeAngle");
                        l.outerConeAngle = light.Value <float>("outerConeAngle");
                    }

                    extension.lights.Add(l);
                }
            }
            else
            {
                extension.lightIndex = (int)extensionToken.Value["light"];
            }

            return(extension);
        }
예제 #2
0
        public override void Serialize(ExporterEntry entry, Dictionary <string, Extension> extensions, UnityEngine.Object component = null)
        {
            var light = component as Light;

            if (ExporterSettings.Lighting.lightMap && light.bakingOutput.isBaked)
            {
                return;
            }

            if (entry.root.Extensions == null)
            {
                entry.root.Extensions = new Dictionary <string, Extension>();
            }

            KHR_lights_punctualExtension globalExtension;

            if (!entry.root.Extensions.ContainsKey(ExtensionName))
            {
                globalExtension          = new KHR_lights_punctualExtension();
                globalExtension.isGlobal = true;
                AddExtension(entry.root.Extensions, globalExtension);
            }
            else
            {
                globalExtension = (KHR_lights_punctualExtension)entry.root.Extensions["KHR_lights_punctual"];
            }

            var l = new KHR_lights_punctualExtension.Light();

            l.type      = light.type;
            l.name      = light.name;
            l.intensity = light.intensity;
            l.color     = light.color;

            if (light.type == LightType.Directional)
            {
            }
            else if (light.type == LightType.Point)
            {
                l.range = light.range;
            }
            else if (light.type == LightType.Spot)
            {
                l.range = light.range;
                var spotAngleRad = light.spotAngle * (float)Math.PI / 180;
                spotAngleRad     = spotAngleRad / 2;
                l.innerConeAngle = (spotAngleRad - (float)Math.PI / 180 * 5);
                l.outerConeAngle = spotAngleRad;
            }
            else
            {
                throw new Exception("Only support light type 'directional', 'point' or 'spot' now!");
            }

            globalExtension.lights.Add(l);

            var extension = new KHR_lights_punctualExtension();

            extension.lightIndex = globalExtension.lights.Count - 1;

            AddExtension(extensions, extension);
        }