diff --git a/ChangeLog.md b/ChangeLog.md index ee5bd262..a48ac995 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unrelease] - +### Added +- *Generate Lightmap UVs* option in the glTF import inspector lets you create a secondary texture coordinate set (similar to the Model Import Settings from other formats; #238) + ## [4.3.4] - 2021-10-26 ### Added - Option to turn off Editor import by adding `GLTFAST_EDITOR_IMPORT_OFF` to the project's *Scripting Define Symbols* in the *Player Settings* (#256) diff --git a/Editor/Scripts/EditorImportSettings.cs b/Editor/Scripts/EditorImportSettings.cs new file mode 100644 index 00000000..be802195 --- /dev/null +++ b/Editor/Scripts/EditorImportSettings.cs @@ -0,0 +1,33 @@ +// Copyright 2020-2021 Andreas Atteneder +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using UnityEngine.Serialization; + +namespace GLTFast.Editor { + + /// + /// Editor Import specific settings (not relevant at runtime) + /// + [Serializable] + public class EditorImportSettings { + + /// + /// Creates a secondary UV set on all meshes, if there is none present already. + /// Often used for lightmaps. + /// + public bool generateSecondaryUVSet; + } +} \ No newline at end of file diff --git a/Editor/Scripts/EditorImportSettings.cs.meta b/Editor/Scripts/EditorImportSettings.cs.meta new file mode 100644 index 00000000..520b33d2 --- /dev/null +++ b/Editor/Scripts/EditorImportSettings.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f989e96318ee5422aadb70c4bb252df0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/Scripts/GltfImporter.cs b/Editor/Scripts/GltfImporter.cs index 297974b7..c381a307 100644 --- a/Editor/Scripts/GltfImporter.cs +++ b/Editor/Scripts/GltfImporter.cs @@ -17,6 +17,7 @@ using System; using System.Collections.Generic; +using System.Linq; using UnityEditor; #if UNITY_2020_2_OR_NEWER using UnityEditor.AssetImporters; @@ -24,6 +25,7 @@ using UnityEditor.Experimental.AssetImporters; #endif using UnityEngine; +using UnityEngine.Rendering; using Object = UnityEngine.Object; namespace GLTFast.Editor { @@ -31,6 +33,9 @@ namespace GLTFast.Editor { [ScriptedImporter(1,new [] {"gltf","glb"})] public class GltfImporter : ScriptedImporter { + [SerializeField] + EditorImportSettings editorImportSettings; + [SerializeField] ImportSettings importSettings; @@ -68,8 +73,13 @@ public override void OnImportAsset(AssetImportContext ctx) { logger ); - if (importSettings == null) { + if (editorImportSettings == null) { // Design-time import specific settings + editorImportSettings = new EditorImportSettings(); + } + + if (importSettings == null) { + // Design-time import specific changes to default settings importSettings = new ImportSettings { // Avoid naming conflicts by default nodeNameMethod = ImportSettings.NameImportMethod.OriginalUnique @@ -123,6 +133,9 @@ public override void OnImportAsset(AssetImportContext ctx) { var meshes = m_Gltf.GetMeshes(); if (meshes != null) { foreach (var mesh in meshes) { + if (editorImportSettings.generateSecondaryUVSet && !HasSecondaryUVs(mesh)) { + Unwrapping.GenerateSecondaryUVSet(mesh); + } AddObjectToAsset(ctx, $"meshes/{mesh.name}", mesh); } } @@ -209,6 +222,11 @@ string GetUniqueAssetName(string originalName) { } return originalName; } + + static bool HasSecondaryUVs(Mesh mesh) { + var attributes = mesh.GetVertexAttributes(); + return attributes.Any(attribute => attribute.attribute == VertexAttribute.TexCoord1); + } } }