Mar
24
2015

Mesh Melt shader test

mesh_melt_shader

Quick test for melting mesh effect using vertex extrusion shader

Gif anim preview:
http://pasteboard.co/1QARh81p.gif

Forum topic: http://forum.unity3d.com/threads/has-anyone-ever-written-a-mesh-melter.311135/

Info:
– C# Script adjusts mesh vertex colors, based on the distance to heat point gameobject
– Then shader pushes(extrudes) vertices based on that vertex color (alpha)
– Would be probably better to do everything from script (to allow splitting the object, avoid intersecting faces etc..)

Instructions:
– Download sources below
– Add sphere to scene
– Add Melter.cs script to that sphere
– Create new material “meltmat” and assign it to that sphere
– Assign Custom/MeshMelt2 shader to that material
– Add empty gameobject to scene (this is the heat source)
– Assign heatsource gameobject into Melter.cs “HeatPoint” field in inspector
– Hit play, then inside scene view move heat source near to sphere to see effect

Customizations:
– Melter.cs has public bool, [x] Restore Color, this slowly restores vertex colors
– MeshMelt2.shader, see line 23 & 24 for different effects (melt in normal direction or melt downwards)
– Adjust Extrusion amount in shader material

Shader source: MeshMelt2.shader


Shader "Custom/MeshMelt2" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_LavaTex ("LavaTexture", 2D) = "white" {}
_Amount ("Extrusion Amount", Range(-1,1)) = 0.5
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainTex;
float4 customColor;
};
float _Amount;
void vert (inout appdata_full v,out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o);
v.vertex.xyz += v.normal * v.color.a * _Amount; // move in normal direction
// v.vertex.xyz += float3(0,1,0) * v.color.a * _Amount; // move down
o.customColor = v.color;
}
sampler2D _MainTex;
sampler2D _LavaTex;
void surf (Input IN, inout SurfaceOutput o)
{
float3 main = tex2D(_MainTex, IN.uv_MainTex).rgb;
float2 scroll = float2(_Time.x*0.032f*(1-IN.customColor.a),_Time.y*0.02f*(1-IN.customColor.a));
float3 lava = tex2D(_LavaTex, IN.uv_MainTex+scroll).rgb;
float3 c = lerp(lava,main, IN.customColor.a);
o.Albedo = c;
o.Emission = c*(1-IN.customColor.a);
}
ENDCG
}
Fallback "Diffuse"
}

view raw

MeshMelt.shader

hosted with ❤ by GitHub

Script source: Melter.cs


using UnityEngine;
using System.Collections;
public class Melter : MonoBehaviour {
public Transform heatPoint;
public bool restoreColor=false;
void Start () {
var mesh = GetComponent<MeshFilter>().mesh;
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);
}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);
}
}

view raw

Melter.cs

hosted with ❤ by GitHub

 


8 Comments + Add Comment

  • wa~sounds cool!

  • […] across a deformation shader which was easy and fun to use on a bunch of objects lying around (original inspiration). We discussed a bit about what can be done with it and what features were missing before we could […]

  • I am getting Array errors at 46 if Restore Color is enabled, and 50 if it is not.
    I have checked these lines and cannot find anything wrong with either.

    I am using Unity 5.

    • Yeah getting the same thing, any idea how to fix this?

    • Does it work if you try with default unity sphere? (works here atleast)

      • No, using a default sphere here is an image of the errors I’m getting:

        http://i.imgur.com/j8iENP8.png

        • Script updated, for some reason it didnt get the vertex colors from mesh, even though it worked before.
          (maybe I had modified the builtin sphere vertex colors during that unity session, so it had them, while in default there are no colors).

  • GAME DEVELOPER

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.