Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Dynamic shape path for Lights

Discussion in '2D Experimental Preview' started by CraftedGaming, Jun 20, 2020.

  1. CraftedGaming

    CraftedGaming

    Joined:
    Jun 9, 2017
    Posts:
    37
    I intend to update the Lights.shapePath only to find out it's only a readonly. I don't want to burden myself with adding a lot of dots whenever I update the number of Vector3 in the shape path. This is because I intend to update the shape path via script. can we request for this to be available on the next update?
     
    Last edited: Jul 29, 2020
  2. CraftedGaming

    CraftedGaming

    Joined:
    Jun 9, 2017
    Posts:
    37
    I would like to ask if this problem was fixed in the latest Unity 2020.1 version?
     
  3. CraftedGaming

    CraftedGaming

    Joined:
    Jun 9, 2017
    Posts:
    37
  4. elZach

    elZach

    Joined:
    Apr 23, 2017
    Posts:
    48
    @Chris_Chu any input on why this is a problem?
     
  5. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    There should be no problem at editor time, but run time shape tesselation will be expensive.

    We will be reviewing all our member variables, member functions, and properties to see which can be made public sometime before URP 12.
     
    elZach and NotaNaN like this.
  6. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    For anyone looking for a solution to this, I have handcrafted a workaround to make this somewhat functional.
    Using reflection I can force the internal mesh to update with a given outer path:

    Code (CSharp):
    1.  
    2.         var privateInstanceFlags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
    3.  
    4.         var lightType = Light.GetType();
    5.         var shapeProperty = lightType.GetField("m_ShapePath", privateInstanceFlags);
    6.         shapeProperty.SetValue(light, lightPath);
    7.  
    8.         lightType.GetMethod("UpdateMesh", privateInstanceFlags).Invoke(light, null);
    Given that light is a reference to a Light2D instance and lightPath is the array of Vector3's you want to asign as outer path.

    Using this at runtime and updating it every frame is not cheap, however, I see a lot of room for improvement when profiling:

    Most notably, the internal LightUtility.GenerateShapeMesh method seems to generate a lot of allocations which should not be necessary. Presumably this is caused by using the LibTessDotNet library instead of a custom implementation.

    @Chris_Chu Will you expose and optimise this code path in the near future?
     
    NotaNaN and elZach like this.