public static void UpdatePositionAccessorsBounds(glTFAccessor accessor, Vector3[] positions) { if (positions.Length == 0) { return; } var minX = positions[0].x; var minY = positions[0].y; var minZ = positions[0].z; var maxX = positions[0].x; var maxY = positions[0].y; var maxZ = positions[0].z; foreach (var position in positions) { minX = Mathf.Min(minX, position.x); minY = Mathf.Min(minY, position.y); minZ = Mathf.Min(minZ, position.z); maxX = Mathf.Max(maxX, position.x); maxY = Mathf.Max(maxY, position.y); maxZ = Mathf.Max(maxZ, position.z); } accessor.min = new float[] { minX, minY, minZ }; accessor.max = new float[] { maxX, maxY, maxZ }; }
T[] GetAttrib <T>(glTFAccessor accessor, glTFBufferView view) where T : struct { var attrib = new T[accessor.count]; // var segment = buffers[view.buffer].Storage.GetBytes(); var bytes = new ArraySegment <Byte>(segment.Array, segment.Offset + view.byteOffset + accessor.byteOffset, accessor.count * view.byteStride); bytes.MarshalCoyTo(attrib); return(attrib); }
IEnumerable <int> _GetIndices(glTFAccessor accessor, out int count) { count = accessor.count; var view = bufferViews[accessor.bufferView]; switch ((glComponentType)accessor.componentType) { case glComponentType.UNSIGNED_BYTE: { return(GetAttrib <Byte>(accessor, view).Select(x => (int)(x))); } case glComponentType.UNSIGNED_SHORT: { return(GetAttrib <UInt16>(accessor, view).Select(x => (int)(x))); } case glComponentType.UNSIGNED_INT: { return(GetAttrib <UInt32>(accessor, view).Select(x => (int)(x))); } } throw new NotImplementedException("GetIndices: unknown componenttype: " + accessor.componentType); }
T[] GetAttrib <T>(glTFAccessor accessor, glTFBufferView view) where T : struct { return(GetAttrib <T>(accessor.count, accessor.byteOffset, view)); }
static T[] GetAttrib <T>(this glTF self, glTFAccessor accessor, glTFBufferView view) where T : struct { return(self.GetAttrib <T>(accessor.count, accessor.byteOffset, view)); }
public static void Serialize_gltf_accessors_ITEM(JsonFormatter f, glTFAccessor value) { f.BeginMap(); if (value.bufferView >= 0) { f.Key("bufferView"); f.Value(value.bufferView); } if (value.byteOffset >= 0) { f.Key("byteOffset"); f.Value(value.byteOffset); } if (!string.IsNullOrEmpty(value.type)) { f.Key("type"); f.Value(value.type); } if (true) { f.Key("componentType"); f.Value((int)value.componentType); } if (value.count >= 1) { f.Key("count"); f.Value(value.count); } if (value.max != null && value.max.Length >= 1) { f.Key("max"); Serialize_gltf_accessors__max(f, value.max); } if (value.min != null && value.min.Length >= 1) { f.Key("min"); Serialize_gltf_accessors__min(f, value.min); } if (true) { f.Key("normalized"); f.Value(value.normalized); } if (value.sparse != null) { f.Key("sparse"); Serialize_gltf_accessors__sparse(f, value.sparse); } if (!string.IsNullOrEmpty(value.name)) { f.Key("name"); f.Value(value.name); } if (value.extensions != null) { f.Key("extensions"); value.extensions.Serialize(f); } if (value.extras != null) { f.Key("extras"); value.extras.Serialize(f); } f.EndMap(); }
ArraySegment <Byte> GetBytes(IStorage io, glTFAccessor accessor, glTFBufferView view) { return(GetBytes(io, accessor.ElementSize * accessor.count, accessor.byteOffset, view)); }
public int AddAccessorTo(ExportingGltfData data, int bufferIndex, // GltfBufferTargetType targetType, bool useSparse, Action <NativeArray <byte>, glTFAccessor> minMax = null, int offset = 0, int count = 0) { if (ComponentType == AccessorValueType.FLOAT && AccessorType == AccessorVectorType.VEC3 ) { var values = GetSpan <Vector3>(); // 巨大ポリゴンのモデル対策にValueTupleの型をushort -> uint へ var sparseValuesWithIndex = new List <ValueTuple <int, Vector3> >(); for (int i = 0; i < values.Length; ++i) { var v = values[i]; if (v != Vector3.zero) { sparseValuesWithIndex.Add((i, v)); } } //var status = $"{sparseIndices.Count * 14}/{values.Length * 12}"; if (useSparse && sparseValuesWithIndex.Count > 0 && // avoid empty sparse sparseValuesWithIndex.Count * 16 < values.Length * 12) { // use sparse using (var sparseIndexBin = new NativeArray <byte>(sparseValuesWithIndex.Count * 4, Allocator.Persistent)) using (var sparseValueBin = new NativeArray <byte>(sparseValuesWithIndex.Count * 12, Allocator.Persistent)) { var sparseIndexSpan = sparseIndexBin.Reinterpret <Int32>(1); var sparseValueSpan = sparseValueBin.Reinterpret <Vector3>(1); for (int i = 0; i < sparseValuesWithIndex.Count; ++i) { var(index, value) = sparseValuesWithIndex[i]; sparseIndexSpan[i] = index; sparseValueSpan[i] = value; } var sparseIndexView = data.AppendToBuffer(sparseIndexBin); var sparseValueView = data.AppendToBuffer(sparseValueBin); var accessorIndex = data.Gltf.accessors.Count; var accessor = new glTFAccessor { componentType = (glComponentType)ComponentType, type = AccessorType.ToString(), count = Count, byteOffset = -1, sparse = new glTFSparse { count = sparseValuesWithIndex.Count, indices = new glTFSparseIndices { componentType = (glComponentType)AccessorValueType.UNSIGNED_INT, bufferView = sparseIndexView, }, values = new glTFSparseValues { bufferView = sparseValueView, }, } }; if (minMax != null) { minMax(sparseValueBin, accessor); } data.Gltf.accessors.Add(accessor); return(accessorIndex); } } } var viewIndex = AddViewTo(data, bufferIndex, offset, count); return(AddAccessorTo(data, viewIndex, minMax, 0, count)); }