Skip to content

Commit 996db0e

Browse files
committed
Instanced Entities and better fog
1 parent 89ac743 commit 996db0e

File tree

12 files changed

+95
-149
lines changed

12 files changed

+95
-149
lines changed

coffee/client/module/Entities.coffee

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,29 @@ import * as THREE from './build/three.module.js'
22
class Entities
33
constructor:(options)->
44
@scene=options.scene
5-
@saved={}
65
@nick=options.nick
76
@TWEEN=options.TWEEN
7+
@mobMeshMaterial = new THREE.MeshBasicMaterial {color: new THREE.Color "red" }
8+
@mobMeshGeometry = new THREE.BoxGeometry 1, 1, 1
9+
@maxCount=200
10+
@mobMesh=new THREE.InstancedMesh @mobMeshGeometry,@mobMeshMaterial,@maxCount
11+
@mobMesh.instanceMatrix.setUsage THREE.DynamicDrawUsage
12+
@scene.add @mobMesh
13+
@dummy = new THREE.Object3D()
14+
return
815
update:(entities)->
916
offset=[-0.5,16,-0.5]
17+
num=0
1018
for i of entities
11-
if entities[i].username isnt @nick
12-
pos=[entities[i].position.x+offset[0],entities[i].position.y+offset[1],entities[i].position.z+offset[2]]
13-
if @saved[entities[i].uuid] is undefined
14-
if entities[i].name is "item"
15-
material = new THREE.MeshBasicMaterial( {color: new THREE.Color("blue")} )
16-
geometry = new THREE.BoxGeometry( 0.5, 0.5, 0.5 )
17-
else
18-
material = new THREE.MeshBasicMaterial( {color: new THREE.Color("red")} )
19-
geometry = new THREE.BoxGeometry( 1, 1, 1 )
20-
cube = new THREE.Mesh( geometry, material )
21-
@scene.add(cube)
22-
@saved[entities[i].uuid]=cube
23-
@saved[entities[i].uuid].position.set pos...
24-
@saved[entities[i].uuid].active=true
25-
else
26-
@saved[entities[i].uuid].position.set pos...
27-
@saved[entities[i].uuid].active=true
28-
for i of @saved
29-
if @saved[i].active is false
30-
@scene.remove @saved[i]
31-
delete @saved[i]
32-
for i of @saved
33-
@saved[i].active=false
19+
if entities[i].type is "mob"
20+
num++
21+
@mobMesh.count=num
22+
num=0
23+
for i of entities
24+
if entities[i].type is "mob"
25+
@dummy.position.set entities[i].position.x+offset[0],entities[i].position.y+offset[1],entities[i].position.z+offset[2]
26+
@dummy.updateMatrix()
27+
@mobMesh.setMatrixAt num++, @dummy.matrix
28+
@mobMesh.instanceMatrix.needsUpdate = true
29+
return
3430
export {Entities}

coffee/client/module/FirstPersonControls.coffee

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class FirstPersonControls
1111
16:"sneak"
1212
82:"sprint"
1313
}
14+
@fov=options.fov
1415
@keys={}
1516
@canvas=options.canvas
1617
@camera=options.camera
@@ -69,7 +70,7 @@ class FirstPersonControls
6970
if _this.kc[z.keyCode] isnt undefined and _this.gameState is "gameLock"
7071
_this.socket.emit "move",_this.kc[z.keyCode],true
7172
if _this.kc[z.keyCode] is "sprint"
72-
to={fov:105}
73+
to={fov:_this.fov+10}
7374
new _this.TWEEN.Tween _this.camera
7475
.to to, 200
7576
.easing _this.TWEEN.Easing.Quadratic.Out
@@ -85,7 +86,7 @@ class FirstPersonControls
8586
if _this.kc[z.keyCode] isnt undefined
8687
_this.socket.emit "move",_this.kc[z.keyCode],false
8788
if _this.kc[z.keyCode] is "sprint"
88-
to={fov:95}
89+
to={fov:_this.fov}
8990
new _this.TWEEN.Tween _this.camera
9091
.to to, 200
9192
.easing _this.TWEEN.Easing.Quadratic.Out
@@ -120,7 +121,7 @@ class FirstPersonControls
120121
document.exitPointerLock()
121122
state:(state)->
122123
@gameState=state
123-
console.log "Game state: "+state
124+
# console.log "Game state: "+state
124125
resetState:()->
125126
$(".chat").removeClass("focus")
126127
$(".chat").addClass("blur")

coffee/client/module/World/AnimatedTextureAtlas.coffee

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,14 @@ class AnimatedTextureAtlas
7676
})
7777
xd=true
7878
@material.onBeforeCompile=(shader)->
79-
console.log shader
80-
8179
#Uniforms
8280
shader.uniforms.u_fogColor={value:[0.8, 0.9, 1, 1]}
83-
shader.uniforms.u_fogAmount={value:0.2}
84-
shader.uniforms.u_fogNear={value:10}
85-
shader.uniforms.u_fogFar={value:30}
81+
shader.uniforms.u_fogAmount={value:0.1}
8682
shader.uniforms.time={value:0}
87-
setInterval ()->
88-
shader.uniforms.time.value+=0.05
89-
,10
9083

9184
#Fragment shader
9285
shader.fragmentShader=[
9386
"uniform vec4 u_fogColor;"
94-
"uniform float u_fogNear;"
95-
"uniform float u_fogFar;"
9687
"uniform float u_fogAmount;"
9788
shader.fragmentShader
9889
].join("\n")
@@ -105,18 +96,6 @@ class AnimatedTextureAtlas
10596
].join("\n")
10697
)
10798

108-
#Vertex shader
109-
shader.vertexShader=[
110-
"uniform float time;"
111-
"uniform mat4 u_worldView;"
112-
"attribute vec4 a_position;"
113-
shader.vertexShader
114-
].join("\n")
115-
shader.vertexShader=shader.vertexShader.replace(
116-
"#include <begin_vertex>"
117-
"vec3 transformed = vec3( position.x + sin( time + position.y ) / 8.0, position.y, position.z );"
118-
)
119-
12099
materialShader = shader
121100
return
122101
@atlasCreator=new TextureAtlasCreator({

coffee/client/module/World/World.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ class World
9797
geometry.setAttribute 'color',new THREE.BufferAttribute(new Float32Array(cell.colors), 3)
9898
if mesh is undefined
9999
@cellMesh[cellId]=new THREE.Mesh geometry,@material
100+
@cellMesh[cellId].frustumCulled = false
101+
_this=@
102+
@cellMesh[cellId].onAfterRender = ()->
103+
_this.cellMesh[cellId].frustumCulled = true
104+
_this.cellMesh[cellId].onAfterRender = ->
100105
@scene.add @cellMesh[cellId]
101106
else
102107
@cellMesh[cellId].geometry=geometry
@@ -189,6 +194,7 @@ class World
189194
return false
190195
_setCell: (cellX,cellY,cellZ,buffer,biome)->
191196
#Wysyłanie do ChunkWorkera informacji nowej komórce
197+
192198
@cellUpdateTime=performance.now()
193199
@chunkWorker.postMessage {
194200
type:"setCell"

coffee/client/module/World/chunk.worker.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,13 @@ handlers={
226226
terrain.cellTerrain.setVoxel data...
227227
genCellGeo: (data)->
228228
if ((terrain.cellTerrain.vec3 data...) of terrain.cellTerrain.cells) is true
229+
p1=performance.now()
229230
geo=terrain.genCellGeo data...
231+
p2=performance.now()
230232
postMessage {
231233
cell:geo
232234
info:data
235+
p:performance.now()
233236
}
234237
setCell: (data)->
235238
terrain.cellTerrain.setCell data[0],data[1],data[2],data[3]

coffee/client/module/index.coffee

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,19 @@ init = ()->
2121
PixelRatio:window.devicePixelRatio
2222
}
2323
scene=new THREE.Scene
24-
camera = new THREE.PerspectiveCamera 95, 2, 0.1, 1000
24+
camera = new THREE.PerspectiveCamera 70, 2, 0.1, 1000
2525
camera.rotation.order = "YXZ"
2626
camera.position.set 26, 26, 26
2727

2828
#Skybox
29-
rt = new THREE.WebGLCubeRenderTarget al.get("skybox").image.height
30-
rt.fromEquirectangularTexture renderer, al.get "skybox"
31-
scene.background = rt
29+
scene.background = new THREE.Color("#adc8ff")
3230

3331
#Światła
3432
ambientLight=new THREE.AmbientLight 0xcccccc
3533
scene.add ambientLight
3634
directionalLight = new THREE.DirectionalLight 0x333333, 2
3735
directionalLight.position.set(1, 1, 0.5).normalize()
3836
scene.add directionalLight
39-
#Text geometry
40-
# new THREE.TextGeometry 'Hello three.js!', {
41-
# size: 80
42-
# height: 5
43-
# curveSegments: 12
44-
# bevelEnabled: true
45-
# bevelThickness: 10
46-
# bevelSize: 8
47-
# bevelOffset: 0
48-
# bevelSegments: 5
49-
# }
5037

5138
#Informacja o gpu komputera
5239
console.warn gpuInfo()
@@ -82,6 +69,7 @@ init = ()->
8269
micromove: 0.3
8370
socket
8471
TWEEN
72+
fov:70
8573
}
8674

8775
#Czat
@@ -160,15 +148,16 @@ init = ()->
160148
#Interfejs dat.gui
161149
gui = new GUI()
162150
params={
163-
fog:false
164-
chunkdist:4
151+
fog:true
152+
chunkdist:3
165153
}
154+
color = new THREE.Color "#adc8ff"
155+
near = 0.5*16
156+
far = 3*16
157+
scene.fog = new THREE.Fog color, near, far
166158
gui.add( params, 'fog' ).name( 'Enable fog' ).listen().onChange ()->
167159
if params.fog
168160
#Mgła
169-
color = new THREE.Color "#adc8ff"
170-
near = 3*16-13
171-
far = 3*16-3
172161
scene.fog = new THREE.Fog color, near, far
173162
else
174163
scene.fog = null

src/client/module/Entities.js

Lines changed: 22 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/module/FirstPersonControls.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/module/World/AnimatedTextureAtlas.js

Lines changed: 2 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)