Adam's Lair Forum

game development and casual madness
It is currently 2017/12/16, 09:02

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: 2017/02/15, 17:03 
Newbie
Newbie

Joined: 2017/01/15, 18:59
Posts: 4
Location: Germany
Role: Hobbyist
Hello friendly people of this lair,

I have started fiddling around with Duality and stumbled over some things regarding Tilemaps. First of all here is the code I wrote To create an empty Tilemap and store it as a prefab in the contentprovider + a Method for Creating a TileSet:
Code:
    public class TileClusterPart // creates the subgameobject of the tilemap as prefab on instantiation
    {
        private string tileMapName;
        ContentRef<Prefab> p;
        ContentRef<Tileset> ts;
        public TileClusterPart(string tileMapName, Transform t, ContentRef<Pixmap> defaultTiles,GameObject parent)
        {
            GameObject g = new GameObject(tileMapName, parent);
            this.tileMapName = tileMapName;
            this.ts = CreateTileSet("defaultTest", defaultTiles);
            Tilemap tm = new Tilemap();
            tm.Resize(128, 64);
            tm.Tileset = ts;
            TilemapRenderer tr = new TilemapRenderer();
            g.AddComponent(t);
            g.AddComponent(tm);
            g.AddComponent(tr);
            Prefab p = new Prefab(g);
            ContentProvider.AddContent("res/maps/actual/+" + tileMapName, p);
            this.p = ContentProvider.RequestContent<Prefab>("res/maps/actual/+" + tileMapName);
        }
        public static ContentRef<Tileset> CreateTileSet(string tileSetName, ContentRef<Pixmap> tiles)
        {
            var tsri = new TilesetRenderInput();
            tsri.Id = tileSetName; //trivial, use?
            tsri.Name = tileSetName;
            tsri.SourceData = tiles;
            tsri.SourceTileSize = new Point2(8, 8);
            tsri.SourceTileSpacing = 0;
            tsri.TargetFormat = Duality.Drawing.TexturePixelFormat.Rgba;
            tsri.TargetMagFilter = Duality.Drawing.TextureMagFilter.Nearest;
            tsri.TargetMinFilter = Duality.Drawing.TextureMinFilter.Nearest;
            tsri.TargetTileMargin = 0;
            Tileset ts = new Tileset();
            ts.TileSize = new Point2(8, 8);
            ts.RenderConfig.Add(tsri);//multiple possible?
            ts.TileInput = new RawList<TileInput>();
            ts.Compile();
            ContentProvider.AddContent("resource/TileSet/" + tileSetName, ts);

            return ContentProvider.RequestContent<Tileset>("resource/TileSet/" + tileSetName);
            //or this
            return new ContentRef<Tileset>(ts, "resource/TileSet/" + tileSetName);
        }
    }


I haven't actually tested the code so I don't even know if it works, but anyways, I got some Questions:
Instantiation
- Is it actually okay, or even possible, to store the generated Objects inside a Prefab and load them when needed as a Child, or would it be better to store the data individually?
Static Method
-where is the difference between the id, and the name? Is one of them being used internally somehow and can one of them, or even both, be null without problems?
-can you use multiple Pixmaps(Tilesets) for a TileMap? Because it seems possible to have multiple TilesetRenderInputs inside a RenderConfig
-can you store MetaData for Tiles inside a TileMap? For example multiple ints that can be accessed?
-and how do you get a ContentRef of a Created Object without loading it? I had two ideas. The second one seems valid but in both cases I got the feeling im doing things twice by giving it to the ContentProvider and then creating a ContentRef.

Maybe someone can answer any of my questions. It would be alot of help!

Thanks in advance!


Top
 Profile  
 
PostPosted: 2017/02/15, 19:46 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2031
Location: Germany
Role: Professional
Hey there, and welcome!

I'll quickly go over some of your questions:

afraidOfNight wrote:
- Is it actually okay, or even possible, to store the generated Objects inside a Prefab and load them when needed as a Child, or would it be better to store the data individually?


You can put them in a prefab, sure!

However, Prefabs exist to make it possible to store an object graph inside a file and load it as needed, as well as authoring scenes in the editor using "instances" of a previously created object. If you generate your objects at runtime anyway, you might as well keep them around using a reference of their root GameObject without the detour over a Prefab. As long as you don't add it to a Scene or parent it to a GameObject that is already part of a scene, they won't be part of the game.

You can also clone a GameObject hierarchy using the Clone method, so you could instantiate as many copies of the object graph as you want.

To be clear: I'm not telling you to use one approach over the other (that's for you to decide), just want to point out that they both exist!

afraidOfNight wrote:
-where is the difference between the id, and the name? Is one of them being used internally somehow and can one of them, or even both, be null without problems?


Assuming you are talking about TilesetRenderInput, the name is an arbitrary string that you can use to identify a tileset rendering layer as a developer. The ID identifies the layer internally and is used (the list may be extended in the future) for assigning the generated texture to a texture slot that the material forwards to the shader that is used. Usually, you would want that to be mainTex.

afraidOfNight wrote:
-can you use multiple Pixmaps(Tilesets) for a TileMap? Because it seems possible to have multiple TilesetRenderInputs inside a RenderConfig


Yep, as many as reasonably fit into a single shader. Four is a safe bet, eight should be fine too for most machines, but I wouldn't know what you would use so many layers for anyway. xD

afraidOfNight wrote:
-can you store MetaData for Tiles inside a TileMap? For example multiple ints that can be accessed?


Nope. We have an issue on that on GitHub, but more concept work needs to be done on this and there are lots of other tasks that have priority right now, so my recommendation would be to roll your own workaround for now. Use the tile index to do a lookup on a custom data structure to retrieve the additional data you want to store.

afraidOfNight wrote:
-and how do you get a ContentRef of a Created Object without loading it? I had two ideas. The second one seems valid but in both cases I got the feeling im doing things twice by giving it to the ContentProvider and then creating a ContentRef.


ContentRefs are only for Resources, not for objects in general. The reason they exist is that they enable you to talk about Resources without actually loading them, or to replace / reload them at runtime without having to fix up all references. For non-Resource objects, you'd just pass around the reference and that's it.

In the special case of a Resource - like a Prefab - being generated, you should indeed use ContentRefs, like you did. The easiest way to obtain a ContentRef is by implicit casting:

Code:
Prefab foo = ...;
ContentRef<Prefab> fooRef = foo; // Done!


And that's it.

It becomes more complicated when you want references to your generated Resource to be serializable, in which case the ContentProvider plays an important role, as it provides the API to resolve a Resource by its path / special name. In that case, you would have to make sure that the generated Prefab is registered in the ContentProvider before the first ContentRef to it tries to resolve. Otherwise, you might as well not add it to the ContentProvider at all.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/02/15, 20:56 
Newbie
Newbie

Joined: 2017/01/15, 18:59
Posts: 4
Location: Germany
Role: Hobbyist
Wow that actually clarified alot of things for me, thanks!

Regarding:
Quote:
Yep, as many as reasonably fit into a single shader. Four is a safe bet, eight should be fine too for most machines, but I wouldn't know what you would use so many layers for anyway. xD


I wrote code to procedually generate Clusters of Pixels(tiles) while using Predefined sets of "tiles".
If you now want to shift from one set of tiles to another set of tiles, you need them both inside of one Cluster. 4 is actually a pretty good maxAmount, because it is the max number of adjacent Clusters(Tilemaps).

I didnt mention this earlier, but is it actually possible to change the baseColor of only one Tile? I know its possible to change it for a Tileset, but that wont do it.


Top
 Profile  
 
PostPosted: 2017/02/15, 21:11 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2031
Location: Germany
Role: Professional
Not sure I can follow on your use case, but that's okay xD

What I can tell you though is that if you need custom rendering tricks, like re-coloring specific tiles, you may want to consider implementing a custom TilemapRenderer that you can use instead of the regular one. It's not a beginner's task, but not that difficult either once you got to know Duality for a bit - 400 lines in total (including property definitions, comments and empty lines) and you're free to draw whatever you like. As long as it implements ICmpTilemapRenderer, it will work together with the Tilemap Editor just fine.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/02/21, 23:00 
Newbie
Newbie

Joined: 2017/01/15, 18:59
Posts: 4
Location: Germany
Role: Hobbyist
I didn't even know the code was accessible :D
Well anyways I think I'll go with stacking tilemaps and custom Metadata for now.

Another Question: Why is Vector-Matrix-Multiplication not implemented? Im actually transforming the Vectors to MathNet.Numerics, computing them and then transforming them back ^^

If I would customCode it: are the Mats row or col Major?


Top
 Profile  
 
PostPosted: 2017/02/22, 19:02 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2031
Location: Germany
Role: Professional
To transform a vector using a matrix, use their static Transform method. All of the vector types should have one of those. ^^

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group