Skip to content

Commit

Permalink
モーフへのノイズ付加に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
mfakane committed Dec 31, 2022
1 parent 32314be commit 1a9cdf6
Show file tree
Hide file tree
Showing 15 changed files with 1,217 additions and 986 deletions.
4 changes: 3 additions & 1 deletion ApplyNoisePlugin/ApplyNoiseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ public override void Run(CommandArgs e)
KeyShiftWidth = 0,
NoiseValueInterval = 5,
NoiseValue = NoiseValue.Default,
NormalizeWeight = true,
IsPositionEnabled = transformer.CanApplyTranslation,
IsPositionLocalVisible = transformer.CanTranslateByLocal,
IsRotationEnabled = transformer.CanApplyRotation,
IsRotationLocalVisible = transformer.CanRotateByLocal,
IsWeightEnabled = transformer.CanApplyWeight,
IsEnvironmentEnabled = false,
IsPositionLocal = transformer.CanRotateByLocal,
IsRotationLocal = transformer.CanRotateByLocal,
Expand All @@ -64,7 +66,7 @@ public override void Run(CommandArgs e)
);

using (Scene.BeginUndoBlock())
transformer.ApplyNoise(context, f.IsPositionLocal, f.IsRotationLocal);
transformer.ApplyNoise(context, f.IsPositionLocal, f.IsRotationLocal, f.NormalizeWeight);
}

public override string Description => "選択したキーフレームの移動および回転値に指定したオフセットを与えます。";
Expand Down
2,053 changes: 1,096 additions & 957 deletions ApplyNoisePlugin/ApplyNoiseForm.Designer.cs

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion ApplyNoisePlugin/ApplyNoiseForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public bool IsRotationEnabled
set => rotationPanel.Visible = value;
}

public bool IsWeightEnabled
{
get => weightPanel.Visible;
set => weightPanel.Visible = value;
}

public bool IsEnvironmentEnabled
{
get => environmentPanel.Visible;
Expand All @@ -42,6 +48,12 @@ public int NoiseValueInterval
set => noiseValueIntervalNumericUpDown.Value = value;
}

public bool NormalizeWeight
{
get => normalizeWeightCheckBox.Checked;
set => normalizeWeightCheckBox.Checked = value;
}

public int KeyShiftWidth
{
get => (int)keyShiftNoiseNumericUpDown.Value;
Expand All @@ -54,6 +66,7 @@ public NoiseValue NoiseValue
new(
new((float)positionXNumericUpDown.Value, (float)positionYNumericUpDown.Value, (float)positionZNumericUpDown.Value),
MathHelper.ToRadians(new Vector3((float)rotationXNumericUpDown.Value, (float)rotationYNumericUpDown.Value, (float)rotationZNumericUpDown.Value)),
(float)weightNumericUpDown.Value,
(float)gravityNumericUpDown.Value,
new((float)gravityXNumericUpDown.Value, (float)gravityYNumericUpDown.Value, (float)gravityZNumericUpDown.Value)
);
Expand All @@ -66,6 +79,7 @@ public NoiseValue NoiseValue
rotationXNumericUpDown.Value = (decimal)degrees.X;
rotationYNumericUpDown.Value = (decimal)degrees.Y;
rotationZNumericUpDown.Value = (decimal)degrees.Z;
weightNumericUpDown.Value = (decimal)value.Weight;
gravityNumericUpDown.Value = (decimal)value.Gravity;
gravityXNumericUpDown.Value = (decimal)value.GravityDirection.X;
gravityYNumericUpDown.Value = (decimal)value.GravityDirection.Y;
Expand Down Expand Up @@ -119,7 +133,7 @@ public ApplyNoiseForm()
foreach (var i in controls)
{
var changing = false;
var isPercentage = new[] { "gravityXTrackBar", "gravityYTrackBar", "gravityZTrackBar" }.Contains(i.TrackBar.Name);
var isPercentage = new[] { "weightTrackBar", "gravityXTrackBar", "gravityYTrackBar", "gravityZTrackBar" }.Contains(i.TrackBar.Name);

i.TrackBar.ValueChanged += (sender, e) =>
{
Expand Down
4 changes: 2 additions & 2 deletions ApplyNoisePlugin/ApplyNoiseForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
1 change: 1 addition & 0 deletions ApplyNoisePlugin/ApplyNoisePlugin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ MikuMikuMoving.exe のあるフォルダの Plugins フォルダに本 dll を
Version 0.5, Sat, 31 Dec 2022

アンドゥ処理の安定性向上
モーフへのノイズ付加に対応

Version 0.4, Thu, 29 Dec 2022

Expand Down
4 changes: 3 additions & 1 deletion ApplyNoisePlugin/Transform/CameraLayerTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class CameraLayerTransformer : KeyFrameTransformer<CameraFrameData>
public override bool CanTranslateByLocal => false;

public override bool CanRotateByLocal => false;

public override bool CanApplyWeight => false;

CameraLayerTransformer(CameraLayer layer)
: base(layer.Frames.GetKeyFrames()) =>
Expand All @@ -28,7 +30,7 @@ protected override void ReplaceAllKeyFrames(IEnumerable<CameraFrameData> keyFram
protected override CameraFrameData GetFrame(long frameNumber) =>
layer.Frames.GetFrame(frameNumber);

protected override void ApplyNoiseToKeyFrame(CameraFrameData keyFrame, NoiseValue value, bool translateByLocal, bool rotateByLocal)
protected override void ApplyNoiseToKeyFrame(CameraFrameData keyFrame, NoiseValue value, bool translateByLocal, bool rotateByLocal, bool applyWeight)
{
keyFrame.Position += value.Position;
keyFrame.Angle += value.Rotation;
Expand Down
7 changes: 5 additions & 2 deletions ApplyNoisePlugin/Transform/CompositeTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ public class CompositeTransformer : ITransformer

public bool CanRotateByLocal =>
transformers.Any(x => x.CanRotateByLocal);

public bool CanApplyWeight =>
transformers.Any(x => x.CanApplyWeight);

CompositeTransformer(IReadOnlyCollection<ITransformer> transformers) =>
this.transformers = transformers;

public static ITransformer? Create(IReadOnlyCollection<ITransformer> transformers) =>
transformers.Any() ? new CompositeTransformer(transformers) : null;

public void ApplyNoise(NoiseContext context, bool translateByLocal, bool rotateByLocal)
public void ApplyNoise(NoiseContext context, bool translateByLocal, bool rotateByLocal, bool normalizeWeight)
{
foreach (var transformer in transformers)
transformer.ApplyNoise(context, translateByLocal, rotateByLocal);
transformer.ApplyNoise(context, translateByLocal, rotateByLocal, normalizeWeight);
}
}
3 changes: 2 additions & 1 deletion ApplyNoisePlugin/Transform/ITransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface ITransformer
bool CanTranslateByLocal { get; }
bool CanApplyRotation { get; }
bool CanRotateByLocal { get; }
void ApplyNoise(NoiseContext context, bool translateByLocal, bool rotateByLocal);
bool CanApplyWeight { get; }
void ApplyNoise(NoiseContext context, bool translateByLocal, bool rotateByLocal, bool normalizeWeight);
}
16 changes: 9 additions & 7 deletions ApplyNoisePlugin/Transform/KeyFrameTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public abstract class KeyFrameTransformer<T> : ITransformer
public long? SelectedMaximumFrameNumber =>
selectedKeyFrames.Any() ? selectedKeyFrames.Max(x => x.FrameNumber) : null;

public bool CanApplyTranslation => true;
public virtual bool CanApplyTranslation => true;

public abstract bool CanTranslateByLocal { get; }

public bool CanApplyRotation => true;
public virtual bool CanApplyRotation => true;

public abstract bool CanRotateByLocal { get; }

public abstract bool CanApplyWeight { get; }

protected KeyFrameTransformer(IReadOnlyCollection<T> keyFrames)
{
Expand All @@ -32,9 +34,9 @@ protected KeyFrameTransformer(IReadOnlyCollection<T> keyFrames)

protected abstract void ReplaceAllKeyFrames(IEnumerable<T> keyFrames);
protected abstract T GetFrame(long frameNumber);
protected abstract void ApplyNoiseToKeyFrame(T keyFrame, NoiseValue value, bool translateByLocal, bool rotateByLocal);
protected abstract void ApplyNoiseToKeyFrame(T keyFrame, NoiseValue value, bool translateByLocal, bool rotateByLocal, bool normalizeWeight);

IEnumerable<T> GetAppliedKeyFrames(NoiseContext context, bool translateByLocal, bool rotateByLocal)
IEnumerable<T> GetAppliedKeyFrames(NoiseContext context, bool translateByLocal, bool rotateByLocal, bool normalizeWeight)
{
// 時折フレーム番号が重複していることがあるので ToLookup してから最初に出現したもののみ取り扱う
var existingKeyFrames = keyFrames.ToLookup(x => x.FrameNumber).ToDictionary(x => x.Key, x => x.First());
Expand All @@ -47,7 +49,7 @@ IEnumerable<T> GetAppliedKeyFrames(NoiseContext context, bool translateByLocal,
? existingKeyFrame
: GetFrame(value.FrameNumber);

ApplyNoiseToKeyFrame(newKeyFrame, value.Value, translateByLocal, rotateByLocal);
ApplyNoiseToKeyFrame(newKeyFrame, value.Value, translateByLocal, rotateByLocal, normalizeWeight);
newKeyFrame.FrameNumber = value.ShiftedFrameNumber;
newKeyFrame.Selected = true;

Expand All @@ -62,6 +64,6 @@ IEnumerable<T> GetAppliedKeyFrames(NoiseContext context, bool translateByLocal,
return newKeyFrames.Values;
}

public void ApplyNoise(NoiseContext context, bool translateByLocal, bool rotateByLocal) =>
ReplaceAllKeyFrames(GetAppliedKeyFrames(context, translateByLocal, rotateByLocal));
public void ApplyNoise(NoiseContext context, bool translateByLocal, bool rotateByLocal, bool normalizeWeight) =>
ReplaceAllKeyFrames(GetAppliedKeyFrames(context, translateByLocal, rotateByLocal, normalizeWeight));
}
43 changes: 43 additions & 0 deletions ApplyNoisePlugin/Transform/MorphTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using Linearstar.MikuMikuMoving.Framework;
using MikuMikuPlugin;

namespace Linearstar.MikuMikuMoving.ApplyNoisePlugin.Transform;

public class MorphTransformer : KeyFrameTransformer<MorphFrameData>
{
readonly Morph morph;

public override bool CanApplyTranslation => false;

public override bool CanTranslateByLocal => false;

public override bool CanApplyRotation => false;

public override bool CanRotateByLocal => false;

public override bool CanApplyWeight => true;

MorphTransformer(Morph morph)
: base(morph.Frames.GetKeyFrames()) =>
this.morph = morph;

public static IEnumerable<MorphTransformer> FromModel(Model model) =>
model.Morphs
.Where(morph => morph.SelectedFrames.Any())
.Select(morph => new MorphTransformer(morph));

protected override void ReplaceAllKeyFrames(IEnumerable<MorphFrameData> keyFrames) =>
morph.Frames.ReplaceAllKeyFrames(keyFrames.ToList());

protected override MorphFrameData GetFrame(long frameNumber) =>
morph.Frames.GetFrame(frameNumber);

protected override void ApplyNoiseToKeyFrame(MorphFrameData keyFrame, NoiseValue value, bool translateByLocal, bool rotateByLocal, bool normalizeWeight)
{
var newWeight = keyFrame.Weight + value.Weight;

keyFrame.Weight = normalizeWeight ? MathHelper.Clamp(newWeight, 0, 1) : newWeight;
}
}
6 changes: 4 additions & 2 deletions ApplyNoisePlugin/Transform/MotionLayerTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public class MotionLayerTransformer : MotionTransformer

public override bool CanTranslateByLocal => true;

public override bool CanRotateByLocal => bone?.BoneFlags.HasFlag(BoneType.Rotate) ?? false;

public override bool CanRotateByLocal => bone?.BoneFlags.HasFlag(BoneType.Rotate) ?? false;

public override bool CanApplyWeight => false;

MotionLayerTransformer(Bone? bone, MotionLayer motionLayer)
: base(bone, motionLayer.Frames.GetKeyFrames())
{
Expand Down
4 changes: 3 additions & 1 deletion ApplyNoisePlugin/Transform/MotionTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ public abstract class MotionTransformer : KeyFrameTransformer<MotionFrameData>
{
readonly Bone? bone;

public override bool CanApplyWeight => false;

public MotionTransformer(Bone? bone, IReadOnlyCollection<MotionFrameData> keyFrames)
: base(keyFrames) =>
this.bone = bone;

protected abstract override void ReplaceAllKeyFrames(IEnumerable<MotionFrameData> keyFrames);

protected override void ApplyNoiseToKeyFrame(MotionFrameData keyFrame, NoiseValue value, bool translateByLocal, bool rotateByLocal)
protected override void ApplyNoiseToKeyFrame(MotionFrameData keyFrame, NoiseValue value, bool translateByLocal, bool rotateByLocal, bool applyWeight)
{
if (bone?.BoneFlags.HasFlag(BoneType.Rotate) != false)
keyFrame.Quaternion = ApplyRotation(bone, keyFrame.Quaternion, value.Rotation, rotateByLocal);
Expand Down
4 changes: 4 additions & 0 deletions ApplyNoisePlugin/Transform/NoiseValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Linearstar.MikuMikuMoving.ApplyNoisePlugin.Transform;
public record NoiseValue(
Vector3 Position,
Vector3 Rotation,
float Weight,
float Gravity,
Vector3 GravityDirection
)
Expand All @@ -15,6 +16,7 @@ Vector3 GravityDirection
new(
new(5),
MathHelper.ToRadians(new Vector3(5)),
0.5f,
10,
new(1)
);
Expand All @@ -33,6 +35,7 @@ public static NoiseValue Create(Random random, NoiseValue width) =>
new(
Random3(random, width.Position),
Random3(random, width.Rotation),
Random(random, width.Weight),
Random(random, width.Gravity),
Random3(random, width.GravityDirection)
);
Expand All @@ -41,6 +44,7 @@ public static NoiseValue Interpolate(NoiseValue a, NoiseValue b, float amount) =
new(
Vector3.Lerp(a.Position, b.Position, amount),
Vector3.Lerp(a.Rotation, b.Rotation, amount),
MathHelper.Lerp(a.Weight, b.Weight, amount),
MathHelper.Lerp(a.Gravity, b.Gravity, amount),
Vector3.Lerp(a.GravityDirection, b.GravityDirection, amount)
);
Expand Down
2 changes: 2 additions & 0 deletions ApplyNoisePlugin/Transform/Transformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static class Transformer
case EditMode.ModelMode when scene.ActiveModel is { } activeModel:
return CompositeTransformer.Create(activeModel.Bones
.SelectMany(MotionLayerTransformer.FromBone)
.Cast<ITransformer>()
.Concat(MorphTransformer.FromModel(activeModel))
.ToArray());
case EditMode.AccessoryMode:
return CompositeTransformer.Create(scene.Accessories
Expand Down
36 changes: 25 additions & 11 deletions PluginFramework/Framework/SceneExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,31 @@ public void Dispose()
IEnumerable<IKeyFrameBuffer> GetKeyFrameBuffers()
{
foreach (var model in scene.Models)
foreach (var layer in model.Bones.SelectMany(x => x.Layers))
yield return KeyFrameBuffer.Create(
layer.Frames.GetKeyFrames(),
layer.Frames.ReplaceAllKeyFrames,
layer.Selected,
x =>
{
if (layer.Selected == x) return;
layer.Selected = x;
}
);
{
foreach (var morph in model.Morphs)
yield return KeyFrameBuffer.Create(
morph.Frames.GetKeyFrames(),
morph.Frames.ReplaceAllKeyFrames,
morph.Selected,
x =>
{
if (morph.Selected == x) return;
morph.Selected = x;
}
);

foreach (var layer in model.Bones.SelectMany(x => x.Layers))
yield return KeyFrameBuffer.Create(
layer.Frames.GetKeyFrames(),
layer.Frames.ReplaceAllKeyFrames,
layer.Selected,
x =>
{
if (layer.Selected == x) return;
layer.Selected = x;
}
);
}

foreach (var layer in scene.Accessories.SelectMany(x => x.Layers))
yield return KeyFrameBuffer.Create(
Expand Down

0 comments on commit 1a9cdf6

Please sign in to comment.