move generic Voxel render traits to Mods.Common
This commit is contained in:
116
OpenRA.Mods.Common/Traits/Render/RenderVoxels.cs
Normal file
116
OpenRA.Mods.Common/Traits/Render/RenderVoxels.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Graphics;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public interface IRenderActorPreviewVoxelsInfo
|
||||
{
|
||||
IEnumerable<VoxelAnimation> RenderPreviewVoxels(
|
||||
ActorPreviewInitializer init, RenderVoxelsInfo rv, string image, WRot orientation, int facings, PaletteReference p);
|
||||
}
|
||||
|
||||
public class RenderVoxelsInfo : ITraitInfo, IRenderActorPreviewInfo, Requires<IBodyOrientationInfo>
|
||||
{
|
||||
[Desc("Defaults to the actor name.")]
|
||||
public readonly string Image = null;
|
||||
|
||||
[Desc("Custom palette name")]
|
||||
public readonly string Palette = null;
|
||||
|
||||
[Desc("Custom PlayerColorPalette: BaseName")]
|
||||
public readonly string PlayerPalette = "player";
|
||||
public readonly string NormalsPalette = "normals";
|
||||
public readonly string ShadowPalette = "shadow";
|
||||
|
||||
[Desc("Change the image size.")]
|
||||
public readonly float Scale = 10;
|
||||
|
||||
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 virtual object Create(ActorInitializer init) { return new RenderVoxels(init.Self, this); }
|
||||
|
||||
public virtual IEnumerable<IActorPreview> RenderPreview(ActorPreviewInitializer init)
|
||||
{
|
||||
var body = init.Actor.Traits.Get<BodyOrientationInfo>();
|
||||
var race = init.Get<RaceInit, string>();
|
||||
var ownerName = init.Get<OwnerInit>().PlayerName;
|
||||
var sequenceProvider = init.World.Map.SequenceProvider;
|
||||
var image = Image ?? init.Actor.Name;
|
||||
var facings = body.QuantizedFacings == -1 ?
|
||||
init.Actor.Traits.Get<IQuantizeBodyOrientationInfo>().QuantizedBodyFacings(init.Actor, sequenceProvider, race) :
|
||||
body.QuantizedFacings;
|
||||
var palette = init.WorldRenderer.Palette(Palette ?? PlayerPalette + ownerName);
|
||||
|
||||
var ifacing = init.Actor.Traits.GetOrDefault<IFacingInfo>();
|
||||
var facing = ifacing != null ? init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : ifacing.GetInitialFacing() : 0;
|
||||
var orientation = WRot.FromFacing(facing);
|
||||
var components = init.Actor.Traits.WithInterface<IRenderActorPreviewVoxelsInfo>()
|
||||
.SelectMany(rvpi => rvpi.RenderPreviewVoxels(init, this, image, orientation, facings, palette))
|
||||
.ToArray();
|
||||
|
||||
yield return new VoxelPreview(components, WVec.Zero, 0, Scale, LightPitch,
|
||||
LightYaw, LightAmbientColor, LightDiffuseColor, body.CameraPitch,
|
||||
palette, init.WorldRenderer.Palette(NormalsPalette), init.WorldRenderer.Palette(ShadowPalette));
|
||||
}
|
||||
}
|
||||
|
||||
public class RenderVoxels : IRender, INotifyOwnerChanged
|
||||
{
|
||||
readonly List<VoxelAnimation> components = new List<VoxelAnimation>();
|
||||
Actor self;
|
||||
RenderVoxelsInfo info;
|
||||
IBodyOrientation body;
|
||||
WRot camera;
|
||||
WRot lightSource;
|
||||
|
||||
public RenderVoxels(Actor self, RenderVoxelsInfo info)
|
||||
{
|
||||
this.self = self;
|
||||
this.info = info;
|
||||
body = self.Trait<IBodyOrientation>();
|
||||
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; }
|
||||
|
||||
protected PaletteReference colorPalette, normalsPalette, shadowPalette;
|
||||
public IEnumerable<IRenderable> 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;
|
||||
}
|
||||
|
||||
yield return new VoxelRenderable(
|
||||
components, self.CenterPosition, 0, camera, info.Scale,
|
||||
lightSource, info.LightAmbientColor, info.LightDiffuseColor,
|
||||
colorPalette, normalsPalette, shadowPalette);
|
||||
}
|
||||
|
||||
public string Image { get { return info.Image ?? self.Info.Name; } }
|
||||
public void Add(VoxelAnimation v) { components.Add(v); }
|
||||
public void Remove(VoxelAnimation v) { components.Remove(v); }
|
||||
}
|
||||
}
|
||||
92
OpenRA.Mods.Common/Traits/Render/WithVoxelBarrel.cs
Normal file
92
OpenRA.Mods.Common/Traits/Render/WithVoxelBarrel.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Graphics;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public class WithVoxelBarrelInfo : ITraitInfo, 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;
|
||||
|
||||
public object Create(ActorInitializer init) { return new WithVoxelBarrel(init.Self, this); }
|
||||
|
||||
public IEnumerable<VoxelAnimation> RenderPreviewVoxels(ActorPreviewInitializer init, RenderVoxelsInfo rv, string image, WRot orientation, int facings, PaletteReference p)
|
||||
{
|
||||
var body = init.Actor.Traits.Get<BodyOrientationInfo>();
|
||||
var armament = init.Actor.Traits.WithInterface<ArmamentInfo>()
|
||||
.First(a => a.Name == Armament);
|
||||
var t = init.Actor.Traits.WithInterface<TurretedInfo>()
|
||||
.First(tt => tt.Turret == armament.Turret);
|
||||
|
||||
var voxel = VoxelProvider.GetVoxel(image, Sequence);
|
||||
|
||||
var turretFacing = init.Contains<TurretFacingInit>() ? init.Get<TurretFacingInit, int>() : t.InitialFacing;
|
||||
var turretOrientation = body.QuantizeOrientation(new WRot(WAngle.Zero, WAngle.Zero, WAngle.FromFacing(turretFacing) - orientation.Yaw), facings);
|
||||
var turretOffset = body.LocalToWorld(t.Offset.Rotate(orientation));
|
||||
|
||||
yield return new VoxelAnimation(voxel, () => turretOffset, () => new[] { turretOrientation, orientation },
|
||||
() => false, () => 0);
|
||||
}
|
||||
}
|
||||
|
||||
public class WithVoxelBarrel
|
||||
{
|
||||
WithVoxelBarrelInfo info;
|
||||
Actor self;
|
||||
Armament armament;
|
||||
Turreted turreted;
|
||||
IBodyOrientation body;
|
||||
|
||||
public WithVoxelBarrel(Actor self, WithVoxelBarrelInfo info)
|
||||
{
|
||||
this.self = self;
|
||||
this.info = info;
|
||||
body = self.Trait<IBodyOrientation>();
|
||||
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 VoxelAnimation(VoxelProvider.GetVoxel(rv.Image, info.Sequence),
|
||||
BarrelOffset, BarrelRotation,
|
||||
() => false, () => 0));
|
||||
}
|
||||
|
||||
WVec BarrelOffset()
|
||||
{
|
||||
var localOffset = info.LocalOffset + new WVec(-armament.Recoil, WRange.Zero, WRange.Zero);
|
||||
var turretOffset = turreted != null ? turreted.Position(self) : WVec.Zero;
|
||||
var turretOrientation = turreted != null ? turreted.LocalOrientation(self) : WRot.Zero;
|
||||
|
||||
var quantizedBody = body.QuantizeOrientation(self, self.Orientation);
|
||||
var quantizedTurret = body.QuantizeOrientation(self, turretOrientation);
|
||||
return turretOffset + body.LocalToWorld(localOffset.Rotate(quantizedTurret).Rotate(quantizedBody));
|
||||
}
|
||||
|
||||
IEnumerable<WRot> BarrelRotation()
|
||||
{
|
||||
var b = self.Orientation;
|
||||
var qb = body.QuantizeOrientation(self, b);
|
||||
yield return turreted.LocalOrientation(self) + WRot.FromYaw(b.Yaw - qb.Yaw);
|
||||
yield return qb;
|
||||
}
|
||||
}
|
||||
}
|
||||
62
OpenRA.Mods.Common/Traits/Render/WithVoxelBody.cs
Normal file
62
OpenRA.Mods.Common/Traits/Render/WithVoxelBody.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. 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
|
||||
{
|
||||
[Desc("Also returns a default selection size that is calculated automatically from the voxel dimensions.")]
|
||||
public class WithVoxelBodyInfo : ITraitInfo, IQuantizeBodyOrientationInfo, IRenderActorPreviewVoxelsInfo, Requires<RenderVoxelsInfo>
|
||||
{
|
||||
public readonly string Sequence = "idle";
|
||||
|
||||
public object Create(ActorInitializer init) { return new WithVoxelBody(init.Self, this); }
|
||||
|
||||
public IEnumerable<VoxelAnimation> RenderPreviewVoxels(ActorPreviewInitializer init, RenderVoxelsInfo rv, string image, WRot orientation, int facings, PaletteReference p)
|
||||
{
|
||||
var body = init.Actor.Traits.Get<BodyOrientationInfo>();
|
||||
var voxel = VoxelProvider.GetVoxel(image, "idle");
|
||||
var bodyOrientation = new[] { body.QuantizeOrientation(orientation, facings) };
|
||||
yield return new VoxelAnimation(voxel, () => WVec.Zero,
|
||||
() => bodyOrientation,
|
||||
() => false, () => 0);
|
||||
}
|
||||
|
||||
public int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race) { return 0; }
|
||||
}
|
||||
|
||||
public class WithVoxelBody : IAutoSelectionSize
|
||||
{
|
||||
int2 size;
|
||||
|
||||
public WithVoxelBody(Actor self, WithVoxelBodyInfo info)
|
||||
{
|
||||
var body = self.Trait<IBodyOrientation>();
|
||||
var rv = self.Trait<RenderVoxels>();
|
||||
|
||||
var voxel = VoxelProvider.GetVoxel(rv.Image, info.Sequence);
|
||||
rv.Add(new VoxelAnimation(voxel, () => WVec.Zero,
|
||||
() => new[] { body.QuantizeOrientation(self, self.Orientation) },
|
||||
() => false, () => 0));
|
||||
|
||||
// Selection size
|
||||
var rvi = self.Info.Traits.Get<RenderVoxelsInfo>();
|
||||
var s = (int)(rvi.Scale * voxel.Size.Aggregate(Math.Max));
|
||||
size = new int2(s, s);
|
||||
}
|
||||
|
||||
public int2 SelectionSize(Actor self) { return size; }
|
||||
}
|
||||
}
|
||||
72
OpenRA.Mods.Common/Traits/Render/WithVoxelTurret.cs
Normal file
72
OpenRA.Mods.Common/Traits/Render/WithVoxelTurret.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Graphics;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public class WithVoxelTurretInfo : ITraitInfo, 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";
|
||||
|
||||
public object Create(ActorInitializer init) { return new WithVoxelTurret(init.Self, this); }
|
||||
|
||||
public IEnumerable<VoxelAnimation> RenderPreviewVoxels(ActorPreviewInitializer init, RenderVoxelsInfo rv, string image, WRot orientation, int facings, PaletteReference p)
|
||||
{
|
||||
var body = init.Actor.Traits.Get<BodyOrientationInfo>();
|
||||
var t = init.Actor.Traits.WithInterface<TurretedInfo>()
|
||||
.First(tt => tt.Turret == Turret);
|
||||
|
||||
var voxel = VoxelProvider.GetVoxel(image, Sequence);
|
||||
var turretOffset = body.LocalToWorld(t.Offset.Rotate(orientation));
|
||||
|
||||
var turretFacing = init.Contains<TurretFacingInit>() ? init.Get<TurretFacingInit, int>() : t.InitialFacing;
|
||||
var turretBodyOrientation = new WRot(WAngle.Zero, WAngle.Zero, WAngle.FromFacing(turretFacing) - orientation.Yaw);
|
||||
var turretOrientation = new[] { turretBodyOrientation, body.QuantizeOrientation(orientation, facings) };
|
||||
yield return new VoxelAnimation(voxel, () => turretOffset, () => turretOrientation, () => false, () => 0);
|
||||
}
|
||||
}
|
||||
|
||||
public class WithVoxelTurret
|
||||
{
|
||||
readonly Actor self;
|
||||
readonly Turreted turreted;
|
||||
readonly IBodyOrientation body;
|
||||
|
||||
public WithVoxelTurret(Actor self, WithVoxelTurretInfo info)
|
||||
{
|
||||
this.self = self;
|
||||
body = self.Trait<IBodyOrientation>();
|
||||
turreted = self.TraitsImplementing<Turreted>()
|
||||
.First(tt => tt.Name == info.Turret);
|
||||
|
||||
var rv = self.Trait<RenderVoxels>();
|
||||
rv.Add(new VoxelAnimation(VoxelProvider.GetVoxel(rv.Image, info.Sequence),
|
||||
() => turreted.Position(self), TurretRotation,
|
||||
() => false, () => 0));
|
||||
}
|
||||
|
||||
IEnumerable<WRot> TurretRotation()
|
||||
{
|
||||
var b = self.Orientation;
|
||||
var qb = body.QuantizeOrientation(self, b);
|
||||
yield return turreted.LocalOrientation(self) + WRot.FromYaw(b.Yaw - qb.Yaw);
|
||||
yield return qb;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user