Skip to content

Commit

Permalink
Further optimization, render distance now ~14
Browse files Browse the repository at this point in the history
  • Loading branch information
RepublicOfMars committed Apr 30, 2021
1 parent 6afe250 commit bb75fba
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
6 changes: 4 additions & 2 deletions Sources/ScenesShell/Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Block {
var breaking = false
var hardness : Int
var isVisible = true

var sidesToRender : [String] = []

func updateBlock() {
switch type{
Expand Down Expand Up @@ -117,7 +119,7 @@ class Block {
let blockColor = Color(red:UInt8(Double(color.red) * timeOfDayMultiplier),
green:UInt8(Double(color.green) * timeOfDayMultiplier),
blue:UInt8(Double(color.blue) * timeOfDayMultiplier))
Cube(center:location.convertToDouble()).renderCube(camera:camera, canvas:canvas, color:blockColor, outline:selected)
Cube(center:location.convertToDouble(), sidesToRender:sidesToRender).renderCube(camera:camera, canvas:canvas, color:blockColor, outline:selected)
} else {
var colorMultiplier = 0.0

Expand All @@ -128,7 +130,7 @@ class Block {
let redBreak = UInt8(Double(color.red) * colorMultiplier * timeOfDayMultiplier)
let greenBreak = UInt8(Double(color.green) * colorMultiplier * timeOfDayMultiplier)
let blueBreak = UInt8(Double(color.blue) * colorMultiplier * timeOfDayMultiplier)
Cube(center:location.convertToDouble()).renderCube(camera:camera, canvas:canvas, color:Color(red:redBreak, green:greenBreak, blue:blueBreak), outline:selected)
Cube(center:location.convertToDouble(), sidesToRender:sidesToRender).renderCube(camera:camera, canvas:canvas, color:Color(red:redBreak, green:greenBreak, blue:blueBreak), outline:selected)
self.breaking = false
}
}
Expand Down
28 changes: 21 additions & 7 deletions Sources/ScenesShell/Cube.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@ import Scenes
class Cube {
var center : Point3d
var size : Double
var sidesToRender : [String] = []

init(center:Point3d, size:Double=1.0) {
init(center:Point3d, size:Double=1.0, sidesToRender:[String]=["+x", "-x", "+y", "-y", "+z", "-z"]) {
self.center = center
self.size = size
self.sidesToRender = sidesToRender
}

func getSquares() -> [Square] { //get squares for sides of the cube
var sides : [Square] = []

sides.append(Square(center:Point3d(x:center.x+(size/2),y:center.y,z:center.z), axis:"x", size:size))
sides.append(Square(center:Point3d(x:center.x-(size/2),y:center.y,z:center.z), axis:"x", size:size))
sides.append(Square(center:Point3d(x:center.x,y:center.y+(size/2),z:center.z), axis:"y", size:size))
sides.append(Square(center:Point3d(x:center.x,y:center.y-(size/2),z:center.z), axis:"y", size:size))
sides.append(Square(center:Point3d(x:center.x,y:center.y,z:center.z+(size/2)), axis:"z", size:size))
sides.append(Square(center:Point3d(x:center.x,y:center.y,z:center.z-(size/2)), axis:"z", size:size))
if sidesToRender.contains("+x") {
sides.append(Square(center:Point3d(x:center.x+(size/2),y:center.y,z:center.z), axis:"x", size:size))
}
if sidesToRender.contains("-x") {
sides.append(Square(center:Point3d(x:center.x-(size/2),y:center.y,z:center.z), axis:"x", size:size))
}
if sidesToRender.contains("+y") {
sides.append(Square(center:Point3d(x:center.x,y:center.y+(size/2),z:center.z), axis:"y", size:size))
}
if sidesToRender.contains("-y") {
sides.append(Square(center:Point3d(x:center.x,y:center.y-(size/2),z:center.z), axis:"y", size:size))
}
if sidesToRender.contains("+z") {
sides.append(Square(center:Point3d(x:center.x,y:center.y,z:center.z+(size/2)), axis:"z", size:size))
}
if sidesToRender.contains("-z") {
sides.append(Square(center:Point3d(x:center.x,y:center.y,z:center.z-(size/2)), axis:"z", size:size))
}

return sides
}
Expand Down
31 changes: 20 additions & 11 deletions Sources/ScenesShell/NewWorld.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class SimpleWorld {
private func nearbyBlocks(cameraPosition:Point3d) -> [Block] {
var output : [Block] = []
var workingArray : [Double] = []
var sidesRendered = 0

for y in verticalInBounds(Int(cameraPosition.y)-renderDistance) ... verticalInBounds(Int(cameraPosition.y)+renderDistance) {
for x in horizontalInBounds(Int(cameraPosition.x)-renderDistance) ... horizontalInBounds(Int(cameraPosition.x)+renderDistance) {
Expand All @@ -125,15 +126,16 @@ class SimpleWorld {
if block.isVisible && block.type != "air" {
output.append(block)
workingArray.append(block.location.convertToDouble().distanceFrom(point:cameraPosition))
sidesRendered += block.sidesToRender.count
}
}
}
}

if output.count > 448 && renderDistance > 2 {
if sidesRendered > 1800 && renderDistance > 2 {
renderDistance -= 1
}
if output.count < 320 && renderDistance < 12 {
if sidesRendered < 1500 && renderDistance < 32 {
renderDistance += 1
}

Expand Down Expand Up @@ -163,6 +165,10 @@ class SimpleWorld {
updateNeighborVisibility(at:at)
}
}

private func updateBlockSidesToRender(at:BlockPoint3d, to:[String]) {
Blocks[verticalInBounds(at.y)][horizontalInBounds(at.x)][horizontalInBounds(at.z)].sidesToRender = to
}

private func createTree(at:BlockPoint3d) {
let trunkHeight = Int.random(in:3...5)
Expand Down Expand Up @@ -190,18 +196,21 @@ class SimpleWorld {

public func updateBlockVisibility(at:BlockPoint3d) {
var visible = false
if getBlock(at:BlockPoint3d(x:at.x+1, y:at.y, z:at.z)).type == "air" {visible = true}
if getBlock(at:BlockPoint3d(x:at.x-1, y:at.y, z:at.z)).type == "air" {visible = true}
if getBlock(at:BlockPoint3d(x:at.x, y:at.y+1, z:at.z)).type == "air" {visible = true}
if getBlock(at:BlockPoint3d(x:at.x, y:at.y-1, z:at.z)).type == "air" {visible = true}
if getBlock(at:BlockPoint3d(x:at.x, y:at.y, z:at.z+1)).type == "air" {visible = true}
if getBlock(at:BlockPoint3d(x:at.x, y:at.y, z:at.z-1)).type == "air" {visible = true}
var sidesToRender : [String] = []
if getBlock(at:BlockPoint3d(x:at.x+1, y:at.y, z:at.z)).type == "air" {visible = true; sidesToRender.append("+x")}
if getBlock(at:BlockPoint3d(x:at.x-1, y:at.y, z:at.z)).type == "air" {visible = true; sidesToRender.append("-x")}
if getBlock(at:BlockPoint3d(x:at.x, y:at.y+1, z:at.z)).type == "air" {visible = true; sidesToRender.append("+y")}
if getBlock(at:BlockPoint3d(x:at.x, y:at.y-1, z:at.z)).type == "air" {visible = true; sidesToRender.append("-y")}
if getBlock(at:BlockPoint3d(x:at.x, y:at.y, z:at.z+1)).type == "air" {visible = true; sidesToRender.append("+z")}
if getBlock(at:BlockPoint3d(x:at.x, y:at.y, z:at.z-1)).type == "air" {visible = true; sidesToRender.append("-z")}

if visible {
setBlock(at:BlockPoint3d(x:at.x, y:at.y, z:at.z), to:"visible")
setBlock(at:at, to:"visible")
} else {
setBlock(at:BlockPoint3d(x:at.x, y:at.y, z:at.z), to:"invisible")
setBlock(at:at, to:"invisible")
}

updateBlockSidesToRender(at:at, to:sidesToRender)
}

public func updateNeighborVisibility(at:BlockPoint3d) {
Expand Down Expand Up @@ -289,7 +298,7 @@ class SimpleWorld {
canvas.render(FillStyle(color:Color(red:255, green:255, blue:0)))
canvas.render(splash)

let version = Text(location:Point(x:0, y:(canvas.canvasSize!.height)), text:" v0.3.2")
let version = Text(location:Point(x:0, y:(canvas.canvasSize!.height)), text:" v0.3.3")
version.font = "\((canvas.canvasSize!.height/64))pt Arial"
version.baseline = .bottom
version.alignment = .left
Expand Down
12 changes: 6 additions & 6 deletions Sources/ScenesShell/Path3d.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ class Path3d {
canvas.render(StrokeStyle(color:Color(red:64, green:64, blue:64)))
} else {
var colorOutline = (red:color.red, green:color.green, blue:color.blue)
if color.red > 0 {
colorOutline.red -= 1
if color.red >= 4 {
colorOutline.red -= 4
}
if color.green > 0 {
colorOutline.green -= 1
if color.green >= 4 {
colorOutline.green -= 4
}
if color.blue > 0 {
colorOutline.blue -= 1
if color.blue >= 4 {
colorOutline.blue -= 4
}
canvas.render(StrokeStyle(color:Color(red:colorOutline.red, green:colorOutline.green, blue:colorOutline.blue)))
}
Expand Down
11 changes: 10 additions & 1 deletion Sources/ScenesShell/Splash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ public func splashText() -> String {
"No more walking through walls!",
"The IGIS servers are on fire right now!",
"Now with better Render Distance!"]

let quotes = ["\"I'm a doctor, not a world-builder!\" -McCoy, early 2300s",
"\"How do you have so much free time?\" -Garrett, 2021",
"\"Eat any good books lately?\" -Q, late 2300s",
"\"Stop playing Minecraft in class\" -All of my teachers, 2021"]

return text[Int.random(in:0..<text.count)]
if Int.random(in:0..<3) != 0 {
return text[Int.random(in:0..<text.count)]
} else {
return quotes[Int.random(in:0..<quotes.count)]
}
}

0 comments on commit bb75fba

Please sign in to comment.