Example #1
0
        private void ExportSceneLighting(VCIExporter exporter, glTF gltf)
        {
            var lightmapTextureExporter = new LightmapTextureExporter(TextureExporter, gltf);

            var existsLightmappedMesh = false;

            for (var i = 0; i < exporter.Nodes.Count; i++)
            {
                var node     = exporter.Nodes[i];
                var gltfNode = gltf.nodes[i];

                var renderer = node.GetComponent <MeshRenderer>();
                if (renderer != null)
                {
                    var useLightmapExtension = false;
#if UNITY_EDITOR
                    var contributeGi = UnityEditor.GameObjectUtility.GetStaticEditorFlags(node.gameObject)
                                       .HasFlag(UnityEditor.StaticEditorFlags.ContributeGI);
                    var receiveLightmap         = renderer.receiveGI == ReceiveGI.Lightmaps;
                    var isLightmapExistsInScene =
                        LightmapSettings.lightmaps != null && LightmapSettings.lightmaps.Length > 0;
                    useLightmapExtension = contributeGi && receiveLightmap && isLightmapExistsInScene;
#endif

                    if (useLightmapExtension)
                    {
                        var originalRenderer = _originalNodes[i].GetComponent <MeshRenderer>();

                        var lightmapUnityIndex = originalRenderer.lightmapIndex;
                        if (lightmapUnityIndex < 0 || lightmapUnityIndex >= LightmapSettings.lightmaps.Length)
                        {
                            continue;
                        }

                        var lightmapGltfIndex = lightmapTextureExporter.GetOrAddColorTexture(lightmapUnityIndex);

                        if (lightmapGltfIndex >= 0)
                        {
                            var so     = originalRenderer.lightmapScaleOffset;
                            var scale  = new Vector2(so.x, so.y);
                            var offset = new Vector2(so.z, so.w);
                            offset.y = (offset.y + scale.y - 1) * -1.0f;

                            var VCAST_vci_lightmap = new glTF_VCAST_vci_lightmap
                            {
                                lightmap = new glTF_VCAST_vci_Lightmap
                                {
                                    texture = new glTFLightmapTextureInfo {
                                        index = lightmapGltfIndex
                                    },
                                    offset = new[] { offset.x, offset.y },
                                    scale  = new[] { scale.x, scale.y },
                                },
                            };

                            var f = new UniJSON.JsonFormatter();
                            glTF_VCAST_vci_lightmap_Serializer.Serialize(f, VCAST_vci_lightmap);
                            glTFExtensionExport.GetOrCreate(ref gltfNode.extensions).Add(glTF_VCAST_vci_lightmap.ExtensionName, f.GetStore().Bytes);

                            existsLightmappedMesh = true;
                        }
                    }
                }
            }

            var enableLocationLightingExtension = existsLightmappedMesh;
            if (enableLocationLightingExtension)
            {
                var cubemapExporter    = new CubemapTextureExporter(TextureExporter, glTF);
                var skyboxExporter     = new SkyboxExporter(cubemapExporter);
                var lightProbeExporter = new LightProbeExporter();

                var VCAST_vci_location_lighting = new glTF_VCAST_vci_location_lighting
                {
                    locationLighting = new glTF_VCAST_vci_LocationLighting
                    {
                        lightmapCompressionMode =
                            glTF_VCAST_vci_LocationLighting.ConvertLightmapCompressionMode(lightmapTextureExporter
                                                                                           .CompressionType),
                        lightmapDirectionalMode =
                            glTF_VCAST_vci_LocationLighting.ConvertLightmapDirectionalMode(lightmapTextureExporter
                                                                                           .DirectionalType),
                        lightmapTextures = lightmapTextureExporter.RegisteredColorTextureIndexArray
                                           .Select(x => new glTFLightmapTextureInfo {
                            index = x
                        })
                                           .ToArray(),
                        skyboxCubemap = skyboxExporter.Export(1024),
                        lightProbes   = lightProbeExporter.Export(),
                    },
                };

                var f = new UniJSON.JsonFormatter();
                glTF_VCAST_vci_location_lighting_Serializer.Serialize(f, VCAST_vci_location_lighting);
                glTFExtensionExport.GetOrCreate(ref gltf.extensions).Add(glTF_VCAST_vci_location_lighting.ExtensionName, f.GetStore().Bytes);
            }

            for (var i = 0; i < exporter.Nodes.Count; i++)
            {
                var node     = exporter.Nodes[i];
                var gltfNode = gltf.nodes[i];

                var reflectionProbe = _originalNodes[i].GetComponent <ReflectionProbe>();
                if (reflectionProbe == null)
                {
                    continue;
                }

                var exportReflectionProbeExtension = false;
                var isModeActive    = reflectionProbe.mode == ReflectionProbeMode.Baked;
                var texture         = reflectionProbe.bakedTexture;
                var isTextureExists = texture != null && texture.dimension == TextureDimension.Cube;

                exportReflectionProbeExtension = !Application.isPlaying && isModeActive && isTextureExists;
                if (!exportReflectionProbeExtension)
                {
                    continue;
                }

                var reflectionProbeCubemapExporter = new CubemapTextureExporter(TextureExporter, glTF);

                var offset = reflectionProbe.center;
                var size   = reflectionProbe.size;

                var VCAST_vci_reflectionProbe = new glTF_VCAST_vci_reflectionProbe
                {
                    reflectionProbe = new glTF_VCAST_vci_ReflectionProbe
                    {
                        boxOffset        = new[] { -offset.x, offset.y, offset.z }, // invert X-axis
                        boxSize          = new[] { size.x, size.y, size.z },
                        intensity        = reflectionProbe.intensity,
                        useBoxProjection = reflectionProbe.boxProjection,
                        cubemap          = reflectionProbeCubemapExporter.Export(texture, reflectionProbe.resolution,
                                                                                 includeMipmaps: true),
                    },
                };

                var f = new UniJSON.JsonFormatter();
                glTF_VCAST_vci_reflectionProbe_Serializer.Serialize(f, VCAST_vci_reflectionProbe);
                glTFExtensionExport.GetOrCreate(ref gltfNode.extensions).Add(glTF_VCAST_vci_reflectionProbe.ExtensionName, f.GetStore().Bytes);
            }
        }
Example #2
0
 public SkyboxExporter(CubemapTextureExporter exporter)
 {
     _exporter = exporter;
 }