Skip to content

Commit

Permalink
Source Engine: Fix some lightmap issues
Browse files Browse the repository at this point in the history
* Support surfaces without any light styles
* Support sdk_ prefixes for lightmappedgeneric/worldvertextransition (Mapbase support)
  • Loading branch information
magcius committed Oct 25, 2024
1 parent 89c5fea commit a7da874
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/SourceEngine/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,10 @@ export class BSPRenderer {

for (const faceIdx of this.liveFaceSet.values()) {
const lightmapUpdater = this.lightmapUpdaters[faceIdx];
if (lightmapUpdater !== null && lightmapUpdater.checkDirty(renderContext))
if (lightmapUpdater !== null) {
lightmapUpdater.update(renderContext);
lightmapUpdater.buildLightmap(renderContext, this.startLightmapPageIndex);
}

const faceInfo = this.bsp.faceInfos[faceIdx];
if (faceInfo.surfaceIndex >= 0)
Expand Down Expand Up @@ -798,8 +800,10 @@ export class BSPRenderer {
const faceIdx = surface.surface.faceList[k];

const lightmapUpdater = this.lightmapUpdaters[faceIdx];
if (lightmapUpdater !== null && lightmapUpdater.checkDirty(renderContext))
if (lightmapUpdater !== null) {
lightmapUpdater.update(renderContext);
lightmapUpdater.buildLightmap(renderContext, this.startLightmapPageIndex);
}
}
}

Expand Down
21 changes: 16 additions & 5 deletions src/SourceEngine/Materials/Lightmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,16 @@ function lightmapPackRuntimeBumpmap(dstPage: LightmapPage, location: Readonly<Fa
}
}

const enum FaceLightmapUpdaterState {
NotReady,
NeedsUpload,
Idle,
}

export class FaceLightmapUpdater {
// The styles that we built our lightmaps for.
private lightmapStyleIntensities: number[];
private initialized: boolean = false;
private state = FaceLightmapUpdaterState.NotReady;
private wantsLightmap: boolean = false;
private wantsBumpmap: boolean = false;

Expand All @@ -225,13 +230,13 @@ export class FaceLightmapUpdater {
}

public setMaterial(materialInstance: BaseMaterial): void {
assert(!this.initialized);
this.initialized = true;
assert(this.state === FaceLightmapUpdaterState.NotReady);
this.state = FaceLightmapUpdaterState.NeedsUpload;
this.wantsLightmap = materialInstance.wantsLightmap;
this.wantsBumpmap = materialInstance.wantsBumpmappedLightmap;
}

public checkDirty(renderContext: SourceRenderContext): boolean {
private checkDirty(renderContext: SourceRenderContext): boolean {
const worldLightingState = renderContext.worldLightingState;

if (!this.wantsLightmap)
Expand All @@ -246,8 +251,13 @@ export class FaceLightmapUpdater {
return false;
}

public update(renderContext: SourceRenderContext): void {
if (this.state === FaceLightmapUpdaterState.Idle && this.checkDirty(renderContext))
this.state = FaceLightmapUpdaterState.NeedsUpload;
}

public buildLightmap(renderContext: SourceRenderContext, pageOffset: number): void {
if (!this.initialized)
if (this.state !== FaceLightmapUpdaterState.NeedsUpload)
return;

const worldLightingState = renderContext.worldLightingState;
Expand Down Expand Up @@ -293,5 +303,6 @@ export class FaceLightmapUpdater {

dstPage.uploadDirty = true;
renderContext.debugStatistics.lightmapsBuilt++;
this.state = FaceLightmapUpdaterState.Idle;
}
}
4 changes: 2 additions & 2 deletions src/SourceEngine/Materials/Material_Generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,13 +1117,13 @@ export class Material_Generic extends BaseMaterial {
super.initParameters();

const shaderTypeStr = this.vmt._Root.toLowerCase();
if (shaderTypeStr === 'lightmappedgeneric')
if (shaderTypeStr === 'lightmappedgeneric' || shaderTypeStr === 'sdk_lightmappedgeneric')
this.shaderType = GenericShaderType.LightmappedGeneric;
else if (shaderTypeStr === 'vertexlitgeneric')
this.shaderType = GenericShaderType.VertexLitGeneric;
else if (shaderTypeStr === 'unlitgeneric')
this.shaderType = GenericShaderType.UnlitGeneric;
else if (shaderTypeStr === 'worldvertextransition')
else if (shaderTypeStr === 'worldvertextransition' || shaderTypeStr === 'sdk_worldvertextransition')
this.shaderType = GenericShaderType.WorldVertexTransition;
else if (shaderTypeStr === 'black')
this.shaderType = GenericShaderType.Black;
Expand Down

0 comments on commit a7da874

Please sign in to comment.