Skip to content

Commit

Permalink
zzre: Fix loading of worlds with untextured materials
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Oct 22, 2023
1 parent 42d9816 commit 9cc6989
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion zzre/game/Zanzarah.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Zanzarah(ITagContainer diContainer, IZanzarahContainer zanzarahContainer)
tagContainer.AddTag(UI = new UI(this));
this.zanzarahContainer = zanzarahContainer;

var savegame = new zzio.Savegame() { sceneId = 3200 };
var savegame = new zzio.Savegame() { sceneId = 2800 };
/*using (var fileStream = new System.IO.FileStream(@"C:\dev\zanzarah\Save\_0004.dat", System.IO.FileMode.Open, System.IO.FileAccess.Read))
using (var reader = new System.IO.BinaryReader(fileStream))
savegame = zzio.Savegame.ReadNew(reader);
Expand Down
22 changes: 18 additions & 4 deletions zzre/rendering/WorldRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace zzre.rendering;
public class WorldRenderer : BaseDisposable
{
private readonly ITagContainer diContainer;
private readonly GraphicsDevice graphicsDevice;
private readonly DeviceBufferRange locationRange;
private readonly Camera camera;
private readonly Texture whiteTexture;

private WorldBuffers? worldBuffers;
public WorldBuffers? WorldBuffers
Expand All @@ -24,8 +28,6 @@ public WorldBuffers? WorldBuffers
}
}

private readonly DeviceBufferRange locationRange;
private readonly Camera camera;
private Frustum viewFrustum;
private ModelStandardMaterial[] materials = Array.Empty<ModelStandardMaterial>();
public IReadOnlyList<ModelStandardMaterial> Materials => materials;
Expand All @@ -42,21 +44,27 @@ public WorldRenderer(ITagContainer diContainer)
camera = diContainer.GetTag<Camera>();
var locationBuffer = diContainer.GetTag<LocationBuffer>();
locationRange = locationBuffer.Add(Location);

graphicsDevice = diContainer.GetTag<GraphicsDevice>();
whiteTexture = graphicsDevice.ResourceFactory.CreateTexture(new(1, 1, 1, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.Sampled, TextureType.Texture2D));
whiteTexture.Name = "White";
graphicsDevice.UpdateTexture(whiteTexture, new byte[] { 255, 255, 255, 255 }, 0, 0, 0, 1, 1, 1, 0, 0);
}

protected override void DisposeManaged()
{
base.DisposeManaged();
diContainer.GetTag<LocationBuffer>().Remove(locationRange);
DisposeMaterials();
whiteTexture.Dispose();
}

private void DisposeMaterials()
{
var textureLoader = diContainer.GetTag<IAssetLoader<Texture>>();
foreach (var material in materials)
{
if (textureLoader is not CachedAssetLoader<Texture>)
if (textureLoader is not CachedAssetLoader<Texture> && material.MainTexture.Texture != whiteTexture)
{
material.MainTexture.Texture?.Dispose();
material.Sampler.Sampler?.Dispose();
Expand All @@ -80,7 +88,13 @@ private void LoadMaterials()
foreach (var (rwMaterial, index) in worldBuffers.Materials.Indexed())
{
var material = materials[index] = new ModelStandardMaterial(diContainer);
(material.MainTexture.Texture, material.Sampler.Sampler) = textureLoader.LoadTexture(textureBase, rwMaterial);
if (rwMaterial.isTextured)
(material.MainTexture.Texture, material.Sampler.Sampler) = textureLoader.LoadTexture(textureBase, rwMaterial);
else
{
material.MainTexture.Texture = whiteTexture;
material.Sampler.Sampler = graphicsDevice.PointSampler;
}
material.LinkTransformsTo(camera);
material.World.BufferRange = locationRange;
material.Uniforms.Ref = ModelStandardMaterialUniforms.Default;
Expand Down

0 comments on commit 9cc6989

Please sign in to comment.