Skip to content

Instantly share code, notes, and snippets.

@lyuma
Created July 30, 2018 02:23
Show Gist options
  • Save lyuma/7e3dc3ccd661f894a1c215ceee87ce23 to your computer and use it in GitHub Desktop.
Save lyuma/7e3dc3ccd661f894a1c215ceee87ce23 to your computer and use it in GitHub Desktop.
/*
Plaid by Lyuma - 3d version
MIT License
Copyright (c) 2018 Lyuma
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
Shader "LyumaShader/Plaid" {
// Plaid by Lyuma - 3d
Properties {
_Color ("Color", Color) = (1,1,1,1)
}
SubShader {
Tags {
"Queue"="Geometry"
"RenderType"="Opaque"
}
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma multi_compile_fog
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 2.0
uniform float4 _Color;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 screenPosition : TEXCOORD1;
float4 pixelScreenPos : TEXCOORD2;
};
static float3 cameraPos = _WorldSpaceCameraPos; //(mul(mvMat, float4(0., 0., 0., 1.))).xyz; //mvMat._14_24_34_44;
static float3 objectPos = mul(unity_ObjectToWorld, float4(0., 0., 0., 1.)).xyz;
static float3 cameraToObj3D = (cameraPos - objectPos) * float3(1., 1., 1.) + float3(0., 0., 0.00001);
float4 drawHLine(float2 uv, float4 color, float y, float rep, float w) {
float d = abs(fmod(uv.y - y, rep));
return color * float(saturate(w/d) > 0.99);
};
float4 drawVLine(float2 uv, float4 color, float x, float rep, float w) {
float d = abs(fmod(uv.x - x, rep));
return color * float(saturate(w/d) > 0.99);
};
void blender(inout float3 color, float4 addColor, float2 pixelSnap) {
float mixa = addColor.a;
float diago = fmod(fmod(floor(pixelSnap.x + pixelSnap.y), 4.) + 4., 4.);
mixa = diago;
color = color * (1.-mixa) + addColor.xyz * mixa;
}
float2 screenUVAtPosition(float4 srcScreenPosition, float3 srcWorldDirection) {
// Compute projective scaling factor...
float perspectiveDivide = 1.0f / srcScreenPosition.w;
// Scale our view ray to unit depth.
float3 direction = srcWorldDirection * perspectiveDivide;
// Calculate our UV within the screen (for reading depth buffer)
float2 screenUV = (srcScreenPosition.xy * perspectiveDivide) * 0.5f + 0.5f;
#ifdef UNITY_UV_STARTS_AT_TOP
screenUV.y = 1 - screenUV.y;
#endif
// VR stereo support
return UnityStereoTransformScreenSpaceTex(screenUV);
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.screenPosition = o.vertex;
o.pixelScreenPos = ComputeScreenPos(o.vertex);
return o;
}
float4 frag (v2f i) : SV_Target {
float2 uv = i.screenPosition.xy / i.screenPosition.w;
float2 hpc = _ScreenParams.xy;// * 0.5f;
float2 pixelPos = floor ((i.pixelScreenPos.xy / i.pixelScreenPos.w) * hpc);
float vertViewpoint = cameraToObj3D.x + -.1 * cameraToObj3D.z;
float horizViewpoint = cameraToObj3D.y + -.1 * cameraToObj3D.z;
float3 outColor = _Color.xyz * saturate(sin(_Time.x)) + _Color.yzx * saturate(sin(2.0905 + _Time.x)) + _Color.zxy * saturate(sin(4.181 * _Time.x));
blender(outColor, drawVLine(uv, float4(0,.6,1.,.85), vertViewpoint * .1 + _Time.y * .05 + 0, .4, .1), pixelPos);
blender(outColor, drawHLine(uv, float4(1.,0.,.3,.6), horizViewpoint * .15 + _Time.y * .02 + 0, .4, .1), pixelPos);
blender(outColor, drawVLine(uv, float4(.5,0.,.7,-.85), vertViewpoint * .3 + _Time.y * .02 + .03, .4, .1), pixelPos);
blender(outColor, drawHLine(uv, float4(0,.3,.3,-.6), horizViewpoint * .5 + _Time.y * .05 + .08, .4, .1), pixelPos);
return float4(outColor, 1.);
}
ENDCG
}
Pass{
Name "FORWARD_DELTA"
Tags{
"LightMode" = "ForwardAdd"
}
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"
#pragma multi_compile_fwdadd_fullshadows
#pragma multi_compile_fog
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 2.0
uniform float4 _Color;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 screenPosition : TEXCOORD1;
float4 pixelScreenPos : TEXCOORD2;
};
static float3 cameraPos = _WorldSpaceCameraPos; //(mul(mvMat, float4(0., 0., 0., 1.))).xyz; //mvMat._14_24_34_44;
static float3 objectPos = mul(unity_ObjectToWorld, float4(0., 0., 0., 1.)).xyz;
static float3 cameraToObj3D = (cameraPos - objectPos) * float3(1., 1., 1.) + float3(0., 0., 0.00001);
float4 drawHLine(float2 uv, float4 color, float y, float rep, float w) {
float d = abs(fmod(uv.y - y, rep));
return color * float(saturate(w/d) > 0.99);
};
float4 drawVLine(float2 uv, float4 color, float x, float rep, float w) {
float d = abs(fmod(uv.x - x, rep));
return color * float(saturate(w/d) > 0.99);
};
void blender(inout float3 color, float4 addColor, float2 pixelSnap) {
float mixa = addColor.a;
float diago = fmod(fmod(floor(pixelSnap.x + pixelSnap.y), 4.) + 4., 4.);
mixa = diago;
color = color * (1.-mixa) + addColor.xyz * mixa;
}
float2 screenUVAtPosition(float4 srcScreenPosition, float3 srcWorldDirection) {
// Compute projective scaling factor...
float perspectiveDivide = 1.0f / srcScreenPosition.w;
// Scale our view ray to unit depth.
float3 direction = srcWorldDirection * perspectiveDivide;
// Calculate our UV within the screen (for reading depth buffer)
float2 screenUV = (srcScreenPosition.xy * perspectiveDivide) * 0.5f + 0.5f;
#ifdef UNITY_UV_STARTS_AT_TOP
screenUV.y = 1 - screenUV.y;
#endif
// VR stereo support
return UnityStereoTransformScreenSpaceTex(screenUV);
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.screenPosition = o.vertex;
o.pixelScreenPos = ComputeScreenPos(o.vertex);
return o;
}
float4 frag (v2f i) : SV_Target {
float2 uv = i.screenPosition.xy / i.screenPosition.w;
float2 hpc = _ScreenParams.xy;// * 0.5f;
float2 pixelPos = floor ((i.pixelScreenPos.xy / i.pixelScreenPos.w) * hpc);
float vertViewpoint = cameraToObj3D.x + -.1 * cameraToObj3D.z;
float horizViewpoint = cameraToObj3D.y + -.1 * cameraToObj3D.z;
float3 outColor = _Color.xyz * saturate(sin(_Time.x)) + _Color.yzx * saturate(sin(2.0905 + _Time.x)) + _Color.zxy * saturate(sin(4.181 * _Time.x));
blender(outColor, drawVLine(uv, float4(0,.6,1.,.85), vertViewpoint * .1 + _Time.y * .05 + 0, .4, .1), pixelPos);
blender(outColor, drawHLine(uv, float4(1.,0.,.3,.6), horizViewpoint * .15 + _Time.y * .02 + 0, .4, .1), pixelPos);
blender(outColor, drawVLine(uv, float4(.5,0.,.7,-.85), vertViewpoint * .3 + _Time.y * .02 + .03, .4, .1), pixelPos);
blender(outColor, drawHLine(uv, float4(0,.3,.3,-.6), horizViewpoint * .5 + _Time.y * .05 + .08, .4, .1), pixelPos);
return float4(outColor, 1.);
}
ENDCG
}
// Pass to render object as a shadow caster
Pass {
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile_shadowcaster
#pragma multi_compile_instancing // allow instanced shadow pass for most of the shaders
#pragma shader_feature VR_ONLY_2D
#include "UnityCG.cginc"
uniform float4 _EmissionColor;
struct v2f {
V2F_SHADOW_CASTER;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert( appdata_base v )
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
float4 actualObjectPos = mul(unity_ObjectToWorld, v.vertex);
float4 actualObjectPos2 = v.vertex;
#ifdef SHADOWS_CUBE
o.vec = mul(unity_ObjectToWorld, actualObjectPos2).xyz - _LightPositionRange.xyz;
o.pos = UnityObjectToClipPos(actualObjectPos2);
#else
o.pos = UnityClipSpaceShadowCasterPos(actualObjectPos2, v.normal); \
o.pos = UnityApplyLinearShadowBias(o.pos);
#endif
return o;
}
float4 frag( v2f i ) : SV_Target
{
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment