Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
atteneder committed Jan 10, 2022
1 parent e8a0efd commit 705f60e
Show file tree
Hide file tree
Showing 16 changed files with 359 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store

# Rider
**/.idea
8 changes: 8 additions & 0 deletions Editor.meta

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

8 changes: 8 additions & 0 deletions Editor/Scripts.meta

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

47 changes: 47 additions & 0 deletions Editor/Scripts/GltfValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 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.Diagnostics;
using UnityEngine;

namespace Unity.glTF.Validator {

public static class Validator {

public static Report Validate(string path) {

var processInfo = new ProcessStartInfo {
FileName = "/usr/local/bin/gltf_validator",
Arguments = $"-o \"{path}\"",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
};

var process = Process.Start(processInfo);

if (process != null) {
var json = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
return JsonUtility.FromJson<Report>(json);
}

return null;
}
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/GltfValidator.cs.meta

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

46 changes: 46 additions & 0 deletions Editor/Scripts/MenuItems.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2022 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.IO;
using UnityEditor;
using UnityEngine;

namespace Unity.glTF.Validator.Editor {

public static class MenuItems {

static string SaveFolderPath {
get {
var saveFolderPath = EditorUserSettings.GetConfigValue("glTF.saveFilePath");
if (string.IsNullOrEmpty(saveFolderPath)) {
saveFolderPath = Application.streamingAssetsPath;
}
return saveFolderPath;
}
set => EditorUserSettings.SetConfigValue("glTF.saveFilePath",value);
}

[MenuItem("Tools/glTF-Validator")]
static void MenuGltfValidate() {
var path = EditorUtility.OpenFilePanelWithFilters("Select glTF file to validate", SaveFolderPath, new []{"glTF files", "gltf,glb"} );
if (!string.IsNullOrEmpty(path)) {
SaveFolderPath = Directory.GetParent(path)?.FullName;
var report = Validator.Validate(path);
report.Log();
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/MenuItems.cs.meta

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

134 changes: 134 additions & 0 deletions Editor/Scripts/Report.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2022 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 System.Linq;
using UnityEngine;

namespace Unity.glTF.Validator {

public enum MessageCode {
Unknown,
// ReSharper disable InconsistentNaming
ACCESSOR_ELEMENT_OUT_OF_MAX_BOUND,
ACCESSOR_MAX_MISMATCH,
ACCESSOR_MIN_MISMATCH,
NODE_EMPTY,
UNUSED_OBJECT,
// ReSharper restore InconsistentNaming
}

[Serializable]
public class Report {
public string uri;
public string mimeType;
public string validatorVersion;

public Issue issues;
public Info info;

public static readonly MessageCode[] defaultIgnored = new[] {
MessageCode.UNUSED_OBJECT
};

public void Log(MessageCode[] ignoredCodes = null) {
if (issues != null) {
foreach (var message in issues.messages) {
if(ignoredCodes!=null && ignoredCodes.Contains(message.codeEnum) ) continue;
message.Log();
}
} else {
Debug.Log("glTF validation passed");
}
}
}

[Serializable]
public class Issue {
public int numErrors;
public int numWarnings;
public int numInfos;
public int numHints;
public Message[] messages;
public bool truncated;
}

[Serializable]
public class Message {
public string code;
public string message;
public int severity;
public string pointer;

public MessageCode codeEnum => Enum.TryParse<MessageCode>(code, out var value) ? value : MessageCode.Unknown;

public void Log() {
if (severity < 1) {
Debug.LogError($"{message} ({code}, {pointer})");
} else if (severity < 2) {
Debug.LogWarning($"{message} ({code}, {pointer})");
} else {
Debug.Log($"{message} ({code}, {pointer})");
}
}

public override string ToString() {
return $"{code}: {message} ({pointer})";
}
}

[Serializable]
public class Info {
public string version;
public string generator;
public string[] extensionsUsed;
public string[] extensionsRequired;

public int animationCount;
public int materialCount;
public int drawCallCount;
public int totalVertexCount;
public int totalTriangleCount;
public int maxUVs;
public int maxInfluences;
public int maxAttributes;

public bool hasMorphTargets;
public bool hasSkins;
public bool hasTextures;
public bool hasDefaultScene;
}

[Serializable]
public class Resource {
public string pointer;
public string mimeTyp;
public string storage;
public string uri;
public int byteLength;
public Image image;
}

[Serializable]
public class Image {
public int width;
public int height;
public string format;
public string primaries;
public string transfer;
public int bits;
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/Report.cs.meta

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

16 changes: 16 additions & 0 deletions Editor/Scripts/com.unity.formats.gltf.validator.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "com.unity.formats.gltf.validator.Editor",
"rootNamespace": "",
"references": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

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

7 changes: 7 additions & 0 deletions LICENSE.meta

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

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# glTF-Validator for Unity

This package integrates the [glTF-Validator][gltf-validator] CLI tool into Unity.

It allows you validate a glTF file against the 2.0 specification and reports errors, warnings or info messages.

> Note: Currently it's experimental and only works on macOS!
## Install

You need to have [glTF-Validator][gltf-validator] installed at `/usr/local/bin/gltf_validator` (this exact requirement will be removed).

[gltf-validator]: https://github.com/KhronosGroup/glTF-Validator
7 changes: 7 additions & 0 deletions README.md.meta

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

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "com.unity.formats.gltf.validator",
"version": "0.1.0-preview.1",
"displayName": "glTF-Validator for Unity",
"description": "Validate glTF files",
"unity": "2019.4",
"keywords": [
"mesh",
"gltf",
"asset",
"format",
"draco",
"ktx",
"basis",
"universal",
"basisu"
],
"dependencies": {
"com.unity.modules.jsonserialize": "1.0.0"
},
"type": "tool"
}
7 changes: 7 additions & 0 deletions package.json.meta

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

0 comments on commit 705f60e

Please sign in to comment.