Search Unity

dynamic batching particle,rotate wrong

Discussion in 'Shaders' started by dreamerflyer, Jul 15, 2015.

  1. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Code (CSharp):
    1. half3x3 roMatrixZ={
    2.                       half3(cos(dxTime),-sin(dxTime),0),
    3.                       half3(sin(dxTime),cos(dxTime),0),
    4.                       half3(0.0,0.0,1.0)
    5.                   };
    6.               half3 p=pos.xyz;
    7.          pos.xyz=mul(p,roMatrixZ);
    8.          o.pos = mul(UNITY_MATRIX_MVP,pos);
    When camera see both particles,it seems particlses rotate around camera,not around themself,strange....what is wrong with this?
     
  2. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
  3. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Can you just use the built in rotation over lifetime parameter in the unity particle system?
     
  4. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    This is custom particle system which moving in shader,not unity built in particle system.
     
  5. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    can you rotate the UV in the vertex shader?
    Code (CSharp):
    1.  
    2.  
    3. float2 uvrot = v.texcoord - 0.5;
    4. float sinX = sin(angleInRadians);
    5. float cosX = cos(angleInRadians);
    6. float sinY = sin(angleInRadians);
    7. float2x2 rotationMatrix = float2x2(cosX, -sinX, sinY, cosX);
    8. o.rotatedUV = mul(uvrot, rotationMatrix) + 0.5;
     
  6. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    i rotate the position, not uv.if u want rotation uv, u can.
     
  7. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    any body help?this is unity bug??
     
  8. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Are the particles in viewspace not worldspace? Also dynamic batching changes the vertex positions.
     
  9. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    yes,it seems like that,how to fixed?I don't want to update the position in CPU,C# calculates so many positions not good...
     
  10. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    any help??this is a bug??
     
  11. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Any help?
     
  12. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
  13. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Nobody help?
     
  14. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Day day up
     
  15. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    this is not unity bug???confused..
     
  16. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    what 's up?
     
  17. popupAsylum

    popupAsylum

    Joined:
    Apr 7, 2014
    Posts:
    119
    Looks like you're rotating the particles around the pivot of the model, when something's is dynamically batched its pivot becomes the camera as multiple meshes are batched to 1 model and can't have multiple pivots, vertex positions are modified so they keep the same world position but are now relative to the camera.

    You could store the pivot of rotation in the vertex positions i.e. (0,0,0) and use other data to store the vertex positions, such as vertex color, as this is unmodified by batching. The shader might look something like this (untested);

    Code (CSharp):
    1. half3x3 roMatrixZ={
    2.     half3(cos(dxTime),-sin(dxTime),0),
    3.     half3(sin(dxTime),cos(dxTime),0),
    4.     half3(0.0,0.0,1.0)
    5. };
    6. half3 p = v.color.xyz;
    7. pos.xyz = mul(p,roMatrixZ) + v.vertex;
    8. o.pos = mul(UNITY_MATRIX_MVP,pos);
    Then the pivot should be correct, you'll still have the issue of it rotating around the camera's axis though, you could maybe replace half3(0.0,0.0,1.0) with a shader property?
     
  18. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    thanks reply~~ this idea maybe can work if use native c++.i think when the batching particles is moving in runtime, if i use c# to change the position then transite to shader,the performance is not good,and if i use c++ dll to moving,maybe worse.. So, whether this is unity BUG?can fixed in lower_level?
     
  19. popupAsylum

    popupAsylum

    Joined:
    Apr 7, 2014
    Posts:
    119
    that shouldnt need any plugin, just a change to your mesh to write vertex positions to vertex color and write (0,0,0) to positions, this can be done though scripting.

    its not really a bug just a caveat of the batching system,you could also stop them batching by giving them diffeeent materials
     
  20. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    I known your meaning,I think about update the vertices position saved to color in runtime,when moving particles systems,the performance maybe have issue?
     
  21. popupAsylum

    popupAsylum

    Joined:
    Apr 7, 2014
    Posts:
    119
    Oh yeah updating the positions at runtime in the update loop might perform better if it were done in a plugin, but that's a separate issue from the rotation
     
  22. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    May have other better idea? I want to scale,change color,rotate the particle systems,the dynamic batch will break that.It 's very difficult to do that....
     
    Last edited: Jul 27, 2015
  23. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    any help?
     
  24. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    Can get the base position when batching? if can get that,maybe like this: initPos=v.vertex-batchingbasepos;
    mul(initPos,roMatrixZ)+batchingbasepos ;will work.
     
    Last edited: Jul 31, 2015
  25. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    any help?
     
  26. RepoGames

    RepoGames

    Joined:
    Apr 15, 2016
    Posts:
    69
    Same issue now ;) If performance is not an issue you can try "DisableBatching" = "True" in the tag section, but for me performance matters so I can't do it.

    EDIT: Unity now allows GPU Instancing. It solved problem
     
    Last edited: Jan 23, 2018
  27. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    en ,unity 2018.1 version will work