Turn ModelRenderer and VoxelCache into traits

This commit is contained in:
Gustas
2023-09-19 18:40:57 +03:00
committed by Matthias Mailänder
parent d427072cc9
commit 686040a316
33 changed files with 481 additions and 469 deletions

View File

@@ -1,181 +0,0 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render
{
public interface IRenderActorPreviewVoxelsInfo : ITraitInfoInterface
{
IEnumerable<ModelAnimation> RenderPreviewVoxels(
ActorPreviewInitializer init, RenderVoxelsInfo rv, string image, Func<WRot> orientation, int facings, PaletteReference p);
}
public class RenderVoxelsInfo : TraitInfo, IRenderActorPreviewInfo, Requires<BodyOrientationInfo>
{
[Desc("Defaults to the actor name.")]
public readonly string Image = null;
[Desc("Custom palette name")]
[PaletteReference]
public readonly string Palette = null;
[PaletteReference]
[Desc("Custom PlayerColorPalette: BaseName")]
public readonly string PlayerPalette = "player";
[PaletteReference]
public readonly string NormalsPalette = "normals";
[PaletteReference]
public readonly string ShadowPalette = "shadow";
[Desc("Change the image size.")]
public readonly float Scale = 12;
public readonly WAngle LightPitch = WAngle.FromDegrees(50);
public readonly WAngle LightYaw = WAngle.FromDegrees(240);
public readonly float[] LightAmbientColor = { 0.6f, 0.6f, 0.6f };
public readonly float[] LightDiffuseColor = { 0.4f, 0.4f, 0.4f };
public override object Create(ActorInitializer init) { return new RenderVoxels(init.Self, this); }
public virtual IEnumerable<IActorPreview> RenderPreview(ActorPreviewInitializer init)
{
var body = init.Actor.TraitInfo<BodyOrientationInfo>();
var faction = init.GetValue<FactionInit, string>(this);
var ownerName = init.Get<OwnerInit>().InternalName;
var sequences = init.World.Map.Sequences;
var image = Image ?? init.Actor.Name;
var facings = body.QuantizedFacings == -1 ?
init.Actor.TraitInfo<IQuantizeBodyOrientationInfo>().QuantizedBodyFacings(init.Actor, sequences, faction) :
body.QuantizedFacings;
var palette = init.WorldRenderer.Palette(Palette ?? PlayerPalette + ownerName);
var components = init.Actor.TraitInfos<IRenderActorPreviewVoxelsInfo>()
.SelectMany(rvpi => rvpi.RenderPreviewVoxels(init, this, image, init.GetOrientation(), facings, palette))
.ToArray();
yield return new ModelPreview(components, WVec.Zero, 0, Scale, LightPitch,
LightYaw, LightAmbientColor, LightDiffuseColor, body.CameraPitch,
palette, init.WorldRenderer.Palette(NormalsPalette), init.WorldRenderer.Palette(ShadowPalette));
}
}
public class RenderVoxels : IRender, ITick, INotifyOwnerChanged
{
sealed class AnimationWrapper
{
readonly ModelAnimation model;
bool cachedVisible;
WVec cachedOffset;
public AnimationWrapper(ModelAnimation model)
{
this.model = model;
}
public bool Tick()
{
// Return to the caller whether the renderable position or size has changed
var visible = model.IsVisible;
var offset = model.OffsetFunc?.Invoke() ?? WVec.Zero;
var updated = visible != cachedVisible || offset != cachedOffset;
cachedVisible = visible;
cachedOffset = offset;
return updated;
}
}
public readonly RenderVoxelsInfo Info;
readonly List<ModelAnimation> components = new();
readonly Dictionary<ModelAnimation, AnimationWrapper> wrappers = new();
readonly Actor self;
readonly BodyOrientation body;
readonly WRot camera;
readonly WRot lightSource;
public RenderVoxels(Actor self, RenderVoxelsInfo info)
{
this.self = self;
Info = info;
body = self.Trait<BodyOrientation>();
camera = new WRot(WAngle.Zero, body.CameraPitch - new WAngle(256), new WAngle(256));
lightSource = new WRot(WAngle.Zero, new WAngle(256) - info.LightPitch, info.LightYaw);
}
bool initializePalettes = true;
public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) { initializePalettes = true; }
void ITick.Tick(Actor self)
{
var updated = false;
foreach (var w in wrappers.Values)
updated |= w.Tick();
if (updated)
self.World.ScreenMap.AddOrUpdate(self);
}
protected PaletteReference colorPalette, normalsPalette, shadowPalette;
IEnumerable<IRenderable> IRender.Render(Actor self, WorldRenderer wr)
{
if (initializePalettes)
{
var paletteName = Info.Palette ?? Info.PlayerPalette + self.Owner.InternalName;
colorPalette = wr.Palette(paletteName);
normalsPalette = wr.Palette(Info.NormalsPalette);
shadowPalette = wr.Palette(Info.ShadowPalette);
initializePalettes = false;
}
return new IRenderable[]
{
new ModelRenderable(
components, self.CenterPosition, 0, camera, Info.Scale,
lightSource, Info.LightAmbientColor, Info.LightDiffuseColor,
colorPalette, normalsPalette, shadowPalette)
};
}
IEnumerable<Rectangle> IRender.ScreenBounds(Actor self, WorldRenderer wr)
{
var pos = self.CenterPosition;
foreach (var c in components)
if (c.IsVisible)
yield return c.ScreenBounds(pos, wr, Info.Scale);
}
public string Image => Info.Image ?? self.Info.Name;
public void Add(ModelAnimation m)
{
components.Add(m);
wrappers.Add(m, new AnimationWrapper(m));
}
public void Remove(ModelAnimation m)
{
components.Remove(m);
wrappers.Remove(m);
}
}
}

View File

@@ -1,103 +0,0 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render
{
public class WithVoxelBarrelInfo : ConditionalTraitInfo, IRenderActorPreviewVoxelsInfo, Requires<RenderVoxelsInfo>, Requires<ArmamentInfo>, Requires<TurretedInfo>
{
[Desc("Voxel sequence name to use")]
public readonly string Sequence = "barrel";
[Desc("Armament to use for recoil")]
public readonly string Armament = "primary";
[Desc("Visual offset")]
public readonly WVec LocalOffset = WVec.Zero;
[Desc("Rotate the barrel relative to the body")]
public readonly WRot LocalOrientation = WRot.None;
[Desc("Defines if the Voxel should have a shadow.")]
public readonly bool ShowShadow = true;
public override object Create(ActorInitializer init) { return new WithVoxelBarrel(init.Self, this); }
public IEnumerable<ModelAnimation> RenderPreviewVoxels(
ActorPreviewInitializer init, RenderVoxelsInfo rv, string image, Func<WRot> orientation, int facings, PaletteReference p)
{
if (!EnabledByDefault)
yield break;
var body = init.Actor.TraitInfo<BodyOrientationInfo>();
var armament = init.Actor.TraitInfos<ArmamentInfo>()
.First(a => a.Name == Armament);
var t = init.Actor.TraitInfos<TurretedInfo>()
.First(tt => tt.Turret == armament.Turret);
var model = init.World.ModelCache.GetModelSequence(image, Sequence);
var turretOrientation = t.PreviewOrientation(init, orientation, facings);
WVec BarrelOffset() => body.LocalToWorld(t.Offset + LocalOffset.Rotate(turretOrientation()));
WRot BarrelOrientation() => LocalOrientation.Rotate(turretOrientation());
yield return new ModelAnimation(model, BarrelOffset, BarrelOrientation, () => false, () => 0, ShowShadow);
}
}
public class WithVoxelBarrel : ConditionalTrait<WithVoxelBarrelInfo>
{
readonly Actor self;
readonly Armament armament;
readonly Turreted turreted;
readonly BodyOrientation body;
public WithVoxelBarrel(Actor self, WithVoxelBarrelInfo info)
: base(info)
{
this.self = self;
body = self.Trait<BodyOrientation>();
armament = self.TraitsImplementing<Armament>()
.First(a => a.Info.Name == Info.Armament);
turreted = self.TraitsImplementing<Turreted>()
.First(tt => tt.Name == armament.Info.Turret);
var rv = self.Trait<RenderVoxels>();
rv.Add(new ModelAnimation(self.World.ModelCache.GetModelSequence(rv.Image, Info.Sequence),
BarrelOffset, BarrelRotation,
() => IsTraitDisabled, () => 0, info.ShowShadow));
}
WVec BarrelOffset()
{
// Barrel offset in turret coordinates
var localOffset = Info.LocalOffset + new WVec(-armament.Recoil, WDist.Zero, WDist.Zero);
// Turret coordinates to body coordinates
var bodyOrientation = body.QuantizeOrientation(self.Orientation);
localOffset = localOffset.Rotate(turreted.WorldOrientation) + turreted.Offset.Rotate(bodyOrientation);
// Body coordinates to world coordinates
return body.LocalToWorld(localOffset);
}
WRot BarrelRotation()
{
return Info.LocalOrientation.Rotate(turreted.WorldOrientation);
}
}
}

View File

@@ -1,67 +0,0 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render
{
[Desc("Also returns a default selection size that is calculated automatically from the voxel dimensions.")]
public class WithVoxelBodyInfo : ConditionalTraitInfo, IRenderActorPreviewVoxelsInfo, Requires<RenderVoxelsInfo>
{
public readonly string Sequence = "idle";
public readonly WVec Offset;
[Desc("Defines if the Voxel should have a shadow.")]
public readonly bool ShowShadow = true;
public override object Create(ActorInitializer init) { return new WithVoxelBody(init.Self, this); }
public IEnumerable<ModelAnimation> RenderPreviewVoxels(
ActorPreviewInitializer init, RenderVoxelsInfo rv, string image, Func<WRot> orientation, int facings, PaletteReference p)
{
var body = init.Actor.TraitInfo<BodyOrientationInfo>();
var model = init.World.ModelCache.GetModelSequence(image, Sequence);
yield return new ModelAnimation(model, () => Offset,
() => body.QuantizeOrientation(orientation(), facings),
() => false, () => 0, ShowShadow);
}
}
public class WithVoxelBody : ConditionalTrait<WithVoxelBodyInfo>, IAutoMouseBounds
{
readonly ModelAnimation modelAnimation;
readonly RenderVoxels rv;
public WithVoxelBody(Actor self, WithVoxelBodyInfo info)
: base(info)
{
var body = self.Trait<BodyOrientation>();
rv = self.Trait<RenderVoxels>();
var model = self.World.ModelCache.GetModelSequence(rv.Image, info.Sequence);
modelAnimation = new ModelAnimation(model, () => info.Offset,
() => body.QuantizeOrientation(self.Orientation),
() => IsTraitDisabled, () => 0, info.ShowShadow);
rv.Add(modelAnimation);
}
Rectangle IAutoMouseBounds.AutoMouseoverBounds(Actor self, WorldRenderer wr)
{
return modelAnimation.ScreenBounds(self.CenterPosition, wr, rv.Info.Scale);
}
}
}

View File

@@ -1,66 +0,0 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render
{
public class WithVoxelTurretInfo : ConditionalTraitInfo, IRenderActorPreviewVoxelsInfo, Requires<RenderVoxelsInfo>, Requires<TurretedInfo>
{
[Desc("Voxel sequence name to use")]
public readonly string Sequence = "turret";
[Desc("Turreted 'Turret' key to display")]
public readonly string Turret = "primary";
[Desc("Defines if the Voxel should have a shadow.")]
public readonly bool ShowShadow = true;
public override object Create(ActorInitializer init) { return new WithVoxelTurret(init.Self, this); }
public IEnumerable<ModelAnimation> RenderPreviewVoxels(
ActorPreviewInitializer init, RenderVoxelsInfo rv, string image, Func<WRot> orientation, int facings, PaletteReference p)
{
if (!EnabledByDefault)
yield break;
var t = init.Actor.TraitInfos<TurretedInfo>()
.First(tt => tt.Turret == Turret);
var model = init.World.ModelCache.GetModelSequence(image, Sequence);
var turretOffset = t.PreviewPosition(init, orientation);
var turretOrientation = t.PreviewOrientation(init, orientation, facings);
yield return new ModelAnimation(model, turretOffset, turretOrientation, () => false, () => 0, ShowShadow);
}
}
public class WithVoxelTurret : ConditionalTrait<WithVoxelTurretInfo>
{
readonly Turreted turreted;
public WithVoxelTurret(Actor self, WithVoxelTurretInfo info)
: base(info)
{
turreted = self.TraitsImplementing<Turreted>()
.First(tt => tt.Name == Info.Turret);
var rv = self.Trait<RenderVoxels>();
rv.Add(new ModelAnimation(self.World.ModelCache.GetModelSequence(rv.Image, Info.Sequence),
() => turreted.Position(self), () => turreted.WorldOrientation,
() => IsTraitDisabled, () => 0, info.ShowShadow));
}
}
}