Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aszecsei committed Feb 18, 2016
1 parent 43c9422 commit 603a05c
Show file tree
Hide file tree
Showing 29 changed files with 600 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 2D Volumetric Lighting/Materials.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 8 additions & 0 deletions 2D Volumetric Lighting/Materials/LightShaftMaterial.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 8 additions & 0 deletions 2D Volumetric Lighting/Materials/Light_Material.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions 2D Volumetric Lighting/Prefabs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 8 additions & 0 deletions 2D Volumetric Lighting/Prefabs/Light_Texture.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added 2D Volumetric Lighting/Prefabs/Main Camera.prefab
Binary file not shown.
8 changes: 8 additions & 0 deletions 2D Volumetric Lighting/Prefabs/Main Camera.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added 2D Volumetric Lighting/Prefabs/Platform1.prefab
Binary file not shown.
8 changes: 8 additions & 0 deletions 2D Volumetric Lighting/Prefabs/Platform1.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions 2D Volumetric Lighting/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

131 changes: 131 additions & 0 deletions 2D Volumetric Lighting/Scripts/LightShafter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using UnityEngine;
using System.Collections;

public class LightShafter : MonoBehaviour {

// The number of rays to draw
[SerializeField]
[Range(1, 720)]
public int numberOfRays = 720;

public float lightDistance = 5;

[SerializeField]
[Range(-360, 360)]
public float lightAngle = -90;

[SerializeField]
[Range(0, 360)]
public float lightCone = 90;

[SerializeField]
public float lightSourceSize = 0;

public LayerMask layerMask;

public bool DrawDebugRays = true;

public Color lightColor = Color.white;

private GameObject[] shafts;

public Material shaftMaterial;

public GameObject PointLight;

[Range(0, 1)]
public float shaftWidth = 0.2f;

void Start()
{
Vector3 initialLocation = transform.position + (new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / 2);
Vector3 locationOffset = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / numberOfRays;
float RaySpacing = lightCone / numberOfRays;
float initialAngle = lightAngle - (lightCone / 2);

PointLight.GetComponent<Light>().color = lightColor;

shafts = new GameObject[numberOfRays];
for (int i = 0; i < numberOfRays; i++)
{
shafts[i] = new GameObject();
shafts[i].name = "LightShaft " + (i + 1);
shafts[i].transform.parent = transform;
shafts[i].transform.position = transform.position;
shafts[i].layer = LayerMask.NameToLayer("Light");
LineRenderer shaftRenderer = shafts[i].AddComponent<LineRenderer>();
shaftRenderer.useWorldSpace = true;
shaftRenderer.material = shaftMaterial;
shaftRenderer.receiveShadows = false;
shaftRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
shaftRenderer.SetWidth(locationOffset.magnitude, shaftWidth);
shaftRenderer.SetColors(lightColor, Color.black);

Vector3 origin = initialLocation - (locationOffset * i);
Vector3 direction = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance, Mathf.Sin(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance);

shaftRenderer.SetPosition(0, origin);
shaftRenderer.SetPosition(1, origin + direction);
}
}

void LateUpdate()
{
float RaySpacing = lightCone / numberOfRays;
float initialAngle = lightAngle - (lightCone / 2);

Vector3 initialLocation = transform.position + (new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / 2);
Vector3 locationOffset = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / numberOfRays;

for (int i = 0; i < numberOfRays; i++)
{
Vector3 origin = initialLocation - (locationOffset * i);
Vector3 direction = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance, Mathf.Sin(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance);
RaycastHit2D hit = Physics2D.Raycast(origin, direction, lightDistance, layerMask);

float travelledDistance = lightDistance;

LineRenderer shaftRenderer = shafts[i].GetComponent<LineRenderer>();

if (hit)
{
travelledDistance = hit.distance;
}

float distanceFrac = travelledDistance / lightDistance;

shaftRenderer.SetWidth(locationOffset.magnitude, locationOffset.magnitude + ((shaftWidth - locationOffset.magnitude) * distanceFrac));
shaftRenderer.SetColors(lightColor, new Color(lightColor.r * (1 - distanceFrac), lightColor.g * (1 - distanceFrac), lightColor.b * (1 - distanceFrac)));

shaftRenderer.SetPosition(0, origin);
shaftRenderer.SetPosition(1, origin + (direction * distanceFrac));
}
}

void OnDrawGizmos()
{
if (DrawDebugRays)
{
float RaySpacing = lightCone / numberOfRays;
float initialAngle = lightAngle - (lightCone / 2);

Vector3 initialLocation = transform.position + (new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / 2);
Vector3 locationOffset = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (lightAngle - 90)), Mathf.Sin(Mathf.Deg2Rad * (lightAngle - 90))) * lightSourceSize / numberOfRays;

for (int i = 0; i < numberOfRays; i++)
{
Vector3 origin = initialLocation - (locationOffset * i);
Vector3 direction = new Vector3(Mathf.Cos(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance, Mathf.Sin(Mathf.Deg2Rad * (i * RaySpacing + initialAngle)) * lightDistance);
RaycastHit2D hit = Physics2D.Raycast(origin, direction, lightDistance, layerMask);
if (hit)
{
Gizmos.DrawRay(origin, (direction * hit.distance / lightDistance));
}
else
{
Gizmos.DrawRay(origin, direction);
}
}
}
}
}
12 changes: 12 additions & 0 deletions 2D Volumetric Lighting/Scripts/LightShafter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions 2D Volumetric Lighting/Scripts/LightingCamera.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using UnityEngine;
using System.Collections;

public class LightingCamera : MonoBehaviour {

protected int width;
protected int height;
protected RenderTexture target;
protected Camera camComponent;

void Start()
{
camComponent = GetComponent<Camera>();
target = new RenderTexture(Screen.width, Screen.height, 0);
width = Screen.width;
height = Screen.height;
target.name = "TextureFromCamera_" + gameObject.name;
target.Create();
camComponent.targetTexture = target;
}

void Update()
{
if (width != Screen.width || height != Screen.height)
{
target.Release();
Destroy(target);
target = new RenderTexture(Screen.width, Screen.height, 0);
width = Screen.width;
height = Screen.height;
target.name = "TextureFromCamera_" + gameObject.name;
target.Create();
camComponent.targetTexture = target;
}
}
}
12 changes: 12 additions & 0 deletions 2D Volumetric Lighting/Scripts/LightingCamera.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions 2D Volumetric Lighting/Scripts/MainCamera.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using UnityEngine;

namespace UnityStandardAssets.ImageEffects
{
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Image Effects/Custom/Main Camera Overlay")]
public class MainCamera : PostEffectsBase
{
public enum OverlayBlendMode
{
Additive = 0,
ScreenBlend = 1,
Multiply = 2,
Overlay = 3,
AlphaBlend = 4,
}

public OverlayBlendMode blendMode = OverlayBlendMode.Overlay;
public float intensity = 1.0f;

public Shader overlayShader = null;
private Material overlayMaterial = null;

public Camera lightingCamera;


public override bool CheckResources()
{
CheckSupport(false);

overlayMaterial = CheckShaderAndCreateMaterial(overlayShader, overlayMaterial);

if (!isSupported)
ReportAutoDisable();
return isSupported;
}

void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (CheckResources() == false)
{
Graphics.Blit(source, destination);
return;
}

Vector4 UV_Transform = new Vector4(1, 0, 0, 1);

#if UNITY_WP8
// WP8 has no OS support for rotating screen with device orientation,
// so we do those transformations ourselves.
if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
UV_Transform = new Vector4(0, -1, 1, 0);
}
if (Screen.orientation == ScreenOrientation.LandscapeRight) {
UV_Transform = new Vector4(0, 1, -1, 0);
}
if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
UV_Transform = new Vector4(-1, 0, 0, -1);
}
#endif

overlayMaterial.SetVector("_UV_Transform", UV_Transform);
overlayMaterial.SetFloat("_Intensity", intensity);
overlayMaterial.SetTexture("_Overlay", lightingCamera.targetTexture);
Graphics.Blit(source, destination, overlayMaterial, (int)blendMode);
}
}
}
12 changes: 12 additions & 0 deletions 2D Volumetric Lighting/Scripts/MainCamera.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions 2D Volumetric Lighting/Shaders.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 603a05c

Please sign in to comment.