Skip to content

Commit

Permalink
Support KHR_materials_anisotropy (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
lexaknyazev authored Jul 9, 2024
1 parent de37778 commit 95ed362
Show file tree
Hide file tree
Showing 19 changed files with 591 additions and 0 deletions.
1 change: 1 addition & 0 deletions ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
|INVALID_EXTENSION_NAME_FORMAT|Extension name has invalid format.|Warning|
|INVALID_GL_VALUE|Invalid value `%1` for GL type '`%2`'.|Error|
|KHR_LIGHTS_PUNCTUAL_LIGHT_SPOT_ANGLES|outerConeAngle (`%2`) is less than or equal to innerConeAngle (`%1`).|Error|
|KHR_MATERIALS_ANISOTROPY_ANISOTROPY_TEXTURE_TEXCOORD|Normal and anisotropy textures should use the same texture coords.|Warning|
|KHR_MATERIALS_CLEARCOAT_CLEARCOAT_NORMAL_TEXTURE_TEXCOORD|Normal and clearcoat normal textures should use the same texture coords.|Warning|
|KHR_MATERIALS_DISPERSION_NO_VOLUME|The dispersion extension needs to be combined with the volume extension.|Warning|
|KHR_MATERIALS_EMISSIVE_STRENGTH_ZERO_FACTOR|Emissive strength has no effect when the emissive factor is zero or undefined.|Warning|
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ NuGet package [Third-party contribution]: https://www.nuget.org/packages/GltfVal
- Extensions validation
- EXT_texture_webp
- KHR_lights_punctual
- KHR_materials_anisotropy
- KHR_materials_clearcoat
- KHR_materials_dispersion
- KHR_materials_emissive_strength
Expand Down
7 changes: 7 additions & 0 deletions lib/src/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,13 @@ class SemanticError extends IssueType {
(args) => 'outerConeAngle (${args[1]}) is less than or equal to '
'innerConeAngle (${args[0]}).');

static final SemanticError khrMaterialsAnisotropyAnisotropyTextureTexCoord =
SemanticError._(
'KHR_MATERIALS_ANISOTROPY_ANISOTROPY_TEXTURE_TEXCOORD',
(args) => 'Normal and anisotropy textures '
'should use the same texture coords.',
Severity.Warning);

static final SemanticError
khrMaterialsClearcoatClearcoatNormalTextureTexCoord = SemanticError._(
'KHR_MATERIALS_CLEARCOAT_CLEARCOAT_NORMAL_TEXTURE_TEXCOORD',
Expand Down
87 changes: 87 additions & 0 deletions lib/src/ext/KHR_materials_anisotropy/khr_materials_anisotropy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2024 The Khronos Group Inc.
//
// SPDX-License-Identifier: Apache-2.0

library gltf.extensions.khr_materials_anisotropy;

import 'package:gltf/src/base/gltf_property.dart';
import 'package:gltf/src/ext/extensions.dart';

const String KHR_MATERIALS_ANISOTROPY = 'KHR_materials_anisotropy';

const String ANISOTROPY_STRENGTH = 'anisotropyStrength';
const String ANISOTROPY_ROTATION = 'anisotropyRotation';
const String ANISOTROPY_TEXTURE = 'anisotropyTexture';

const List<String> KHR_MATERIALS_ANISOTROPY_MEMBERS = <String>[
ANISOTROPY_STRENGTH,
ANISOTROPY_ROTATION,
ANISOTROPY_TEXTURE
];

class KhrMaterialsAnisotropy extends GltfProperty {
final double anisotropyStrength;
final double anisotropyRotation;
final TextureInfo anisotropyTexture;

KhrMaterialsAnisotropy._(this.anisotropyStrength, this.anisotropyRotation,
this.anisotropyTexture, Map<String, Object> extensions, Object extras)
: super(extensions, extras);

static KhrMaterialsAnisotropy fromMap(
Map<String, Object> map, Context context) {
if (context.validate) {
checkMembers(map, KHR_MATERIALS_ANISOTROPY_MEMBERS, context);
}

final anisotropyStrength =
getFloat(map, ANISOTROPY_STRENGTH, context, min: 0, max: 1, def: 0);
final anisotropyRotation =
getFloat(map, ANISOTROPY_ROTATION, context, def: 0);
final anisotropyTexture = getObjectFromInnerMap<TextureInfo>(
map, ANISOTROPY_TEXTURE, context, TextureInfo.fromMap);

final extensions = getExtensions(map, KhrMaterialsAnisotropy, context);

final anisotropy = KhrMaterialsAnisotropy._(
anisotropyStrength,
anisotropyRotation,
anisotropyTexture,
extensions,
getExtras(map, context));

context.registerObjectsOwner(
anisotropy, [anisotropyTexture, ...extensions.values]);

return anisotropy;
}

@override
void link(Gltf gltf, Context context) {
if (anisotropyTexture != null) {
context.path.add(ANISOTROPY_TEXTURE);
anisotropyTexture.link(gltf, context);

Object o = this;
while (o != null) {
o = context.owners[o];
if (o is Material) {
o.needsTangent = true;
final normalTexture = o.normalTexture;
if (normalTexture != null &&
normalTexture.texCoord != anisotropyTexture.texCoord) {
context.addIssue(
SemanticError.khrMaterialsAnisotropyAnisotropyTextureTexCoord);
}
break;
}
}
context.path.removeLast();
}
}
}

const Extension khrMaterialsAnisotropyExtension = Extension(
KHR_MATERIALS_ANISOTROPY, <Type, ExtensionDescriptor>{
Material: ExtensionDescriptor(KhrMaterialsAnisotropy.fromMap)
});
3 changes: 3 additions & 0 deletions lib/src/ext/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ library gltf.extensions;
import 'package:gltf/src/base/gltf_property.dart';
import 'package:gltf/src/ext/EXT_texture_webp/ext_texture_webp.dart';
import 'package:gltf/src/ext/KHR_lights_punctual/khr_lights_punctual.dart';
import 'package:gltf/src/ext/KHR_materials_anisotropy/khr_materials_anisotropy.dart';
import 'package:gltf/src/ext/KHR_materials_clearcoat/khr_materials_clearcoat.dart';
import 'package:gltf/src/ext/KHR_materials_dispersion/khr_materials_dispersion.dart';
import 'package:gltf/src/ext/KHR_materials_emissive_strength/khr_materials_emissive_strength.dart';
Expand All @@ -38,6 +39,7 @@ import 'package:meta/meta.dart';

export 'package:gltf/src/ext/EXT_texture_webp/ext_texture_webp.dart';
export 'package:gltf/src/ext/KHR_lights_punctual/khr_lights_punctual.dart';
export 'package:gltf/src/ext/KHR_materials_anisotropy/khr_materials_anisotropy.dart';
export 'package:gltf/src/ext/KHR_materials_clearcoat/khr_materials_clearcoat.dart';
export 'package:gltf/src/ext/KHR_materials_dispersion/khr_materials_dispersion.dart';
export 'package:gltf/src/ext/KHR_materials_emissive_strength/khr_materials_emissive_strength.dart';
Expand Down Expand Up @@ -101,6 +103,7 @@ class ResourceValidatableExtensionEntry {
const List<Extension> kDefaultExtensions = <Extension>[
extTextureWebPExtension,
khrLightsPunctualExtension,
khrMaterialsAnisotropyExtension,
khrMaterialsClearcoatExtension,
khrMaterialsDispersionExtension,
khrMaterialsEmissiveStrengthExtension,
Expand Down
13 changes: 13 additions & 0 deletions test/ext/KHR_materials_anisotropy/assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"material": {
"name": "material.KHR_materials_anisotropy",
"tests": {
"custom_property.gltf": "Custom property",
"different_texcoords.gltf": "Normal and anisotropy textures with different texcoords",
"no_tangent_space.gltf": "No tangent space",
"out_of_range.gltf": "Out of range values",
"unexpected_extension.gltf": "Unexpected extension object location",
"valid.gltf": "Valid"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"asset": {
"version": "2.0"
},
"extensionsUsed": [
"KHR_materials_anisotropy"
],
"materials": [
{
"extensions": {
"KHR_materials_anisotropy": {
"customProperty": true
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"uri": "test/ext/KHR_materials_anisotropy/data/material/custom_property.gltf",
"mimeType": "model/gltf+json",
"validatorVersion": "2.0.0-dev.3.10",
"issues": {
"numErrors": 0,
"numWarnings": 1,
"numInfos": 1,
"numHints": 0,
"messages": [
{
"code": "UNEXPECTED_PROPERTY",
"message": "Unexpected property.",
"severity": 1,
"pointer": "/materials/0/extensions/KHR_materials_anisotropy/customProperty"
},
{
"code": "UNUSED_OBJECT",
"message": "This object may be unused.",
"severity": 2,
"pointer": "/materials/0"
}
],
"truncated": false
},
"info": {
"version": "2.0",
"extensionsUsed": [
"KHR_materials_anisotropy"
],
"animationCount": 0,
"materialCount": 1,
"hasMorphTargets": false,
"hasSkins": false,
"hasTextures": false,
"hasDefaultScene": false,
"drawCallCount": 0,
"totalVertexCount": 0,
"totalTriangleCount": 0,
"maxUVs": 0,
"maxInfluences": 0,
"maxAttributes": 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"asset": {
"version": "2.0"
},
"extensionsUsed": [
"KHR_materials_anisotropy"
],
"materials": [
{
"normalTexture": {
"index": 0
},
"extensions": {
"KHR_materials_anisotropy": {
"anisotropyTexture": {
"index": 1,
"texCoord": 1
}
}
}
}
],
"textures": [
{
}, {
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"uri": "test/ext/KHR_materials_anisotropy/data/material/different_texcoords.gltf",
"mimeType": "model/gltf+json",
"validatorVersion": "2.0.0-dev.3.10",
"issues": {
"numErrors": 0,
"numWarnings": 1,
"numInfos": 1,
"numHints": 0,
"messages": [
{
"code": "KHR_MATERIALS_ANISOTROPY_ANISOTROPY_TEXTURE_TEXCOORD",
"message": "Normal and anisotropy textures should use the same texture coords.",
"severity": 1,
"pointer": "/materials/0/extensions/KHR_materials_anisotropy/anisotropyTexture"
},
{
"code": "UNUSED_OBJECT",
"message": "This object may be unused.",
"severity": 2,
"pointer": "/materials/0"
}
],
"truncated": false
},
"info": {
"version": "2.0",
"extensionsUsed": [
"KHR_materials_anisotropy"
],
"animationCount": 0,
"materialCount": 1,
"hasMorphTargets": false,
"hasSkins": false,
"hasTextures": true,
"hasDefaultScene": false,
"drawCallCount": 0,
"totalVertexCount": 0,
"totalTriangleCount": 0,
"maxUVs": 0,
"maxInfluences": 0,
"maxAttributes": 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"asset": {
"version": "2.0"
},
"extensionsUsed": [
"KHR_materials_anisotropy"
],
"meshes": [
{
"primitives": [
{
"attributes": {
"POSITION": 0,
"TEXCOORD_0": 1
},
"material": 0
}
]
}
],
"materials": [
{
"extensions": {
"KHR_materials_anisotropy": {
"anisotropyTexture": {
"index": 0
}
}
}
}
],
"textures": [
{ }
],
"accessors": [
{
"componentType": 5126,
"count": 3,
"type": "VEC3",
"min": [0, 0, 0],
"max": [1, 1, 1]
},
{
"componentType": 5126,
"count": 3,
"type": "VEC2"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"uri": "test/ext/KHR_materials_anisotropy/data/material/no_tangent_space.gltf",
"mimeType": "model/gltf+json",
"validatorVersion": "2.0.0-dev.3.10",
"issues": {
"numErrors": 1,
"numWarnings": 0,
"numInfos": 1,
"numHints": 0,
"messages": [
{
"code": "MESH_PRIMITIVE_NO_TANGENT_SPACE",
"message": "Material requires a tangent space but the mesh primitive does not provide it and the material does not contain a normal map to generate it.",
"severity": 0,
"pointer": "/meshes/0/primitives/0/material"
},
{
"code": "UNUSED_OBJECT",
"message": "This object may be unused.",
"severity": 2,
"pointer": "/meshes/0"
}
],
"truncated": false
},
"info": {
"version": "2.0",
"extensionsUsed": [
"KHR_materials_anisotropy"
],
"animationCount": 0,
"materialCount": 1,
"hasMorphTargets": false,
"hasSkins": false,
"hasTextures": true,
"hasDefaultScene": false,
"drawCallCount": 1,
"totalVertexCount": 3,
"totalTriangleCount": 1,
"maxUVs": 1,
"maxInfluences": 0,
"maxAttributes": 2
}
}
Loading

0 comments on commit 95ed362

Please sign in to comment.