Example #1
0
        protected override void Serialize(UnityEngine.Object sourceAsset)
        {
            this.texture = sourceAsset as UnityEngine.Cubemap;
            //先把原始图片导出来
            this.ExportTexture();

            var path   = PathHelper.GetTexturePath(this.texture);
            var mipmap = this.texture.mipmapCount > 1;
            //
            {
                this._root.Images.Add(new Image()
                {
                    Uri = ExportSetting.instance.GetExportPath(path)
                });
            }
            //
            {
                var filterMode = this.texture.filterMode;
                var wrapMode   = this.texture.wrapMode;

                var sampler = new Sampler();
                this._root.Samplers.Add(sampler);
                if (wrapMode == TextureWrapMode.Repeat)
                {
                    sampler.WrapS = GLTF.Schema.WrapMode.Repeat;
                    sampler.WrapT = GLTF.Schema.WrapMode.Repeat;
                }
                else
                {
                    sampler.WrapS = GLTF.Schema.WrapMode.ClampToEdge;
                    sampler.WrapT = GLTF.Schema.WrapMode.ClampToEdge;
                }
                sampler.MagFilter = filterMode == FilterMode.Point ? MagFilterMode.Nearest : MagFilterMode.Linear;
                if (!mipmap)
                {
                    sampler.MagFilter = filterMode == FilterMode.Point ? MagFilterMode.Nearest : MagFilterMode.Linear;
                }
                else if (filterMode == FilterMode.Point)
                {
                    sampler.MinFilter = MinFilterMode.NearestMipmapNearest;
                }
                else if (filterMode == FilterMode.Bilinear)
                {
                    sampler.MinFilter = MinFilterMode.LinearMipmapNearest;
                }
                else if (filterMode == FilterMode.Trilinear)
                {
                    sampler.MinFilter = MinFilterMode.LinearMipmapLinear;
                }
            }
            //
            {
                var gltfTexture = new GLTF.Schema.Texture();
                this._root.Textures.Add(gltfTexture);

                gltfTexture.Sampler    = new SamplerId();
                gltfTexture.Source     = new ImageId();
                gltfTexture.Extensions = new Dictionary <string, IExtension>()
                {
                    {
                        TextureExtension.EXTENSION_NAME,
                        new TextureExtension()
                        {
                            anisotropy = this.texture.anisoLevel,
                            format     = GetTextureFormat(),
                            levels     = mipmap ? 0 : 1
                        }
                    }
                };
            }
        }
Example #2
0
        public static void Export(List <GameObject> roots, string exportPath)
        {
            string sceneName = PathHelper.CurSceneName;

            SerializeObject.Clear();
            //路径
            string scenePath = sceneName + ".scene.json";

            PathHelper.SetSceneOrPrefabPath(scenePath);

            var scene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene();

            var sceneEntity = SerializeObject.currentData.CreateEntity();

            var sceneComp = SerializeObject.currentData.CreateComponent(SerializeClass.Scene);

            sceneComp.properties.SetString("name", sceneName.Substring(sceneName.LastIndexOf('/') + 1));
            sceneEntity.AddComponent(sceneComp);

            var treeComp = SerializeObject.currentData.CreateComponent(SerializeClass.TreeNode);

            treeComp.properties.SetString("name", "Root");
            treeComp.properties.SetReference("scene", sceneComp.uuid);
            sceneEntity.AddComponent(treeComp);

            var sceneSetting = ExportSetting.instance.scene;
            // 环境光和光照贴图
            var sceneLightComp = SerializeObject.currentData.CreateComponent(SerializeClass.SceneLight);

            sceneLightComp.properties.SetColor("ambientColor", RenderSettings.ambientLight);
            if (sceneSetting.lightmap)
            {
                sceneLightComp.properties.SetNumber("lightmapIntensity", UnityEditor.Lightmapping.indirectOutputScale);
                sceneLightComp.properties.SetLightmaps(exportPath);
            }

            sceneEntity.AddComponent(sceneLightComp);

            if (sceneSetting.staticBatching)
            {
                var staticBatching = SerializeObject.currentData.CreateComponent(SerializeClass.StaticBatching);
                sceneEntity.AddComponent(staticBatching);
            }

            // 雾
            if (RenderSettings.fog && sceneSetting.fog)
            {
                var fogComp = SerializeObject.currentData.CreateComponent(SerializeClass.Fog);
                if (RenderSettings.fogMode == FogMode.Linear)
                {
                    fogComp.properties.SetInt("mode", 0);
                    fogComp.properties.SetNumber("near", RenderSettings.fogStartDistance);
                    fogComp.properties.SetNumber("far", RenderSettings.fogEndDistance);
                }
                else
                {
                    fogComp.properties.SetInt("mode", 1);
                    fogComp.properties.SetNumber("density", RenderSettings.fogDensity);
                }
                fogComp.properties.SetColor("color", RenderSettings.fogColor);
                sceneEntity.AddComponent(fogComp);
            }

            foreach (var child in roots)
            {
                var childEntity = SerializeObject.SerializeEntity(child);
                if (childEntity != null)
                {
                    treeComp.AddChild(childEntity.treeNode);
                }
            }

            SerializeContext.Export(exportPath, scenePath);
        }
Example #3
0
        /**
         * 绘制窗口
         */
        void OnGUI()
        {
            var setting = ExportToolsSetting.instance;

            this._scrollPosition = GUILayout.BeginScrollView(this._scrollPosition, GUILayout.Width(WIN_WIDTH), GUILayout.Height(400));
            GUILayout.Space(SMALL_SPACE);
            //------------------------目录选择------------------------
            {
                GUILayout.Label("当前导出路径");
                GUILayout.BeginHorizontal();
                GUILayout.TextField(ExportConfig.instance.exportPath);
                if (GUILayout.Button("选择目录", GUILayout.Width(100)))
                {
                    ExportConfig.instance.exportPath = EditorUtility.OpenFolderPanel("当前要导出的路径", Application.dataPath, "");
                    ExportConfig.instance.Save(PathHelper.ConfigPath);
                    AssetDatabase.Refresh();
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.Space(SPACE);
            //------------------------辅助选项------------------------
            {
                GUILayout.BeginHorizontal();
                setting.debugLog            = GUILayout.Toggle(setting.debugLog, new GUIContent("输出日志", "勾选后,方便查看输出信息"));
                setting.prefabResetPos      = GUILayout.Toggle(setting.prefabResetPos, new GUIContent("坐标归零", "勾选后,将导出的预制体坐标归零"));
                setting.exportOriginalImage = GUILayout.Toggle(setting.exportOriginalImage, new GUIContent("导出原始图片", "勾选后,jpg和png会直接使用原始图片导出"));
                // ExportToolsSetting.unityNormalTexture = GUILayout.Toggle(ExportToolsSetting.unityNormalTexture, new GUIContent("使用Unity法线贴图", "勾选后,时使用Unity转换后的法线贴图导出"));

                GUILayout.EndHorizontal();
            }
            GUILayout.Space(SPACE);
            this._lightSetting = EditorGUILayout.Foldout(this._lightSetting, "光照设置");
            if (this._lightSetting)
            {
                GUILayout.BeginVertical();
                setting.lightType = (ExportLightType)EditorGUILayout.EnumPopup(setting.lightType, GUILayout.MaxWidth(100));
                GUILayout.EndVertical();
            }
            GUILayout.Space(SMALL_SPACE);
            this._meshSetting = EditorGUILayout.Foldout(this._meshSetting, "网格设置");
            if (this._meshSetting)
            {
                GUILayout.BeginHorizontal();
                setting.enableNormals = GUILayout.Toggle(setting.enableNormals, new GUIContent("Normals", "取消后,不导出Normals"));
                setting.enableColors  = GUILayout.Toggle(setting.enableColors, new GUIContent("Colors", "取消后,不导出Colors"));
                setting.enableBones   = GUILayout.Toggle(setting.enableBones, new GUIContent("Bones", "取消后,不导出Bones"));
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                this._serializeObject.Update();
                if (this._meshIgnoresProperty != null)
                {
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.PropertyField(this._meshIgnoresProperty, new GUIContent("忽略对象:", "在忽略列表中的对象网格属性全部导出"), true);
                    if (EditorGUI.EndChangeCheck())
                    {
                        _serializeObject.ApplyModifiedProperties();
                    }
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.Space(SPACE);
            //------------------------主功能------------------------
            {
                //资源导出
                _resourceToolOpen = GUILayout.Toggle(_resourceToolOpen, "--------资源导出工具--------");
                if (_resourceToolOpen)
                {
                    GUILayout.Space(SPACE);
                    GUILayout.BeginHorizontal();
                    if (Selection.activeGameObject)
                    {
                        if (GUILayout.Button("导出当前选中对象"))
                        {
                            _frameCount    = 0;
                            _isBuzy        = true;
                            _info          = "导出中...";
                            _curExportType = ExportType.PREFAB;
                        }
                    }
                    else
                    {
                        GUILayout.Label("请选中场景中要导出的对象", _showStyle);
                    }
                    GUILayout.EndHorizontal();
                    GUILayout.Space(SPACE);
                }
                //场景导出
                _sceneToolOpen = GUILayout.Toggle(_sceneToolOpen, "--------场景导出工具--------");
                if (_sceneToolOpen)
                {
                    GUILayout.Space(SPACE);
                    GUILayout.BeginHorizontal();
                    if (GUILayout.Button("导出当前场景"))
                    {
                        PathHelper.SetOutPutPath(ExportConfig.instance.exportPath, PathHelper.CurSceneName);

                        _frameCount    = 0;
                        _info          = "导出中...";
                        _isBuzy        = true;
                        _curExportType = ExportType.SCENE;
                    }
                    GUILayout.EndHorizontal();
                    GUILayout.Space(SPACE);
                }
            }

            GUILayout.EndScrollView();
            GUI.Label(new Rect(0, WIN_HEIGHT - 15, WIN_WIDTH, 15), "状态:" + _info);
        }