KhronosGroup / UnityGLTF

Runtime glTF 2.0 Loader for Unity3D
MIT License
1.77k stars 483 forks source link

Support for Line rendering #560

Closed nodermatt closed 5 months ago

nodermatt commented 4 years ago

Does UnityGLTF support rendering nodes that encompass points and not triangles? I am working with a glTF model that contains a node with a mesh that consists of a line with 2 vertices (full file added as attachment). 3d viewer can render the model, but UnityGltf throws an exception "Index was outside the bounds of the array". image

The reason being that AsUnits array only contains two elements (0, 1) thus the index +2 is out of bounds:

public static void FlipFaces(ref AttributeAccessor attributeAccessor)
        {
            for (int i = 0; i < attributeAccessor.AccessorContent.AsTriangles.Length; i += 3)
            {
                uint temp = attributeAccessor.AccessorContent.AsUInts[i];
// this line causes OutOfBoundsException
                attributeAccessor.AccessorContent.AsUInts[i] = attributeAccessor.AccessorContent.AsUInts[i + 2];
                attributeAccessor.AccessorContent.AsUInts[i + 2] = temp;
            }
        }

The method calling this function (TransfromAttributes) checks, if the INDICES attribute is present and then runs the FlipFaces method. I noticed that the SemanticProperties class doesn't contain the MODE value and that there is no check for the MODE attribute

public static class SemanticProperties
    {
        public static readonly string POSITION;
        public static readonly string NORMAL;
        public static readonly string JOINT;
        public static readonly string WEIGHT;
        public static readonly string TANGENT;
        public static readonly string INDICES;
         // methods omitted
    }

Does UnityGltf support different mode types lik LINE as defined in [primitivemodes]https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#primitivemode)?

I think it is necessary to add a check for the MODE property. Since a line doesn't have faces, they can't be flipped.

if (attributeAccessors.ContainsKey(SemanticProperties.POSITION))
            {
                AttributeAccessor attributeAccessor = attributeAccessors[SemanticProperties.POSITION];
                SchemaExtensions.ConvertVector3CoordinateSpace(ref attributeAccessor, SchemaExtensions.CoordinateSpaceConversionScale);
            }
            if (attributeAccessors.ContainsKey(SemanticProperties.INDICES))
            {
                AttributeAccessor attributeAccessor = attributeAccessors[SemanticProperties.INDICES];
// only flip flaces, if the mode type is triangles
                SchemaExtensions.FlipFaces(ref attributeAccessor);
            }

Model: line.txt

pfcDorn commented 5 months ago

Please update to the latest version and open a new issue if the problem persists. Thanks!