Initial Commit

This commit is contained in:
Simeon Radivoev 2022-02-12 12:53:50 +02:00
commit ee5c2f922d
Signed by: simeonradivoev
GPG key ID: 7611A451D2A5D37A
2255 changed files with 547750 additions and 0 deletions

View file

@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace Light2D
{
public static class Light2DMenu
{
[MenuItem("GameObject/Light2D/Lighting System", false, 6)]
public static void CreateLightingSystem()
{
LightingSystemCreationWindow.CreateWindow();
}
[MenuItem("GameObject/Light2D/Light Obstacle", false, 6)]
public static void CreateLightObstacle()
{
var baseObjects = Selection.gameObjects.Select(o => o.GetComponent<Renderer>()).Where(r => r != null).ToList();
if (baseObjects.Count == 0)
{
Debug.LogError("Can't create light obstacle from selected object. You need to select any object with renderer attached to it to create light obstacle.");
}
foreach (var gameObj in baseObjects)
{
var name = gameObj.name + " Light Obstacle";
var child = gameObj.transform.Find(name);
var obstacleObj = child == null ? new GameObject(name) : child.gameObject;
foreach (var obstacleSprite in obstacleObj.GetComponents<LightObstacleSprite>())
Util.Destroy(obstacleSprite);
obstacleObj.transform.parent = gameObj.transform;
obstacleObj.transform.localPosition = Vector3.zero;
obstacleObj.transform.localRotation = Quaternion.identity;
obstacleObj.transform.localScale = Vector3.one;
obstacleObj.AddComponent<LightObstacleSprite>();
}
}
[MenuItem("GameObject/Light2D/Light Source", false, 6)]
public static void CreateLightSource()
{
var obj = new GameObject("Light");
if (LightingSystem.Instance != null)
obj.layer = LightingSystem.Instance.LightSourcesLayer;
var light = obj.AddComponent<LightSprite>();
light.Material = AssetDatabase.LoadAssetAtPath<Material>("Assets/Light2D/Materials/Light60Points.mat");
light.Sprite = Resources.Load<Sprite>("DefaultLight");
light.Color = new Color(1, 1, 1, 0.5f);
Selection.activeObject = obj;
}
[MenuItem("GameObject/Light2D/Enable 2DTK Support", false, 6)]
public static void Enable2DToolkitSupport()
{
var targets = (BuildTargetGroup[]) Enum.GetValues(typeof (BuildTargetGroup));
foreach (var target in targets)
DefineSymbol("LIGHT2D_2DTK", target);
}
[MenuItem("GameObject/Light2D/Disable 2DTK Support", false, 6)]
public static void Disable2DToolkitSupport()
{
var targets = (BuildTargetGroup[])Enum.GetValues(typeof(BuildTargetGroup));
foreach (var target in targets)
UndefineSymbol("LIGHT2D_2DTK", target);
}
public static void DefineSymbol(string symbol, BuildTargetGroup target)
{
UndefineSymbol(symbol, target);
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(target);
if (!defines.EndsWith(";"))
defines += ";";
defines += symbol;
PlayerSettings.SetScriptingDefineSymbolsForGroup(target, defines);
}
public static void UndefineSymbol(string symbol, BuildTargetGroup target)
{
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(target);
defines = defines.Replace(symbol + ";", "");
defines = defines.Replace(";" + symbol, "");
defines = defines.Replace(symbol, "");
PlayerSettings.SetScriptingDefineSymbolsForGroup(target, defines);
}
}
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ebdbe3567bf79fe4cb574c7f5f7be247
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View file

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace Light2D
{
public class LightingSystemCreationWindow : EditorWindow
{
private int _lightObstaclesLayer;
private int _lightSourcesLayer;
private int _ambientLightLayer;
public static void CreateWindow()
{
var window = GetWindow<LightingSystemCreationWindow>("Lighting system creation window");
window.position = new Rect(200, 200, 500, 140);
}
void OnGUI()
{
if (FindObjectOfType<LightingSystem>())
{
GUILayout.Label("WARNING: existing lighting system is found.\nIt is recommended to remove it first, before adding new one.", EditorStyles.boldLabel);
}
GUILayout.Label("Select layers you wish to use. You could modify them later in created object.");
_lightObstaclesLayer = EditorGUILayout.LayerField("Light Obstacles", _lightObstaclesLayer);
_lightSourcesLayer = EditorGUILayout.LayerField("Light Sources", _lightSourcesLayer);
_ambientLightLayer = EditorGUILayout.LayerField("Ambient Light", _ambientLightLayer);
if (GUILayout.Button("Create"))
{
var mainCamera = Camera.main;
var lighingSystem = mainCamera.GetComponent<LightingSystem>() ?? mainCamera.gameObject.AddComponent<LightingSystem>();
var prefab = Resources.Load<GameObject>("Lighting Camera");
var lightingSystemObj = (GameObject)Instantiate(prefab);
lightingSystemObj.name = lightingSystemObj.name.Replace("(Clone)", "");
lightingSystemObj.transform.parent = mainCamera.transform;
lightingSystemObj.transform.localPosition = Vector3.zero;
lightingSystemObj.transform.localScale = Vector3.one;
lightingSystemObj.transform.localRotation = Quaternion.identity;
var config = lightingSystemObj.GetComponent<LightingSystemPrefabConfig>();
lighingSystem.LightCamera = lightingSystemObj.GetComponent<Camera>();
lighingSystem.AmbientLightComputeMaterial = config.AmbientLightComputeMaterial;
lighingSystem.LightOverlayMaterial = config.LightOverlayMaterial;
lighingSystem.AmbientLightBlurMaterial = lighingSystem.LightSourcesBlurMaterial = config.BlurMaterial;
DestroyImmediate(config);
lighingSystem.LightCamera.depth = mainCamera.depth - 1;
lighingSystem.LightCamera.cullingMask = 1 << _lightSourcesLayer;
lighingSystem.LightSourcesLayer = _lightSourcesLayer;
lighingSystem.AmbientLightLayer = _ambientLightLayer;
lighingSystem.LightObstaclesLayer = _lightObstaclesLayer;
mainCamera.cullingMask &=
~((1 << _lightSourcesLayer) | (1 << _ambientLightLayer) | (1 << _lightObstaclesLayer));
Close();
}
}
}
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ad2fb8c16fef93e48b8b1174fe1392df
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View file

@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace Light2D
{
[CustomEditor(typeof(LightingSystem))]
public class LightingSystemEditor : Editor
{
private SerializedProperty _lightPixelSize;
private SerializedProperty _lightCameraSizeAdd;
private SerializedProperty _lightCameraFovAdd;
private SerializedProperty _enableAmbientLight;
private SerializedProperty _blurLightSources;
private SerializedProperty _blurAmbientLight ;
private SerializedProperty _hdr ;
private SerializedProperty _lightObstaclesAntialiasing;
private SerializedProperty _ambientLightComputeMaterial;
private SerializedProperty _lightOverlayMaterial;
private SerializedProperty _lightSourcesBlurMaterial;
private SerializedProperty _ambientLightBlurMaterial;
private SerializedProperty _lightCamera;
private SerializedProperty _lightSourcesLayer;
private SerializedProperty _ambientLightLayer;
private SerializedProperty _lightObstaclesLayer;
private SerializedProperty _lightObstaclesReplacementShaderLayer;
private SerializedProperty _lightObstaclesDistance;
private SerializedProperty _lightTexturesFilterMode;
private SerializedProperty _enableNormalMapping;
private SerializedProperty _affectOnlyThisCamera;
#if LIGHT2D_2DTK
private float _old2dtkCamSize;
private DateTime _sizeChangeTime;
#endif
void OnEnable()
{
_lightPixelSize = serializedObject.FindProperty("LightPixelSize");
_lightCameraSizeAdd = serializedObject.FindProperty("LightCameraSizeAdd");
_lightCameraFovAdd = serializedObject.FindProperty("LightCameraFovAdd");
_enableAmbientLight = serializedObject.FindProperty("EnableAmbientLight");
_blurLightSources = serializedObject.FindProperty("BlurLightSources");
_blurAmbientLight = serializedObject.FindProperty("BlurAmbientLight");
_hdr = serializedObject.FindProperty("HDR");
_lightObstaclesAntialiasing = serializedObject.FindProperty("LightObstaclesAntialiasing");
_ambientLightComputeMaterial = serializedObject.FindProperty("AmbientLightComputeMaterial");
_lightOverlayMaterial = serializedObject.FindProperty("LightOverlayMaterial");
_lightSourcesBlurMaterial = serializedObject.FindProperty("LightSourcesBlurMaterial");
_ambientLightBlurMaterial = serializedObject.FindProperty("AmbientLightBlurMaterial");
_lightCamera = serializedObject.FindProperty("LightCamera");
_lightSourcesLayer = serializedObject.FindProperty("LightSourcesLayer");
_ambientLightLayer = serializedObject.FindProperty("AmbientLightLayer");
_lightObstaclesLayer = serializedObject.FindProperty("LightObstaclesLayer");
_lightObstaclesReplacementShaderLayer = serializedObject.FindProperty("LightObstaclesReplacementShaderLayer");
_lightObstaclesDistance = serializedObject.FindProperty("LightObstaclesDistance");
_lightTexturesFilterMode = serializedObject.FindProperty("LightTexturesFilterMode");
_enableNormalMapping = serializedObject.FindProperty("EnableNormalMapping");
_affectOnlyThisCamera = serializedObject.FindProperty("AffectOnlyThisCamera");
}
public override void OnInspectorGUI()
{
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update();
if (Application.isPlaying)
GUI.enabled = false;
var lightingSystem = (LightingSystem)target;
var cam = lightingSystem.GetComponent<Camera>();
bool isMobileTarget = EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS ||
EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android;
if (cam == null)
{
EditorGUILayout.LabelField("WARNING: No attached camera found.");
}
EditorGUILayout.PropertyField(_lightPixelSize, new GUIContent("Light Pixel Size"));
bool sizeChanged = false;
#if LIGHT2D_2DTK
var tk2dCamera = lightingSystem.GetComponent<tk2dCamera>();
var tk2dCamSize = tk2dCamera == null
? (cam == null ? 0 : cam.orthographicSize)
: tk2dCamera.ScreenExtents.yMax;
var currSizeChanged = !Mathf.Approximately(tk2dCamSize, _old2dtkCamSize);
_old2dtkCamSize = tk2dCamSize;
if (currSizeChanged) _sizeChangeTime = DateTime.Now;
sizeChanged = (DateTime.Now - _sizeChangeTime).TotalSeconds < 0.2f;
#endif
if (cam != null)
{
float size;
if (cam.orthographic)
{
#if LIGHT2D_2DTK
float zoom = (tk2dCamera == null ? 1 : tk2dCamera.ZoomFactor);
size = (cam.orthographicSize*zoom + _lightCameraSizeAdd.floatValue) * 2f;
#else
size = (cam.orthographicSize + _lightCameraSizeAdd.floatValue)*2f;
#endif
}
else
{
var halfFov = (cam.fieldOfView + _lightCameraFovAdd.floatValue)*Mathf.Deg2Rad/2f;
size = Mathf.Tan(halfFov)*_lightObstaclesDistance.floatValue*2;
}
if (!Application.isPlaying)
{
int lightTextureHeight = Mathf.RoundToInt(size/_lightPixelSize.floatValue);
var oldSize = lightTextureHeight;
lightTextureHeight = EditorGUILayout.IntField("Light Texture Height", lightTextureHeight);
if (lightTextureHeight%2 != 0)
lightTextureHeight++;
if (lightTextureHeight < 16)
{
if (lightTextureHeight < 8)
lightTextureHeight = 8;
EditorGUILayout.LabelField("WARNING: Light Texture Height is too small.");
EditorGUILayout.LabelField(" 50-180 is recommended.");
}
if (lightTextureHeight > (isMobileTarget ? 200 : 400))
{
if (lightTextureHeight > 1024)
lightTextureHeight = 1024;
EditorGUILayout.LabelField("WARNING: Light Texture Height is too big.");
EditorGUILayout.LabelField(" 50-180 is recommended.");
}
if (oldSize != lightTextureHeight && !sizeChanged)
{
_lightPixelSize.floatValue = size/lightTextureHeight;
}
}
}
if (cam == null || cam.orthographic)
{
EditorGUILayout.PropertyField(_lightCameraSizeAdd, new GUIContent("Light Camera Size Add"));
}
else
{
EditorGUILayout.PropertyField(_lightCameraFovAdd, new GUIContent("Light Camera Fov Add"));
EditorGUILayout.PropertyField(_lightObstaclesDistance, new GUIContent("Camera To Light Obstacles Distance"));
}
EditorGUILayout.PropertyField(_hdr, new GUIContent("64 Bit Color"));
EditorGUILayout.PropertyField(_lightObstaclesAntialiasing, new GUIContent("Light Obstacles Antialiasing"));
EditorGUILayout.PropertyField(_enableNormalMapping, new GUIContent("Normal Mapping"));
if (_enableNormalMapping.boolValue && isMobileTarget)
{
EditorGUILayout.LabelField("WARNING: Normal mapping is not supported on mobiles.");
}
//EditorGUILayout.PropertyField(_affectOnlyThisCamera, new GUIContent("Affect Only This Camera"));
_lightTexturesFilterMode.enumValueIndex = (int)(FilterMode)EditorGUILayout.EnumPopup(
"Texture Filtering", (FilterMode)_lightTexturesFilterMode.enumValueIndex);
EditorGUILayout.PropertyField(_blurLightSources, new GUIContent("Blur Light Sources"));
bool normalGuiEnableState = GUI.enabled;
if (!_blurLightSources.boolValue)
GUI.enabled = false;
EditorGUILayout.PropertyField(_lightSourcesBlurMaterial, new GUIContent(" Light Sources Blur Material"));
GUI.enabled = normalGuiEnableState;
EditorGUILayout.PropertyField(_enableAmbientLight, new GUIContent("Enable Ambient Light"));
if (!_enableAmbientLight.boolValue)
GUI.enabled = false;
EditorGUILayout.PropertyField(_blurAmbientLight, new GUIContent(" Blur Ambient Light"));
var oldEnabled = GUI.enabled;
if (!_blurAmbientLight.boolValue)
GUI.enabled = false;
EditorGUILayout.PropertyField(_ambientLightBlurMaterial, new GUIContent(" Ambient Light Blur Material"));
GUI.enabled = oldEnabled;
EditorGUILayout.PropertyField(_ambientLightComputeMaterial, new GUIContent(" Ambient Light Compute Material"));
GUI.enabled = normalGuiEnableState;
EditorGUILayout.PropertyField(_lightOverlayMaterial, new GUIContent("Light Overlay Material"));
EditorGUILayout.PropertyField(_lightCamera, new GUIContent("Lighting Camera"));
_lightSourcesLayer.intValue = EditorGUILayout.LayerField(new GUIContent("Light Sources Layer"), _lightSourcesLayer.intValue);
_lightObstaclesLayer.intValue = EditorGUILayout.LayerField(new GUIContent("Light Obstacles Layer"), _lightObstaclesLayer.intValue);
EditorGUILayout.PropertyField(_lightObstaclesReplacementShaderLayer, new GUIContent("Light Obstacles Replacement Shader Layer"));
_ambientLightLayer.intValue = EditorGUILayout.LayerField(new GUIContent("Ambient Light Layer"), _ambientLightLayer.intValue);
// Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
serializedObject.ApplyModifiedProperties();
}
}
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 88da0bc46cb6e9b4b877ed03f2fae52b
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View file

@ -0,0 +1,69 @@
using UnityEditor;
using UnityEngine;
namespace Light2D
{
public class PassToggleDrawer : MaterialPropertyDrawer
{
private string arg;
public PassToggleDrawer()
{
}
public PassToggleDrawer(string arg)
{
this.arg = arg;
}
static bool IsPropertyTypeSuitable(MaterialProperty prop)
{
return prop.type == MaterialProperty.PropType.Float || prop.type == MaterialProperty.PropType.Range;
}
public override void OnGUI(Rect position, MaterialProperty prop, string label, MaterialEditor editor)
{
if (!IsPropertyTypeSuitable(prop))
{
return;
}
// Setup
bool value = (prop.floatValue != 0.0f);
EditorGUI.BeginChangeCheck();
EditorGUI.showMixedValue = prop.hasMixedValue;
// Show the toggle control
value = EditorGUI.Toggle(position, label, value);
EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck())
{
// Set the new value if it has changed
prop.floatValue = value ? 1.0f : 0.0f;
if (!string.IsNullOrWhiteSpace(arg))
{
foreach (Material target in prop.targets)
{
target.SetShaderPassEnabled(arg, value);
}
}
}
}
public override void Apply(MaterialProperty prop)
{
if (prop.hasMixedValue)
return;
if (!string.IsNullOrWhiteSpace(arg))
{
foreach (Material target in prop.targets)
{
target.SetShaderPassEnabled(arg, prop.floatValue != 0.0f);
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bdef1ebd119531d4fb637cba78d0da68
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: