Category:Bits of code: Difference between revisions
Jump to navigation
Jump to search
(frist post on page) |
(Perlin noise Cell Shader) |
||
Line 13: | Line 13: | ||
== Editor code == | == Editor code == | ||
= Shaders = | = Shaders = | ||
== Cell Shading with a perlin noise == | |||
Shader with unlit normal colors (not properly working yet) and cell shaded contour with a perlin noise function to break up the line art. | |||
<source> | |||
Shader "Cutie/Z_Valid_Shaders/VertexColorUnlitStylishNormalPerspectiveCellShaded" { | |||
Properties { | |||
_MainTex ("Base (RGB)", 2D) = "black" {} | |||
_Color ("Front Colour", Color) = (1.0, 1.0, 1.0, 1.0) | |||
_LeftColor ("Left Colour", Color) = (1.0, 1.0, 1.0, 1.0) | |||
_RightColor ("Right Colour", Color) = (1.0, 1.0, 1.0, 1.0) | |||
_ShadowColor ("Shadow Colour", Color) = (1.0, 1.0, 1.0, 1.0) | |||
_ShadowIntensity ("Shadow Intensity", Range(-1.0,1.0)) = 0.2 | |||
_TextureOpacity ("Texture Participation", Range(0.0,1.0)) = 1.0 | |||
_ForegroundDepth ("Foreground Fog Depth", Float) = 5.0 | |||
_ForegroundColor("Foreground Colour", Color) = (0.0,0.0,0.0,1.0) | |||
_BackgroundDepth ("Backgroud Fog Depth", Float) = 3.0 | |||
_BackgroundColor("Background Colour", Color) = (0.4, 0.5, 1.0, 1.0) | |||
[MaterialToggle] _Additive("Additive Texture", Float) = 1 | |||
_InkWidth("Ink Width", Range(0,1)) = 0.1 | |||
_InkColor("Ink Colour", Color) = (0.0,0.0,0.0,0.0) | |||
_NoiseAmount("Noise Amount", Range(0,1)) = 0.7 | |||
samplerPerlinPerm2D("Perlin Perm", 2D) = "white" {} | |||
samplerPerlinGrad2D("Perlin Grad", 2D) = "white" {} | |||
} | |||
SubShader { | |||
Tags { "Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Opaque" } | |||
pass { | |||
Cull Back | |||
ZWrite On | |||
CGPROGRAM | |||
#pragma vertex vert | |||
#pragma fragment frag | |||
uniform sampler2D _MainTex; | |||
uniform float4 _MainTex_ST; | |||
uniform fixed4 _Color; //Front Color | |||
uniform fixed _ShadowIntensity; | |||
uniform fixed4 _LeftColor; | |||
uniform fixed4 _RightColor; | |||
uniform fixed4 _ShadowColor; | |||
uniform fixed _TextureOpacity; | |||
uniform half _ForegroundDepth; | |||
uniform half _BackgroundDepth; | |||
uniform fixed4 _ForegroundColor; | |||
uniform fixed4 _BackgroundColor; | |||
uniform bool _Additive; | |||
uniform fixed4 _InkColor; | |||
uniform half _InkWidth; | |||
uniform sampler2D samplerPerlinPerm2D; | |||
uniform sampler2D samplerPerlinGrad2D; | |||
uniform half _NoiseAmount; | |||
struct vertexInput{ | |||
half4 vertex: POSITION; | |||
half4 vertColor: Color; | |||
half4 textureCoord: TEXCOORD0; | |||
half3 normal : NORMAL; | |||
}; | |||
struct vertexOutput{ | |||
half4 pos: SV_POSITION; | |||
half4 col: Color; | |||
half4 tex: TEXCOORD0; | |||
half4 posWorld: TEXCOORD1; | |||
fixed3 norm : NORMAL; | |||
fixed3 viewDir : TEXCOORD2; | |||
half dist: HALF; | |||
}; | |||
//Perlin noise from : http://www.decarpentier.nl/scape-procedural-basics | |||
float perlinNoise(float2 p, float seed) | |||
{ | |||
// Calculate 2D integer coordinates i and fraction p. | |||
float2 i = floor(p); | |||
float2 f = p - i; | |||
// Get weights from the coordinate fraction | |||
float2 w = f * f * f * (f * (f * 6 - 15) + 10); | |||
float4 w4 = float4(1, w.x, w.y, w.x * w.y); | |||
// Get the four randomly permutated indices from the noise lattice nearest to | |||
// p and offset these numbers with the seed number. | |||
float4 perm = tex2D(samplerPerlinPerm2D, i / 256) + seed; | |||
// Permutate the four offseted indices again and get the 2D gradient for each | |||
// of the four permutated coordinates-seed pairs. | |||
float4 g1 = tex2D(samplerPerlinGrad2D, perm.xy) * 2 - 1; | |||
float4 g2 = tex2D(samplerPerlinGrad2D, perm.zw) * 2 - 1; | |||
// Evaluate the four lattice gradients at p | |||
float a = dot(g1.xy, f); | |||
float b = dot(g2.xy, f + float2(-1, 0)); | |||
float c = dot(g1.zw, f + float2( 0, -1)); | |||
float d = dot(g2.zw, f + float2(-1, -1)); | |||
// Bi-linearly blend between the gradients, using w4 as blend factors. | |||
float4 grads = float4(a, b - a, c - a, a - b - c + d); | |||
float n = dot(grads, w4); | |||
// Return the noise value, roughly normalized in the range [-1, 1] | |||
return n * 1.5 ; | |||
} | |||
vertexOutput vert(vertexInput v){ | |||
vertexOutput o; | |||
o.pos = mul( UNITY_MATRIX_MVP, v.vertex); | |||
o.tex = v.textureCoord; | |||
o.posWorld = mul(_Object2World, v.vertex); | |||
o.col = v.vertColor; | |||
o.dist = saturate ((o.posWorld.z>0)?(o.posWorld.z / _BackgroundDepth):(-o.posWorld.z / _ForegroundDepth)); | |||
o.norm = normalize(mul( float4(v.normal, 0.0), _World2Object).xyz),1.0; | |||
o.viewDir = normalize(_WorldSpaceCameraPos.xyz - o.posWorld.xyz); | |||
return o; | |||
} | |||
fixed4 frag(vertexOutput i): COLOR{ | |||
float4 c = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw); | |||
half4 normalColor = (-i.viewDir.z * _Color) + (i.viewDir.y * _Color) | |||
+ (i.viewDir.x * _LeftColor) | |||
+ (-i.viewDir.x * _RightColor) | |||
- (i.viewDir.y * _ShadowIntensity * _ShadowColor) | |||
- (-i.viewDir.y * _ShadowIntensity * _ShadowColor); | |||
//* i.col; | |||
fixed4 finalColor = (1-i.dist) * (_Additive==1 ? saturate(float4(c.rgb,1.0) * _TextureOpacity * c.a + normalColor * (c.a * (1.0 - _TextureOpacity))) | |||
: saturate(float4(c.rgb,1.0) * _TextureOpacity * c.a * normalColor) + normalColor * (1.0 - c.a * _TextureOpacity)) | |||
+ i.dist * ((i.posWorld.z>0) ? (_BackgroundColor) : (_ForegroundColor)); | |||
//Ink | |||
fixed perlin = (0.2 + perlinNoise(i.posWorld.xz,1)) * _NoiseAmount; | |||
fixed inkPass = 1 - saturate((dot(i.viewDir, i.norm) - _InkWidth * (perlin + 1 - _NoiseAmount)) * pow(2,10) + _InkWidth); | |||
//fixed4 inkColor = inkPass * (1-i.dist) * _InkColor + i.dist * ((i.posWorld.z>0) ? (_BackgroundColor) : (_ForegroundColor)); - Distance Affected - | |||
//return fixed4(-i.norm.z,-i.norm.z,-i.norm.z,1.0); | |||
return (1-inkPass) * finalColor + inkPass * _InkColor; | |||
} | |||
ENDCG | |||
} | |||
} | |||
// FallBack "Diffuse" | |||
} | |||
</source> |
Latest revision as of 11:36, 2 January 2016
For now, I'm using C# and Unity, so code here is going to be C#, may be a bit of shader code or stuff.
It's meant to be flexible, and not project specific, and will be updated each time I get a newer version, so I don't know, you may want to find a rss somewhere and keep yourself up-to-date.
If you see bugs, if you have questions, if for reasons you need to vent and feel like insulting me, please email me at dev@laei.org
C#
Curves, surface and meshing
Theses are stuff to make Unity create geometry, either at runtime or in the editor
Systems
Editor code
Shaders
Cell Shading with a perlin noise
Shader with unlit normal colors (not properly working yet) and cell shaded contour with a perlin noise function to break up the line art.
Shader "Cutie/Z_Valid_Shaders/VertexColorUnlitStylishNormalPerspectiveCellShaded" {
Properties {
_MainTex ("Base (RGB)", 2D) = "black" {}
_Color ("Front Colour", Color) = (1.0, 1.0, 1.0, 1.0)
_LeftColor ("Left Colour", Color) = (1.0, 1.0, 1.0, 1.0)
_RightColor ("Right Colour", Color) = (1.0, 1.0, 1.0, 1.0)
_ShadowColor ("Shadow Colour", Color) = (1.0, 1.0, 1.0, 1.0)
_ShadowIntensity ("Shadow Intensity", Range(-1.0,1.0)) = 0.2
_TextureOpacity ("Texture Participation", Range(0.0,1.0)) = 1.0
_ForegroundDepth ("Foreground Fog Depth", Float) = 5.0
_ForegroundColor("Foreground Colour", Color) = (0.0,0.0,0.0,1.0)
_BackgroundDepth ("Backgroud Fog Depth", Float) = 3.0
_BackgroundColor("Background Colour", Color) = (0.4, 0.5, 1.0, 1.0)
[MaterialToggle] _Additive("Additive Texture", Float) = 1
_InkWidth("Ink Width", Range(0,1)) = 0.1
_InkColor("Ink Colour", Color) = (0.0,0.0,0.0,0.0)
_NoiseAmount("Noise Amount", Range(0,1)) = 0.7
samplerPerlinPerm2D("Perlin Perm", 2D) = "white" {}
samplerPerlinGrad2D("Perlin Grad", 2D) = "white" {}
}
SubShader {
Tags { "Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Opaque" }
pass {
Cull Back
ZWrite On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform fixed4 _Color; //Front Color
uniform fixed _ShadowIntensity;
uniform fixed4 _LeftColor;
uniform fixed4 _RightColor;
uniform fixed4 _ShadowColor;
uniform fixed _TextureOpacity;
uniform half _ForegroundDepth;
uniform half _BackgroundDepth;
uniform fixed4 _ForegroundColor;
uniform fixed4 _BackgroundColor;
uniform bool _Additive;
uniform fixed4 _InkColor;
uniform half _InkWidth;
uniform sampler2D samplerPerlinPerm2D;
uniform sampler2D samplerPerlinGrad2D;
uniform half _NoiseAmount;
struct vertexInput{
half4 vertex: POSITION;
half4 vertColor: Color;
half4 textureCoord: TEXCOORD0;
half3 normal : NORMAL;
};
struct vertexOutput{
half4 pos: SV_POSITION;
half4 col: Color;
half4 tex: TEXCOORD0;
half4 posWorld: TEXCOORD1;
fixed3 norm : NORMAL;
fixed3 viewDir : TEXCOORD2;
half dist: HALF;
};
//Perlin noise from : http://www.decarpentier.nl/scape-procedural-basics
float perlinNoise(float2 p, float seed)
{
// Calculate 2D integer coordinates i and fraction p.
float2 i = floor(p);
float2 f = p - i;
// Get weights from the coordinate fraction
float2 w = f * f * f * (f * (f * 6 - 15) + 10);
float4 w4 = float4(1, w.x, w.y, w.x * w.y);
// Get the four randomly permutated indices from the noise lattice nearest to
// p and offset these numbers with the seed number.
float4 perm = tex2D(samplerPerlinPerm2D, i / 256) + seed;
// Permutate the four offseted indices again and get the 2D gradient for each
// of the four permutated coordinates-seed pairs.
float4 g1 = tex2D(samplerPerlinGrad2D, perm.xy) * 2 - 1;
float4 g2 = tex2D(samplerPerlinGrad2D, perm.zw) * 2 - 1;
// Evaluate the four lattice gradients at p
float a = dot(g1.xy, f);
float b = dot(g2.xy, f + float2(-1, 0));
float c = dot(g1.zw, f + float2( 0, -1));
float d = dot(g2.zw, f + float2(-1, -1));
// Bi-linearly blend between the gradients, using w4 as blend factors.
float4 grads = float4(a, b - a, c - a, a - b - c + d);
float n = dot(grads, w4);
// Return the noise value, roughly normalized in the range [-1, 1]
return n * 1.5 ;
}
vertexOutput vert(vertexInput v){
vertexOutput o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
o.tex = v.textureCoord;
o.posWorld = mul(_Object2World, v.vertex);
o.col = v.vertColor;
o.dist = saturate ((o.posWorld.z>0)?(o.posWorld.z / _BackgroundDepth):(-o.posWorld.z / _ForegroundDepth));
o.norm = normalize(mul( float4(v.normal, 0.0), _World2Object).xyz),1.0;
o.viewDir = normalize(_WorldSpaceCameraPos.xyz - o.posWorld.xyz);
return o;
}
fixed4 frag(vertexOutput i): COLOR{
float4 c = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
half4 normalColor = (-i.viewDir.z * _Color) + (i.viewDir.y * _Color)
+ (i.viewDir.x * _LeftColor)
+ (-i.viewDir.x * _RightColor)
- (i.viewDir.y * _ShadowIntensity * _ShadowColor)
- (-i.viewDir.y * _ShadowIntensity * _ShadowColor);
//* i.col;
fixed4 finalColor = (1-i.dist) * (_Additive==1 ? saturate(float4(c.rgb,1.0) * _TextureOpacity * c.a + normalColor * (c.a * (1.0 - _TextureOpacity)))
: saturate(float4(c.rgb,1.0) * _TextureOpacity * c.a * normalColor) + normalColor * (1.0 - c.a * _TextureOpacity))
+ i.dist * ((i.posWorld.z>0) ? (_BackgroundColor) : (_ForegroundColor));
//Ink
fixed perlin = (0.2 + perlinNoise(i.posWorld.xz,1)) * _NoiseAmount;
fixed inkPass = 1 - saturate((dot(i.viewDir, i.norm) - _InkWidth * (perlin + 1 - _NoiseAmount)) * pow(2,10) + _InkWidth);
//fixed4 inkColor = inkPass * (1-i.dist) * _InkColor + i.dist * ((i.posWorld.z>0) ? (_BackgroundColor) : (_ForegroundColor)); - Distance Affected -
//return fixed4(-i.norm.z,-i.norm.z,-i.norm.z,1.0);
return (1-inkPass) * finalColor + inkPass * _InkColor;
}
ENDCG
}
}
// FallBack "Diffuse"
}
This category currently contains no pages or media.