Skip to content

Commit 603a05c

Browse files
committed
initial commit
1 parent 43c9422 commit 603a05c

29 files changed

+600
-0
lines changed

2D Volumetric Lighting/Materials.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

2D Volumetric Lighting/Materials/LightShaftMaterial.mat.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

2D Volumetric Lighting/Materials/Light_Material.mat.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2D Volumetric Lighting/Prefabs.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

2D Volumetric Lighting/Prefabs/Light_Texture.prefab.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

2D Volumetric Lighting/Prefabs/Main Camera.prefab.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

2D Volumetric Lighting/Prefabs/Platform1.prefab.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2D Volumetric Lighting/Scripts.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class LightShafter : MonoBehaviour {
5+
6+
// The number of rays to draw
7+
[SerializeField]
8+
[Range(1, 720)]
9+
public int numberOfRays = 720;
10+
11+
public float lightDistance = 5;
12+
13+
[SerializeField]
14+
[Range(-360, 360)]
15+
public float lightAngle = -90;
16+
17+
[SerializeField]
18+
[Range(0, 360)]
19+
public float lightCone = 90;
20+
21+
[SerializeField]
22+
public float lightSourceSize = 0;
23+
24+
public LayerMask layerMask;
25+
26+
public bool DrawDebugRays = true;
27+
28+
public Color lightColor = Color.white;
29+
30+
private GameObject[] shafts;
31+
32+
public Material shaftMaterial;
33+
34+
public GameObject PointLight;
35+
36+
[Range(0, 1)]
37+
public float shaftWidth = 0.2f;
38+
39+
void Start()
40+
{
41+
Vector3 initialLocation = transform.position + (new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / 2);
42+
Vector3 locationOffset = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / numberOfRays;
43+
float RaySpacing = lightCone / numberOfRays;
44+
float initialAngle = lightAngle - (lightCone / 2);
45+
46+
PointLight.GetComponent<Light>().color = lightColor;
47+
48+
shafts = new GameObject[numberOfRays];
49+
for (int i = 0; i < numberOfRays; i++)
50+
{
51+
shafts[i] = new GameObject();
52+
shafts[i].name = "LightShaft " + (i + 1);
53+
shafts[i].transform.parent = transform;
54+
shafts[i].transform.position = transform.position;
55+
shafts[i].layer = LayerMask.NameToLayer("Light");
56+
LineRenderer shaftRenderer = shafts[i].AddComponent<LineRenderer>();
57+
shaftRenderer.useWorldSpace = true;
58+
shaftRenderer.material = shaftMaterial;
59+
shaftRenderer.receiveShadows = false;
60+
shaftRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
61+
shaftRenderer.SetWidth(locationOffset.magnitude, shaftWidth);
62+
shaftRenderer.SetColors(lightColor, Color.black);
63+
64+
Vector3 origin = initialLocation - (locationOffset * i);
65+
Vector3 direction = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance, Mathf.Sin(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance);
66+
67+
shaftRenderer.SetPosition(0, origin);
68+
shaftRenderer.SetPosition(1, origin + direction);
69+
}
70+
}
71+
72+
void LateUpdate()
73+
{
74+
float RaySpacing = lightCone / numberOfRays;
75+
float initialAngle = lightAngle - (lightCone / 2);
76+
77+
Vector3 initialLocation = transform.position + (new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / 2);
78+
Vector3 locationOffset = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / numberOfRays;
79+
80+
for (int i = 0; i < numberOfRays; i++)
81+
{
82+
Vector3 origin = initialLocation - (locationOffset * i);
83+
Vector3 direction = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance, Mathf.Sin(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance);
84+
RaycastHit2D hit = Physics2D.Raycast(origin, direction, lightDistance, layerMask);
85+
86+
float travelledDistance = lightDistance;
87+
88+
LineRenderer shaftRenderer = shafts[i].GetComponent<LineRenderer>();
89+
90+
if (hit)
91+
{
92+
travelledDistance = hit.distance;
93+
}
94+
95+
float distanceFrac = travelledDistance / lightDistance;
96+
97+
shaftRenderer.SetWidth(locationOffset.magnitude, locationOffset.magnitude + ((shaftWidth - locationOffset.magnitude) * distanceFrac));
98+
shaftRenderer.SetColors(lightColor, new Color(lightColor.r * (1 - distanceFrac), lightColor.g * (1 - distanceFrac), lightColor.b * (1 - distanceFrac)));
99+
100+
shaftRenderer.SetPosition(0, origin);
101+
shaftRenderer.SetPosition(1, origin + (direction * distanceFrac));
102+
}
103+
}
104+
105+
void OnDrawGizmos()
106+
{
107+
if (DrawDebugRays)
108+
{
109+
float RaySpacing = lightCone / numberOfRays;
110+
float initialAngle = lightAngle - (lightCone / 2);
111+
112+
Vector3 initialLocation = transform.position + (new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / 2);
113+
Vector3 locationOffset = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / numberOfRays;
114+
115+
for (int i = 0; i < numberOfRays; i++)
116+
{
117+
Vector3 origin = initialLocation - (locationOffset * i);
118+
Vector3 direction = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance, Mathf.Sin(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance);
119+
RaycastHit2D hit = Physics2D.Raycast(origin, direction, lightDistance, layerMask);
120+
if (hit)
121+
{
122+
Gizmos.DrawRay(origin, (direction * hit.distance / lightDistance));
123+
}
124+
else
125+
{
126+
Gizmos.DrawRay(origin, direction);
127+
}
128+
}
129+
}
130+
}
131+
}

2D Volumetric Lighting/Scripts/LightShafter.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class LightingCamera : MonoBehaviour {
5+
6+
protected int width;
7+
protected int height;
8+
protected RenderTexture target;
9+
protected Camera camComponent;
10+
11+
void Start()
12+
{
13+
camComponent = GetComponent<Camera>();
14+
target = new RenderTexture(Screen.width, Screen.height, 0);
15+
width = Screen.width;
16+
height = Screen.height;
17+
target.name = "TextureFromCamera_" + gameObject.name;
18+
target.Create();
19+
camComponent.targetTexture = target;
20+
}
21+
22+
void Update()
23+
{
24+
if (width != Screen.width || height != Screen.height)
25+
{
26+
target.Release();
27+
Destroy(target);
28+
target = new RenderTexture(Screen.width, Screen.height, 0);
29+
width = Screen.width;
30+
height = Screen.height;
31+
target.name = "TextureFromCamera_" + gameObject.name;
32+
target.Create();
33+
camComponent.targetTexture = target;
34+
}
35+
}
36+
}

2D Volumetric Lighting/Scripts/LightingCamera.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace UnityStandardAssets.ImageEffects
5+
{
6+
[ExecuteInEditMode]
7+
[RequireComponent(typeof(Camera))]
8+
[AddComponentMenu("Image Effects/Custom/Main Camera Overlay")]
9+
public class MainCamera : PostEffectsBase
10+
{
11+
public enum OverlayBlendMode
12+
{
13+
Additive = 0,
14+
ScreenBlend = 1,
15+
Multiply = 2,
16+
Overlay = 3,
17+
AlphaBlend = 4,
18+
}
19+
20+
public OverlayBlendMode blendMode = OverlayBlendMode.Overlay;
21+
public float intensity = 1.0f;
22+
23+
public Shader overlayShader = null;
24+
private Material overlayMaterial = null;
25+
26+
public Camera lightingCamera;
27+
28+
29+
public override bool CheckResources()
30+
{
31+
CheckSupport(false);
32+
33+
overlayMaterial = CheckShaderAndCreateMaterial(overlayShader, overlayMaterial);
34+
35+
if (!isSupported)
36+
ReportAutoDisable();
37+
return isSupported;
38+
}
39+
40+
void OnRenderImage(RenderTexture source, RenderTexture destination)
41+
{
42+
if (CheckResources() == false)
43+
{
44+
Graphics.Blit(source, destination);
45+
return;
46+
}
47+
48+
Vector4 UV_Transform = new Vector4(1, 0, 0, 1);
49+
50+
#if UNITY_WP8
51+
// WP8 has no OS support for rotating screen with device orientation,
52+
// so we do those transformations ourselves.
53+
if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
54+
UV_Transform = new Vector4(0, -1, 1, 0);
55+
}
56+
if (Screen.orientation == ScreenOrientation.LandscapeRight) {
57+
UV_Transform = new Vector4(0, 1, -1, 0);
58+
}
59+
if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
60+
UV_Transform = new Vector4(-1, 0, 0, -1);
61+
}
62+
#endif
63+
64+
overlayMaterial.SetVector("_UV_Transform", UV_Transform);
65+
overlayMaterial.SetFloat("_Intensity", intensity);
66+
overlayMaterial.SetTexture("_Overlay", lightingCamera.targetTexture);
67+
Graphics.Blit(source, destination, overlayMaterial, (int)blendMode);
68+
}
69+
}
70+
}

2D Volumetric Lighting/Scripts/MainCamera.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2D Volumetric Lighting/Shaders.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)