-
-
Save NeatWolf/eb1767bc3e57b34a064802141d917416 to your computer and use it in GitHub Desktop.
Creates a monochromatic texture with color values from a given animation curve
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/CurveDissolve" | |
{ | |
Properties | |
{ | |
_Color ("Color", Color) = (1,1,1,1) | |
_MainTex ("Albedo (RGB)", 2D) = "white" {} | |
_Noise("Noise", 2D) = "white" {} | |
_CurveTexture("Curve texture", 2D) = "white" {} | |
_Cutoff("Cutoff", Range(0,1)) = 0 | |
_Glossiness ("Smoothness", Range(0,1)) = 0.5 | |
_Metallic ("Metallic", Range(0,1)) = 0.0 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
LOD 200 | |
CGPROGRAM | |
// Physically based Standard lighting model, and enable shadows on all light types | |
#pragma surface surf Standard fullforwardshadows | |
// Use shader model 3.0 target, to get nicer looking lighting | |
#pragma target 3.0 | |
sampler2D _MainTex; | |
struct Input | |
{ | |
float2 uv_MainTex; | |
}; | |
half _Glossiness; | |
half _Metallic; | |
fixed4 _Color; | |
float _Cutoff; | |
sampler2D _CurveTexture; | |
sampler2D _Noise; | |
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. | |
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. | |
// #pragma instancing_options assumeuniformscaling | |
UNITY_INSTANCING_BUFFER_START(Props) | |
// put more per-instance properties here | |
UNITY_INSTANCING_BUFFER_END(Props) | |
void surf (Input IN, inout SurfaceOutputStandard o) | |
{ | |
// Albedo comes from a texture tinted by color | |
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; | |
//Instead of just using _Cutoff for the test, map it to the _CurveTexture | |
clip(tex2D(_Noise, IN.uv_MainTex).x - tex2D(_CurveTexture, float2(_Cutoff, 0.0)).x); | |
o.Albedo = c.rgb; | |
// Metallic and smoothness come from slider variables | |
o.Metallic = _Metallic; | |
o.Smoothness = _Glossiness; | |
o.Alpha = c.a; | |
} | |
ENDCG | |
} | |
FallBack "Diffuse" | |
} |
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 System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
public class CurveToTexture : EditorWindow { | |
private Texture2D generatedTexture; | |
private Vector2Int textureDimensions; | |
private AnimationCurve animationCurve = new AnimationCurve(); | |
[MenuItem("Tools/Curve to Texture")] | |
private static void ShowWindow() { | |
var window = GetWindow<CurveToTexture>(); | |
window.titleContent = new GUIContent("CurveToTexture"); | |
window.Show(); | |
} | |
private void OnGUI() { | |
//Display fields | |
textureDimensions = EditorGUILayout.Vector2IntField("Dimensions", textureDimensions); | |
animationCurve = EditorGUILayout.CurveField("Animation curve", animationCurve); | |
if (GUILayout.Button("Generate texture")) { | |
GenerateTexture(); | |
} | |
if (GUILayout.Button("Save texture")) { | |
SaveGeneratedTexture(); | |
} | |
//Texture preview | |
if (generatedTexture != null) { | |
EditorGUILayout.LabelField("Generated texture preview"); | |
EditorGUI.DrawPreviewTexture(new Rect(10, 120, 200, 20), generatedTexture); | |
} | |
} | |
private void GenerateTexture() { | |
if (textureDimensions.x <= 0 || textureDimensions.y <= 0) { | |
Debug.LogWarning("Texture dimensions have not be set correctly!"); | |
return; | |
} | |
generatedTexture = new Texture2D(textureDimensions.x, textureDimensions.y); | |
generatedTexture.wrapMode = TextureWrapMode.Clamp; | |
for (int i = 0; i < textureDimensions.x; i++) { | |
for (int j = 0; j < textureDimensions.y; j++) { | |
float colValue = animationCurve.Evaluate((float) i / (float) textureDimensions.x); | |
generatedTexture.SetPixel(i, j, new Color(colValue, 0.0f, 0.0f, 0.0f)); | |
} | |
} | |
generatedTexture.Apply(); | |
} | |
private void SaveGeneratedTexture() { | |
GenerateTexture(); | |
string savePath = EditorUtility.SaveFilePanelInProject("Save texture", "curve_tex.png", "png", "Save texture"); | |
if (savePath.Length != 0) { | |
byte[] bytes = generatedTexture.EncodeToPNG(); | |
File.WriteAllBytes(savePath, bytes); | |
} | |
AssetDatabase.Refresh(); | |
//Ping texture | |
EditorUtility.FocusProjectWindow(); | |
EditorGUIUtility.PingObject(AssetDatabase.LoadMainAssetAtPath(savePath)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment