Skip to content

Commit

Permalink
feat: *Generate Lightmap UVs* option in the glTF import inspector let…
Browse files Browse the repository at this point in the history
…s you create a secondary texture coordinate set (similar to the Model Import Settings from other formats; closes #238)
  • Loading branch information
atteneder committed Oct 26, 2021
1 parent b86dddc commit 10937fc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions Editor/Scripts/EditorImportSettings.cs
Original file line number Diff line number Diff line change
@@ -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 {

/// <summary>
/// Editor Import specific settings (not relevant at runtime)
/// </summary>
[Serializable]
public class EditorImportSettings {

/// <summary>
/// Creates a secondary UV set on all meshes, if there is none present already.
/// Often used for lightmaps.
/// </summary>
public bool generateSecondaryUVSet;
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/EditorImportSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion Editor/Scripts/GltfImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
#else
using UnityEditor.Experimental.AssetImporters;
#endif
using UnityEngine;
using UnityEngine.Rendering;
using Object = UnityEngine.Object;

namespace GLTFast.Editor {

[ScriptedImporter(1,new [] {"gltf","glb"})]
public class GltfImporter : ScriptedImporter {

[SerializeField]
EditorImportSettings editorImportSettings;

[SerializeField]
ImportSettings importSettings;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 10937fc

Please sign in to comment.