Skip to content

Commit

Permalink
General code and comment cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thojmr committed Apr 12, 2022
1 parent a86ab04 commit ee2d509
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class MeshBlendShape
public string MeshName;//like SkinnedMeshRenderer.name
public int VertCount;//To differentiate 2 meshes with the same names use vertex count comparison
public string UncensorGUID;//Stores the uncensorGUID used with this blendshape
public BlendShapeController.BlendShape BlendShape;//Store just a single Frame for now, though its possible to have multiple frames. Preg+ only uses 1
public BlendShape BlendShape;//Store just a single Frame for now, though its possible to have multiple frames. Preg+ only uses 1

public MeshBlendShape(string meshName, BlendShapeController.BlendShape blendShape, int vertCount, string uncensorGUID)
public MeshBlendShape(string meshName, BlendShape blendShape, int vertCount, string uncensorGUID)
{
MeshName = meshName;
BlendShape = blendShape;
Expand Down Expand Up @@ -259,7 +259,7 @@ internal List<MeshBlendShape> LoopAndCreateBlendShape(List<SkinnedMeshRenderer>
/// <summary>
/// Convert a BlendShape to MeshBlendShape, used for storing as character card data
/// </summary>
internal MeshBlendShape ConvertToMeshBlendShape(string smrMeshName, BlendShapeController.BlendShape blendShape, string uncensorGUID)
internal MeshBlendShape ConvertToMeshBlendShape(string smrMeshName, BlendShape blendShape, string uncensorGUID)
{
if (blendShape == null) return null;
return new MeshBlendShape(smrMeshName, blendShape, blendShape.vertexCount, uncensorGUID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,13 @@ internal float[] DoClothMeasurement(SkinnedMeshRenderer clothSmr, SkinnedMeshRen

//Get the pre calculated preg verts for this mesh
var renderKey = GetMeshKey(clothSmr);
md.TryGetValue(renderKey, out MeshData _md);

var exists = md.TryGetValue(renderKey, out MeshData _md);
if (!exists)
{
if (PregnancyPlusPlugin.DebugLog.Value) PregnancyPlusPlugin.Logger.LogWarning($" DoClothMeasurement cant find MeshData for {renderKey}");
return new float[clothSmr.sharedMesh.vertexCount];
}

//Check for existing offset values, init if none found
var clothingOffsetsHasValue = md[renderKey].HasClothingOffsets;
var clothOffsets = new float[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,14 @@ internal Task[] LoopAndApplyMeshChanges(List<SkinnedMeshRenderer> smrs, MeshInfl

/// <summary>
/// For a skinned mesh renderer check for cached mesh info, then apply inflation when needed
/// This is technically the core logic loop for a single mesh
/// </summary>
/// <param name="smr">skinnedMeshRender to change</param>
/// <param name="meshInflateFlags">Contains any flags needed for mesh computation</param>
/// <param name="isClothingMesh">If this smr is a cloth mesh</param>
/// <param name="bodySmr">The characters body mesh</param>
/// <param name="isMainBody">Whether this mesh is the character body mesh</param>
/// <param name="ignoreKey">Any smr matching key will be ignored (Prevent computing main body mesh twice)</param>
/// <returns>boolean true if threaded</returns>
internal async Task StartMeshChanges(SkinnedMeshRenderer smr, MeshInflateFlags meshInflateFlags, bool isClothingMesh = false,
SkinnedMeshRenderer bodySmr = null, bool isMainBody = false, string ignoreKey = null)
{
Expand All @@ -229,7 +231,11 @@ internal async Task StartMeshChanges(SkinnedMeshRenderer smr, MeshInflateFlags m
if (needsComputeVerts)
{
//If it turns out that the current mesh is cached, then this will just return early
await ComputeMeshVerts(smr, isClothingMesh, bodySmr, meshInflateFlags, renderKey, isMainBody);
var vertsChanged = await ComputeVerts(smr, isClothingMesh, bodySmr, meshInflateFlags, renderKey, isMainBody);

//Compute the deltas used to make blendshapes
if (vertsChanged)
await ComputeDeltas(smr, renderKey, meshInflateFlags);
}

if (ignoreMeshList.Contains(renderKey)) return;
Expand Down Expand Up @@ -267,11 +273,11 @@ public bool NeedsComputeVerts(SkinnedMeshRenderer smr, string renderKey, MeshInf


/// <summary>
/// Just a helper function to combine searching for verts in a mesh, and then applying the transforms
/// Searching for valid verts in a mesh, and compute the mesh shapes
/// </summary>
/// <returns>Will return true if threaded</returns>
internal async Task ComputeMeshVerts(SkinnedMeshRenderer smr, bool isClothingMesh, SkinnedMeshRenderer bodyMeshRenderer, MeshInflateFlags meshInflateFlags,
string renderKey, bool isMainBody = false)
/// <returns>Will return true if verts were re-computed</returns>
internal async Task<bool> ComputeVerts(SkinnedMeshRenderer smr, bool isClothingMesh, SkinnedMeshRenderer bodyMeshRenderer,
MeshInflateFlags meshInflateFlags, string renderKey, bool isMainBody = false)
{
//The list of bones to get verticies for (Belly area verts). If a mesh does not contain one of these bones in smr.bones, it is skipped
#if KKS
Expand All @@ -288,10 +294,10 @@ internal async Task ComputeMeshVerts(SkinnedMeshRenderer smr, bool isClothingMes
hasVertsToProcess = await GetFilteredVerticieIndexes(smr, PregnancyPlusPlugin.MakeBalloon.Value ? null : boneFilters);

//If mesh was just added to the ignore list, stop here
if (ignoreMeshList.Contains(renderKey)) return;
if (ignoreMeshList.Contains(renderKey)) return false;

//If no belly verts found, or verts already cached, then we can skip this mesh
if (!hasVertsToProcess) return;
if (!hasVertsToProcess) return false;

//Get the newly created/or existing MeshData obj
md.TryGetValue(renderKey, out _md);
Expand All @@ -305,13 +311,12 @@ internal async Task ComputeMeshVerts(SkinnedMeshRenderer smr, bool isClothingMes
//Inflate the mesh to give it the round appearance
await GetInflatedVerticies(smr, bellyInfo.SphereRadius, isClothingMesh, bodyMeshRenderer, meshInflateFlags);

//Compute the deltas used to make blendshapes
await ComputeDeltas(smr, renderKey, meshInflateFlags);
return true;
}


/// <summary>
/// Apply the inflation to a blendshape once done computing inflated verts and deltas
/// Apply the computed shape to a blendshape. If the shape didn't change, apply the new blendshape weights
/// </summary>
internal void FinalizeInflation(SkinnedMeshRenderer smr, MeshInflateFlags meshInflateFlags, string blendShapeTag = null)
{
Expand All @@ -320,7 +325,7 @@ internal void FinalizeInflation(SkinnedMeshRenderer smr, MeshInflateFlags meshIn
//Apply computed mesh back to body as a blendshape
var appliedMeshChanges = ApplyInflation(smr, rendererName, meshInflateFlags.OverWriteMesh, blendShapeTempTagName, meshInflateFlags.bypassWhen0);

//When inflation is actively happening as clothing changes, make sure the new clothing grows too
//When HScene inflation is actively happening while user changes clothing, make sure the new clothing updates shape too
if (isDuringInflationScene) AppendToQuickInflateList(smr);

//If the inflation is applied, update the previous slider config values
Expand All @@ -329,11 +334,11 @@ internal void FinalizeInflation(SkinnedMeshRenderer smr, MeshInflateFlags meshIn


/// <summary>
/// Compute and cache the bind pose (T-pose) mesh vertex positions, we need this to ignore all character animations when computing the belly shape,
/// and to align the meshes together. Results get cached for subsiquent passes
/// Compute and cache the bind pose (T-pose) mesh vertex positions, we need this in order to ignore all character animations when computing the belly shape.
/// It also aligns the meshes together. Cache the computed results
/// </summary>
/// <returns>Will return true if threaded</returns>
internal async Task ComputeBindPoseMesh(SkinnedMeshRenderer smr, SkinnedMeshRenderer bodySmr, bool isClothingMesh, MeshInflateFlags meshInflateFlags, bool isMainBody = false)
internal async Task ComputeBindPoseMesh(SkinnedMeshRenderer smr, SkinnedMeshRenderer bodySmr, bool isClothingMesh,
MeshInflateFlags meshInflateFlags, bool isMainBody = false)
{
if (smr == null)
{
Expand Down Expand Up @@ -381,6 +386,14 @@ internal async Task ComputeBindPoseMesh(SkinnedMeshRenderer smr, SkinnedMeshRend
//Put threadpool work inside task and await the results
await Task.Run(() =>
{
//Check again since some time will have passed since task start
var _exists = md.TryGetValue(rendererName, out MeshData _meshData);
if (!_exists)
{
if (PregnancyPlusPlugin.DebugLog.Value) PregnancyPlusPlugin.Logger.LogWarning($" ComputeBindPoseMesh.Task.Run cant find MeshData for {rendererName}");
return;
}

//Spread work across multiple threads
md[rendererName].originalVertices = Threading.RunParallel(unskinnedVerts, (_, i) => {
//Get the skinned vert position from the bindpose matrix we computed earlier
Expand Down Expand Up @@ -480,6 +493,14 @@ internal async Task GetInflatedVerticies(SkinnedMeshRenderer smr, float sphereRa
//Put threadpool work inside task and await the results
await Task.Run(() =>
{
//Check again since some time will have passed since task start
var _exists = md.TryGetValue(rendererName, out MeshData _meshData);
if (!_exists)
{
if (PregnancyPlusPlugin.DebugLog.Value) PregnancyPlusPlugin.Logger.LogWarning($" GetInflatedVerticies.Task.Run cant find MeshData for {rendererName}");
return;
}

//Spread work across multiple threads
md[rendererName].inflatedVertices = Threading.RunParallel(origVerts, (_, i) =>
{
Expand Down
1 change: 1 addition & 0 deletions PregnancyPlus/PregnancyPlus.Core/Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Compile Include="..\PregnancyPlus.Core\tools\NormalsSolver.cs" />
<Compile Include="..\PregnancyPlus.Core\tools\BlendShape\BlendShapeController.cs" />
<Compile Include="..\PregnancyPlus.Core\tools\BlendShape\BlendShapeTools.cs" />
<Compile Include="..\PregnancyPlus.Core\tools\BlendShape\BlendShape.cs" />
<Compile Include="..\PregnancyPlus.Core\tools\ConfigurationManagerAttributes.cs" />
<Compile Include="..\PregnancyPlus.Core\tools\Smoothing\SmoothMeshUtils.cs" />
<Compile Include="..\PregnancyPlus.Core\tools\Smoothing\SmoothFilter.cs" />
Expand Down
2 changes: 1 addition & 1 deletion PregnancyPlus/PregnancyPlus.Core/tools/AnimationCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace KK_PregnancyPlus
{

//This partial class contains the animation curves used in mesh transformations, much better curvature than a simple linear mathf.lerp
//This contains the animation curves used in mesh transformations, much better curvature than linear mathf.lerp
public static class AnimCurve
{

Expand Down
45 changes: 45 additions & 0 deletions PregnancyPlus/PregnancyPlus.Core/tools/BlendShape/BlendShape.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using UnityEngine;
using MessagePack;

namespace KK_PregnancyPlus
{
//This format contains all the info a blendshape needs to be created (For a single blendshape frame). It also server as the format we will save to a character card later
[MessagePackObject(keyAsPropertyName: true)]
public class BlendShape //This is technically a blendshape frame, but w/e. Its already set in stone
{
public string name;
private float _frameWeight = 100;//The range that _weight has to stay within
public float frameWeight
{
set { _frameWeight = Mathf.Clamp(value, 0, 100); }
get { return _frameWeight <= 0 ? 100 : _frameWeight; }//Fix for old cards not having frameWeight prop, set them to 100
}
private float _weight = 100;//The current weight
public float weight
{
set { _weight = value; }
get { return _weight; }
}
public Vector3[] verticies;
public Vector3[] normals;
public Vector3[] tangents;

[IgnoreMember]
public bool isInitilized
{
get { return name != null; }
}

[IgnoreMember]
public int vertexCount
{
get { return verticies.Length; }
}

[IgnoreMember]
public string log
{
get { return $"name {name} weight {_weight} frameWeight {_frameWeight} vertexCount {vertexCount}"; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MessagePack;

namespace KK_PregnancyPlus
{
Expand All @@ -12,50 +11,10 @@ public class BlendShapeController
public BlendShape blendShape = new BlendShape();
public SkinnedMeshRenderer smr = null;


//This format contains all the info a blendshape needs to be created (For a single blendshape frame). It also server as the format we will save to a character card later
[MessagePackObject(keyAsPropertyName: true)]
public class BlendShape //This is technically a blendshape frame, but w/e. Its already set in stone
{
public string name;
private float _frameWeight = 100;//The range that _weight has to stay within
public float frameWeight
{
set { _frameWeight = Mathf.Clamp(value, 0, 100); }
get { return _frameWeight <= 0 ? 100 : _frameWeight; }//Fix for old cards not having frameWeight prop, set them to 100
}
private float _weight = 100;//The current weight
public float weight
{
set { _weight = value; }
get { return _weight; }
}
public Vector3[] verticies;
public Vector3[] normals;
public Vector3[] tangents;

[IgnoreMember]
public bool isInitilized
{
get { return name != null; }
}

[IgnoreMember]
public int vertexCount
{
get { return verticies.Length; }
}

[IgnoreMember]
public string log
{
get { return $"name {name} weight {_weight} frameWeight {_frameWeight} vertexCount {vertexCount}"; }
}
}

#region Constructors

/// <summary>
/// Constructor that takes in the pre computed blendshape deltas, and applies them to a target SMR
/// Constructor that takes the pre computed blendshape deltas, and applies them to a target SMR
/// </summary>
/// <param name="md">The meshData obhect that has our computed blendshape deltas</param>
/// <param name="blendShapeName">Desired name of the blend shape, should be unique</param>
Expand All @@ -79,7 +38,7 @@ public BlendShapeController(MeshData md, string blendShapeName, SkinnedMeshRende


/// <summary>
/// Constructor overload that takes a saved blendshape and sets it to the correct mesh
/// Constructor overload that takes an existing Preg+ blendshape and sets it to an SMR
/// Typically used when loading BlendShape from card
/// </summary>
/// <param name="smr">The current active mesh</param>
Expand All @@ -106,12 +65,12 @@ public BlendShapeController(SkinnedMeshRenderer _smr, string blendShapeName)


/// <summary>
/// Use this constructor just to access any methods inside
/// Use this constructor just to access any methods inside this class
/// </summary>
public BlendShapeController() { }



#endregion Constructors


/// <summary>
Expand Down Expand Up @@ -330,6 +289,7 @@ public int GetBlendShapeIndex(SkinnedMeshRenderer smr, string blendShapeName)

/// <summary>
/// Remove a single blendshape from a mesh
/// Unfortunately Unity makes this difficult, by not having an API to remove 1 at a time. So we reomve all and add back all -1
/// </summary>
/// <param name="smr">The mesh containing the blendshapes</param>
public bool RemoveBlendShape(SkinnedMeshRenderer smr)
Expand Down Expand Up @@ -366,7 +326,7 @@ public bool RemoveBlendShape(SkinnedMeshRenderer smr)


/// <summary>
/// Copy all existing blendshape frames on a mesh
/// Copy all blendshape frames from an SMR
/// </summary>
/// <param name="smr">The mesh containing the blendshapes</param>
internal Dictionary<int, BlendShape[]> CopyAllBlendShapeFrames(SkinnedMeshRenderer smr)
Expand Down

0 comments on commit ee2d509

Please sign in to comment.