Skip to content

Commit

Permalink
New Icon and better lighting data handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stackotter committed Jun 13, 2021
1 parent c5bfc08 commit d19ac4a
Show file tree
Hide file tree
Showing 30 changed files with 360 additions and 112 deletions.
12 changes: 12 additions & 0 deletions DeltaClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
78052348261433A800079A01 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7805221C261433A700079A01 /* Assets.xcassets */; };
780523B6261433A800079A01 /* DeltaClientApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7805229D261433A700079A01 /* DeltaClientApp.swift */; };
781829A5262A6F7E000F59B0 /* ChunkLighting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 781829A4262A6F7E000F59B0 /* ChunkLighting.swift */; };
781B150D2675615A001AE32B /* BinaryUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 781B150C2675615A001AE32B /* BinaryUtil.swift */; };
781B151226756854001AE32B /* ChunkLightingUpdateData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 781B151126756854001AE32B /* ChunkLightingUpdateData.swift */; };
781B151A267569D1001AE32B /* UpdateChunkLighting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 781B1519267569D1001AE32B /* UpdateChunkLighting.swift */; };
781F48BC266651B00030F71C /* SetBlock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 781F48BB266651B00030F71C /* SetBlock.swift */; };
7824FC2B2662F60300414468 /* InteractiveMTKView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7824FC2A2662F60300414468 /* InteractiveMTKView.swift */; };
7824FC332662F67100414468 /* InputState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7824FC322662F67100414468 /* InputState.swift */; };
Expand Down Expand Up @@ -419,6 +422,9 @@
780522A9261433A700079A01 /* DeltaClientTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeltaClientTests.swift; sourceTree = "<group>"; };
780522AA261433A700079A01 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
781829A4262A6F7E000F59B0 /* ChunkLighting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChunkLighting.swift; sourceTree = "<group>"; };
781B150C2675615A001AE32B /* BinaryUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryUtil.swift; sourceTree = "<group>"; };
781B151126756854001AE32B /* ChunkLightingUpdateData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChunkLightingUpdateData.swift; sourceTree = "<group>"; };
781B1519267569D1001AE32B /* UpdateChunkLighting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateChunkLighting.swift; sourceTree = "<group>"; };
781F48BB266651B00030F71C /* SetBlock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetBlock.swift; sourceTree = "<group>"; };
7824FC2A2662F60300414468 /* InteractiveMTKView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InteractiveMTKView.swift; sourceTree = "<group>"; };
7824FC322662F67100414468 /* InputState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputState.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1167,6 +1173,7 @@
7868887426243D9E00BD3F03 /* MathUtil.swift */,
78D91829261FB4CC00A4782F /* RequestMethod.swift */,
78D7E4CA2633D6FD00FB1A3B /* URL.swift */,
781B150C2675615A001AE32B /* BinaryUtil.swift */,
78D91837261FB4CC00A4782F /* C */,
78FA271D2664FABB00F3BDF5 /* Observable.swift */,
78FA27272664FD4400F3BDF5 /* Event.swift */,
Expand Down Expand Up @@ -1678,6 +1685,7 @@
78D91861261FB4CC00A4782F /* Chunk.swift */,
78D9185D261FB4CC00A4782F /* ChunkSection.swift */,
781829A4262A6F7E000F59B0 /* ChunkLighting.swift */,
781B151126756854001AE32B /* ChunkLightingUpdateData.swift */,
78D91862261FB4CC00A4782F /* ChunkData.swift */,
787BEBEE2674BEBD00F50B2C /* UnpackedChunkData.swift */,
);
Expand All @@ -1692,6 +1700,7 @@
787BEBF62674C2B300F50B2C /* UpdateChunk.swift */,
78FA273E266501F300F3BDF5 /* RemoveChunk.swift */,
781F48BB266651B00030F71C /* SetBlock.swift */,
781B1519267569D1001AE32B /* UpdateChunkLighting.swift */,
);
path = Event;
sourceTree = "<group>";
Expand Down Expand Up @@ -1878,6 +1887,7 @@
files = (
78D91A54261FB4CE00A4782F /* InboundNetworkLayer.swift in Sources */,
78E232522628138300B08B4B /* EventProtocol.swift in Sources */,
781B150D2675615A001AE32B /* BinaryUtil.swift in Sources */,
78D91A46261FB4CD00A4782F /* CloseWindowClientboundPacket.swift in Sources */,
78D91AA0261FB4E100A4782F /* ServerListView.swift in Sources */,
78D919EC261FB4CD00A4782F /* KeepAliveServerboundPacket.swift in Sources */,
Expand Down Expand Up @@ -1955,6 +1965,7 @@
78D91A70261FB4CE00A4782F /* CardinalDirection.swift in Sources */,
78D9196D261FB4CD00A4782F /* CampfireCookingRecipe.swift in Sources */,
78D91A43261FB4CD00A4782F /* VehicleMoveClientboundPacket.swift in Sources */,
781B151226756854001AE32B /* ChunkLightingUpdateData.swift in Sources */,
78D919AF261FB4CD00A4782F /* ServerDescriptor.swift in Sources */,
787EA9E3266E12560019C35B /* CallerComponent.swift in Sources */,
78D91A86261FB4CE00A4782F /* MinecraftLocale.swift in Sources */,
Expand Down Expand Up @@ -2208,6 +2219,7 @@
78E0C13126337F82009D882D /* MicrosoftAuth.swift in Sources */,
78D91A50261FB4CD00A4782F /* SetCompressionPacket.swift in Sources */,
78D91991261FB4CD00A4782F /* PlayerEntityAction.swift in Sources */,
781B151A267569D1001AE32B /* UpdateChunkLighting.swift in Sources */,
78640853262C545C00FE3A10 /* CacheBlockModel.pb.swift in Sources */,
78D9195B261FB4CD00A4782F /* SmokingRecipe.swift in Sources */,
78D919D4261FB4CD00A4782F /* VehicleMoveServerboundPacket.swift in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions DeltaClient/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
{
"images" : [
{
"filename" : "Icon-MacOS-16x16@1x.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"filename" : "Icon-MacOS-32x32@1x actual.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"filename" : "Icon-MacOS-32x32@1x actual-1.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"filename" : "Icon-MacOS-32x32@2x.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"filename" : "Icon-MacOS-128x128@1x.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"filename" : "Icon-MacOS-128x128@2x.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"filename" : "Icon-MacOS-256x256@1x.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"filename" : "Icon-MacOS-256x256@2x.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"filename" : "Icon-MacOS-512x512@1x.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"filename" : "Icon-MacOS-512x512@2x.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion DeltaClient/Sources/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Client {
return
}
let position = Position(x: x, y: y, z: z)
if let lighting = server.world?.lighting[position.chunk] {
if let lighting = server.world?.chunk(at: position.chunk)?.lighting {
log.info("skyLight: \(lighting.getSkyLightLevel(at: position))")
log.info("blockLight: \(lighting.getBlockLightLevel(at: position))")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,15 @@ struct UpdateLightPacket: ClientboundPacket {
emptyBlockLightMask = packetReader.readVarInt()

skyLightArrays = []
var numArrays = 0
for i in 0..<16 {
numArrays += Int(skyLightMask >> i) & 0x01
}
var numArrays = BinaryUtil.setBits(of: skyLightMask, n: Chunk.numSections).count
for _ in 0..<numArrays {
let length = packetReader.readVarInt()
let bytes = packetReader.readByteArray(length: length)
skyLightArrays.append(bytes)
}

blockLightArrays = []
numArrays = 0
for i in 0..<16 {
numArrays += Int(blockLightMask >> i) & 0x01
}
numArrays = BinaryUtil.setBits(of: blockLightMask, n: Chunk.numSections).count
for _ in 0..<numArrays {
let length = packetReader.readVarInt()
let bytes = packetReader.readByteArray(length: length)
Expand All @@ -54,36 +48,16 @@ struct UpdateLightPacket: ClientboundPacket {
}

func handle(for server: Server) throws {
// NOTE: we just ignore the first and last sections sent for now (above and below the world)
var skyLightIndex = 0
var blockLightIndex = 0
if let world = server.world {
let chunkLighting = world.lighting[chunkPosition] ?? ChunkLighting()
for i in 0..<(Chunk.numSections + 1) {
let sectionNum = i - 1
if (skyLightMask >> i) & 0x1 == 1 {
if i == 0 {
skyLightIndex += 1
continue
}
chunkLighting.updateSectionSkyLight(with: skyLightArrays[skyLightIndex], for: sectionNum)
} else if (emptySkyLightMask >> i) & 0x1 == 1 {
// empty sky light section
chunkLighting.updateSectionSkyLight(with: [UInt8](repeating: 0, count: 2048), for: sectionNum)
}

if (blockLightMask >> i) & 0x1 == 1 {
if i == 0 {
blockLightIndex += 1
continue
}
chunkLighting.updateSectionBlockLight(with: blockLightArrays[blockLightIndex], for: sectionNum)
} else if (emptyBlockLightMask >> i) & 0x1 == 1 {
// empty block light section
chunkLighting.updateSectionBlockLight(with: [UInt8](repeating: 0, count: 2048), for: sectionNum)
}
}
world.lighting[chunkPosition] = chunkLighting
let data = ChunkLightingUpdateData(
trustEdges: trustEdges,
skyLightMask: skyLightMask,
blockLightMask: blockLightMask,
emptySkyLightMask: emptySkyLightMask,
emptyBlockLightMask: emptyBlockLightMask,
skyLightArrays: skyLightArrays,
blockLightArrays: blockLightArrays)
world.updateChunkLighting(at: chunkPosition, with: data)
}
}
}
90 changes: 69 additions & 21 deletions DeltaClient/Sources/Render/Mesh/ChunkSectionMesh.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ class ChunkSectionMesh: Mesh {
uv: face.uvs[uvIndex],
light: face.light,
textureIndex: face.textureIndex,
tintIndex: face.tintIndex)
tintIndex: face.tintIndex,
skyLightLevel: 15,
blockLightLevel: 15)
)
}
}
Expand All @@ -136,52 +138,71 @@ class ChunkSectionMesh: Mesh {
always be returned. If the block at `sectionIndex` is at y-level 0 or 255 the down or up neighbours
will be omitted respectively (as there will be none). Otherwise, all neighbours are included.
The function is implemented the verbose non-dynamic way it is to improve performance.
- Returns: A a mapping from each possible direction to a corresponding block state.
- Returns: A mapping from each possible direction to a corresponding block state.
*/
func getNeighbouringBlockStates(ofBlockAt index: Int) -> [FaceDirection: UInt16] {
// convert a section relative index to a chunk relative index
let indexInChunk = index + sectionPosition.sectionY * Chunk.Section.numBlocks
var neighbouringBlockStates: [FaceDirection: UInt16] = [:]

let neighbourIndices = getNeighbourIndices(ofBlockAt: index)
for (faceDirection, neighbourBlock) in neighbourIndices {
let blockState: UInt16?
if let direction = neighbourBlock.chunkDirection {
blockState = neighbourChunks[direction]?.getBlock(at: neighbourBlock.index)
} else {
blockState = chunk.getBlock(at: neighbourBlock.index)
}
neighbouringBlockStates[faceDirection] = blockState
}

return neighbouringBlockStates
}

/**
Returns a map from each direction to a cardinal direction and a chunk relative block index.
The cardinal direction is which chunk a neighbour resides in. If the cardinal direction for
a neighbour is nil then the neighbour is in the current chunk.
- Parameter index: A section-relative block index
*/
func getNeighbourIndices(ofBlockAt index: Int) -> [FaceDirection: (chunkDirection: CardinalDirection?, index: Int)] {
let indexInChunk = index + sectionPosition.sectionY * Chunk.Section.numBlocks
var neighbouringIndices: [FaceDirection: (chunkDirection: CardinalDirection?, index: Int)] = [:]

if indexInChunk % Chunk.blocksPerLayer >= Chunk.width {
neighbouringBlockStates[.north] = chunk.getBlock(at: indexInChunk - Chunk.width)
neighbouringIndices[.north] = (nil, indexInChunk - Chunk.width)
} else {
let neighbourBlockIndex = indexInChunk + Chunk.blocksPerLayer - Chunk.width
neighbouringBlockStates[.north] = neighbourChunks[.north]?.getBlock(at: neighbourBlockIndex)
neighbouringIndices[.north] = (chunkDirection: .north, index: indexInChunk + Chunk.blocksPerLayer - Chunk.width)
}

if indexInChunk % Chunk.blocksPerLayer < Chunk.blocksPerLayer - Chunk.width {
neighbouringBlockStates[.south] = chunk.getBlock(at: indexInChunk + Chunk.width)
neighbouringIndices[.south] = (nil, indexInChunk + Chunk.width)
} else {
let neighbourBlockIndex = indexInChunk - Chunk.blocksPerLayer + Chunk.width
neighbouringBlockStates[.south] = neighbourChunks[.south]?.getBlock(at: neighbourBlockIndex)
neighbouringIndices[.south] = (chunkDirection: .south, index: indexInChunk - Chunk.blocksPerLayer + Chunk.width)
}

if indexInChunk % Chunk.width != Chunk.width - 1 {
neighbouringBlockStates[.east] = chunk.getBlock(at: indexInChunk + 1)
neighbouringIndices[.east] = (nil, indexInChunk + 1)
} else {
let neighbourBlockIndex = indexInChunk - 15
neighbouringBlockStates[.east] = neighbourChunks[.east]?.getBlock(at: neighbourBlockIndex)
neighbouringIndices[.east] = (chunkDirection: .east, index: indexInChunk - 15)
}

if indexInChunk % Chunk.width != 0 {
neighbouringBlockStates[.west] = chunk.getBlock(at: indexInChunk - 1)
neighbouringIndices[.west] = (nil, indexInChunk - 1)
} else {
let neighbourBlockIndex = indexInChunk + 15
neighbouringBlockStates[.west] = neighbourChunks[.west]?.getBlock(at: neighbourBlockIndex)
neighbouringIndices[.west] = (chunkDirection: .west, index: indexInChunk + 15)
}

if indexInChunk < Chunk.numBlocks - Chunk.blocksPerLayer {
neighbouringBlockStates[.up] = chunk.getBlock(at: indexInChunk + Chunk.blocksPerLayer)
neighbouringIndices[.up] = (nil, indexInChunk + Chunk.blocksPerLayer)
}

if indexInChunk >= Chunk.blocksPerLayer {
neighbouringBlockStates[.down] = chunk.getBlock(at: indexInChunk - Chunk.blocksPerLayer)
neighbouringIndices[.down] = (nil, indexInChunk - Chunk.blocksPerLayer)
}

return neighbouringBlockStates
return neighbouringIndices
}

/**
Expand Down Expand Up @@ -212,7 +233,34 @@ class ChunkSectionMesh: Mesh {
return cullingNeighbours
}

/// Generates a lookup table to quickly convert from section block index to block position
/// Returns the sky light level for the block at the specified section-relative block index.
func getSkyLightLevel(ofBlockAt index: Int) -> UInt8 {
let neighbourIndices = getNeighbourIndices(ofBlockAt: index)
var levels: [UInt8] = []
for (_, neighbourBlock) in neighbourIndices {
let level: UInt8?
if let direction = neighbourBlock.chunkDirection {
level = neighbourChunks[direction]?.lighting.getSkyLightLevel(atIndex: neighbourBlock.index)
} else {
level = chunk.lighting.getSkyLightLevel(atIndex: neighbourBlock.index)
}
levels.append(level ?? ChunkLighting.defaultSkyLightLevel)
}

// get light levels for top and bottom blocks in chunk
let indexAdjustment = Chunk.Section.numBlocks - Chunk.blocksPerLayer
if index < Chunk.blocksPerLayer && sectionPosition.sectionY == 0 {
let lightIndex = index + indexAdjustment
levels.append(chunk.lighting.getSkyLightLevel(atIndex: lightIndex, inSectionAt: -1))
} else if index > indexAdjustment && sectionPosition.sectionY == (Chunk.numSections - 1) {
let lightIndex = index - indexAdjustment
levels.append(chunk.lighting.getSkyLightLevel(atIndex: lightIndex, inSectionAt: Chunk.numSections))
}

return levels.max() ?? 0
}

/// Generates a lookup table to quickly convert from section block index to block position.
private static func generateIndexLookup() -> [Position] {
var lookup: [Position] = []
for y in 0..<16 {
Expand Down
6 changes: 6 additions & 0 deletions DeltaClient/Sources/Render/Renderer/ChunkRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class ChunkRenderer {

private var blockPaletteManager: BlockPaletteManager

var frozenSectionCount: Int {
frozenSectionsAccessQueue.sync {
return frozenSections.count
}
}

init(
for chunk: Chunk,
at position: ChunkPosition,
Expand Down
2 changes: 2 additions & 0 deletions DeltaClient/Sources/Render/Renderer/Shader/ChunkShaders.metal
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct Vertex
float light;
uint16_t textureIndex;
int8_t tintIndex;
int8_t skyLightLevel;
int8_t blockLightLevel;
};

struct RasteriserData
Expand Down
12 changes: 7 additions & 5 deletions DeltaClient/Sources/Render/Renderer/Shader/Vertex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import Foundation
import simd

struct Vertex {
var position: simd_float3
var uv: simd_float2
var light: Float
var textureIndex: uint16
var tintIndex: Int8
let position: simd_float3
let uv: simd_float2
let light: Float
let textureIndex: uint16
let tintIndex: Int8
let skyLightLevel: UInt8
let blockLightLevel: UInt8
}
Loading

0 comments on commit d19ac4a

Please sign in to comment.