Finalize shader changes, improve shading for lower lods and make flat-view rotatable

This commit is contained in:
Lukas Rieger (Blue) 2022-11-09 16:56:53 +01:00
parent daa6d02af9
commit 4ebb90f7d1
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
3 changed files with 37 additions and 26 deletions

View File

@ -173,10 +173,13 @@ export class MapControls {
let maxAngleForZoom = this.getMaxPerspectiveAngleForDistance(this.manager.distance); let maxAngleForZoom = this.getMaxPerspectiveAngleForDistance(this.manager.distance);
// rotation // rotation
if (this.manager.ortho === 0) {
this.mouseRotate.update(delta, map); this.mouseRotate.update(delta, map);
this.keyRotate.update(delta, map); this.keyRotate.update(delta, map);
this.touchRotate.update(delta, map); this.touchRotate.update(delta, map);
// snap rotation to north on orthographic view
if (this.manager.ortho !== 0 && Math.abs(this.manager.rotation) < 0.3) {
this.manager.rotation = softClamp(this.manager.rotation, 0, 0, 0.05);
} }
// tilt // tilt

View File

@ -85,6 +85,8 @@ export class LowresTileLoader {
return; return;
} }
const scale = Math.pow(this.tileSettings.lodFactor, this.lod - 1);
let material = new ShaderMaterial({ let material = new ShaderMaterial({
uniforms: { uniforms: {
...this.uniforms, ...this.uniforms,
@ -98,7 +100,12 @@ export class LowresTileLoader {
type: 't', type: 't',
value: texture value: texture
}, },
lod: {value: this.lod} lod: {
value: this.lod
},
lodScale: {
value: scale
}
}, },
vertexShader: this.vertexShader, vertexShader: this.vertexShader,
fragmentShader: this.fragmentShader, fragmentShader: this.fragmentShader,
@ -112,7 +119,6 @@ export class LowresTileLoader {
let object = new Mesh(this.geometry, material); let object = new Mesh(this.geometry, material);
const scale = Math.pow(this.tileSettings.lodFactor, this.lod - 1);
object.position.set(tileX * this.tileSettings.tileSize.x * scale, 0, tileZ * this.tileSettings.tileSize.z * scale); object.position.set(tileX * this.tileSettings.tileSize.x * scale, 0, tileZ * this.tileSettings.tileSize.z * scale);
object.scale.set(scale, 1, scale); object.scale.set(scale, 1, scale);

View File

@ -28,8 +28,6 @@ export const LOWRES_FRAGMENT_SHADER = `
${ShaderChunk.logdepthbuf_pars_fragment} ${ShaderChunk.logdepthbuf_pars_fragment}
#define PI 3.1415926535897932 #define PI 3.1415926535897932
#define PI_D10 0.3141592653589793
#define PI_D5 0.6283185307179586
#ifndef texture #ifndef texture
#define texture texture2D #define texture texture2D
@ -49,7 +47,8 @@ uniform TileMap hiresTileMap;
uniform sampler2D textureImage; uniform sampler2D textureImage;
uniform vec2 tileSize; uniform vec2 tileSize;
uniform vec2 textureSize; uniform vec2 textureSize;
uniform int lod; uniform float lod;
uniform float lodScale;
varying vec3 vPosition; varying vec3 vPosition;
varying vec3 vWorldPosition; varying vec3 vWorldPosition;
@ -88,29 +87,32 @@ void main() {
float heightX = metaToHeight(texture(textureImage, posToMetaUV(vPosition.xz + vec2(1.0, 0.0)))); float heightX = metaToHeight(texture(textureImage, posToMetaUV(vPosition.xz + vec2(1.0, 0.0))));
float heightZ = metaToHeight(texture(textureImage, posToMetaUV(vPosition.xz + vec2(0.0, 1.0)))); float heightZ = metaToHeight(texture(textureImage, posToMetaUV(vPosition.xz + vec2(0.0, 1.0))));
float diff = (height - heightX) + (height - heightZ); float heightDiff = ((height - heightX) + (height - heightZ)) / lodScale;
vec3 defaultRGB = color.rgb + clamp(diff * 0.04, -0.2, 0.04); float shade = clamp(heightDiff * 0.06, -0.2, 0.04);
if(lod == 1) {
vec3 occRGB = color.rgb + clamp(diff * 0.02, -0.1, 0.04);
float ao = 0.0; float ao = 0.0;
float aoStrength = 0.0;
if(lod == 1.0) {
aoStrength = smoothstep(PI - 0.8, PI - 0.2, acos(-clamp(viewMatrix[1][2], 0.0, 1.0)));
aoStrength *= 1.0 - smoothstep(200.0, 500.0, vDistance);
if (aoStrength > 0.0) {
const float r = 3.0; const float r = 3.0;
const float step = 0.2; const float step = 0.2;
const float o = step / r * 0.1;
for (float vx = -r; vx <= r; vx++) { for (float vx = -r; vx <= r; vx++) {
for (float vz = -r; vz <= r; vz++) { for (float vz = -r; vz <= r; vz++) {
diff = height - metaToHeight(texture(textureImage, posToMetaUV(vPosition.xz + vec2(vx * step, vz * step)))); heightDiff = height - metaToHeight(texture(textureImage, posToMetaUV(vPosition.xz + vec2(vx * step, vz * step))));
if (diff < 0.0) { if (heightDiff < 0.0) {
ao -= step / r; ao -= o;
}
}
} }
} }
} }
occRGB += ao * 0.1;
color.rgb = mix(defaultRGB, occRGB, smoothstep(PI - PI_D5, PI - PI_D10, acos(-clamp(viewMatrix[1][2], 0.0, 1.0)))); color.rgb += mix(shade, shade * 0.3 + ao, aoStrength);
} else {
color.rgb = defaultRGB;
}
float blockLight = metaToLight(meta); float blockLight = metaToLight(meta);
float light = mix(blockLight, 15.0, sunlightStrength); float light = mix(blockLight, 15.0, sunlightStrength);
color.rgb *= mix(ambientLight, 1.0, light / 15.0); color.rgb *= mix(ambientLight, 1.0, light / 15.0);