Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement extension KHR_animation_pointer #7

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Runtime/Scripts/AccessorData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ enum AccessorUsage
Rotation = 1 << 11,
Scale = 1 << 12,
Weight = 1 << 13,
RequiredForInstantiation = 1 << 14
RequiredForInstantiation = 1 << 14,
Pointer = 1 << 15,
}

abstract class AccessorDataBase
Expand Down
77 changes: 77 additions & 0 deletions Runtime/Scripts/AnimationCameraGameObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-FileCopyrightText: 2023 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace GLTFast {
public class AnimationCameraGameObject : MonoBehaviour {
public Camera targetCamera;
public bool orthographic;
public float localScale;

public float nearClipPlane;
float nearClipPlaneOld;
public float farClipPlane;
float farClipPlaneOld;

public float xMag;
public float yMag;
float xMagOld;
float yMagOld;

public float fov;
float fovOld;

void OnEnable() {
if (targetCamera == null) {
Debug.LogError("Target camera not set on animated camera!");
}
}

void LateUpdate() {
bool orthoChanged = false;
if(farClipPlane != farClipPlaneOld) {
if(orthographic) {
orthoChanged = true;
farClipPlane = farClipPlane >= 0 ? farClipPlane : float.MaxValue;
}
targetCamera.farClipPlane = localScale * farClipPlane;
farClipPlaneOld = farClipPlane;
}

if(nearClipPlane != nearClipPlaneOld) {
targetCamera.nearClipPlane = localScale * nearClipPlane;
nearClipPlaneOld = nearClipPlane;
orthoChanged = true;
}

if(xMag != xMagOld) {
xMagOld = xMag;
orthoChanged = true;
}

if(yMag != yMagOld) {
yMagOld = yMag;
orthoChanged = true;
}

if(fov != fovOld) {
targetCamera.fieldOfView = fov * Mathf.Rad2Deg;
fovOld = fov;
}

if(orthographic && orthoChanged) {
targetCamera.projectionMatrix = Matrix4x4.Ortho(
-xMag,
xMag,
-yMag,
yMag,
nearClipPlane,
farClipPlane
);
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/AnimationCameraGameObject.cs.meta

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

214 changes: 214 additions & 0 deletions Runtime/Scripts/AnimationData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// SPDX-FileCopyrightText: 2023 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0

#if UNITY_ANIMATION

using System;
using GLTFast.Schema;
using UnityEngine;

namespace GLTFast {

public enum TargetType {
Unknown = -1,
Camera,
Material,
Mesh,
Node,
}

public class AnimationData {
public TargetType TargetType = TargetType.Unknown;
public Type AnimationClipType;
public GltfAccessorAttributeType AccessorType;
public string TargetProperty;
public int TargetId = -1;
public string[] PropertyNames;

public static AnimationData TranslationData() {
var template = "localPosition.";
return new AnimationData
{
TargetType = TargetType.Node,
AnimationClipType = typeof(Transform),
AccessorType = GltfAccessorAttributeType.VEC3,
TargetProperty = "translationNative",
PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z"}
};
}

public static AnimationData RotationData() {
var template = "localRotation.";
return new AnimationData
{
TargetType = TargetType.Node,
AnimationClipType = typeof(Transform),
AccessorType = GltfAccessorAttributeType.VEC4,
TargetProperty = "rotationNative",
PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z", $"{template}w"}
};
}

public static AnimationData ScaleData() {
var template = "localScale.";
return new AnimationData
{
TargetType = TargetType.Node,
AnimationClipType = typeof(Transform),
AccessorType = GltfAccessorAttributeType.VEC3,
TargetProperty = "scaleNative",
PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z"}
};
}

public static AnimationData WeightData() {
return new AnimationData
{
TargetType = TargetType.Node,
AnimationClipType = typeof(MeshRenderer),
AccessorType = GltfAccessorAttributeType.SCALAR,
TargetProperty = "weights"
};
}

public static AnimationData GeneratePointerData(string pointerPath) {
var data = new AnimationData();

switch (pointerPath) {
case string p when p.StartsWith("/cameras/"):
data.TargetType = TargetType.Camera;
data.AnimationClipType = typeof(AnimationCameraGameObject);
data.TargetId = ParsePointerTargetId(pointerPath["/cameras/".Length..]);
data.TargetProperty = pointerPath[$"/cameras/{data.TargetId}/".Length..];
break;
case string p when p.StartsWith("/materials/"):
data.TargetType = TargetType.Material;
data.AnimationClipType = typeof(Renderer);
data.TargetId = ParsePointerTargetId(pointerPath["/materials/".Length..]);
data.TargetProperty = pointerPath[$"/materials/{data.TargetId}/".Length..];
break;
case string p when p.StartsWith("/meshes/"):
data.TargetType = TargetType.Mesh;
data.AnimationClipType = typeof(UnityEngine.MeshRenderer);
data.TargetId = ParsePointerTargetId(pointerPath["/meshes/".Length..]);
data.TargetProperty = pointerPath[$"/meshes/{data.TargetId}/".Length..];
break;
case string p when p.StartsWith("/nodes/"):
data.TargetType = TargetType.Node;
if (pointerPath[^7..].Equals("weights")) {
data.AnimationClipType = typeof(UnityEngine.MeshRenderer);
} else {
data.AnimationClipType = typeof(Transform);
}
data.TargetId = ParsePointerTargetId(pointerPath["/nodes/".Length..]);
data.TargetProperty = pointerPath[$"/nodes/{data.TargetId}/".Length..];
break;
}

string template;
switch(data.TargetProperty) {
// Core
case "rotation":
template = "localRotation.";
data.PropertyNames = new[] {$"{template}x", $"{template}y", $"{template}z", $"{template}w"};
data.AccessorType = GltfAccessorAttributeType.VEC4;
break;
case "scale":
template = "localScale.";
data.PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z"};
data.AccessorType = GltfAccessorAttributeType.VEC3;
break;
case "translation":
template = "localPosition.";
data.PropertyNames = new [] {$"{template}x", $"{template}y", $"{template}z"};
data.AccessorType = GltfAccessorAttributeType.VEC3;
break;
case "weights":
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "orthographic/xmag":
data.PropertyNames = new [] {"xMag"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "orthographic/ymag":
data.PropertyNames = new [] {"yMag"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "orthographic/zfar":
data.PropertyNames = new [] {"farClipPlane"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "orthographic/znear":
data.PropertyNames = new [] {"nearClipPlane"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "perspective/yfov":
data.PropertyNames = new [] {"fov"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "perspective/zfar":
data.PropertyNames = new [] {"farClipPlane"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "perspective/znear":
data.PropertyNames = new [] {"nearClipPlane"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "pbrMetallicRoughness/baseColorFactor":
template = "material.baseColorFactor.";
data.PropertyNames = new[] {$"{template}r", $"{template}g", $"{template}b", $"{template}a"};
data.AccessorType = GltfAccessorAttributeType.VEC4;
break;
case "pbrMetallicRoughness/metallicFactor":
data.PropertyNames = new[] {"material.metallicFactor"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "pbrMetallicRoughness/roughnessFactor":
data.PropertyNames = new[] {"material.roughnessFactor"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "alphaCutoff":
data.PropertyNames = new[] {"material.alphaCutoff"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "emissiveFactor":
template = "material.emissiveFactor.";
data.PropertyNames = new[] {$"{template}r", $"{template}g", $"{template}b"};
data.AccessorType = GltfAccessorAttributeType.VEC3;
break;
case "normalTexture/scale":
data.PropertyNames = new[] {"material.normalTexture_scale"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;
case "occlusionTexture/strength":
data.PropertyNames = new[] {"material.occlusionTexture_strength"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;

// KHR_materials_transmission
case "extensions/KHR_materials_transmission/transmissionFactor":
data.PropertyNames = new[] {"material.transmissionFactor"};
data.AccessorType = GltfAccessorAttributeType.SCALAR;
break;

default:
#if DEBUG
Debug.LogWarning($"glTF animation pointer {pointerPath} is not supported.");
#endif
break;
}

return data;
}

public static int ParsePointerTargetId(string name) {
var split = name[..name.IndexOf("/")];
if(int.TryParse(split, out var targetId)) {
return targetId;
}
return -1;
}
}
}
#endif

11 changes: 11 additions & 0 deletions Runtime/Scripts/AnimationData.cs.meta

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

Loading