public int ValidateAttribute(GeometryAttribute attr, int expectedCount)
 {
     if (expectedCount >= 0 && attr.ElementCount != expectedCount)
     {
         throw new Exception($"Attribute {attr.Descriptor.Name} size {attr.ElementCount} not match the expected size {expectedCount}");
     }
     return(attr.ElementCount);
 }
Beispiel #2
0
        public static void WriteAttribute(Stream stream, GeometryAttribute attribute, string name, long size)
        {
            var buffer = attribute.ToBuffer();

            if (buffer.NumBytes() != size)
            {
                throw new Exception($"Internal error while writing attribute, expected number of bytes was {size} but instead was {buffer.NumBytes()}");
            }
            if (buffer.Name != name)
            {
                throw new Exception($"Internal error while writing attribute, expected name was {name} but instead was {buffer.Name}");
            }
            stream.Write(buffer);
        }
        public static IArray <Vector4> AttributeToColors(this GeometryAttribute attr)
        {
            var desc = attr.Descriptor;

            if (desc.DataType == DataType.dt_float32)
            {
                if (desc.DataArity == 4)
                {
                    return(attr.AsType <Vector4>().Data);
                }
                if (desc.DataArity == 3)
                {
                    return(attr.AsType <Vector3>().Data.Select(vc => new Vector4(vc, 1f)));
                }
                if (desc.DataArity == 2)
                {
                    return(attr.AsType <Vector2>().Data.Select(vc => new Vector4(vc.X, vc.Y, 0, 1f)));
                }
                if (desc.DataArity == 1)
                {
                    return(attr.AsType <float>().Data.Select(vc => new Vector4(vc, vc, vc, 1f)));
                }
            }
            if (desc.DataType == DataType.dt_int8)
            {
                if (desc.DataArity == 4)
                {
                    return(attr.AsType <Byte4>().Data.Select(b => new Vector4((float)b.X / 255f, (float)b.Y / 255f, (float)b.Z / 255f, (float)b.W / 255f)));
                }
                if (desc.DataArity == 3)
                {
                    return(attr.AsType <Byte3>().Data.Select(b => new Vector4((float)b.X / 255f, (float)b.Y / 255f, (float)b.Z / 255f, 1f)));
                }
                if (desc.DataArity == 2)
                {
                    return(attr.AsType <Byte2>().Data.Select(b => new Vector4((float)b.X / 255f, (float)b.Y / 255f, 0f, 1f)));
                }
                if (desc.DataArity == 1)
                {
                    return(attr.AsType <byte>().Data.Select(b => new Vector4((float)b / 255f, (float)b / 255f, (float)b / 255f, 1f)));
                }
            }
            Debug.WriteLine($"Failed to recongize color format {attr.Descriptor}");
            return(null);
        }
Beispiel #4
0
 public G3DBuilder Add(GeometryAttribute attr)
 {
     Attributes.Add(attr);
     return(this);
 }
 public static GeometryAttribute <T> CheckArityAndAssociation <T>(this GeometryAttribute <T> self, int arity, Association assoc) where T : unmanaged
 => self?.CheckArity(arity)?.CheckAssociation(assoc);
 public static long GetByteSize(this GeometryAttribute attribute)
 => (long)attribute.ElementCount * attribute.Descriptor.DataElementSize;
 public static GeometryAttribute <T> CheckAssociation <T>(this GeometryAttribute <T> self, Association assoc) where T : unmanaged
 => self?.Descriptor?.Association == assoc ? self : null;
 public static GeometryAttribute <T> CheckArity <T>(this GeometryAttribute <T> self, int arity) where T : unmanaged
 => self?.Descriptor?.DataArity == arity ? self : null;
 public static bool IsNormalAttribute(this GeometryAttribute attr)
 => attr.IsType <Vector3>() && attr.Descriptor.Semantic == "normal";
 public static IGeometryAttributes SetAttribute(this IGeometryAttributes self, GeometryAttribute attr)
 => self.Attributes.Where(a => !a.Descriptor.Equals(attr.Descriptor)).Append(attr).ToGeometryAttributes();
Beispiel #11
0
 public static IGeometryAttributes Replace(this IGeometryAttributes self, Func <AttributeDescriptor, bool> selector, GeometryAttribute attribute)
 => self.Attributes.Where(a => !selector(a.Descriptor)).Append(attribute).ToGeometryAttributes();