Skip to content

Commit

Permalink
Merge branch 'v0.2.1p1'
Browse files Browse the repository at this point in the history
  • Loading branch information
chengkehan committed Apr 27, 2017
2 parents 2408caa + 2f0940d commit a10be76
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 73 deletions.
7 changes: 7 additions & 0 deletions Assets/GPUSkinning/Editor/GPUSkinningPlayerMonoEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public override void OnInspectorGUI()
}
}

EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(serializedObject.FindProperty("individualDifferenceEnabled"), new GUIContent("Individual Difference"));
if(EditorGUI.EndChangeCheck())
{
player.Player.IndividualDifferenceEnabled = serializedObject.FindProperty("individualDifferenceEnabled").boolValue;
}

GPUSkinningAnimation anim = serializedObject.FindProperty("anim").objectReferenceValue as GPUSkinningAnimation;
SerializedProperty defaultPlayingClipIndex = serializedObject.FindProperty("defaultPlayingClipIndex");
if (clipsName == null && anim != null)
Expand Down
28 changes: 28 additions & 0 deletions Assets/GPUSkinning/Scripts/GPUSkinningExecuteOncePerFrame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GPUSkinningExecuteOncePerFrame
{
private int frameCount = -1;

public bool CanBeExecute()
{
if (Application.isPlaying)
{
return frameCount != Time.frameCount;
}
else
{
return true;
}
}

public void MarkAsExecuted()
{
if (Application.isPlaying)
{
frameCount = Time.frameCount;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 38 additions & 9 deletions Assets/GPUSkinning/Scripts/GPUSkinningPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public MeshRenderer MeshRenderer

private float time = 0;

private float timeDiff = 0;

private GPUSkinningClip playingClip = null;

private GPUSkinningPlayerResources res = null;
Expand Down Expand Up @@ -52,6 +54,19 @@ public bool IsPlaying
}
}

private bool individualDifferenceEnabled = true;
public bool IndividualDifferenceEnabled
{
get
{
return individualDifferenceEnabled;
}
set
{
individualDifferenceEnabled = value;
}
}

private List<GPUSkinningPlayerJoint> joints = null;
public List<GPUSkinningPlayerJoint> Joints
{
Expand Down Expand Up @@ -109,7 +124,7 @@ public GPUSkinningPlayer(GameObject attachToThisGo, GPUSkinningPlayerResources r
mf = go.AddComponent<MeshFilter>();
}

mr.sharedMaterial = res.mtrl.Material;
mr.sharedMaterial = res.mtrl;
mf.sharedMesh = res.mesh;

mpb = new MaterialPropertyBlock();
Expand Down Expand Up @@ -138,7 +153,8 @@ public void Play(string clipName)
}
else if(playingClip.wrapMode == GPUSkinningWrapMode.Loop)
{
time = Random.Range(0, playingClip.length);
time = 0;
timeDiff = Random.Range(0, playingClip.length);
}
else
{
Expand Down Expand Up @@ -182,26 +198,25 @@ private void Update_Internal(float timeDelta)
return;
}

if(res == null || res.mtrl == null || res.mtrl.Material == null)
if(res == null || res.mtrl == null || res.mtrl == null)
{
return;
}

if (playingClip.wrapMode == GPUSkinningWrapMode.Loop)
{
UpdateMaterial();
time += timeDelta;
UpdateMaterial(timeDelta);
}
else if(playingClip.wrapMode == GPUSkinningWrapMode.Once)
{
if (time >= playingClip.length)
{
time = playingClip.length;
UpdateMaterial();
UpdateMaterial(timeDelta);
}
else
{
UpdateMaterial();
UpdateMaterial(timeDelta);
time += timeDelta;
if(time > playingClip.length)
{
Expand All @@ -215,11 +230,11 @@ private void Update_Internal(float timeDelta)
}
}

private void UpdateMaterial()
private void UpdateMaterial(float deltaTime)
{
int frameIndex = GetFrameIndex();
GPUSkinningFrame frame = playingClip.frames[frameIndex];
res.UpdateMaterial();
res.Update(deltaTime);
res.UpdatePlayingData(mpb, playingClip, frameIndex, frame, playingClip.rootMotionEnabled && rootMotionEnabled);
mr.SetPropertyBlock(mpb);
UpdateJoints(frame);
Expand All @@ -240,6 +255,20 @@ private void UpdateMaterial()

private int GetFrameIndex()
{
float time = 0;
if(WrapMode == GPUSkinningWrapMode.Once)
{
time = this.time;
}
else if(WrapMode == GPUSkinningWrapMode.Loop)
{
time = res.Time + (individualDifferenceEnabled ? this.timeDiff : 0);
}
else
{
throw new System.NotImplementedException();
}

if (Mathf.Approximately(playingClip.length, time))
{
return (int)(playingClip.length * playingClip.fps) - 1;
Expand Down
51 changes: 0 additions & 51 deletions Assets/GPUSkinning/Scripts/GPUSkinningPlayerMaterial.cs

This file was deleted.

9 changes: 7 additions & 2 deletions Assets/GPUSkinning/Scripts/GPUSkinningPlayerMono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class GPUSkinningPlayerMono : MonoBehaviour
[SerializeField]
private bool rootMotionEnabled = false;

[HideInInspector]
[SerializeField]
private bool individualDifferenceEnabled = true;

private static GPUSkinningPlayerMonoManager playerManager = new GPUSkinningPlayerMonoManager();

private GPUSkinningPlayer player = null;
Expand Down Expand Up @@ -74,14 +78,15 @@ public void Init()
res = new GPUSkinningPlayerResources();
res.anim = anim;
res.mesh = mesh;
res.mtrl = new GPUSkinningPlayerMaterial(new Material(mtrl));
res.mtrl = new Material(mtrl);
res.texture = GPUSkinningUtil.CreateTexture2D(textureRawData, anim);
res.mtrl.Material.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;
res.mtrl.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;
res.texture.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;
}

player = new GPUSkinningPlayer(gameObject, res);
player.RootMotionEnabled = Application.isPlaying ? rootMotionEnabled : false;
player.IndividualDifferenceEnabled = individualDifferenceEnabled;

if (anim != null && anim.clips != null && anim.clips.Length > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/GPUSkinning/Scripts/GPUSkinningPlayerMonoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Register(GPUSkinningAnimation anim, Mesh mesh, Material originalMtrl

if(item.mtrl == null)
{
item.mtrl = new GPUSkinningPlayerMaterial(new Material(originalMtrl));
item.mtrl = new Material(originalMtrl);
}

if(item.texture == null)
Expand Down
28 changes: 20 additions & 8 deletions Assets/GPUSkinning/Scripts/GPUSkinningPlayerResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@ public class GPUSkinningPlayerResources

public Mesh mesh = null;

public GPUSkinningPlayerMaterial mtrl = null;
public Material mtrl = null;

public Texture2D texture = null;

public List<GPUSkinningPlayerMono> players = new List<GPUSkinningPlayerMono>();

private GPUSkinningExecuteOncePerFrame executeOncePerFrame = new GPUSkinningExecuteOncePerFrame();

private float time = 0;
public float Time
{
get
{
return time;
}
}

private static int shaderPropID_GPUSkinning_TextureMatrix = -1;

private static int shaderPropID_GPUSkinning_NumPixelsPerFrame = 0;
Expand Down Expand Up @@ -49,7 +60,7 @@ public void Destroy()

if (mtrl != null)
{
mtrl.Destroy();
Object.Destroy(mtrl);
mtrl = null;
}

Expand All @@ -66,14 +77,15 @@ public void Destroy()
}
}

public void UpdateMaterial()
public void Update(float deltaTime)
{
if(mtrl.MaterialCanBeSetData())
if(executeOncePerFrame.CanBeExecute())
{
mtrl.MarkMaterialAsSet();
mtrl.Material.SetTexture(shaderPropID_GPUSkinning_TextureMatrix, texture);
mtrl.Material.SetFloat(shaderPropID_GPUSkinning_NumPixelsPerFrame, anim.bones.Length * 3/*treat 3 pixels as a float3x4*/);
mtrl.Material.SetVector(shaderPropID_GPUSkinning_TextureSize, new Vector4(anim.textureWidth, anim.textureHeight, 0, 0));
executeOncePerFrame.MarkAsExecuted();
mtrl.SetTexture(shaderPropID_GPUSkinning_TextureMatrix, texture);
mtrl.SetFloat(shaderPropID_GPUSkinning_NumPixelsPerFrame, anim.bones.Length * 3/*treat 3 pixels as a float3x4*/);
mtrl.SetVector(shaderPropID_GPUSkinning_TextureSize, new Vector4(anim.textureWidth, anim.textureHeight, 0, 0));
time += deltaTime;
}
}

Expand Down

0 comments on commit a10be76

Please sign in to comment.