Skip to content

Commit 7a09acb

Browse files
committed
add simple fog effect
1 parent 6f2aa13 commit 7a09acb

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

src/Renderer/WorldChunkModule.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,15 @@ class ChunksModule {
565565
draw() {
566566
const {renderer} = this, {ctx} = renderer;
567567
const prg = renderer.getProgram("showBlock");
568-
prg.use().setUni("mvpMatrix", renderer.mainCamera.projview);
568+
prg.use()
569+
// .setUni("vMatrix", renderer.mainCamera.view)
570+
// .setUni("pMatrix", renderer.mainCamera.projection)
571+
.setUni("mvMatrix", renderer.mainCamera.view)
572+
.setUni("mvpMatrix", renderer.mainCamera.projview)
573+
.setUni("fogColor", [0.62, 0.81, 1.0, 1.0])
574+
// 3 ~ 4 chunks
575+
.setUni("fogNear", 48)
576+
.setUni("fogFar", 64);
569577
const mainPlayer = this.world.mainPlayer;
570578
let chunk = this.world.getChunkByBlockXYZ(...[...mainPlayer.position].map(n => n < 0? n - 1: n));
571579
const meshs = Object.entries(this.meshs).map(([chunkKey, mesh]) => {

src/Renderer/WorldRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class WorldRenderer extends Render {
2121
ctx.depthFunc(ctx.LEQUAL);
2222
ctx.enable(ctx.CULL_FACE);
2323
ctx.frontFace(ctx.CCW);
24-
this.mainCamera = new Camera(this.aspectRatio, { fovy: 70, pitch: -90 * Math.PI / 180, position: [0, 20, 0] });
24+
this.mainCamera = new Camera(this.aspectRatio, { fovy: 60, pitch: -90 * Math.PI / 180, position: [0, 20, 0] });
2525
this.addCamera(this.mainCamera);
2626
if (this.isWebGL2)
2727
this.createProgram("showBlock", glsl.showBlock_webgl2.vert, glsl.showBlock_webgl2.frag)

src/Renderer/glsl.js

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,39 @@ export let showBlock_webgl2 = {
66
in vec3 position;
77
in vec4 color;
88
in vec3 textureCoord;
9-
uniform mat4 mvpMatrix;
10-
out vec4 vColor;
11-
out vec3 vTextureCoord;
9+
// uniform mat4 mMatrix;
10+
// uniform mat4 vMatrix;
11+
// uniform mat4 pMatrix;
12+
uniform mat4 mvMatrix;
13+
uniform mat4 mvpMatrix;
14+
out vec4 vColor;
15+
out vec3 vTextureCoord;
16+
out vec3 vPos;
1217
void main(void) {
1318
vColor = color;
1419
vTextureCoord = textureCoord;
15-
gl_Position = mvpMatrix * vec4(position, 1.0);
20+
vec4 pos = vec4(position, 1.0);
21+
vPos = (mvMatrix * pos).xyz;
22+
gl_Position = mvpMatrix * pos;
1623
}`,
1724
frag: `#version 300 es
1825
precision highp int;
1926
precision highp float;
2027
precision highp sampler2DArray;
2128
uniform sampler2DArray blockTex;
22-
in vec4 vColor;
23-
in vec3 vTextureCoord;
29+
uniform vec4 fogColor;
30+
uniform float fogNear;
31+
uniform float fogFar;
32+
in vec4 vColor;
33+
in vec3 vTextureCoord;
34+
in vec3 vPos;
2435
out vec4 fragmentColor;
2536
void main(void){
2637
vec4 smpColor = texture(blockTex, vTextureCoord);
2738
if (smpColor.a <= 0.3) discard;
28-
fragmentColor = vColor * smpColor;
39+
float fogDistance = length(vPos);
40+
float fogAmount = smoothstep(fogNear, fogFar, fogDistance);
41+
fragmentColor = mix(vColor * smpColor, fogColor, fogAmount);
2942
}`
3043
};
3144

@@ -40,13 +53,20 @@ export let showBlock = {
4053
attribute vec3 position;
4154
attribute vec4 color;
4255
attribute vec3 textureCoord;
43-
uniform mat4 mvpMatrix;
44-
varying vec4 vColor;
45-
varying vec3 vTextureCoord;
56+
// uniform mat4 mMatrix;
57+
// uniform mat4 vMatrix;
58+
// uniform mat4 pMatrix;
59+
uniform mat4 mvMatrix;
60+
uniform mat4 mvpMatrix;
61+
varying vec4 vColor;
62+
varying vec3 vTextureCoord;
63+
varying vec3 vPos;
4664
void main(void) {
4765
vColor = color;
4866
vTextureCoord = textureCoord;
49-
gl_Position = mvpMatrix * vec4(position, 1.0);
67+
vec4 pos = vec4(position, 1.0);
68+
vPos = (mvMatrix * pos).xyz;
69+
gl_Position = mvpMatrix * pos;
5070
}`,
5171
// fs中的vColor是vs中传进来的
5272
// precision指定精确度 此为精密度中的float
@@ -57,12 +77,18 @@ export let showBlock = {
5777
precision mediump float;
5878
#endif
5979
uniform sampler2D blockTex;
60-
varying vec4 vColor;
61-
varying vec3 vTextureCoord;
80+
uniform vec4 fogColor;
81+
uniform float fogNear;
82+
uniform float fogFar;
83+
varying vec4 vColor;
84+
varying vec3 vTextureCoord;
85+
varying vec3 vPos;
6286
void main(void){
6387
vec4 smpColor = texture2D(blockTex, vTextureCoord.xy);
6488
if (smpColor.a <= 0.3) discard;
65-
gl_FragColor = vColor * smpColor;
89+
float fogDistance = length(vPos);
90+
float fogAmount = smoothstep(fogNear, fogFar, fogDistance);
91+
gl_FragColor = mix(vColor * smpColor, fogColor, fogAmount);
6692
}`
6793
};
6894

0 commit comments

Comments
 (0)