
[ad_1]
Game Development Stack Exchange is a query and reply web site for skilled and impartial sport builders. It solely takes a minute to enroll.
Anybody can ask a query
Anybody can reply
The finest solutions are voted up and rise to the highest
Asked
Viewed
555 occasions
I’m attempting to discover ways to use AnimationCurve to vary a float worth over time.
However i’m not actually certain learn how to do it
Say i’ve the next class:
public class SpiritAway : MonoBehaviour
{
public List<SkinnedMeshRenderer> MeshList;
public List<Material> DissolveList;
public float proportion = 0.0f;
public bool IsDissolveing;
public AnimationCurve Curve;
personal void Start()
{
proportion = 0.0f;
for (int i = 0; i < MeshList.Count; i++)
{
for (int j = 0; j < MeshList[i].supplies.Length; j++)
{
if (MeshList[i].supplies[j].shader.title == "Dissolve")
{
DissolveList.Add(MeshList[i].supplies[j]);
}
}
}
}
// Update is named as soon as per body
void Update()
{
if (IsDissolveing)
{
if (proportion < 1)
{
proportion += 0.01f;
for (int i = 0; i < DissolveList.Count; i++)
{
DissolveList[i].SetFloat("_alphaClipDissolve", proportion);
}
}
}
}
Now i want to incrase the share worth with the share of the animation curve to get a extra clean and managed transition of the worth.
Could anybody clarify how that is potential?
$endgroup$
1
It appears to be like to me like you possibly can exchange this with:
public float velocity = 1f/30f;
public Renderer[] renderers;
public AnimationCurve curve;
MaterialPropertyBlock _block;
struct MaterialSlot {
public readonly Renderer renderer;
public readonly int index;
public MaterialSlot(Renderer renderer, int index) {
this.renderer = renderer;
this.index = index;
}
}
personal IEnumerator Start()
{
// Define a cloth property block for modifying alpha
// with out copying supplies (lighter on allocations/rubbish).
var properties = new MaterialPropertyBlock();
// Cache an ID for our parameter, to keep away from a number of string lookups.
int id = Shader.PropertyToIndex("_alphaClipDissolve");
float perecentage = 0f;
// Read our alpha worth from this place on the curve
// and apply it to our property block.
properties.SetFloat(id, curve.Evaluate(proportion));
// Find all supplies that want this alpha adjusted, and cache them.
var slots = new List<MaterialSlot>();
foreach(var renderer in renderers) {
// Look on the renderer's supplies with out copying them.
var supplies = renderer.sharedMaterials;
for(int i = 0; i < supplies.Length; i++) {
if(supplies[i].shader.title == "Dissolve") {
// Initialize the alpha worth for this materials.
renderer.SetPropertyBlock(properties, i);
// Cache a reference to its slot so we are able to iterate it later.
var slot = new MaterialSlot(renderer, i);
slots.Add(slot);
}
}
}
// Keep updating the alpha on subsequent frames till we're finished.
whereas(proportion < 1f) {
// Wait a body, then resume.
yield return null;
// Advance the animation at a set price over time.
proportion += Time.deltaTime * velocity;
// Read our alpha worth from this place on the curve
// and apply it to our property block.
properties.SetFloat(id, curve.Evaluate(proportion));
// Apply this modified property block to each "Dissolve" materials slot.
foreach(var slot in slots) {
slot.renderer.SetPropertyBlock(properties, slot.index);
}
}
// p.c >= 1f, we're finished! End this coroutine.
}
$endgroup$
You should log in to reply this query.
Not the reply you are on the lookout for? Browse different questions tagged .
lang-cs
[ad_2]