private void MakeIndexData() { IndexData16 = new IndexData16() { triangleCount = 2, indices = new ushort[2 * 3] }; /* * indices = new ushort[triangleCount * 3]; * * // Indices are encoded using the high water mark encoding from webgl-loader. Indices are decoded as follows: * ushort highest = 0; * for (int i = 0; i < indices.Length; i++) * { * ushort code = reader.ReadUInt16(); * indices[i] = (ushort)(highest - code); * * if (code == 0) * highest++; * } * * */ // triangles to draw anti clockwise IndexData16.indices[0] = 0; IndexData16.indices[1] = 3; IndexData16.indices[2] = 1; IndexData16.indices[3] = 0; IndexData16.indices[4] = 2; IndexData16.indices[5] = 3; }
/// <summary> /// This determines the order the triangles are made. Must be anti clockwise /// </summary> private void MakeTriangleIndices() { // winding should be anticlockwise MeshIndexData16 = new IndexData16() { triangleCount = (uint)_TriangleCount, indices = new ushort[_TriangleCount * 3] }; try { int m = 0; for (int y = 0; y < _GridSize - 1; y++) // bottom row to row just before top { // var r = (y * _GridSize) + y; var r = y * _GridSize; for (int x = 0; x < _GridSize - 1; x++) { /* MeshIndexData16.indices[m] = (ushort)(r); * MeshIndexData16.indices[m+1] = (ushort)(r + _GridSize + 2); * MeshIndexData16.indices[m+2] = (ushort)(r + _GridSize + 1); * MeshIndexData16.indices[m+3] = (ushort)(r); * MeshIndexData16.indices[m+4] = (ushort)(r+1); * MeshIndexData16.indices[m+5] = (ushort)(r + _GridSize + 2); */ // triangle winding order is anti clockwise from SW, NE, NW, SW, SE, NE MeshIndexData16.indices[m] = (ushort)(r); MeshIndexData16.indices[m + 1] = (ushort)(r + _GridSize + 1); MeshIndexData16.indices[m + 2] = (ushort)(r + _GridSize); MeshIndexData16.indices[m + 3] = (ushort)(r); MeshIndexData16.indices[m + 4] = (ushort)(r + 1); MeshIndexData16.indices[m + 5] = (ushort)(r + _GridSize + 1); m = m + 6; r++; } } } catch (Exception exception) { Console.WriteLine($"Unexpected exception: {exception}"); } // high water mark encoding ushort highest = 0; ushort code; for (int i = 0; i < MeshIndexData16.indices.Length; i++) { code = (ushort)(highest - MeshIndexData16.indices[i]); MeshIndexData16.indices[i] = code; if (code == 0) { highest++; } } }
public void EncodeIndices(IndexData16 idxData) { /* * * So you can also get a pretty big win by exploiting pre-transform vertex cache optimization. * I call it “high water mark encoding.” Basically: for such a properly optimized index list, * the next index you see is either (a) one you’ve seen before or (b) one higher than the current highest seen index. * So, instead of encoding actual indices, you can instead encode them relative to this high water mark (the largest index yet to be seen, initialized to 0). * You see “n” and that corresponds to an index of (high water mark – n). When you see 0, you also increment high watermark. * The benefit here is that the encoded indices are very small, and then you can do some kind of varint coding, * then your encoded indices are a bit more than a byte on average. If you plan on zipping later, then make sure the variants are byte-aligned and LSB-first. * * uint16_t highest = 0; * uint16_t code; * * // Write main indices * for (size_t i = 0, icount = mMesh.indices.size(); i < icount; i++) { * code = highest - mMesh.indices[i]; * ostream.write(&code, sizeof(uint16_t)); * if (code == 0) highest++; * } * * // Write all vertices on the edge of the tile (W, S, E, N) * writeEdgeIndices<uint16_t>(ostream, mMesh, bounds.min.x, 0); * writeEdgeIndices<uint16_t>(ostream, mMesh, bounds.min.y, 1); * writeEdgeIndices<uint16_t>(ostream, mMesh, bounds.max.x, 0); * writeEdgeIndices<uint16_t>(ostream, mMesh, bounds.max.y, 1); * } * * */ ushort highest = 0; ushort code; for (int i = 0; i < idxData.indices.Length; i++) { code = (ushort)(highest - idxData.indices[i]); idxData.indices[i] = code; if (code == 0) { highest++; } } /* * triangleCount = reader.ReadUInt32(); * indices = new ushort[triangleCount * 3]; * * // Indices are encoded using the high water mark encoding from webgl-loader. Indices are decoded as follows: * ushort highest = 0; * for (int i = 0; i<indices.Length; i++) * { * ushort code = reader.ReadUInt16(); * indices[i] = (ushort) (highest - code); * * if (code == 0) * highest++; * } */ }