Skip to content

Commit 7ce13d2

Browse files
committed
fix ambient occulusion mistakes
1 parent 50f5784 commit 7ce13d2

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

src/scripts/world/AnimatedTextureAtlas.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ class AnimatedTextureAtlas {
138138
transparent: true,
139139
alphaTest: 0.1
140140
})
141-
142-
console.log(this.material)
143141
}
144142
}
145143

src/scripts/world/ChunkMesher.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ChunkMesher {
3939
]
4040
}
4141

42-
getUvForFace (block, type) {
42+
getUvForFace (block, type, opt) {
4343
let xd, toxX, toxY
4444
if (
4545
this.blocksTex[block.name] !== undefined ||
@@ -85,11 +85,21 @@ class ChunkMesher {
8585
const y1 = 1 - this.q * toxY - this.q
8686
const x2 = this.q * toxX + this.q
8787
const y2 = 1 - this.q * toxY
88-
return [x1, y1, x2, y1, x1, y2, x1, y2, x2, y1, x2, y2]
88+
const uv = [
89+
[x1, y1],
90+
[x1, y2],
91+
[x2, y1],
92+
[x2, y2]
93+
]
94+
if (opt) {
95+
return [...uv[0], ...uv[1], ...uv[2], ...uv[2], ...uv[1], ...uv[3]]
96+
} else {
97+
return [...uv[0], ...uv[2], ...uv[1], ...uv[1], ...uv[2], ...uv[3]]
98+
}
8999
}
90100

91-
genBlockFace (type, block, pos) {
92-
const uv = this.getUvForFace(block, type)
101+
genBlockFace (type, block, pos, opt) {
102+
const uv = this.getUvForFace(block, type, opt)
93103
const norm = []
94104
for (let i = 0; i < 6; i++) {
95105
norm.push(...this.neighbours[type])
@@ -102,7 +112,13 @@ class ChunkMesher {
102112
py: [5, 7, 3, 2],
103113
ny: [1, 0, 4, 6]
104114
})[type]
105-
const ids = [0, 1, 2, 2, 1, 3]
115+
let ids
116+
if (opt) {
117+
ids = [1, 3, 0, 0, 3, 2]
118+
} else {
119+
ids = [0, 1, 2, 2, 1, 3]
120+
}
121+
106122
const posx = []
107123
const pt = [
108124
[-0.5, -0.5, 0.5],
@@ -126,8 +142,31 @@ class ChunkMesher {
126142

127143
addFace (tVertexBuffer, VertexBuffer, type, pos) {
128144
const block = this.chunkTerrain.getBlock(...pos)
129-
const faceVertex = this.genBlockFace(type, block, pos)
130-
this.ambientOcclusion(block, pos, faceVertex, type)
145+
146+
const col = this.ambientOcclusion(block, pos, type)
147+
let faceVertex
148+
if (col[0] + col[3] >= col[1] + col[2]) {
149+
faceVertex = this.genBlockFace(type, block, pos, true)
150+
faceVertex.color = [
151+
...col[2],
152+
...col[3],
153+
...col[0],
154+
...col[0],
155+
...col[3],
156+
...col[1]
157+
]
158+
} else {
159+
faceVertex = this.genBlockFace(type, block, pos, false)
160+
faceVertex.color = [
161+
...col[0],
162+
...col[2],
163+
...col[1],
164+
...col[1],
165+
...col[2],
166+
...col[3]
167+
]
168+
}
169+
131170
this.push(
132171
tVertexBuffer,
133172
VertexBuffer,
@@ -136,7 +175,7 @@ class ChunkMesher {
136175
)
137176
}
138177

139-
ambientOcclusion (block, pos, faceVertex, type) {
178+
ambientOcclusion (block, pos, type) {
140179
const loaded = {}
141180
for (let x = -1; x <= 1; x++) {
142181
for (let y = -1; y <= 1; y++) {
@@ -202,15 +241,7 @@ class ChunkMesher {
202241
col[i][2] /= ile
203242
}
204243
}
205-
206-
faceVertex.color = [
207-
...col[0],
208-
...col[2],
209-
...col[1],
210-
...col[1],
211-
...col[2],
212-
...col[3]
213-
]
244+
return col
214245
}
215246

216247
push (tVertexBuffer, VertexBuffer, faceVertex, transparent) {

src/scripts/world/CustomRender.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ const CustomRender = {
9999

100100
break
101101
}
102-
this.ambientOcclusion(block, pos, faceVertex, side)
102+
const col = this.ambientOcclusion(block, pos, side)
103+
faceVertex.color = [
104+
...col[0],
105+
...col[2],
106+
...col[1],
107+
...col[1],
108+
...col[2],
109+
...col[3]
110+
]
103111
this.push(
104112
tVertexBuffer,
105113
VertexBuffer,
@@ -337,7 +345,16 @@ const CustomRender = {
337345

338346
break
339347
}
340-
this.ambientOcclusion(block, pos, faceVertex, side)
348+
this.ambientOcclusion(block, pos, side)
349+
const col = this.ambientOcclusion(block, pos, side)
350+
faceVertex.color = [
351+
...col[0],
352+
...col[2],
353+
...col[1],
354+
...col[1],
355+
...col[2],
356+
...col[3]
357+
]
341358
this.push(
342359
tVertexBuffer,
343360
VertexBuffer,

0 commit comments

Comments
 (0)