Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active January 23, 2019 06:53
Show Gist options
  • Select an option

  • Save unitycoder/da7fc9d4c4264ea633db to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/da7fc9d4c4264ea633db to your computer and use it in GitHub Desktop.

Revisions

  1. unitycoder revised this gist Apr 12, 2015. 1 changed file with 11 additions and 12 deletions.
    23 changes: 11 additions & 12 deletions Melter.cs
    Original file line number Diff line number Diff line change
    @@ -2,42 +2,41 @@
    using System.Collections;

    public class Melter : MonoBehaviour {

    public Transform heatPoint;
    public bool restoreColor=false;

    void Start () {
    var mesh = GetComponent<MeshFilter>().mesh;
    var verts = mesh.vertices;
    var cols = mesh.colors;
    var cols = new Color[mesh.vertexCount];
    for (int i = 0; i < mesh.vertexCount; i++)
    {
    cols[i] = Color.white;
    }
    mesh.colors = cols;
    UpdateColor();
    }

    void Update () {
    UpdateColor();
    }


    void UpdateColor()
    {
    float viewDistance = 1f;
    float fadeSpeed = 1f;

    var mesh = GetComponent<MeshFilter>().mesh;
    var verts = mesh.vertices;
    var cols = mesh.colors;

    for (int i = 0; i < mesh.vertexCount; i++)
    {
    //float distance = Vector3.Distance(heatPoint.position.normalized, verts[i].normalized);
    float dist = Mathf.Exp(Vector3.Distance(heatPoint.position, verts[i])); // broken
    float distance = Remap (dist,0f,3f,0f,1f);

    if (restoreColor)
    {
    cols[i].a = Mathf.Clamp01(cols[i].a-(viewDistance-distance)*Time.deltaTime*fadeSpeed);
    @@ -47,9 +46,9 @@ void UpdateColor()
    }
    mesh.colors = cols;
    }

    float Remap(float val, float low1, float high1, float low2, float high2)
    {
    return low2 + (val-low1)*(high2-low2)/(high1-low1);
    }
    }
    }
  2. unitycoder created this gist Mar 24, 2015.
    55 changes: 55 additions & 0 deletions Melter.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    using UnityEngine;
    using System.Collections;

    public class Melter : MonoBehaviour {

    public Transform heatPoint;
    public bool restoreColor=false;

    void Start () {
    var mesh = GetComponent<MeshFilter>().mesh;
    var verts = mesh.vertices;
    var cols = mesh.colors;
    for (int i = 0; i < mesh.vertexCount; i++)
    {
    cols[i] = Color.white;
    }
    mesh.colors = cols;
    UpdateColor();
    }

    void Update () {
    UpdateColor();
    }


    void UpdateColor()
    {
    float viewDistance = 1f;
    float fadeSpeed = 1f;

    var mesh = GetComponent<MeshFilter>().mesh;
    var verts = mesh.vertices;
    var cols = mesh.colors;

    for (int i = 0; i < mesh.vertexCount; i++)
    {
    //float distance = Vector3.Distance(heatPoint.position.normalized, verts[i].normalized);
    float dist = Mathf.Exp(Vector3.Distance(heatPoint.position, verts[i])); // broken
    float distance = Remap (dist,0f,3f,0f,1f);

    if (restoreColor)
    {
    cols[i].a = Mathf.Clamp01(cols[i].a-(viewDistance-distance)*Time.deltaTime*fadeSpeed);
    }else{
    cols[i].a = Mathf.Min(cols[i].a,Mathf.Clamp01(cols[i].a-(viewDistance-distance)*Time.deltaTime*fadeSpeed));
    }
    }
    mesh.colors = cols;
    }

    float Remap(float val, float low1, float high1, float low2, float high2)
    {
    return low2 + (val-low1)*(high2-low2)/(high1-low1);
    }
    }