Is it possible to change the length of shadows. For example when you add a 2d point light to scene, the sprites shadows length extend to infinity and its not a desirable effect in a top down rpg. So is there any way you can change its length or do i have to create my own sprite shadows in this case. Thanks in advance.
Same problem. Iâm using a point light to simulate the sunlight, and the shadow just extend to infinity on everything.
Hope they would add an attribute or something in the next update.
This is actually something I wanted to do, but Iâm hesitant to add something official only because the shadows need to be reworked. If you wanted to make the changes yourself, I can try and give you some advice on how to do this.
Thx for the reply.
I was wondering if there is any script or shader way to make it, would you like to give me some advice?
Maybe for meantime there should be a personal refactor of yours that we can fiddle more into? I am interested in this also.
Bump.
Also looking for a solution
It has been some time since 2drenderer team has responded to the feedbacks since the last official demo. We would like to see some respond to :
- Shadow support for tilemap
- Soft Shadow
- Missing Emission channel
- Adjustable shadow length
- Directional light type
- Self-Shadow is practically not usable due to its inaccuracy with the source sprite
And for the last but not least, the way the light layer works, because 2drenderer is rendering light buffer per layer it is affecting it, this is far too performance consuming. Maybe there canât be anything about this, but stillâŚ
Bump. Iâm also stuck with it
I manipulated the FreeForm of the 2D Light to create a polygon in the limits of the collision enter.
But its doesnât the better solution of my problem, because I want use the properties existentes in the Point type, for example.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;
# if UNITY_EDITOR
using UnityEditorInternal;
# endif
[ExecuteAlways]
[RequireComponent(typeof(Light2D))]
public class Light2DAdaptative : MonoBehaviour
{
[Header("Custom Attributes")]
[SerializeField]
[Min(3)]
private int sides = 4;
[SerializeField]
[Min(0.01f)]
private float radius = 1f;
[SerializeField]
[Range(0, 359)]
private int angle = 45;
[SerializeField]
private LayerMask layersFilter;
[Header("Base Attributes")]
[SerializeField]
[Min(0)]
private int lightOrder = 0;
[SerializeField]
[ColorUsage(false)]
private Color color = Color.white;
[SerializeField]
[Range(0, 1f)]
private float shadowIntensity = 0f;
private Light2D currentLight;
public Light2D Light
{
get
{
if (this.currentLight == null)
{
this.currentLight = GetComponent<Light2D>();
this.currentLight.lightType = Light2D.LightType.Freeform;
}
return this.currentLight;
}
}
private void OnValidate()
{
ChangeProperties();
TryResizeLight();
}
private void OnEnable()
{
#if UNITY_EDITOR
ComponentUtility.MoveComponentUp(this);
#endif
TryResizeLight();
}
private void Update()
{
TryResizeLight();
}
private void ChangeProperties()
{
Light.lightOrder = this.lightOrder;
Light.color = this.color;
Light.shadowIntensity = this.shadowIntensity;
}
private void TryResizeLight()
{
var lightNaturalPoints = this.Light.shapePath.Length;
if (this.sides != lightNaturalPoints)
{
this.sides = lightNaturalPoints;
Debugs.Log("Create or reduce more points in Freeform light 2d component to see the correct draw form");
}
ResizeLight();
}
private void ResizeLight()
{
var localRadius = this.radius;
var sides = this.sides;
var baseAngle = Mathf.PI * 2 / sides;
var offsetAngle = Mathf.Deg2Rad * this.angle;
for (int i = 0; i < sides; i++)
{
var localAngle = baseAngle * i;
var position = new Vector3(Mathf.Cos(localAngle + offsetAngle), Mathf.Sin(localAngle + offsetAngle), 0f) * localRadius;
RaycastHit2D value = Physics2D.Raycast(transform.position, position, localRadius, this.layersFilter);
var relativePosition = transform.localPosition;
if (value.collider != null)
{
Vector3 transportPoint = transform.InverseTransformPoint(value.point);
relativePosition += transportPoint;
}
else
{
relativePosition += position;
}
this.Light.shapePath[i] = relativePosition;
}
}
}
With that, I can manipulate the polygon precision, changing the side number. With that, I can change the shadow, because I donât project the light in the wall. I create a custom polygon that create the light until the wall.
[OBS]
- I have errors with that, when the light throut the wall collider
I know this is a bit of an old topic but could you give some hints as to where to start with something like this? I tried looking into it myself but ended up a bit lost; I plan to dig into it a bit more soon but would appreciate the help in getting started, or even a small sample project/script if possible as MrPaparoz mentioned.
The current 2D lighting support seems extremely limited in where it would be applicable, and offering a way to limit the shadow length would go a long way in improving its usability for top-down games (and other projects). The game Iâm working on at the moment is an open world top-down sandbox/RPG, and having dynamic shadows would be a huge help in making the world feel more âaliveâ (something thatâs a pretty big focus in this game). Unfortunately having a pot cast an infinite shadow would be incredibly immersion breaking, but having it not cast a shadow at all while taller objects (like trees) do just makes everything feel disjointed.
Not having any way to make the sun cast shadows really limits its usefulness as well; the entire world looks flat unless the player is forced to always be carrying a torch (which doesnât make sense given the presence of a day/night system), and using a large light just outside of the camera as a fake sun doesnât work because all that does is cast a bunch of infinite shadows.
While I understand it would be a lot of work to implement, I do think a rework of the shadows makes sense; to be honest even aside from my current project, I have a hard time thinking of any way to ever use the current setup without a) limiting myself to only tall objects of simple shapes (pillars, perfectly straight trees, etc) where infinite shadows make sense (too simplistic/limited of a game world) or b) using it exclusively for line of sight (preventing players from seeing whatâs around the corner) which I could do without lighting/shadows anyway. In all other use cases I can think of (even outside of top-down games) the current shadows just wouldnât cut it, because the only two options are âno shadowâ and âinfinite shadowâ with nothing in between.
Some features Iâd love to see:
-Allow setting a shadow length per object as suggested in this topic (bare minimum to make it usable imo)
-Ambient/sun shadows (give the object a âheightâ value, give the global light a âsun directionâ value, small shadows are cast based on objectâs height value and the time of day)
-A âheightâ value for both shadow casters and light sources; this would be a better way of implementing shadow lengths (and perhaps integrated with ambient/sun shadows as well by giving the sun/global light a height value), for example if an object has a height of 2 and the playerâs torch has a height of 5, the shadow is shorter depending on how close the player is, and if the object has a height of 10, the shadow is infinite
-Some way to provide an actual shape for the shadow being cast? For example an additional texture that could be stretched/skewed depending on light direction/height values, so more complex shapes could have more realistic shadows (so a dead treeâs shadow could show the branches as well etc); this would be the Holy Grail imo, though Iâm not sure it would even be possible to implement in a way that worked well across various use cases and/or performed well
The first two features would be enough to meet the needs of my current project (preferably implemented using the 3rd idea for more control over how it works, and more realism when multiple light sources exist at different heights like street lamps vs torches), and would make the 2D lighting support a lot more widely viable than I think it is currently. The last idea likely isnât possible/practical to implement but I figured Iâd throw it out there anyway just in case (it would be pretty cool to have â3Dâ/shaped shadows in a 2D project).
This ended up being a longer post than I meant it to be (oops), but dynamic lighting is something Iâd love to use for a lot of the game ideas I have, and itâll likely be some time before Iâm able to work on 3D projects, so seeing some improvements to the 2D Lighting system (or at least having a better idea of where to start in making a custom one) in the meantime would be great.
bump, Iâm also looking for to set the shadow caster 2D with height
anyone has any ideas how to make this? tks
There are actually two other threads expressing interest in a solution to this hurdle.
https://forum.unity.com/threads/shadow-caster-2d-component-shadow-too-long.1019599/
I was wondering if some sort of clipping code might be the easiest way to create a workaround.
ShadowCaster2D needs some love. Interesting to hear it needs reworking because there isnât too much there, at the surface anyway. For example, even the _shapePathField is hidden and I had to use reflections to change the value.
I think thereâs was a capability to vote for improvements somewhere on the site. Would that help to bump the internal priority of this?
You are correct.
As far as the responses from other Unity departments I have talked to, they have said: âyes, this is the best way to signal a featureâs priority to usâ (or something along the lines of that).
Everybody, send your feature requests!
I have submitted mine for :
Emissive Channel
Tilemap Shadow (soft)
Ability to write to both Normal and Light buffer
you can check out the urp codes, and find the _ShadowRadius parameter which current decided by lightsâ bounding sphere. Iâm trying to add some data into the shadow mesh and change this value in âHidden/ShadowGroup2Dâ, this should work
I have a top view game and also need to limit the length of the shadow (i.e. the âheightâ of the sprite casting the shadow)
Iâll look into this next week
any news about this ?
Iâm also interested in shadow length.