Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: willbir on February 22, 2017, 05:54:40 AM

Title: Converting bitmap images to map terrain.
Post by: willbir on February 22, 2017, 05:54:40 AM
This has probably been done before but I've been experimenting with converting bitmap images to map terrain. The way I've done it is using some JavaScript to read an image and then use that data to generate crpl code which when copied to a map and run will modify the terrain to reproduce the image. The code is attached if anyone else wants to have a go with it (if run locally chrome throws an error but IE works).

Title: Re: Converting bitmap images to map terrain.
Post by: FOXX on February 24, 2017, 12:25:46 PM
Is this also possible for Particle Fleet?? [PRPL]
Title: Re: Converting bitmap images to map terrain.
Post by: planetfall on February 24, 2017, 01:08:51 PM
Excellent tool you have here. Thanks for sharing it!

Quote from: FOXX on February 24, 2017, 12:25:46 PM
Is this also possible for Particle Fleet?? [PRPL]

In theory, you'd just have to change SetTerrain to SetLand.
Title: Re: Converting bitmap images to map terrain.
Post by: Stickman on February 24, 2017, 01:35:11 PM
Quote from: planetfall on February 24, 2017, 01:08:51 PM
In theory, you'd just have to change SetTerrain to SetLand.

FOXX led me here to test it and this simple change works.
To get better results, you need to change a line in the html file like that:

h= Math.floor((0.299*r + 0.587*g + 0.114*b)/51.2);

Changing from 25.6 to 51.2 seems to convert 10 CW3 land heights to 5 PF land heights nicely.

Amazingly cool tool
Title: Re: Converting bitmap images to map terrain.
Post by: GoodMorning on February 24, 2017, 05:29:32 PM
Nice. Using IE for a lack of security on a local sandbox application is not new, but... caution advised.
Title: Re: Converting bitmap images to map terrain.
Post by: willbir on February 24, 2017, 05:43:38 PM
Quote from: GoodMorning on February 24, 2017, 05:29:32 PM
Nice. Using IE for a lack of security on a local sandbox application is not new, but... caution advised.

If you look at the code you can verify there is nothing nefarious going on, the error is due to Chrome considering local files cross-origin so the canvas becomes tainted (https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image).
Title: Re: Converting bitmap images to map terrain.
Post by: GoodMorning on February 24, 2017, 06:04:26 PM
I didn't say it wasn't reasonable, only that there's a reason it is the way it is.

Good tool.
Title: Re: Converting bitmap images to map terrain.
Post by: FOXX on February 24, 2017, 07:32:37 PM
Yeah, this is a really cool tool. I am busy with a lot of things now but after i finish that, this will be a point of interest.
'I can see little figures in space' :)

I have tried this with some randome images i got from internet. [see attachments]
If there is a way to set x and y points and maybe a rotate option that would be great. [or is that already in the script?]

I think with some scripting this can be even more great.
Title: Re: Converting bitmap images to map terrain.
Post by: willbir on February 25, 2017, 04:26:54 AM
Quote from: FOXX on February 24, 2017, 07:32:37 PM
I have tried this with some randome images i got from internet. [see attachments]
If there is a way to set x and y points and maybe a rotate option that would be great. [or is that already in the script?]


You can do that in Photoshop before you create the terrain, if you move the crpl line "I <-f <-h setterrain" just above the endif then transparent parts of an image will leave the terrain unchanged.

I find vector images like these (https://pixabay.com/en/photos/?image_type=vector&order=popular) produce good results.


Title: Re: Converting bitmap images to map terrain.
Post by: FOXX on February 25, 2017, 07:16:43 AM
Quote from: willbir on February 25, 2017, 04:26:54 AM
You can do that in Photoshop before you create the terrain, if you move the crpl line "I <-f <-h setterrain" just above the endif then transparent parts of an image will leave the terrain unchanged.

I find vector images like these (https://pixabay.com/en/photos/?image_type=vector&order=popular) produce good results.
Thanks for the tip and link !!
Title: Re: Converting bitmap images to map terrain.
Post by: FOXX on February 25, 2017, 02:19:49 PM
The land created in Particle Fleet is upside down :(
Can somebody look at the script to change this please.

[I don't want the fish to be 'dead']

You can turn the image upside down but maybe it;s better if the script gets updated for this.
Title: Re: Converting bitmap images to map terrain.
Post by: willbir on February 25, 2017, 02:29:24 PM
Quote from: FOXX on February 25, 2017, 02:19:49 PM
The land created in Particle Fleet is upside down :(
Can somebody look at the script to change this please.

[I don't want the fish to be 'dead']

changing the line
I <-f <-h setterrain
to
I <-imageY <-f sub <-h setterrain

should flip it
Title: Re: Converting bitmap images to map terrain.
Post by: FOXX on February 25, 2017, 02:44:55 PM
Quote from: willbir on February 25, 2017, 02:29:24 PM
changing the line
I <-f <-h setterrain
to
I <-imageY <-f sub <-h setterrain

should flip it
Just tried it, works perfect :)

Now the thing is that by default the land sticks to the SouthWest corner. Is there a way to set coordinates?
Or at least an option or scripting for setting the spawnpoint of the land to the North?
Title: Re: Converting bitmap images to map terrain.
Post by: GoodMorning on February 25, 2017, 06:32:58 PM
Making the land position be set relative to CurrentX and CurrentY will do it.
Title: Re: Converting bitmap images to map terrain.
Post by: FOXX on February 25, 2017, 06:59:15 PM
Quote from: GoodMorning on February 25, 2017, 06:32:58 PM
Making the land position be set relative to CurrentX and CurrentY will do it.
Thanks GM.
Title: Re: Converting bitmap images to map terrain.
Post by: Dark6al3a on May 25, 2022, 09:53:56 AM
instructions for using this ?
I've got a really good idea with this but idk how to use it