/// <summary> /// Associates this <see cref="Accessor"/> with a <see cref="BufferView"/> /// </summary> /// <param name="buffer">The <see cref="BufferView"/> source.</param> /// <param name="bufferByteOffset">The start byte offset within <paramref name="buffer"/>.</param> /// <param name="itemCount">The number of items in the accessor.</param> /// <param name="dimensions">The <see cref="DimensionType"/> item type.</param> /// <param name="encoding">The <see cref="EncodingType"/> item encoding.</param> /// <param name="normalized">The item normalization mode.</param> public void SetVertexData(BufferView buffer, int bufferByteOffset, int itemCount, DimensionType dimensions = DimensionType.VEC3, EncodingType encoding = EncodingType.FLOAT, Boolean normalized = false) { Guard.NotNull(buffer, nameof(buffer)); Guard.MustShareLogicalParent(this, buffer, nameof(buffer)); Guard.MustBePositiveAndMultipleOf(dimensions.DimCount() * encoding.ByteLength(), 4, nameof(encoding)); Guard.IsFalse(buffer.IsIndexBuffer, nameof(buffer)); SetData(buffer, bufferByteOffset, itemCount, dimensions, encoding, normalized); }
/// <summary> /// Calculates the number of bytes to which this accessors reads /// taking into account if the source <see cref="BufferView"/> is strided. /// </summary> /// <returns>The number of bytes to access.</returns> internal int GetAccessorByteLength(DimensionType dim, EncodingType enc, int count) { var elementByteSize = dim.DimCount() * enc.ByteLength(); if (this.ByteStride == 0) { return(elementByteSize * count); } return((this.ByteStride * (count - 1)) + elementByteSize); }
/// <summary> /// Associates this <see cref="Accessor"/> with a <see cref="BufferView"/> /// </summary> /// <param name="buffer">The <see cref="BufferView"/> source.</param> /// <param name="bufferByteOffset">The start byte offset within <paramref name="buffer"/>.</param> /// <param name="itemCount">The number of items in the accessor.</param> /// <param name="dimensions">The <see cref="DimensionType"/> item type.</param> /// <param name="encoding">The <see cref="EncodingType"/> item encoding.</param> /// <param name="normalized">The item normalization mode.</param> public void SetVertexData(BufferView buffer, int bufferByteOffset, int itemCount, DimensionType dimensions = DimensionType.VEC3, EncodingType encoding = EncodingType.FLOAT, Boolean normalized = false) { Guard.NotNull(buffer, nameof(buffer)); Guard.MustShareLogicalParent(this, buffer, nameof(buffer)); Guard.MustBePositiveAndMultipleOf(dimensions.DimCount() * encoding.ByteLength(), 4, nameof(encoding)); if (buffer.DeviceBufferTarget.HasValue) { Guard.IsTrue(buffer.DeviceBufferTarget.Value == BufferMode.ARRAY_BUFFER, nameof(buffer)); } SetData(buffer, bufferByteOffset, itemCount, dimensions, encoding, normalized); }
internal static void CheckAccess(Validation.ValidationContext result, BufferView bv, int accessorByteOffset, DimensionType dim, EncodingType enc, bool nrm, int count) { if (nrm) { if (enc != EncodingType.UNSIGNED_BYTE && enc != EncodingType.UNSIGNED_SHORT) { result.AddDataError("Normalized", "Only (u)byte and (u)short accessors can be normalized."); } } var elementByteSize = dim.DimCount() * enc.ByteLength(); if (bv.IsVertexBuffer) { if (bv.ByteStride == 0) { result.CheckSchemaIsMultipleOf("ElementByteSize", elementByteSize, 4); } } if (bv.IsIndexBuffer) { if (dim != DimensionType.SCALAR) { result.AddLinkError(("BufferView", bv.LogicalIndex), $"is an IndexBuffer, but accessor dimensions is: {dim}"); } if (enc == EncodingType.BYTE) { result.AddLinkError(("BufferView", bv.LogicalIndex), $"is an IndexBuffer, but accessor encoding is (s)byte"); } if (enc == EncodingType.SHORT) { result.AddLinkError(("BufferView", bv.LogicalIndex), $"is an IndexBuffer, but accessor encoding is (s)short"); } if (enc == EncodingType.FLOAT) { result.AddLinkError(("BufferView", bv.LogicalIndex), $"is an IndexBuffer, but accessor encoding is float"); } if (nrm) { result.AddLinkError(("BufferView", bv.LogicalIndex), $"is an IndexBuffer, but accessor is normalized"); } } if (bv.ByteStride > 0) { if (bv.ByteStride < elementByteSize) { result.AddLinkError("ElementByteSize", $"Referenced bufferView's byteStride value {bv.ByteStride} is less than accessor element's length {elementByteSize}."); } // "Accessor's total byteOffset {0} isn't a multiple of componentType length {1}."; return; } var accessorByteLength = bv.GetAccessorByteLength(dim, enc, count); // "Accessor(offset: {0}, length: {1}) does not fit referenced bufferView[% 3] length %4."; result.CheckArrayRangeAccess(("BufferView", bv.LogicalIndex), accessorByteOffset, accessorByteLength, bv.Content); }