Skip to content

Commit 371d5e5

Browse files
committed
distance-based fog
1 parent eb620d9 commit 371d5e5

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

src/client/coffee/World/AnimatedTextureAtlas.coffee

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,41 +66,63 @@ class TextureAtlasCreator
6666
row=(tick-1)%h;
6767
return {row,col}
6868
class AnimatedTextureAtlas
69-
constructor:(options)->
69+
constructor:(game)->
7070
_this=@
71-
@al=options.al
71+
@game=game
7272
@material=new THREE.MeshStandardMaterial({
7373
side: 0
7474
map:null
7575
vertexColors: true
7676
})
77-
xd=true
77+
@uni=
78+
view:new THREE.Vector3
79+
farnear:new THREE.Vector2
80+
color:new THREE.Vector4
7881
@material.onBeforeCompile=(shader)->
79-
#Uniforms
80-
shader.uniforms.u_fogColor={value:[0.8, 0.9, 1, 1]}
81-
shader.uniforms.u_fogAmount={value:0.1}
82-
shader.uniforms.time={value:0}
8382

83+
#Uniforms
84+
shader.uniforms.u_viewPos={value:_this.uni.view}
85+
shader.uniforms.u_fogColor={value:_this.uni.color}
86+
shader.uniforms.u_farnear={value:_this.uni.farnear}
8487
#Fragment shader
8588
shader.fragmentShader=[
89+
"uniform vec3 u_viewPos;"
8690
"uniform vec4 u_fogColor;"
87-
"uniform float u_fogAmount;"
91+
"uniform float u_fogNear;"
92+
"uniform float u_fogFar;"
93+
"uniform vec2 u_farnear;"
8894
shader.fragmentShader
8995
].join("\n")
9096
# # shader.fragmentShader="uniform vec4 fogColor;\nuniform float fogAmount;\n"+shader.fragmentShader
9197
shader.fragmentShader=shader.fragmentShader.replace(
9298
"gl_FragColor = vec4( outgoingLight, diffuseColor.a );"
9399
[
100+
"float dist=length(u_viewPos-vViewPosition);"
101+
"float fogAmount = smoothstep(u_farnear.x, u_farnear.y, dist);"
94102
"gl_FragColor = vec4( outgoingLight, diffuseColor.a );"
95-
"gl_FragColor = mix(gl_FragColor,u_fogColor,u_fogAmount);"
103+
"gl_FragColor = mix(gl_FragColor,u_fogColor,max(0.15,fogAmount));"
96104
].join("\n")
97105
)
98106

99-
materialShader = shader
107+
#Vertex shader
108+
shader.vertexShader=[
109+
"uniform float time;"
110+
"uniform mat4 u_worldView;"
111+
"attribute vec4 a_position;"
112+
shader.vertexShader
113+
].join("\n")
114+
shader.vertexShader=shader.vertexShader.replace(
115+
"#include <fog_vertex>"
116+
[
117+
"vec4 vViewPosition4 = modelViewMatrix * vec4(position, 1);"
118+
"vViewPosition = vViewPosition4.xyz;"
119+
].join("\n")
120+
121+
)
100122
return
101123
@atlasCreator=new TextureAtlasCreator({
102-
textureX:@al.get "blocksAtlasFull"
103-
textureMapping:@al.get "blocksMappingFull"
124+
textureX:@game.al.get "blocksAtlasFull"
125+
textureMapping:@game.al.get "blocksMappingFull"
104126
})
105127
savedTextures=[]
106128
for i in [0..9]

src/client/coffee/World/World.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class World
1515
@blocksDef=@game.al.get "blocksDef"
1616
@models={}
1717
@cellTerrain=new CellTerrain {cellSize:@game.cellSize,blocksDef:@blocksDef}
18-
@ATA=new AnimatedTextureAtlas {al:@game.al}
18+
@ATA=new AnimatedTextureAtlas @game
1919
@material=@ATA.material
2020
@cellUpdateTime=null
2121
@renderTime=100

src/client/coffee/index.coffee

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,16 @@ class Game
4141
PixelRatio:window.devicePixelRatio
4242
@scene=new THREE.Scene
4343
@dimBg=
44-
"minecraft:overworld":new THREE.Color "#adc8ff"
45-
"minecraft:the_end":new THREE.Color "#011433"
46-
"minecraft:the_nether":new THREE.Color "#85280f"
44+
"minecraft:overworld":[173/255, 200/255, 255/255]
45+
"minecraft:the_end":[1/255, 20/255, 51/255]
46+
"minecraft:the_nether":[133/255, 40/255, 15/255]
4747
@camera = new THREE.PerspectiveCamera @fov, 2, 0.1, 1000
4848
@camera.rotation.order = "YXZ"
4949
@camera.position.set 26, 26, 26
5050
@scene.add new THREE.AmbientLight 0xcccccc
5151
directionalLight = new THREE.DirectionalLight 0x333333, 2
5252
directionalLight.position.set(1, 1, 0.5).normalize()
5353
@scene.add directionalLight
54-
5554
console.warn gpuInfo()
5655

5756
@nick=document.location.hash.substring 1,document.location.hash.length
@@ -94,8 +93,12 @@ class Game
9493
_this.dimension=dim
9594
console.log "Player dimension has been changed: #{dim}"
9695
_this.world.resetWorld()
97-
_this.scene.background=_this.dimBg[dim]
98-
_this.scene.fog.color=_this.dimBg[dim]
96+
bg=_this.dimBg[dim]
97+
_this.scene.background=new THREE.Color bg...
98+
_this.world.ATA.uni.color.x=bg[0]
99+
_this.world.ATA.uni.color.y=bg[1]
100+
_this.world.ATA.uni.color.z=bg[2]
101+
_this.world.ATA.uni.color.w=1
99102
return
100103
"mapChunk":(sections,x,z,biomes,dim)->
101104
_this.world._computeSections sections,x,z,biomes,dim
@@ -149,19 +152,14 @@ class Game
149152

150153
gui = new dat.GUI
151154
@params=
152-
fog:false
153155
chunkdist:3
154-
gui.add( @params, "fog" ).name( "Enable fog" ).listen().onChange ()->
155-
if _this.params.fog
156-
_this.scene.fog = new THREE.Fog _this.dimBg[_this.dimension], (_this.params.chunkdist-2.5)*16, (_this.params.chunkdist-0.5)*16
157-
else
158-
_this.scene.fog = null
156+
@world.ATA.uni.farnear.x=(@params.chunkdist-1.5)*16
157+
@world.ATA.uni.farnear.y=(@params.chunkdist-0.5)*16
159158
gui.add( @world.material, "wireframe" ).name( "Wireframe" ).listen()
160159
chunkDist=gui.add( @params, "chunkdist",0,10,1).name( "Render distance" ).listen()
161160
chunkDist.onChange (val)->
162-
if _this.scene.fog isnt null
163-
_this.scene.fog.near=(val-2.5)*16
164-
_this.scene.fog.far=(val-0.5)*16
161+
_this.world.ATA.uni.farnear.x=(val-1.5)*16
162+
_this.world.ATA.uni.farnear.y=(val-0.5)*16
165163
console.log val
166164
return
167165
@mouse=false
@@ -209,6 +207,7 @@ class Game
209207
if @FPC.gameState is "inventory"
210208
@pii.render()
211209
@inv_bar.tick()
210+
@world.ATA.uni.view.copy(@camera.position).applyMatrix4(@camera.matrixWorldInverse)
212211

213212
@renderer.render @scene, @camera
214213
return

0 commit comments

Comments
 (0)