24
2015
Mesh Melt shader test
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} |
Script source: Melter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
—
Related Posts
8 Comments + Add Comment
Leave a comment
Recent posts
- MotionVector Effect: Object “disappears” when paused
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
Recent Comments
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
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