Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

MeshInstanceRenderer - possibility to consider scale and color of the texture to be drawn?

Discussion in 'Graphics for ECS' started by FM-Productions, Jun 3, 2018.

  1. FM-Productions

    FM-Productions

    Joined:
    May 1, 2017
    Posts:
    72
    Hi,

    I wanted to start off by saying I'm a little mad - this is a repost because my previous post was deleted. The reason I got was "duplicate post", but I couldn't find my exact question and I have searched on the forum.
    The ECS is fairly new and I have a difficult time to find good resources and tutorials
    , so why deleting this post?

    Here is my original question:

    There are these requirements for my game and I would like to know if it is possible to achieve them with the MeshInstanceRenderer.

    So in my game I want to:
    - set position of a sprite
    - set the rotation of a sprite
    - set the scale of a sprite, uniform - one value for both axes
    - set the color of my sprite.

    - without the use of a GameObject.

    The SpriteInstanceRenderer that is available on Github here (https://github.com/paullj/unity-ecs-instanced-sprite-renderer) let's me scale the sprite with a little bit of tweaking but not rotate it.
    As for the size and color adjustment:
    One way I tried to do it was to render with batches of 1 and change the mesh for the size and the material for the color each time.
    As you can imagine, performance was abysmal.

    So do you have a hint how to go about this?
    I might have to resort to regular gameObjects for rendering again.

    Kind regards
     
    Last edited: Jun 3, 2018
    S_Darkwell likes this.
  2. Afonso-Lage

    Afonso-Lage

    Joined:
    Jul 8, 2012
    Posts:
    70
    As mentioned on previous post, you can se the color either on Mesh (by vertex basis) ou on Material, which will require a new material per color setting.

    And here is the scale system script again:

    Component:
    Code (CSharp):
    1. public struct Scale : IComponentData
    2. {
    3.     public float3 Value;
    4. }
    System:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using Unity.Collections;
    7. using Unity.Entities;
    8. using Unity.Mathematics;
    9. using Unity.Transforms;
    10.  
    11. public class ScaleSystem : ComponentSystem
    12. {
    13.     struct ScaleData
    14.     {
    15.         public int Length;
    16.         [ReadOnly] public ComponentDataArray<Scale> Scales;
    17.         public ComponentDataArray<TransformMatrix> Matrices;
    18.     }
    19.  
    20.     [Inject] private ScaleData _Data;
    21.  
    22.     protected override void OnUpdate()
    23.     {
    24.         for (var i = 0; i < _Data.Length; i++)
    25.         {
    26.             var baseMat = _Data.Matrices[i].Value;
    27.             var scaleMat = math.scale(_Data.Scales[i].Value);
    28.             var newMat = math.mul(baseMat, scaleMat);
    29.  
    30.             _Data.Matrices[i] = new TransformMatrix { Value = newMat };
    31.         }
    32.     }
    33. }
    34.  
     
    FM-Productions likes this.
  3. FM-Productions

    FM-Productions

    Joined:
    May 1, 2017
    Posts:
    72
    Awesome! Thanks again!

    So I have actually found a thread that covers scaling, together with your post.
    But I'm still unsure how to go about the colors. Since this is purely cosmetic, I'm fine though. I'll definitely look into the option to set the colors for the Mesh and apply a shader that takes it into account.

    Here is the scaling post:
    https://forum.unity.com/threads/transformmatrixcomponent-and-scaling.524054/
    And here is the gist on github of a full system:
    https://gist.github.com/JoeCoo7/f497af9b1ba2ab5babae3060635a9c6a

    @Afonso-Lage
    math.scale() works really well. One optimization though: I would make it a JobComponentSystem class, so that the calculations happen in parallel.
     
  4. FM-Productions

    FM-Productions

    Joined:
    May 1, 2017
    Posts:
    72
    Here is my script. My BulletBDStruct component has 2 separate scale values that have to be combined to get the real scale, a z-rotation (rotationVisual) and of course a position. The following job calculates the Matrix of the component.
    ModelMatrix is actually the same as TransformMatrix, the different struct is only used to be able to interact with different systems.

    Code (CSharp):
    1. public class BulletMatrixTransformJobSystem : JobComponentSystem
    2.     {
    3.  
    4.         [ComputeJobOptimization]
    5.         private struct BulletMatrixTransformJob : IJobProcessComponentData<BulletBDStruct, ModelMatrix>
    6.         {
    7.             public void Execute(ref BulletBDStruct bullet, ref ModelMatrix transform)
    8.             {
    9.                 float scale = bullet.baseScaling * bullet.scaleMultiplier;
    10.                 float3 scaleVec = new float3(scale, scale, scale);
    11.                 transform.Value = math.mul(math.rottrans(Quaternion.Euler(0, 0, bullet.rotationVisual), new float3(bullet.posX, bullet.posY, bullet.posZ)), math.scale(scaleVec));
    12.             }
    13.         }
    14.  
    15.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    16.         {
    17.             var job = new BulletMatrixTransformJob();
    18.             return job.Schedule(this, 20, inputDeps);
    19.         }
    20.     }
     
    Last edited: Jun 3, 2018
    Afonso-Lage likes this.
  5. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    327
    So, my assumption, is that your search scope was rather too focused on the words you were looking for where as other questions have been answered, that answer yours. I know personally I find myself searching this forum for questions via Google, since using the unity search feature gives far from ideal results :p

    Here are two examples I know of personally that matched what you were looking for in terms of a built in mechanics.

    https://forum.unity.com/threads/property-blocks-for-instanced-material-component.523094/
    https://forum.unity.com/threads/how-to-modify-the-scale-of-an-entitys-tranform.526219/

    I have also seen the "scaled" question about 3 times on this forum already. These features are coming, and while build-able solutions are doable, I wouldn't go so far as to say they are necessarily viable

    My recommendation for the future, would be to NOT repost without contacting the moderators as 1) they deleted it within the same day of you posting, so it is unlikely they wouldn't see your inquiry on elaborating why.
     
    FM-Productions and hippocoder like this.
  6. FM-Productions

    FM-Productions

    Joined:
    May 1, 2017
    Posts:
    72
    Okay, sorry. Usually I don't mind that much, but since I haven't gotten the chance to copy the useful Afonso-Lange posted, I was a bit startled. Luckily I had an older version of the thread opened in another tab, so I could see his user tag and write him a message about the code he posted.

    And thanks for posting the link the the property blocks thread. I think that is what I would have to use in the future for color swaps. But I would have never looked for those keywords.

    As for the scaling problem, I am very satisfied with the solution I posted above. position, rotation and scale are applied to the ModelMatrix and rendered by a custom system that works very similar to the regular MeshInstanceRendererSystem I think. The system was shared by a community member here: https://gist.github.com/JoeCoo7/f497af9b1ba2ab5babae3060635a9c6a#file-renderersystem
     
  7. FM-Productions

    FM-Productions

    Joined:
    May 1, 2017
    Posts:
    72
    So to conclude this thread, some awesome people from this forum helped me to solve the color problem by using a MaterialPropertyBlock as parameter for the DrawMeshInstanced function. You can find the solution in this thread:
    https://forum.unity.com/threads/instanced-sprite-renderer-example.525235/#post-3523381

    And the repository for a sprite rendering system that can manage scale and color here:
    https://github.com/Necromantic/ECS-Sandbox

    A small showcase can be found here:


    Thanks to @Necromantic and @Rennan24 for helping me out!
     
    Last edited: Jun 7, 2018
    Covfefeh likes this.