Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void load() {
- int totalOut = mHeader.gridX * mHeader.gridY * mHeader.gridZ;
- int numCubes = 0;
- mBlocks = new List<M4Block>(totalOut);
- Uint8List cubeMask = new Uint8List(totalOut);
- int curOut = 0;
- while(curOut < totalOut) {
- int curByte = mReader.getUInt8();
- if((curByte & 0x80) != 0) {
- int bitMask = curByte & 0x7F;
- int i = 0;
- while(curOut < totalOut && i < 7) {
- if((bitMask & (1 << i)) != 0) {
- cubeMask[curOut] = 1;
- ++numCubes;
- }
- ++i;
- ++curOut;
- }
- } else {
- int value = ((curByte & 0x40) >> 6);
- int repeat = curByte & 0x3F;
- int i = 0;
- while(curOut < totalOut && i < repeat) {
- cubeMask[curOut] = value;
- if(value != 0) {
- ++numCubes;
- }
- ++i;
- ++curOut;
- }
- }
- }
- int cur = 0;
- var rnd = new Random();
- mVertexData = new VertexData(28, numCubes * 24);
- mIndices = new Uint32List(numCubes * 36);
- mIndicesWireframe = new Uint32List(numCubes * 48);
- for(int x = 0; x < mHeader.gridX; ++x) {
- for(int y = 0; y < mHeader.gridY; ++y) {
- for(int z = 0; z < mHeader.gridZ; ++z) {
- if(cubeMask[cur] == 0) {
- mBlocks[cur] = null;
- ++cur;
- continue;
- }
- M4Block block;
- if(mParent.version > 1) {
- var colorIndex = mReader.getUInt8();
- block = new M4Block(mColorTable[colorIndex], colorIndex);
- } else {
- block = new M4Block(0xFFFFFFFF, 0);
- }
- mBlocks[cur] = block;
- int r = rnd.nextInt(255);
- int g = rnd.nextInt(255);
- int b = rnd.nextInt(255);
- int a = 0xFF;
- int color = (r & 0xFF) | ((g & 0xFF) << 8) | ((b & 0xFF) << 16) | ((a & 0xFF) << 24);
- num cx = mHeader.bboxMin.x + x * mHeader.blockSize;
- num cz = mHeader.bboxMin.y + y * mHeader.blockSize;
- num cy = mHeader.bboxMin.z + z * mHeader.blockSize;
- Vector3 bottom = new Vector3(cx, cy, cz);
- Vector3 top = new Vector3(cx + mHeader.blockSize, cy + mHeader.blockSize, cz + mHeader.blockSize);
- this.writeBlock(bottom, top, color);
- ++cur;
- }
- }
- }
- mCubeCount = numCubes;
- mVertexBuffer.setDataTyped(mVertexData.data);
- mIndexBuffer.setIndices(mIndices);
- mIndexBufferWF.setIndices(mIndicesWireframe);
- VertexElement colorElem = new VertexElement(VertexSemantic.Color, 0, 4, type: DataType.Byte);
- colorElem.setNormalized();
- mGeometry.getDrawState().BlendEnabled = true;
- mGeometry..setProgram(ProgramCollection.instance.get("M4"))
- ..setIndexBuffer(mIndexBuffer)
- ..setVertexBuffer(mVertexBuffer)
- ..setStride(28)
- ..setTriangleCount(numCubes * 12)
- ..setVertexCount(numCubes * 24)
- ..addNewElement(VertexSemantic.Position, 0, 3)
- ..addNewElement(VertexSemantic.Normal, 0, 3)
- ..addElement(colorElem)
- ..finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement