Commit
- renamed some shaders
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// note that the sprite must have it's pivot in the center for this to work | ||
Shader "prime[31]/Sprite Light Kit/Shadowed" | ||
{ | ||
Properties | ||
{ | ||
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} | ||
_Color ("Tint", Color) = (1,1,1,1) | ||
_ShadowColor ( "Shadow Color", Color ) = ( 0, 0, 0.2, 0.9 ) | ||
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 | ||
|
||
_HorizontalSkew ( "Horizontal Skew", Float ) = 0 | ||
_VerticalScale ( "Vertical Scale", Float ) = 1 | ||
_VerticalOffset ( "Vertical Offset", Float ) = 0 | ||
|
||
_HorizontalTranslation ( "Horizontal Translation", Float ) = 0 | ||
_VerticalTranslation ( "Vertical Translation", Float ) = 0 | ||
} | ||
|
||
|
||
SubShader | ||
{ | ||
Tags | ||
{ | ||
"Queue" = "Transparent" | ||
"IgnoreProjector" = "True" | ||
"RenderType" = "Transparent" | ||
"PreviewType" = "Plane" | ||
"CanUseSpriteAtlas" = "True" | ||
} | ||
|
||
Cull Off | ||
Lighting Off | ||
ZWrite Off | ||
Fog { Mode Off } | ||
Blend One OneMinusSrcAlpha | ||
|
||
Pass | ||
{ | ||
CGPROGRAM | ||
#pragma vertex vert | ||
#pragma fragment frag | ||
#pragma multi_compile DUMMY PIXELSNAP_ON | ||
#include "UnityCG.cginc" | ||
|
||
struct appdata_t | ||
{ | ||
float4 vertex : POSITION; | ||
float4 color : COLOR; | ||
float2 texcoord : TEXCOORD0; | ||
}; | ||
|
||
struct v2f | ||
{ | ||
float4 vertex : SV_POSITION; | ||
fixed4 color : COLOR; | ||
half2 texcoord : TEXCOORD0; | ||
}; | ||
|
||
sampler2D _MainTex; | ||
fixed4 _Color; | ||
fixed4 _ShadowColor; | ||
float _HorizontalSkew; | ||
float _VerticalScale; | ||
float _VerticalOffset; | ||
float _HorizontalTranslation; | ||
float _VerticalTranslation; | ||
|
||
|
||
v2f vert( appdata_t IN ) | ||
{ | ||
v2f OUT; | ||
OUT.texcoord = IN.texcoord; | ||
|
||
float4x4 transformMatrix = float4x4 | ||
( | ||
1, _HorizontalSkew, 0, _HorizontalTranslation, | ||
0, _VerticalScale, 0, _VerticalTranslation, | ||
0, 0, 1, 0, | ||
0, _VerticalOffset, 0, 1 | ||
); | ||
|
||
float4 skewedVertex = mul( transformMatrix, IN.vertex ); | ||
OUT.vertex = mul( UNITY_MATRIX_MVP, skewedVertex ); | ||
|
||
#ifdef PIXELSNAP_ON | ||
OUT.vertex = UnityPixelSnap( OUT.vertex ); | ||
#endif | ||
|
||
return OUT; | ||
} | ||
|
||
fixed4 frag( v2f IN ) : SV_Target | ||
{ | ||
fixed4 c = tex2D( _MainTex, IN.texcoord ); | ||
return c.a * _ShadowColor; | ||
} | ||
ENDCG | ||
} // end Pass | ||
|
||
|
||
|
||
Pass | ||
{ | ||
|
||
CGPROGRAM | ||
#pragma vertex vert | ||
#pragma fragment frag | ||
#pragma multi_compile DUMMY PIXELSNAP_ON | ||
#include "UnityCG.cginc" | ||
|
||
struct appdata_t | ||
{ | ||
float4 vertex : POSITION; | ||
float4 color : COLOR; | ||
float2 texcoord : TEXCOORD0; | ||
}; | ||
|
||
struct v2f | ||
{ | ||
float4 vertex : SV_POSITION; | ||
fixed4 color : COLOR; | ||
half2 texcoord : TEXCOORD0; | ||
}; | ||
|
||
fixed4 _Color; | ||
|
||
v2f vert(appdata_t IN) | ||
{ | ||
v2f OUT; | ||
OUT.vertex = mul( UNITY_MATRIX_MVP, IN.vertex ); | ||
OUT.texcoord = IN.texcoord; | ||
OUT.color = IN.color * _Color; | ||
#ifdef PIXELSNAP_ON | ||
OUT.vertex = UnityPixelSnap( OUT.vertex ); | ||
#endif | ||
|
||
return OUT; | ||
} | ||
|
||
sampler2D _MainTex; | ||
|
||
fixed4 frag(v2f IN) : SV_Target | ||
{ | ||
fixed4 c = tex2D( _MainTex, IN.texcoord ) * IN.color; | ||
c.rgb *= c.a; | ||
return c; | ||
} | ||
ENDCG | ||
} // end pass | ||
|
||
|
||
|
||
|
||
} // end SubShader | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using UnityEngine; | ||
using System.Collections.Generic; | ||
|
||
|
||
namespace Prime31 | ||
{ | ||
public class SpriteLightKitLightManager : MonoBehaviour | ||
{ | ||
List<Vector3> _spriteLightPositions = new List<Vector3>(); | ||
|
||
|
||
void Awake() | ||
{ | ||
var slk = FindObjectOfType<SpriteLightKit>(); | ||
|
||
var allGOs = FindObjectsOfType<GameObject>(); | ||
for( var i = 0; i < allGOs.Length; i++ ) | ||
{ | ||
if( ( slk.lightLayer.value & 1 << allGOs[i].layer ) != 0 ) | ||
{ | ||
var pos = allGOs[i].transform.position; | ||
pos.z = 0f; | ||
_spriteLightPositions.Add( pos ); | ||
} | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// returns the weighted average position of any lights within range or the original position passed in if everything | ||
/// is further than maxDistance | ||
/// </summary> | ||
/// <returns>The nearest light.</returns> | ||
/// <param name="position">Position.</param> | ||
/// <param name="maxDistance">Max distance.</param> | ||
public Vector3 getAffectedAverageLightPos( Vector3 position, float maxSqrDistance ) | ||
{ | ||
position.z = 0; | ||
|
||
// we want the weighted average position of any lights that are close enough | ||
var totalWeight = 0f; | ||
var accumulatedPosition = Vector3.zero; | ||
for( var i = 0; i < _spriteLightPositions.Count; i++ ) | ||
{ | ||
var sqrDistance = sqrDistanceBetweenVectors( position, _spriteLightPositions[i] ); | ||
if( sqrDistance < maxSqrDistance ) | ||
{ | ||
// weight should be greater for closer lights and less for further away | ||
var weight = maxSqrDistance - sqrDistance; | ||
|
||
// tally the total weight | ||
totalWeight += weight; | ||
accumulatedPosition += ( weight * _spriteLightPositions[i] ); | ||
} | ||
} | ||
|
||
// if we have a totalWeight we need to take into account | ||
if( totalWeight > 0 ) | ||
{ | ||
return accumulatedPosition * ( 1f / totalWeight ); | ||
} | ||
|
||
return position; | ||
} | ||
|
||
|
||
public float sqrDistanceBetweenVectors( Vector3 a, Vector3 b ) | ||
{ | ||
return new Vector2( a.x - b.x, a.y - b.y ).sqrMagnitude; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
|
||
|
||
namespace Prime31 | ||
{ | ||
public class SpriteLightKitShadow : MonoBehaviour | ||
{ | ||
[Tooltip( "Maximum distance that a light can be from the sprite to still affect it" )] | ||
public float maxLightSqrDistance = 100f; | ||
[Tooltip( "The maximum offset from the sprite that the shadow can be" )] | ||
public float maxShadowTranslation = 0.5f; | ||
[Tooltip( "The averaged light distance is multiplied by this and it affects how far the shadow will offset from the sprite" )] | ||
public float shadowDistanceMultiplier = 1f; | ||
|
||
SpriteLightKitLightManager _slkLightManager; | ||
Transform _transform; | ||
Material _material; | ||
|
||
|
||
void Awake() | ||
{ | ||
_slkLightManager = FindObjectOfType<SpriteLightKitLightManager>(); | ||
_transform = gameObject.transform; | ||
_material = GetComponent<SpriteRenderer>().material; | ||
} | ||
|
||
|
||
void Update() | ||
{ | ||
// we dont want the z component to influence anything | ||
var position = _transform.position; | ||
position.z = 0f; | ||
|
||
var nearestLightPosition = _slkLightManager.getAffectedAverageLightPos( position, maxLightSqrDistance ); | ||
|
||
//Debug.DrawLine( position, nearestLightPosition, Color.red, 0.1f ); | ||
|
||
var lightDistance = _slkLightManager.sqrDistanceBetweenVectors( position, nearestLightPosition ); | ||
var lightDir = ( position - nearestLightPosition ).normalized * lightDistance * shadowDistanceMultiplier; | ||
lightDir /= maxLightSqrDistance; | ||
|
||
_material.SetFloat( "_HorizontalTranslation", Mathf.Clamp( lightDir.x, -maxShadowTranslation, maxShadowTranslation ) ); | ||
_material.SetFloat( "_VerticalTranslation", Mathf.Clamp( lightDir.y, -maxShadowTranslation, maxShadowTranslation ) ); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.