Make frame properties animatable.

This commit is contained in:
2022-12-21 20:22:41 +08:00
parent 959157255f
commit c5571e7d17
3 changed files with 48 additions and 32 deletions

View File

@@ -52,9 +52,9 @@ namespace Cryville.Crtr.Components {
static readonly ArrayPool<Vector2> _shapePool = new ArrayPool<Vector2>(0x100, 0x10000);
public PolygonSGO() {
SubmitProperty("head", new PropOp.String(v => head.FrameName = v));
SubmitProperty("body", new PropOp.String(v => body.FrameName = v));
SubmitProperty("tail", new PropOp.String(v => tail.FrameName = v));
SubmitProperty("head", new PropOp.String(v => head.FrameName = v), 2);
SubmitProperty("body", new PropOp.String(v => body.FrameName = v), 2);
SubmitProperty("tail", new PropOp.String(v => tail.FrameName = v), 2);
SubmitProperty("shape", new op_set_shape(this), 2);
}
@@ -98,18 +98,13 @@ namespace Cryville.Crtr.Components {
public override void Init() {
base.Init();
head.Load();
body.Load();
tail.Load();
mesh.Init(transform);
List<Material> materials = new List<Material>();
if (head.FrameName != null) AddMat(materials, head.FrameName);
if (body.FrameName != null) AddMat(materials, body.FrameName);
if (tail.FrameName != null) AddMat(materials, tail.FrameName);
mesh.Renderer.materials = materials.ToArray();
var mats = mesh.Renderer.materials = new Material[] { mesh.NewMaterial, mesh.NewMaterial, mesh.NewMaterial };
head.Bind(mats[0]);
body.Bind(mats[1]);
tail.Bind(mats[2]);
UpdateZIndex();
}
@@ -157,9 +152,9 @@ namespace Cryville.Crtr.Components {
int vcpsec = _shapeLength; // Vertex Count Per Section
float width = GetWidth();
float headLength = 0;
if (head.FrameName != null) headLength = width / head.Ratio;
if (head.Frame != null) headLength = width / head.Ratio;
float tailLength = 0;
if (tail.FrameName != null) tailLength = width / tail.Ratio;
if (tail.Frame != null) tailLength = width / tail.Ratio;
float endLength = headLength + tailLength;
if (sumLength <= endLength) {
// The total length of the two ends is longer than the whole mesh, squeeze the two ends
@@ -187,17 +182,17 @@ namespace Cryville.Crtr.Components {
verts = _vertPool.Rent(vc * vcpsec);
uvs = _uvPool.Rent(vc * vcpsec);
int i = 0; int t = 0; float l = 0; int m = 0;
if (head.FrameName != null) { m++; GenerateMeshTo(verts, uvs, out trih, head, ref i, ref t, ref l, 0, headLength, vcpsec, hvc); }
if (body.FrameName != null) { m++; GenerateMeshTo(verts, uvs, out trib, body, ref i, ref t, ref l, headLength, sumLength - tailLength, vcpsec, hvc + bvc); }
if (tail.FrameName != null) { m++; GenerateMeshTo(verts, uvs, out trit, tail, ref i, ref t, ref l, sumLength - tailLength, sumLength, vcpsec, vc); }
if (head.Frame != null) { m++; GenerateMeshTo(verts, uvs, out trih, head, ref i, ref t, ref l, 0, headLength, vcpsec, hvc); }
if (body.Frame != null) { m++; GenerateMeshTo(verts, uvs, out trib, body, ref i, ref t, ref l, headLength, sumLength - tailLength, vcpsec, hvc + bvc); }
if (tail.Frame != null) { m++; GenerateMeshTo(verts, uvs, out trit, tail, ref i, ref t, ref l, sumLength - tailLength, sumLength, vcpsec, vc); }
mesh.Mesh.subMeshCount = m;
m = 0;
mesh.Mesh.SetVertices(verts);
mesh.Mesh.SetUVs(0, uvs);
if (head.FrameName != null) mesh.Mesh.SetTriangles(trih, m++);
if (body.FrameName != null) mesh.Mesh.SetTriangles(trib, m++);
if (tail.FrameName != null) mesh.Mesh.SetTriangles(trit, m++);
if (head.Frame != null) mesh.Mesh.SetTriangles(trih, m++);
if (body.Frame != null) mesh.Mesh.SetTriangles(trib, m++);
if (tail.Frame != null) mesh.Mesh.SetTriangles(trit, m++);
mesh.Mesh.RecalculateNormals();
_vertPool.Return(verts); verts = null;

View File

@@ -4,7 +4,16 @@ using Logger = Cryville.Common.Logger;
namespace Cryville.Crtr.Components {
public class SpriteInfo {
public string FrameName;
string m_frameName;
public string FrameName {
get {
return m_frameName;
}
set {
m_frameName = value;
Reload();
}
}
public SpriteFrame Frame {
get;
private set;
@@ -20,21 +29,38 @@ namespace Cryville.Crtr.Components {
return Rect.width / Rect.height;
}
}
bool _loaded;
Material _mat;
public void Bind(Material mat) {
_loaded = true;
_mat = mat;
Reload();
}
public void Load() {
if (FrameName != null) {
_loaded = true;
Reload();
}
public void Reload() {
if (!_loaded) return;
if (!string.IsNullOrEmpty(FrameName)) {
if (ChartPlayer.frames.ContainsKey(FrameName)) {
Frame = ChartPlayer.frames[FrameName];
}
else {
Logger.Log("main", 4, "Skin", "Texture {0} not found", FrameName);
Frame = null;
}
}
else Frame = null;
if (_mat != null) {
_mat.mainTexture = Frame == null ? null : Frame.Texture;
}
}
}
public class SpritePlane : SpriteBase {
public SpritePlane() {
SubmitProperty("frame", new PropOp.String(v => Frame = v));
SubmitProperty("frame", new PropOp.String(v => Frame = v), 2);
SubmitProperty("fit", new PropOp.Enum<FitMode>(v => Fit = v));
SubmitProperty("opacity", new PropOp.Float(v => Opacity = v));
}
@@ -68,16 +94,11 @@ namespace Cryville.Crtr.Components {
}
protected void OnFrameUpdate() {
if (!mesh.Initialized) return;
if (frameInfo.FrameName == null) {
if (frameInfo.Frame == null) {
mesh.Renderer.enabled = false;
return;
}
mesh.Renderer.enabled = true;
frameInfo.Load();
if (frameInfo.Frame != null)
mesh.Renderer.material.mainTexture = frameInfo.Frame.Texture;
else
Logger.Log("main", 4, "Skin", "Unable to load texture {0}", frameInfo.FrameName);
UpdateUV();
UpdateScale();
UpdateZIndex();
@@ -140,8 +161,8 @@ namespace Cryville.Crtr.Components {
}
public override void Init() {
frameInfo.Load();
InternalInit();
frameInfo.Bind(mesh.Renderer.material);
OnFrameUpdate();
UpdateOpacity();
}

View File

@@ -41,7 +41,7 @@ namespace Cryville.Crtr {
MeshObject.AddComponent<MeshRenderer>();
MeshFilter = MeshObject.GetComponent<MeshFilter>();
Renderer = MeshObject.GetComponent<Renderer>();
Renderer.material = GenericResources.Materials["-SpriteMat"];
Renderer.material = NewMaterial;
Initialized = true;
}
public void Destroy() {