Skip to content

Commit c8b5401

Browse files
committed
Webpack prebuild & items atlas optimization
1 parent b3998f2 commit c8b5401

File tree

1,004 files changed

+294
-8212
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,004 files changed

+294
-8212
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
node_modules
2+
src/client/dist/*
3+
src/client/static/items/*
4+
src/client/static/blocks/blocksDef.json
5+
src/client/static/blocks/blocksMapping-full.json
6+
src/client/static/blocks/blocksMapping.json
7+
src/client/static/blocks/blocksAtlas-full.png

coffee/client/js/InventoryBar.coffee

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ class InventoryBar
7878
if $(list[i]).attr('data-texture') is ""
7979
url=""
8080
else
81-
url="'/assets/items/#{$(list[i]).attr('data-texture')}.png'"
81+
url="/assets/items/itemsAtlas-full.png"
82+
tex=43
83+
items=@game.al.get "itemsMapping"
84+
$(list[i]).css("background-repeat","no-repeat")
85+
pos=items[$(list[i]).attr('data-texture')]
86+
$(list[i]).css("background-position","-#{(pos.x-1)*tex}px -#{(pos.y-1)*tex}px")
87+
$(list[i]).css("background-size","calc(1600px * #{tex/50})")
8288
$(list[i]).css("background-image","url(#{url})")
8389
$(list[i]).html("<div style='z-index:99;text-align:right;position:relative;bottom:-22px;color:white;font-weight:bold;'>"+$(list[i]).attr('data-amount')+"</div>")
8490
if $(list[i]).attr('data-amount') is "0" or $(list[i]).attr('data-amount') is "1"

coffee/client/js/index.coffee

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {PlayerInInventory} from "./PlayerInInventory.js"
1616
import {BlockBreak} from "./BlockBreak.js"
1717
import {BlockPlace} from "./BlockPlace.js"
1818

19+
1920
class Game
2021
constructor:(options)->
2122
_this=@
@@ -138,17 +139,19 @@ class Game
138139
@params=
139140
fog:false
140141
chunkdist:3
141-
color = new THREE.Color "#adc8ff"
142-
near = 0.5*16
143-
far = 2.5*16
144-
# scene.fog = new THREE.Fog color, near, far
145142
gui.add( @params, "fog" ).name( "Enable fog" ).listen().onChange ()->
146143
if _this.params.fog
147-
_this.scene.fog = new THREE.Fog color, near, far
144+
_this.scene.fog = new THREE.Fog (new THREE.Color "#adc8ff"), (_this.params.chunkdist-2.5)*16, (_this.params.chunkdist-0.5)*16
148145
else
149146
_this.scene.fog = null
150147
gui.add( @world.material, "wireframe" ).name( "Wireframe" ).listen()
151-
gui.add( @params, "chunkdist",0,10,1).name( "Render distance" ).listen()
148+
chunkDist=gui.add( @params, "chunkdist",0,10,1).name( "Render distance" ).listen()
149+
chunkDist.onChange (val)->
150+
if _this.scene.fog isnt null
151+
_this.scene.fog.near=(val-2.5)*16
152+
_this.scene.fog.far=(val-0.5)*16
153+
console.log val
154+
return
152155
@mouse=false
153156
$(document).mousedown (e)->
154157
if e.which is 1

coffee/client/webpack.common.coffee

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ module.exports=
1111
entry: './js/index.js'
1212
output:
1313
path: "#{__dirname}/dist"
14-
filename: '[name].bundle.js'
14+
filename: '[contenthash].js'
1515
performance:
1616
maxEntrypointSize: 1.5e6
1717
maxAssetSize: 1.5e6
18-
devtool: 'inline-source-map'
1918
module:
2019
rules: [
2120
{

coffee/client/webpack.dev.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
merge=require "webpack-merge"
33
common=require "./webpack.common.js"
44
module.exports=merge.merge common,
5+
devtool: 'inline-source-map'
56
mode: "development"
67
devServer:
78
contentBase: "#{__dirname}/public"

coffee/server/atlas.coffee

Lines changed: 0 additions & 87 deletions
This file was deleted.

coffee/server/atlasCreator.coffee

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
path=require "path"
2+
fs=require "fs"
3+
Canvas=require "canvas"
4+
5+
module.exports=(pref,size,xpath,buildPath,totalImages,atlasSize,mini,miniAtlasSize)->
6+
createCanvas=Canvas.createCanvas
7+
loadImage=Canvas.loadImage
8+
Image=Canvas.Image
9+
10+
toxelX=1
11+
toxelY=1
12+
miniX=1
13+
miniY=1
14+
loadedImages=0
15+
16+
if not fs.existsSync buildPath
17+
fs.mkdirSync buildPath
18+
19+
20+
canvas=createCanvas atlasSize*size,atlasSize*size
21+
ctx=canvas.getContext '2d'
22+
23+
images={}
24+
textureMapping={}
25+
miniMapping={}
26+
27+
firstLoad=->
28+
fs.readdir xpath, (err, files)->
29+
files.forEach (file)->
30+
filePath="#{xpath}/#{file}"
31+
if path.extname(file) is ".png"
32+
addImageToLoad filePath,file
33+
return
34+
return
35+
return
36+
addImageToLoad=(filePath,name)->
37+
img=new Image
38+
img.onload=->
39+
images[name]=img
40+
loadedImages++
41+
if loadedImages is totalImages
42+
forEachToxel()
43+
img.src=filePath
44+
forEachToxel=->
45+
Object.keys(images).forEach (name)->
46+
img=images[name]
47+
addToxelToAtlas img,name
48+
updateAtlas()
49+
addToxelToAtlas=(img,name)->
50+
w=img.width/size
51+
h=img.height/size
52+
if w>1 or h>1
53+
for i in [0..w-1]
54+
for j in [0..h-1]
55+
ctx.drawImage img,i*size,j*size,size,size,(toxelX-1)*size, (toxelY-1)*size, size,size
56+
textureMapping[name.substr(0,name.length-4)+"@#{i}@#{j}"]={x:toxelX,y:toxelY}
57+
moveToxel()
58+
else
59+
ctx.drawImage img, (toxelX-1)*size, (toxelY-1)*size, size,size
60+
textureMapping[name.substr(0,name.length-4)]={x:toxelX,y:toxelY}
61+
moveToxel()
62+
if mini
63+
miniMapping[name.substr(0,name.length-4)]={x:miniX,y:miniY}
64+
if miniX is miniAtlasSize
65+
miniX=1
66+
miniY+=1
67+
else
68+
miniX+=1
69+
moveToxel=->
70+
if toxelX is atlasSize
71+
toxelX=1
72+
toxelY+=1
73+
else
74+
toxelX+=1
75+
updateAtlas=(path)->
76+
fs.writeFileSync "#{buildPath}/#{pref}Atlas-full.png", canvas.toBuffer('image/png')
77+
console.log "\x1b[33mSAVING: #{buildPath}/#{pref}Atlas-full.png"
78+
79+
fs.writeFileSync "#{buildPath}/#{pref}Mapping-full.json",JSON.stringify(textureMapping,null,2)
80+
console.log "\x1b[33mSAVING: #{buildPath}/#{pref}Mapping-full.json"
81+
if mini
82+
fs.writeFileSync "#{buildPath}/#{pref}Mapping.json",JSON.stringify(miniMapping,null,2)
83+
console.log "\x1b[33mSAVING: #{buildPath}/#{pref}Mapping.json"
84+
85+
console.log "\x1b[32mSuccessfully created #{canvas.width}x#{canvas.height} Texture Atlas!\n\x1b[0m"
86+
firstLoad()
87+
return

coffee/server/blocksAtlas.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
atlas=require("./atlasCreator")("blocks",16,"#{__dirname}/blocks","#{__dirname}/../client/static/assets/blocks",694,36,true,27)

coffee/server/blocksDef.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ for i in [0..maxStateId]
88
else
99
bbox=0
1010
result.push [Block.fromStateId(i).name,bbox]
11-
console.log result
1211
buildPath="#{__dirname}/../client/static/assets/blocks"
1312

1413
fs.writeFileSync "#{buildPath}/blocksDef.json", JSON.stringify(result)
14+
15+
console.log "\x1b[32mGenerated blocksDefinitions: #{buildPath}/blocksDef.json\x1b[0m\n"

coffee/server/itemsAtlas.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
atlas=require("./atlasCreator")("items",50,"#{__dirname}/items","#{__dirname}/../client/static/assets/items",975,32,false,0)

0 commit comments

Comments
 (0)