Unity-Technologies / 2d-extras

Fun 2D Stuff that we'd like to share!
Other
1.54k stars 342 forks source link

RuleTile rotation of GameObjects on XZY tilemap #201

Open Sebvandenbrink opened 4 years ago

Sebvandenbrink commented 4 years ago

I very much enjoy this package. However, the Matrix4x4 rotation of gameObjects has me stumped and I am having a hard time coming to grips with the mathematical concept behind the thing.

I have a swizzled tilemap (XZY instead of XYZ) and when I add gameObjects to my RuleTile tiles (some of which can be rotated), the instantiated prefabs are rotatedly incorrectly.

Is there a way to customize the RuleTile script so it checks for this swizzle? Or a way to customize the bit of the RuleTile script where it sets the GameObject's Quaternion.

Rotation maths often boggles my mind, for some reason.

I need the objects to be rotation around the Y-axis, clearly it does it incorrectly currently, screenshot: https://www.sebasvandenbrink.nl/wp-content/uploads/2020/05/unity_swizzleRuleTile.png

Thank you!

Sebvandenbrink commented 4 years ago

I finally managed to hit upon the correct solution; I changed RuleTile.cs 's line 143 as follows to get it to play along nicely with a swizzle set to XZY:

I switched the upward and forward Vector3s, and then within those two new Vector3 declarations, I switched the Y and Z values. To summarize, I changed the line from: m_GameObjectQuaternion = Quaternion.LookRotation(new Vector3(transform.m02, transform.m12, transform.m22), new Vector3(transform.m01, transform.m11, transform.m21)); To: m_GameObjectQuaternion = Quaternion.LookRotation(new Vector3(transform.m01, transform.m21, transform.m11), new Vector3(transform.m02, transform.m22, transform.m12));

Rotations and Quaternions continue to stump me 😆 but it would be great if the RuleTile could somehow automate instantiating gameObjects in concurrence with the Tilemap's swizzle, regardless of its setting.

ChuanXin-Unity commented 4 years ago

Sorry for the late reply! We will take a look at this to handle this case better.

ChuanXin-Unity commented 4 years ago

Hi, I've added a branch (https://github.com/Unity-Technologies/2d-extras/tree/ruletileswizzle) which could help with this issue hopefully. There is a new option "Apply GameObject Transform" which will handle and apply the original GameObject's Transform if set as well as the orientation issue.

Let me know if this works out for you!

awesomez commented 3 years ago

Hi, I've added a branch (https://github.com/Unity-Technologies/2d-extras/tree/ruletileswizzle) which could help with this issue hopefully. There is a new option "Apply GameObject Transform" which will handle and apply the original GameObject's Transform if set as well as the orientation issue.

Let me know if this works out for you!

Can this be set to remove the 2D tile sprite from the scene as well? -- i.e. I have a 3D tile which only uses the 2D tile as a proxy to pick it from the tileset/palette (where I'd preferably have an icon of the 3D prefab/mesh).

Essentially I want to replace the 2D tiles with 3D meshes/material instances while using the autotile rules/grid normally otherwise. Instead of 2D tiles, my palette would consist of 3D icons / prefabs and use 2D autotile rules.

ChuanXin-Unity commented 3 years ago

It would likely need some code changes to do so.

There is a simple workaround for the SceneView since you will be using instanced GameObjects from these RuleTiles. You could disable or remove the TilemapRenderer component in the Player since it is not used for rendering the 3D meshes. You could keep it in the Editor for level designing purposes or debugging if needed.

Let me know if this works out for you!

awesomez commented 3 years ago

It would likely need some code changes to do so.

There is a simple workaround for the SceneView since you will be using instanced GameObjects from these RuleTiles. You could disable or remove the TilemapRenderer component in the Player since it is not used for rendering the 3D meshes. You could keep it in the Editor for level designing purposes or debugging if needed.

Let me know if this works out for you!

Thanks, @ChuanXin-Unity -- It might work for the short-term (early prototype) stages, but I really need to visualize the height in the Editor (for design purposes).

I can easily see myself needing to visualize the 3D tiles / rotations in the Editor a bit better since I am oftentimes going to need to "layer" these 3d tiles too (just as I often need to do with 2d tiles -- except I can rely on the illusion of height there.) In my use-case, I'll be creating additional tilemaps, incremented by some distance 'higher' than the "base" tilemap's 'height' (in order to visually "stack" mountains/rocks/buildings/facades of varying heights / dimensions on terrain with true Y height) and, therefore, I need a faster way to do this while visualizing this in the Editor. I am aiming at mixing existing 2d sprite-based tileset visuals with 3d volumetric tiles for a unique 2d look, so it would be extra-nice to have 3d tile support in the existing 2d autotile system's package, since the two function (mostly) identical, except for visuals. Ideally, if there was only one change (aside from visuals) I'd appreciate most of all, it would be the ability to add fake 'volumes' of 3d tiles that execute their autotile rules on a series of 2d tilemap layers / 'slices' starting from a 'base' tilemap layer. In other words, add a tile in a specific x/z position to multiple tilemaps at once, if they are stacked upon one another, all the way up to the height of the last tilemap above the current layer (if the user chooses to make their fake 'volume' that tall) -- essentially acting as a fake 'volume' that consists of several tilemap layers originating at the same origin point. This, ideally, would be calculated from a base-layer, so I can handle adding very "tall" structures as quickly as I can "flat" structures (as long as I have enough "stacked" tilemap 'layers' for my 3d tiles -- otherwise the volume operation would fail. This would probably just need to check if there is a tilemap grid in the scene that is 1 unit above the current / existing tilemap you're painting on -- if so, try to add the tiles in its respective 2d array, until the 'volume' has been "filled" across the height requested from the 'volume' executed from the currently-selected tilemap 'slice', no matter which layer height you're currently on.

The goal is that this will let me use "Snaps-like" 3d modular assets more easily alongside modular 2d graphics, and even combine them with 2d tilesets for a unique look.

Currently level design is hell without a better tool for 3d autotiling, and the closest thing Unity has for something like this is this 2d toolset. ProGrids is not really setup for this kind of thing, and I really just need something better. The tilemap autotiles feature is the closest thing Unity has to what I need. :(

ChuanXin-Unity commented 3 years ago

You could stack Tiles onto the Tilemap with a different Z position instead of stacking multiple Tilemap GameObjects. This can be done with the Active Brush Inspector (https://docs.unity3d.com/Manual/Tilemap-Painting.html). There was an experiment awhile back with a 3D version of the RuleTile (https://www.youtube.com/watch?v=R9qeFWsXqKE&ab_channel=JuhaKiili), which could be what you are looking for, although making Rules for that became too complicated for usage.

I can easily see myself needing to visualize the 3D tiles / rotations in the Editor a bit better since I am oftentimes going to need to "layer" these 3d tiles too (just as I often need to do with 2d tiles -- except I can rely on the illusion of height there.) In my use-case, I'll be creating additional tilemaps, incremented by some distance 'higher' than the "base" tilemap's 'height' (in order to visually "stack" mountains/rocks/buildings/facades of varying heights / dimensions on terrain with true Y height) and, therefore, I need a faster way to do this while visualizing this in the Editor. I am aiming at mixing existing 2d sprite-based tileset visuals with 3d volumetric tiles for a unique 2d look, so it would be extra-nice to have 3d tile support in the existing 2d autotile system's package, since the two function (mostly) identical, except for visuals.

If you could explain more about what kind of visualisation you would be expecting, that would be great! Having graphical examples would be nice too!

johnsoncodehk commented 3 years ago

Woo the 3d rule tile is so cool! I want to think about whether it can be implemented on "2d-extras".

awesomez commented 3 years ago

You could stack Tiles onto the Tilemap with a different Z position instead of stacking multiple Tilemap GameObjects. This can be done with the Active Brush Inspector (https://docs.unity3d.com/Manual/Tilemap-Painting.html). There was an experiment awhile back with a 3D version of the RuleTile (https://www.youtube.com/watch?v=R9qeFWsXqKE&ab_channel=JuhaKiili), which could be what you are looking for, although making Rules for that became too complicated for usage.

I can easily see myself needing to visualize the 3D tiles / rotations in the Editor a bit better since I am oftentimes going to need to "layer" these 3d tiles too (just as I often need to do with 2d tiles -- except I can rely on the illusion of height there.) In my use-case, I'll be creating additional tilemaps, incremented by some distance 'higher' than the "base" tilemap's 'height' (in order to visually "stack" mountains/rocks/buildings/facades of varying heights / dimensions on terrain with true Y height) and, therefore, I need a faster way to do this while visualizing this in the Editor. I am aiming at mixing existing 2d sprite-based tileset visuals with 3d volumetric tiles for a unique 2d look, so it would be extra-nice to have 3d tile support in the existing 2d autotile system's package, since the two function (mostly) identical, except for visuals.

If you could explain more about what kind of visualisation you would be expecting, that would be great! Having graphical examples would be nice too!

@ChuanXin-Unity

Sorry for the lack of response! -- I had hoped to get a proper visualization to you by now!

And yes! -- The 3d rule tiles were definitely what I was looking at as being possible in Unity. I had been trying to do a system like that for some time, but I could never get a workable model until recent years. Although you said your UX would have been too complex, at first glance, I can see how this might appear true. But I beg to differ. Your basic 3d tile UI definitely looks usable already, but with a modification or two to the workflow itself, I should be able to make your basic UI handle much more complex use-cases!

I've got some drawings I'll do when I have more time this week that will lay out what can be done to accomplish these sorts of tiles. I'll also try to find the drawings I did on the volumetric approach I was referring to previously to give you an idea of what I'm thinking.

dongfangliu commented 2 years ago

= Quaternion.LookRotation(new Vector3(transform.m01, transform.m21, transform.m11), new Vector3(transform.m02, transform.m22, transform.m12));

Hi, i've checked your branch, that not working on the rotated case.

ChuanXin-Unity commented 2 years ago

Hi, i've checked your branch, that not working on the rotated case.

Hi could you share an example of what isn't working, such as a screenshot or a quick video? Thanks!

dongfangliu commented 2 years ago

Try use a gameobject that the face normal is y-axis. Then use a rule tile and set the gameobject as part of the rule. Set the rule as "rotated" and make a rotated tile on the map, you will find the orientation is still not that correct. This is due to the fact that ruletile.cs(line 614) only rotates on the z-axis.

So, to work around, i use default xyz layout and xy orientation, and make all my tile objects z- axis up.

Hope that will help.


发件人: ChuanXin-Unity @.*** 发送时间: 2022年3月23日 16:59 收件人: Unity-Technologies/2d-extras 抄送: 刘文基; Comment 主题: Re: [Unity-Technologies/2d-extras] RuleTile rotation of GameObjects on XZY tilemap (#201)

Hi, i've checked your branch, that not working on the rotated case.

Hi could you share an example of what isn't working, such as a screenshot or a quick video? Thanks!

― Reply to this email directly, view it on GitHubhttps://github.com/Unity-Technologies/2d-extras/issues/201#issuecomment-1076113241, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AFAEFRZSPVPNCIGJZQON5YDVBLMOHANCNFSM4M5JIRWA. You are receiving this because you commented.Message ID: @.***>

ChuanXin-Unity commented 2 years ago

Hi, I don't think we are getting this unfortunately. If you could provide an example project, send it using the Unity Bug Reporter and post the case number here, that would be very helpful!

Radamnation commented 2 years ago

Hi, I can't find the option to select the "Apply GameObject Transform" option in the RuleTile. Is there something special I need to do to access this option? Will the branch ever be merged with 2d-extras?

awesomez commented 2 years ago

Hi, I don't think we are getting this unfortunately. If you could provide an example project, send it using the Unity Bug Reporter and post the case number here, that would be very helpful!

He is referring to the 3d tiles rotation most likely. This is indeed a problem when trying to use this for 2D "tiles" that use a 3D model instead. :(

Radamnation commented 2 years ago

He is referring to the 3d tiles rotation most likely. This is indeed a problem when trying to use this for 2D "tiles" that use a 3D model instead. :(

Oh, I see... Well I'll try to implement dongfangliu's solution then, not sure if this will be possible in my case. Thanks!

ChuanXin-Unity commented 2 years ago

@Radamnation You should get this option only if you use the RuleTile from the ruletilesswizzle branch (https://github.com/Unity-Technologies/2d-extras/tree/ruletileswizzle). If you have an example project where this does not work, do share it with us using the Unity Bug Reporter and post the case number here. The unfortunate reason why we are not merging this branch to main is because we do not have exact cases to validate that this would fix the issue. It is also possible that the code in the branch may not suit your purposes as well, so providing examples would really help us in determining the issue/s.

Radamnation commented 2 years ago

@ChuanXin-Unity I ended up using @Sebvandenbrink solution, and it worked perfectly for me. To give you more details, we're designing a 2.5D game and using three different tilemap (Floor, Environment and Ceiling) to build our levels. Everything is exactly 1x1x1 unit big in 3D and we use the RuleTile to place 3D objet in our scene. The problem was that for something like a thin wall (1x1x0.5 units), the rotation would always be messed up for the gameObject. Seb's solution fixes that, thanks again Seb!

MimiVRC commented 1 year ago

none of these solutions seem to work anymore using unity 2022.1.7f1 with 2D Tilemap Extras 3.0.2. The lines Sebs suggestions do not exist anymore/have changed to

gameObjectRotation = Quaternion.LookRotation(new Vector3(transform.m02, transform.m12, transform.m22), new Vector3(transform.m01, transform.m11, transform.m21));

changing this to sebs changes does not fix it.

the package from the other branch is also pretty outdated compared to 3.0.2.

no idea how this problem is over 2 years old, with a fix, and it's not fixed in unity itself.

GregsGrog commented 1 year ago

This seems to still not be fixed and solutions still dont work. Is there any suggestions on this?

ChuanXin-Unity commented 1 year ago

This seems to still not be fixed and solutions still dont work. Is there any suggestions on this?

Hi, if you could file a bug report with a sample reproduction of what you are facing using the Unity Bug Reporter and post the case number here, that would be helpful! This would help us validate the problem here. Up to date, there hasn't been any of these reports, which makes it hard for us to ensure that we did not miss something in our solution. Thanks!

GuiTeK commented 1 year ago

I think I'm experiencing a related bug that I'm describing on the Unity Forums: https://forum.unity.com/threads/3d-tile-on-tilemap-does-not-take-into-account-gameobject-rotation.1421633/

Can you confirm this is a bug @ChuanXin-Unity?

ChuanXin-Unity commented 1 year ago

@GuiTeK The issue described in your link is expected for the GameObject Brush, since the transform settings in the GameObject Brush will take precedence.