├── .gitignore ├── Assets ├── LightFlood.unity ├── LightFlood.unity.meta ├── Materials.meta ├── Materials │ ├── LightMaterial.mat │ ├── LightMaterial.mat.meta │ ├── LightTexturedMaterial.mat │ ├── LightTexturedMaterial.mat.meta │ ├── WallBlue.mat │ ├── WallBlue.mat.meta │ ├── WallGreen.mat │ ├── WallGreen.mat.meta │ ├── WallRed.mat │ ├── WallRed.mat.meta │ ├── WallWhite.mat │ ├── WallWhite.mat.meta │ ├── pmBouncy.physicMaterial │ └── pmBouncy.physicMaterial.meta ├── Prefabs.meta ├── Prefabs │ ├── ProjLight.prefab │ └── ProjLight.prefab.meta ├── Scripts.meta ├── Scripts │ ├── FirstPersonCamera.cs │ ├── FirstPersonCamera.cs.meta │ ├── GIRend.cs │ ├── GIRend.cs.meta │ ├── LightFlood.cs │ ├── LightFlood.cs.meta │ ├── LightSource.cs │ ├── LightSource.cs.meta │ ├── LightSourceArea.cs │ ├── LightSourceArea.cs.meta │ ├── LightSourceCone.cs │ ├── LightSourceCone.cs.meta │ ├── LightSourceSphere.cs │ ├── LightSourceSphere.cs.meta │ ├── ShootLights.cs │ └── ShootLights.cs.meta ├── Shaders.meta ├── Shaders │ ├── WallShader.shader │ └── WallShader.shader.meta ├── Textures.meta └── Textures │ ├── free-pencil-image.jpg │ └── free-pencil-image.jpg.meta ├── LICENSE ├── ProjectSettings ├── AudioManager.asset ├── ClusterInputManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── ProjectVersion.txt ├── QualitySettings.asset ├── TagManager.asset ├── TimeManager.asset ├── UnityAdsSettings.asset └── UnityConnectSettings.asset ├── README.md └── img └── teaser.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | /[Bb]uilds/ 6 | /Assets/AssetStoreTools* 7 | 8 | # Autogenerated VS/MD solution and project files 9 | ExportedObj/ 10 | *.csproj 11 | *.unityproj 12 | *.sln 13 | *.suo 14 | *.tmp 15 | *.user 16 | *.userprefs 17 | *.pidb 18 | *.booproj 19 | *.svd 20 | 21 | 22 | # Unity3D generated meta files 23 | *.pidb.meta 24 | 25 | # Unity3D Generated File On Crash Reports 26 | sysinfo.txt 27 | 28 | # Builds 29 | *.apk 30 | *.unitypackage 31 | -------------------------------------------------------------------------------- /Assets/LightFlood.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/LightFlood.unity -------------------------------------------------------------------------------- /Assets/LightFlood.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c38768bd919e969408243a4e38149e48 3 | timeCreated: 1480786675 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9bae62b238274e343b642bcfd22b3692 3 | folderAsset: yes 4 | timeCreated: 1487838756 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Materials/LightMaterial.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/Materials/LightMaterial.mat -------------------------------------------------------------------------------- /Assets/Materials/LightMaterial.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2003eb39754a1aa479a46a2077ecad7b 3 | timeCreated: 1480807565 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Materials/LightTexturedMaterial.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/Materials/LightTexturedMaterial.mat -------------------------------------------------------------------------------- /Assets/Materials/LightTexturedMaterial.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2196cac5498a94a4e833d4726123d660 3 | timeCreated: 1480807565 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Materials/WallBlue.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/Materials/WallBlue.mat -------------------------------------------------------------------------------- /Assets/Materials/WallBlue.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4594195c2a31d0e4286f526f68e2a79b 3 | timeCreated: 1480784180 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Materials/WallGreen.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/Materials/WallGreen.mat -------------------------------------------------------------------------------- /Assets/Materials/WallGreen.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ce35751822a2e3f46a7485f5c59f51b6 3 | timeCreated: 1480784180 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Materials/WallRed.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/Materials/WallRed.mat -------------------------------------------------------------------------------- /Assets/Materials/WallRed.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c5fd2d29bbe65d94da433bd025955bde 3 | timeCreated: 1480784180 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Materials/WallWhite.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/Materials/WallWhite.mat -------------------------------------------------------------------------------- /Assets/Materials/WallWhite.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 832039a0fc9889b4aa789199764514d1 3 | timeCreated: 1480784180 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Materials/pmBouncy.physicMaterial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/Materials/pmBouncy.physicMaterial -------------------------------------------------------------------------------- /Assets/Materials/pmBouncy.physicMaterial.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d88726872dce807429d51bc178a57923 3 | timeCreated: 1481148041 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Prefabs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cc7ed3d94919d1f488cc81c0fe5545ba 3 | folderAsset: yes 4 | timeCreated: 1481147803 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Prefabs/ProjLight.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/Prefabs/ProjLight.prefab -------------------------------------------------------------------------------- /Assets/Prefabs/ProjLight.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cae7d9352340ed04ab637b1942a19fe3 3 | timeCreated: 1481147806 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8c2540db594c5b44e87c78933e284671 3 | folderAsset: yes 4 | timeCreated: 1487838781 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Scripts/FirstPersonCamera.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class FirstPersonCamera : MonoBehaviour 4 | { 5 | public float _translateSpeed = 2f; 6 | public float _rotateSpeed = 2f; 7 | 8 | void Start() 9 | { 10 | } 11 | 12 | void Update() 13 | { 14 | transform.position += Input.GetAxis( "Vertical" ) * transform.forward * _translateSpeed * Time.deltaTime; 15 | 16 | float strafe = 0f; 17 | if( Input.GetKey( KeyCode.A ) ) strafe -= 1f; 18 | if( Input.GetKey( KeyCode.D ) ) strafe += 1f; 19 | transform.position += strafe * transform.right * _translateSpeed * Time.deltaTime; 20 | 21 | float rotate = 0f; 22 | if( Input.GetKey( KeyCode.LeftArrow ) ) rotate -= 1f; 23 | if( Input.GetKey( KeyCode.RightArrow ) ) rotate += 1f; 24 | transform.eulerAngles += rotate * Vector3.up * _rotateSpeed * Time.deltaTime; 25 | 26 | float updown = 0f; 27 | if( Input.GetKey( KeyCode.Q ) ) updown -= 1f; 28 | if( Input.GetKey( KeyCode.E ) ) updown += 1f; 29 | transform.position += updown * transform.up * _translateSpeed * Time.deltaTime; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Assets/Scripts/FirstPersonCamera.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6d8d0c515aeddc746aa519928a0c4e54 3 | timeCreated: 1481147028 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/GIRend.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class GIRend : MonoBehaviour 4 | { 5 | public struct Photon 6 | { 7 | public Vector3 position; 8 | public Vector3 next_dir; 9 | public Vector3 intensity; 10 | public bool alive; 11 | } 12 | 13 | Photon[] _photons; 14 | 15 | public int maxPhotons = 10; 16 | 17 | [Range( 0.95f, 1f )] 18 | public float _alpha = 0.99f; 19 | 20 | public int _resPerMeter = 256; 21 | 22 | [Range( 0f, 0.2f )] 23 | public float _blurAmt = 0.05f; 24 | 25 | [Range( 0f, 1f )] 26 | public float _exposure = 0.5f; 27 | 28 | [Range( 0f, 1f )] 29 | public float _contrast = 0.5f; 30 | 31 | LightSource[] _lights; 32 | float _totalLightSurfaceArea = 0f; 33 | 34 | public bool _render = true; 35 | 36 | void Start() 37 | { 38 | UpdateLights(); 39 | } 40 | 41 | void UpdateLights() 42 | { 43 | _lights = FindObjectsOfType(); 44 | 45 | _totalLightSurfaceArea = 0f; 46 | foreach( var light in _lights ) 47 | _totalLightSurfaceArea += light._surfaceArea /** light.intensity*/; 48 | } 49 | 50 | void Update() 51 | { 52 | UpdateLights(); 53 | 54 | if( !_render ) 55 | return; 56 | 57 | if( _photons == null ) 58 | _photons = new Photon[maxPhotons]; 59 | 60 | // add new photons 61 | if( _lights.Length > 0 ) 62 | for( int i = 0; i < _photons.Length; i++ ) 63 | { 64 | if( !_photons[i].alive ) 65 | { 66 | float u = Random.value * _totalLightSurfaceArea; 67 | LightSource light = null; 68 | float cdf = 0f; 69 | for( int li = 0; li < _lights.Length; li++ ) 70 | { 71 | cdf += /*_lights[li].intensity **/ _lights[li]._surfaceArea; 72 | 73 | if( u <= cdf ) 74 | { 75 | light = _lights[li]; 76 | break; 77 | } 78 | } 79 | 80 | if( light == null ) light = _lights[_lights.Length - 1]; 81 | 82 | light.MakePhoton( ref _photons[i] ); 83 | 84 | _photons[i].alive = true; 85 | } 86 | } 87 | 88 | // trace photons 89 | for( int i = 0; i < _photons.Length; i++ ) 90 | { 91 | //// the should all be alive - no branch needed 92 | //if( !_photons[i].alive ) 93 | // continue; 94 | 95 | RaycastHit hitInfo; 96 | if( Physics.Raycast( _photons[i].position + 0.01f * _photons[i].next_dir, _photons[i].next_dir, out hitInfo ) ) 97 | { 98 | _photons[i].intensity /= 1f + (hitInfo.point - _photons[i].position).magnitude * 0.1f; 99 | //_photons[i].intensity /= Mathf.Max( 1f, (hitInfo.point - _photons[i].position).sqrMagnitude ); 100 | 101 | //if( Random.value < 0.2f ) 102 | // Debug.DrawLine( _photons[i].position, hitInfo.point, new Color( 1f, 1f, 1f, _photons[i].intensity.magnitude*0.1f ), 1f ); 103 | 104 | Color existingRadiance = Color.black; 105 | 106 | LightFlood lf = hitInfo.collider.GetComponent(); 107 | if( lf ) 108 | lf.Hit( ref _photons[i], ref hitInfo, out existingRadiance ); 109 | 110 | // cosine rule for energy loss 111 | _photons[i].intensity *= Vector3.Dot( hitInfo.normal, -_photons[i].next_dir ); 112 | //_photons[i].intensity += 0.1f * new Vector3( existingRadiance.r, existingRadiance.g, existingRadiance.b ); 113 | _photons[i].intensity = new Vector3( _photons[i].intensity.x * lf.color.r, _photons[i].intensity.y * lf.color.g, _photons[i].intensity.z * lf.color.b ); 114 | // add whatever radiance was there before as well 115 | 116 | _photons[i].position = hitInfo.point; 117 | 118 | if( Vector3.Dot( _photons[i].next_dir = Random.onUnitSphere, hitInfo.normal ) < 0f ) 119 | _photons[i].next_dir = -_photons[i].next_dir; 120 | 121 | if( _photons[i].intensity.sqrMagnitude < 0.01f ) _photons[i].alive = false; 122 | } 123 | else 124 | { 125 | //Debug.DrawLine( _photons[i].position, _photons[i].position + 100f * _photons[i].next_dir, new Color( 1f, 1f, 1f, _photons[i].intensity * 0.01f ), 1f ); 126 | 127 | // photon escaped scene 128 | _photons[i].alive = false; 129 | } 130 | } 131 | } 132 | 133 | static GIRend _instance = null; 134 | public static GIRend Instance { get { return _instance != null ? _instance : (_instance = FindObjectOfType()); } } 135 | } 136 | -------------------------------------------------------------------------------- /Assets/Scripts/GIRend.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 28b4e922bada26e45abbece770ab8176 3 | timeCreated: 1480785675 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/LightFlood.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class LightFlood : MonoBehaviour 4 | { 5 | Texture2D _texture; 6 | MeshRenderer _mr; 7 | Color[] pixels; 8 | Color[] pixels2; 9 | 10 | Color _col = Color.white; 11 | public Color color { get { return _col; } } 12 | 13 | int _texRes = 64; 14 | 15 | void Start() 16 | { 17 | float sz = Mathf.Lerp( transform.lossyScale.x, transform.lossyScale.y, 0.5f ); 18 | 19 | _texRes = Mathf.FloorToInt( GIRend.Instance._resPerMeter * sz ); 20 | 21 | _texture = new Texture2D( _texRes, _texRes, TextureFormat.RGBAHalf, false, true ); 22 | _texture.wrapMode = TextureWrapMode.Clamp; 23 | 24 | // Reset all pixels color to transparent 25 | pixels = _texture.GetPixels(); 26 | for( int i = 0; i < pixels.Length; i++ ) 27 | pixels[i] = Color.black; 28 | pixels2 = new Color[pixels.Length]; 29 | 30 | _texture.SetPixels( pixels ); 31 | _texture.Apply(); 32 | 33 | _mr = GetComponent(); 34 | _mr.material.mainTexture = _texture; 35 | } 36 | 37 | void Update() 38 | { 39 | if( !GIRend.Instance._render ) 40 | return; 41 | 42 | float blurAmt = GIRend.Instance._blurAmt; 43 | 44 | for( int i = 0; i < pixels.Length; i++ ) 45 | { 46 | float wt = 0f; 47 | 48 | pixels2[i] = pixels[i] * (1f - 4f * blurAmt); wt += 1f - 4f * blurAmt; 49 | 50 | int x = i % _texRes; 51 | if( x - 1 >= 0 ) { pixels2[i] += blurAmt * pixels[i - 1]; wt += blurAmt; } 52 | if( x + 1 < _texRes ) { pixels2[i] += blurAmt * pixels[i + 1]; wt += blurAmt; } 53 | 54 | if( i - _texRes >= 0 ) { pixels2[i] += blurAmt * pixels[i - _texRes]; wt += blurAmt; } 55 | if( i + _texRes < pixels.Length ) { pixels2[i] += blurAmt * pixels[i + _texRes]; wt += blurAmt; } 56 | 57 | pixels2[i] /= wt; 58 | pixels2[i] *= GIRend.Instance._alpha; 59 | } 60 | 61 | for( int i = 0; i < pixels.Length; i++ ) 62 | pixels[i] = pixels2[i]; 63 | 64 | _texture.SetPixels( pixels ); 65 | 66 | // //_texture.SetPixel( Random.Range( 0, _texture.width - 1 ), Random.Range( 0, _texture.height - 1 ), ); 67 | 68 | _texture.Apply( true, false ); 69 | 70 | _col = _mr.material.GetColor( "_WallColour" ); 71 | _mr.material.SetFloat( "_texRes", _texRes ); 72 | _mr.material.SetFloat( "_exposure", GIRend.Instance._exposure ); 73 | _mr.material.SetFloat( "_contrast", GIRend.Instance._contrast ); 74 | } 75 | 76 | public void Hit( ref GIRend.Photon p, ref RaycastHit hit, out Color existingRadiance ) 77 | { 78 | Vector3 local = transform.InverseTransformPoint( hit.point ); 79 | 80 | Vector3 uv = local + 0.5f * Vector3.one; 81 | uv.z = 0f; 82 | 83 | int x = Mathf.FloorToInt( uv.x * _texture.width ); 84 | int y = Mathf.FloorToInt( uv.y * _texture.height ); 85 | int index = y * _texture.height + x; 86 | index = Mathf.Clamp( index, 0, pixels.Length - 1 ); 87 | 88 | existingRadiance = pixels[index]; 89 | 90 | pixels[index].r += p.intensity.x; 91 | pixels[index].g += p.intensity.y; 92 | pixels[index].b += p.intensity.z; 93 | 94 | pixels[index].a = 255; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Assets/Scripts/LightFlood.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ec268c674c62d34469703bd04174f256 3 | timeCreated: 1480784213 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/LightSource.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public abstract class LightSource : MonoBehaviour 4 | { 5 | public const float LIGHT_INTEN_DIV = 4f; 6 | 7 | public float _surfaceArea = 1f; 8 | 9 | public abstract void MakePhoton( ref GIRend.Photon p ); 10 | 11 | protected Light _light; 12 | public float intensity { get { return _light.intensity / LIGHT_INTEN_DIV; } } // division is a hack - low intensities look best with log tonemap 13 | 14 | protected virtual void Awake() 15 | { 16 | _light = GetComponent(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Assets/Scripts/LightSource.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 580f1e00467aa00449d915af974bd8b9 3 | timeCreated: 1480787326 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/LightSourceArea.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | [RequireComponent(typeof(Light))] 4 | public class LightSourceArea : LightSource 5 | { 6 | [Range( 0f, 90f )] 7 | public float _coneAngleDeg = 90f; 8 | 9 | public Texture2D _texture; 10 | 11 | public override void MakePhoton( ref GIRend.Photon p ) 12 | { 13 | p.position = transform.TransformPoint( new Vector3( Random.value - 0.5f, Random.value - 0.5f, 0f ) ); 14 | 15 | // construct a random direction within cone angle 16 | float azimuth = Random.value * Mathf.PI * 2f; 17 | float altitude = Random.value * _coneAngleDeg * Mathf.Deg2Rad; 18 | 19 | Vector3 local; 20 | local.x = Mathf.Cos( azimuth ) * Mathf.Sin( altitude ); 21 | local.y = Mathf.Sin( azimuth ) * Mathf.Sin( altitude ); 22 | local.z = Mathf.Sqrt( 1f - local.x * local.x - local.y * local.y ); 23 | 24 | p.next_dir = local.x * transform.right + local.y * transform.up - local.z * transform.forward; 25 | 26 | Color col; 27 | if( _texture != null ) 28 | { 29 | Vector3 localPos = transform.InverseTransformPoint( p.position ); 30 | 31 | Vector3 uv = localPos + 0.5f * Vector3.one; 32 | uv.z = 0f; 33 | 34 | col = intensity * _texture.GetPixelBilinear( uv.x, uv.y ); 35 | } 36 | else 37 | { 38 | col = intensity * _light.color; 39 | } 40 | 41 | p.intensity = new Vector3( col.r, col.g, col.b ); 42 | } 43 | 44 | void Update() 45 | { 46 | _surfaceArea = transform.lossyScale.x * transform.lossyScale.y; 47 | 48 | if( _texture != null ) 49 | { 50 | GetComponent().material.mainTexture = _texture; 51 | } 52 | else 53 | { 54 | GetComponent().material.color = _light.color * intensity * 10f; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Assets/Scripts/LightSourceArea.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b7d6a6322c1980e4fb6a58757a74dde5 3 | timeCreated: 1480787326 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/LightSourceCone.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | [RequireComponent(typeof(Light))] 4 | public class LightSourceCone : LightSource 5 | { 6 | [Range( 0f, 90f )] 7 | public float _coneAngleDeg = 20f; 8 | 9 | public override void MakePhoton( ref GIRend.Photon p ) 10 | { 11 | p.position = transform.position; 12 | 13 | // construct a random direction within cone angle 14 | float azimuth = Random.value * Mathf.PI * 2f; 15 | float altitude = Random.value * _coneAngleDeg * Mathf.Deg2Rad; 16 | 17 | Vector3 local; 18 | local.x = Mathf.Cos( azimuth ) * Mathf.Sin( altitude ); 19 | local.y = Mathf.Sin( azimuth ) * Mathf.Sin( altitude ); 20 | local.z = Mathf.Sqrt( 1f - local.x * local.x - local.y * local.y ); 21 | 22 | p.next_dir = local.x * transform.right + local.y * transform.up + local.z * transform.forward; 23 | 24 | 25 | float dotmin = Mathf.Cos( 0.5f * _light.spotAngle * Mathf.Deg2Rad ); 26 | do { p.next_dir = Random.onUnitSphere; } 27 | while( Vector3.Dot( transform.forward, p.next_dir ) <= dotmin ); 28 | 29 | Color col = intensity * _light.color; 30 | p.intensity = new Vector3( col.r, col.g, col.b ); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Assets/Scripts/LightSourceCone.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 08cb25a5b833e3647ac9c7403a857fdb 3 | timeCreated: 1480787326 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/LightSourceSphere.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | [RequireComponent(typeof(Light))] 4 | public class LightSourceSphere : LightSource 5 | { 6 | public float _deathPlane = -15f; 7 | 8 | float _radius = 1f; 9 | 10 | public override void MakePhoton( ref GIRend.Photon p ) 11 | { 12 | Vector3 off = Random.onUnitSphere; 13 | 14 | p.position = transform.position + _radius * off; 15 | 16 | p.next_dir = Random.onUnitSphere; 17 | if( Vector3.Dot( p.next_dir, off ) < 0f ) 18 | p.next_dir = -p.next_dir; 19 | 20 | Color col = intensity * _light.color; 21 | 22 | p.intensity = new Vector3( col.r, col.g, col.b ); 23 | } 24 | 25 | void Update() 26 | { 27 | _radius = 0.5f * (transform.lossyScale.x + transform.lossyScale.y + transform.lossyScale.z) / 3f; 28 | _surfaceArea = Mathf.PI * _radius * _radius; 29 | GetComponent().material.color = _light.color * intensity * 10f; 30 | 31 | if( transform.position.y < _deathPlane ) 32 | Destroy( gameObject ); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Assets/Scripts/LightSourceSphere.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ee459a9c9b1392e45bf41f119caba30f 3 | timeCreated: 1480787326 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/ShootLights.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class ShootLights : MonoBehaviour 5 | { 6 | public float _speedZ = 3f; 7 | public float _speedY = 3f; 8 | 9 | public GameObject _projectilePrefab; 10 | 11 | void Start () 12 | { 13 | } 14 | 15 | void Update () 16 | { 17 | if( Input.GetKeyDown( KeyCode.Space ) ) 18 | { 19 | GameObject proj = Instantiate( _projectilePrefab, transform.position + Vector3.down, Quaternion.identity ) as GameObject; 20 | proj.GetComponent().velocity = _speedZ * transform.forward + _speedY * transform.up; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Assets/Scripts/ShootLights.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b6a3614a414e1c54c9c726d7e57e75fb 3 | timeCreated: 1481147514 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 637c5b7f58d939f4a90428c320ff344b 3 | folderAsset: yes 4 | timeCreated: 1487838832 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Shaders/WallShader.shader: -------------------------------------------------------------------------------- 1 | Shader "Unlit/WallShader" 2 | { 3 | Properties 4 | { 5 | _MainTex( "Texture", 2D ) = "white" {} 6 | _WallColour( "Colour", Color ) = (0.,0.,0.,0.) 7 | } 8 | 9 | SubShader 10 | { 11 | Tags { "RenderType"="Opaque" } 12 | LOD 100 13 | 14 | Pass 15 | { 16 | CGPROGRAM 17 | #pragma vertex vert 18 | #pragma fragment frag 19 | // make fog work 20 | #pragma multi_compile_fog 21 | 22 | #include "UnityCG.cginc" 23 | 24 | struct appdata 25 | { 26 | float4 vertex : POSITION; 27 | float2 uv : TEXCOORD0; 28 | }; 29 | 30 | struct v2f 31 | { 32 | float2 uv : TEXCOORD0; 33 | UNITY_FOG_COORDS(1) 34 | float4 vertex : SV_POSITION; 35 | }; 36 | 37 | sampler2D _MainTex; 38 | float4 _MainTex_ST; 39 | float3 _WallColour; 40 | float _texRes = 64.; 41 | float _exposure = 0.5; 42 | float _contrast = 1.0; 43 | 44 | v2f vert (appdata v) 45 | { 46 | v2f o; 47 | o.vertex = UnityObjectToClipPos(v.vertex); 48 | o.uv = TRANSFORM_TEX(v.uv, _MainTex); 49 | UNITY_TRANSFER_FOG(o,o.vertex); 50 | return o; 51 | } 52 | 53 | float3 ACESFilm( float3 x ) 54 | { 55 | float a = 2.51f; 56 | float b = 0.03f; 57 | float c = 2.43f; 58 | float d = 0.59f; 59 | float e = 0.14f; 60 | return saturate( (x*(a*x + b)) / (x*(c*x + d) + e) ); 61 | } 62 | 63 | fixed4 frag (v2f i) : SV_Target 64 | { 65 | // sample the texture 66 | fixed4 col = tex2D(_MainTex, i.uv); 67 | 68 | //float3 dd = float3(1. / _texRes, 0., -1. / _texRes); 69 | //col += tex2D( _MainTex, i.uv + dd.xy ); 70 | //col += tex2D( _MainTex, i.uv - dd.xy ); 71 | //col += tex2D( _MainTex, i.uv + dd.yx ); 72 | //col += tex2D( _MainTex, i.uv - dd.yx ); 73 | //col /= 5.; 74 | 75 | col.rgb *= _WallColour; 76 | 77 | if( _exposure == 0. ) _exposure = 0.5; 78 | 79 | col.rgb = log( 1 + col.rgb ) * _exposure; 80 | col.rgb = lerp( col.rgb, smoothstep( 0., 1., col.rgb ), _contrast ); 81 | 82 | // apply fog 83 | UNITY_APPLY_FOG(i.fogCoord, col); 84 | return col; 85 | } 86 | ENDCG 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Assets/Shaders/WallShader.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 887cbe83a77bd024382d9a61aee91c4c 3 | timeCreated: 1480803749 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Textures.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e93c73d35506d964f9132323a610f97c 3 | folderAsset: yes 4 | timeCreated: 1487838819 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Textures/free-pencil-image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/Assets/Textures/free-pencil-image.jpg -------------------------------------------------------------------------------- /Assets/Textures/free-pencil-image.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e33f6976c7491064b9cef9a1206f82ce 3 | timeCreated: 1480885709 4 | licenseType: Free 5 | TextureImporter: 6 | fileIDToRecycleName: {} 7 | serializedVersion: 2 8 | mipmaps: 9 | mipMapMode: 0 10 | enableMipMap: 1 11 | linearTexture: 0 12 | correctGamma: 0 13 | fadeOut: 0 14 | borderMipMap: 0 15 | mipMapFadeDistanceStart: 1 16 | mipMapFadeDistanceEnd: 3 17 | bumpmap: 18 | convertToNormalMap: 0 19 | externalNormalMap: 0 20 | heightScale: 0.25 21 | normalMapFilter: 0 22 | isReadable: 1 23 | grayScaleToAlpha: 0 24 | generateCubemap: 0 25 | cubemapConvolution: 0 26 | cubemapConvolutionSteps: 7 27 | cubemapConvolutionExponent: 1.5 28 | seamlessCubemap: 0 29 | textureFormat: -1 30 | maxTextureSize: 2048 31 | textureSettings: 32 | filterMode: -1 33 | aniso: -1 34 | mipBias: -1 35 | wrapMode: -1 36 | nPOTScale: 1 37 | lightmap: 0 38 | rGBM: 0 39 | compressionQuality: 50 40 | allowsAlphaSplitting: 0 41 | spriteMode: 0 42 | spriteExtrude: 1 43 | spriteMeshType: 1 44 | alignment: 0 45 | spritePivot: {x: 0.5, y: 0.5} 46 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 47 | spritePixelsToUnits: 100 48 | alphaIsTransparency: 0 49 | spriteTessellationDetail: -1 50 | textureType: 5 51 | buildTargetSettings: [] 52 | spriteSheet: 53 | serializedVersion: 2 54 | sprites: [] 55 | outline: [] 56 | spritePackingTag: 57 | userData: 58 | assetBundleName: 59 | assetBundleVariant: 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ClusterInputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/ClusterInputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 5.5.0f3 2 | -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityAdsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/UnityAdsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityConnectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/ProjectSettings/UnityConnectSettings.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lightflood 2 | Experimental progressive GI renderer in Unity using physics raycasts for light rays 3 | 4 | ![Teaser](https://raw.githubusercontent.com/huwb/lightflood/master/img/teaser.jpg) 5 | 6 | 7 | ## Overview 8 | This is a hacky experimental CPU progressive lightmapper. Geometry is represented as collideable quads. Quads because lightmap UVs become trivial, and collideable because rays are cast using Physics.Raycast. 9 | 10 | None of this is highly performant (although it does update interactively for small scenes), nor is it highly suitable for production scenarios in its current state. The primary goal here was to have fun and make something lightweight and easy to experiment with. 11 | 12 | 13 | ## Instructions 14 | The scene should 'just work', last opened in Unity 5.5. 15 | 16 | Enable or disable gameobjects in the scene to change it as desired - no scene configuration etc is required, everything is dynamic. 17 | 18 | You can fly around with WASD/arrow keys. Press space to shoot bouncy spheres of light. 19 | -------------------------------------------------------------------------------- /img/teaser.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huwb/lightflood/7ce082f17b978af32e0f4c0a235f77653a226770/img/teaser.jpg --------------------------------------------------------------------------------