Skip to content

Commit

Permalink
zzre: Add dyn backdrop 10_Garden
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Feb 2, 2024
1 parent a8877d9 commit 4e46e9c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 32 deletions.
2 changes: 1 addition & 1 deletion zzre.core/GameTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void BeginFrame()
frameStart = watch.Elapsed;

HasFramerateChanged = false;
if ((frameStart - lastSecond).TotalSeconds >= 1)
if ((frameStart - lastSecond).TotalSeconds >= 1 && curFrametimes.Any())
{
Framerate = (int)(curFrametimes.Count / (frameStart - lastSecond).TotalSeconds + 0.5);
FrametimeAvg = curFrametimes.Average();
Expand Down
12 changes: 6 additions & 6 deletions zzre/game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public Game(ITagContainer diContainer, Savegame savegame)
new systems.PlayerSpawner(this),
new systems.PlayerControls(this),

// Cameras
new systems.FlyCamera(this),
new systems.OverworldCamera(this),
new systems.TriggerCamera(this),
new systems.CreatureCamera(this),

// Models and actors
new systems.ModelLoader(this),
new systems.BackdropLoader(this),
Expand Down Expand Up @@ -131,12 +137,6 @@ public Game(ITagContainer diContainer, Savegame savegame)
new systems.DialogTrading(this),

new systems.NonFairyAnimation(this),

// Cameras
new systems.FlyCamera(this),
new systems.OverworldCamera(this),
new systems.TriggerCamera(this),
new systems.CreatureCamera(this),

// Gameflows
new systems.GotCard(this),
Expand Down
60 changes: 42 additions & 18 deletions zzre/game/resources/ClumpMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,44 @@

namespace zzre.game.resources;

public readonly record struct ClumpMaterialInfo(FOModelRenderType RenderType, RWMaterial RWMaterial);
public readonly record struct ClumpMaterialInfo(
RWMaterial RWMaterial,
ModelMaterial.BlendMode BlendMode = ModelMaterial.BlendMode.Opaque,
bool DepthWrite = true,
bool DepthTest = true,
bool HasEnvMap = false)
{
public ClumpMaterialInfo(FOModelRenderType renderType, RWMaterial rwMaterial) : this(rwMaterial)
{
switch(renderType)
{
case FOModelRenderType.EarlySolid:
case FOModelRenderType.LateSolid:
case FOModelRenderType.Solid:
BlendMode = ModelMaterial.BlendMode.Alpha;
break;
case FOModelRenderType.EarlyAdditive:
case FOModelRenderType.Additive:
BlendMode = ModelMaterial.BlendMode.AdditiveAlpha;
break;
case FOModelRenderType.LateAdditive:
BlendMode = ModelMaterial.BlendMode.AdditiveAlpha;
DepthWrite = false;
break;
case FOModelRenderType.EnvMap32:
case FOModelRenderType.EnvMap64:
case FOModelRenderType.EnvMap96:
case FOModelRenderType.EnvMap128:
case FOModelRenderType.EnvMap255:
BlendMode = ModelMaterial.BlendMode.Alpha;
HasEnvMap = true;
DepthWrite = false;
break;
default:
throw new NotSupportedException($"Unsupported render type for material {renderType}");
}
}
}

public class ClumpMaterial : AResourceManager<ClumpMaterialInfo, ModelMaterial>
{
Expand All @@ -36,23 +73,10 @@ public ClumpMaterial(ITagContainer diContainer)
protected override ModelMaterial Load(ClumpMaterialInfo info)
{
var material = new ModelMaterial(diContainer) { HasTexShift = true, IsInstanced = true };
if (info.RenderType is FOModelRenderType.EarlySolid or FOModelRenderType.LateSolid or FOModelRenderType.Solid)
material.Blend = ModelMaterial.BlendMode.Alpha;
else if (info.RenderType is FOModelRenderType.LateAdditive or FOModelRenderType.EarlyAdditive or FOModelRenderType.Additive)
{
material.Blend = ModelMaterial.BlendMode.AdditiveAlpha;
if (info.RenderType is FOModelRenderType.LateAdditive)
material.DepthWrite = false;
}
else if (info.RenderType is >= FOModelRenderType.EnvMap32 and <= FOModelRenderType.EnvMap255)
{
// TODO: EnvMap materials do not use ZBias
material.Blend = ModelMaterial.BlendMode.Alpha;
material.HasEnvMap = true;
material.DepthWrite = false;
}
else
throw new NotSupportedException($"Unsupported render type for material {info.RenderType}");
material.Blend = info.BlendMode;
material.DepthTest = info.DepthTest;
material.DepthWrite = info.DepthWrite;
material.HasEnvMap = info.HasEnvMap;

(material.Texture.Texture, material.Sampler.Sampler) = textureLoader.LoadTexture(TextureBasePaths, info.RWMaterial);
material.Projection.BufferRange = camera.ProjectionRange;
Expand Down
27 changes: 20 additions & 7 deletions zzre/game/systems/model/BackdropLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,26 @@ private void HandleSceneLoaded(in messages.SceneLoaded message)
if (string.IsNullOrWhiteSpace(backdropName))
return;

if (char.IsDigit(backdropName.First()))
int? dynBackdropId = char.IsDigit(backdropName.First())
? int.Parse(backdropName[..backdropName.IndexOfAnyNot("0123456789".ToArray())])
: null as int?;
switch(dynBackdropId)
{
Console.WriteLine("Warning: Unsupported dynamic backdrop " + backdropName);
case 10: CreateStaticBackdrop("fbgsm01p", depthTest: false, depthWrite: false,
rotation: Quaternion.CreateFromAxisAngle(Vector3.UnitX, MathF.PI / -2));
break;
case null: CreateStaticBackdrop(backdropName); break;
default: Console.WriteLine("Warning: Unsupported dynamic backdrop " + backdropName); break;
}
else
CreateStaticBackdrop(backdropName);
}

private DefaultEcs.Entity CreateStaticBackdrop(string name)
private DefaultEcs.Entity CreateStaticBackdrop(string name, bool depthTest = true, bool depthWrite = true, Quaternion? rotation = null)
{
var entity = ecsWorld.CreateEntity();
entity.Set(new Location());
entity.Set(new Location()
{
LocalRotation = rotation ?? Quaternion.Identity
});
entity.Set(new components.MoveToLocation(camera.Location, RelativePosition: Vector3.Zero));
entity.Set(ManagedResource<ClumpMesh>.Create(resources.ClumpInfo.Backdrop(name + ".dff")));
entity.Set(components.Visibility.Visible);
Expand All @@ -65,10 +73,15 @@ private DefaultEcs.Entity CreateStaticBackdrop(string name)
Color = IColor.White
});

var materialInfo = new resources.ClumpMaterialInfo(zzio.scn.FOModelRenderType.Solid, rwMaterial: null!)
{
DepthTest = depthTest,
DepthWrite = depthWrite
};
var clumpMesh = entity.Get<ClumpMesh>();
entity.Set(new List<materials.ModelMaterial>(clumpMesh.Materials.Count));
entity.Set(ManagedResource<materials.ModelMaterial>.Create(clumpMesh.Materials
.Select(rwMaterial => new resources.ClumpMaterialInfo(zzio.scn.FOModelRenderType.Solid, rwMaterial))
.Select(rwMaterial => materialInfo with { RWMaterial = rwMaterial })
.ToArray()));

return entity;
Expand Down
2 changes: 2 additions & 0 deletions zzre/materials/ModelMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum BlendMode : uint
public bool HasTexShift { set => SetOption(nameof(HasTexShift), value); }
public bool HasEnvMap { set => SetOption(nameof(HasEnvMap), value); }
public bool DepthWrite { set => SetOption(nameof(DepthWrite), value); }
public bool DepthTest { set => SetOption(nameof(DepthTest), value); }
public BlendMode Blend { set => SetOption(nameof(Blend), (uint)value); }

public UniformBinding<Matrix4x4> World { get; }
Expand All @@ -68,6 +69,7 @@ public ModelMaterial(ITagContainer diContainer) : base(diContainer, "model")
AddBinding("mainSampler", Sampler = new(this));
AddBinding("pose", Pose = new(this));
DepthWrite = true;
DepthTest = true;
}
}

Expand Down
7 changes: 7 additions & 0 deletions zzre/shaders/model.mlang
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ option IsSkinned;
option HasTexShift;
option HasEnvMap;
option DepthWrite; // enabled by default in the C# material
option DepthTest; // also enabled by default
option Blend = IsOpaque, IsAlphaBlend, IsAdditiveBlend, IsAdditiveAlphaBlend;

attributes
Expand Down Expand Up @@ -51,13 +52,19 @@ pipeline
output r8_g8_b8_a8_unorm outColor;
output d24_unorm_s8_uint;
depthwrite off;
depthtest off;
}

pipeline if (DepthWrite)
{
depthwrite on;
}

pipeline if (DepthTest)
{
depthtest on;
}

pipeline if (Blend == IsAlphaBlend)
{
blend SrcAlpha + InvSrcAlpha;
Expand Down

0 comments on commit 4e46e9c

Please sign in to comment.