Page 1 of 1

Scattering Objects with a Density Map

Posted: Wed Aug 09, 2017 1:43 am
by ColtonD719
A key component of natural looking vegetation is randomness and variety. In a natural forest or grove of trees, no two trees are the same. They are all of various heights and species. Mimicking this sort of variation in MXS can be rather tedious.

Image

With my first track (pictured) I placed every tree individually, then used Excel to randomize their heights. I liked the end result but it was far too time consuming. SMR 510RR has a method he uses to scatter crowds around tracks, which is very promising, but relies on expensive software. So, for a while I've been searching for a method using more accessible means.

I've finally found a way to do what I want. That is, randomly scatter objects across an area according to a density map, similar to the way probability maps worked in MCM2. I can run through this process in 10-15min, though I'm sure there are ways to make it more efficient. To those smarter than me, I would love to hear any ideas. I imagine with some clever programming, the Blender and Excel steps could be eliminated.

Anyway, here's what I have so far:

Tools Needed:
- Photoshop/GIMP/etc.
- Blender
- Excel
- Text editor, such as Notepad++ (I actually use Dreamweaver)
- Wheels' Blender object placement script: http://www.mediafire.com/download/31zf67qo7ubl7yh/write.placement.v1.zip
- Chebhou's Blender weight & color script: http://www.mediafire.com/file/a0n37y7fpif54ll/weight.vertexcolor.convert.py.zip

In this case we will be placing billboard trees, though this technique could be used for placing crowds, statues, etc.

1. Create a 512x512 8-bit Grayscale probability map in Photoshop/GIMP/etc. White=maximum density, black=0 density
Image
Left, probability map with low opacity reference image. Right, actual map.


2. Open Blender and delete the default scene.

3. Create plane to the size of your MXS world (In Game Size=(terrain.png resolution-1)*terrain.hf scale).

4. Reposition the plane so the corner is at 0, 0 (Location: X=In Game Size/2, Y=-In Game Size/2)
Image
Note: My scale is unusually high in this example.


5. Create a new cube. This will be a placeholder for the object you want to scatter.
⁃ In Object tab, scroll down to Custom Properties. Click "Add," then click "Edit." Enter the following values:
⁃ Property Name: texture
⁃ Property Value: path to your tree billboard (i.e., “mytrack/billboard/mybillboard”)
Image

⁃ Optional: For variety, add additional cubes referencing different billboard files. Give each object a different name (i.e. TreeA, TreeB, TreeC, etc.)
Image


6. Select your cube(s) and hit Ctrl+G to put them into a new group. Name the group “Tree” or similar.
Image


7. Select your Plane. Add new ParticleSystem
Image

⁃ Enter the following values:
⁃ Type: Hair
⁃ Check Advanced
⁃ Physics: Size: 1
⁃ Render: Group
⁃ Render: Dupli Group: Tree (or whatever you named the group)
⁃ Render: Check Pick Random
Image


8. Add New Material “ProbMap”
Image


9. Add New Texture to “ProbMap” material. Import your probability map image.
⁃ Image: Color Space: Linear
Image


10. Switch to Edit Mode. Subdivide the Plane 9 times.
Image


11. With the Plane selected, switch to Vertex Paint. Make sure Display Mode is set to Solid.
Image


12. In the Texture panel on the left, select the probability map texture. Set the following value:
⁃ Brush Mapping: 3D
Image

⁃ Switch to Top view, Orthographic. With large radius and Strength: 1, paint over entire plane
Image
Note: I have experienced a case where using too large of a brush caused a glitchy looking grid to show. Painting over it with a smaller radius cleared it up.


13. Load weight.vertexcolor.convert.py into Text Editor. Run Script.
Image


14. With Plane selected, hit Spacebar, type weight & color, and click the corresponding option. Enter the following values:
⁃ Convert: Vertex color to weight
⁃ Color type: Grayscale
⁃ Hit OK
Image
You should now see a color weight map, similar to the image on the right.


15. Go back to the Particles tab. Enter the following values:
⁃ Vertex Groups: Density: Col
Image

⁃ Optional: switch to Object Mode, change Display Mode to Wireframe.
⁃ Emission: Adjust to taste. This is the number of objects that will be placed.
Image
You should see a scattering of cubes corresponding to your probability map.


16. Switch to the Modifier tab. Under ParticleSystem 1, click Convert
Image


17. You should now have individual cubes all selected. Deselect the plane.

18. Open write.placement.v1.py in the Text Editor. Run Script.

19. Choose filename and destination and Export Some Data.

20. Open the exported file in Excel. The Text Import Wizard should appear. Enter the following values:
⁃ Original Data Type: Delimited. Click Next.
⁃ Delimiters: Check Space. Click Finish.

21. Column B is height. Set all values to -1 to ensure trees remain planted in ground even on a slope.

22. Column D is billboard size. In D1 type “=RANDBETWEEN(50,150)”. Adjust min and max to taste.
⁃ Copy this formula to all values in Column D.

23. Column E is aspect ratio. Set all values to 1, or whatever is appropriate for your billboard.

24. Column F is your billboard file. You should see all the billboard paths you entered in step 5.

25. Clear column G.
Image
Your spreadsheet should look something like this by now.


26. File>Save. Click Continue.

27. Open the saved file in your text editor.
- Add an empty line at the end of the file.
⁃ Save As “billboards”

28. Place the billboards file in your track folder. Open MXS and you should see your track now populated with trees in game.
Image

Of course, there are various ways you can use/alter this approach to suit your needs. For example, you can use it to place statues instead of billboards. Or you can use different probability maps for different types of vegetation. With some tweaking you could even use it to place decals.

Please share any creative uses you have, as well as any suggestions or improvements. Feel free to ask any questions as well, though to be honest I barely have a handle on it myself.

I would have never figured this out without the following sources:
Kamil Kuklo - https://evermotion.org/tutorials/show/10344/scattering-trees-in-blender-tip-of-the-week
Chebhou - https://blender.stackexchange.com/questions/26467/weight-paint-maps-in-out-of-blender-to-allow-editing-in-photoshop-or-other
barrington314 - http://forum.mxsimulator.com/viewtopic.php?f=11&p=789449#p789449
wheels1758 - http://forum.mxsimulator.com/viewtopic.php?f=2&t=25508

Re: Scattering Objects with a Density Map

Posted: Fri Aug 25, 2017 6:37 pm
by barrington314
Pretty cool stuff. Thanks for sharing.

Re: Scattering Objects with a Density Map

Posted: Mon Aug 28, 2017 10:31 am
by Jay
thank you

Re: Scattering Objects with a Density Map

Posted: Fri Jan 11, 2019 2:48 am
by AtlasZoor
Hey man so I started to follow this tutorial and I got all the way down to the "particle system convert". Once I hit convert nothing happens. it does not select all the cubes. when I use the export script in notepad its just the first cubes you place as the different trees. I know this is kinda a specific question but any help will be appreciated! :D

Re: Scattering Objects with a Density Map

Posted: Fri Jan 11, 2019 2:55 am
by AtlasZoor
i just redid the script and all that shows up is what I think is the plane? not sure what is happening. here is a pic to help I guess lol.

Image

Re: Scattering Objects with a Density Map

Posted: Tue Jan 15, 2019 8:17 pm
by ColtonD719
AtlasZoor wrote:...

Yeah, the script will just export whatever objects are selected at the time you run it. So it won’t export the individual scattered objects unless they exist as separate objects in Blender—which is what the “convert” step is supposed to do.

Not sure why the convert isn’t working for you. Before you hit convert, can you see the scattered objects as in the image in step 15? Does anything happen at all when you hit convert? Sometimes it can take a minute or two depending on how many particles you have.

Re: Scattering Objects with a Density Map

Posted: Tue Jan 15, 2019 9:33 pm
by AtlasZoor
ColtonD719 wrote:

Yeah, the script will just export whatever objects are selected at the time you run it. So it won’t export the individual scattered objects unless they exist as separate objects in Blender—which is what the “convert” step is supposed to do.

Not sure why the convert isn’t working for you. Before you hit convert, can you see the scattered objects as in the image in step 15? Does anything happen at all when you hit convert? Sometimes it can take a minute or two depending on how many particles you have.[/quote]

I figured it out earlier I just had to update blender haha. Thanks though! Its actually a lot easier than I thought it would be. really great tutorial. :D

Re: Scattering Objects with a Density Map

Posted: Wed Jan 16, 2019 12:06 am
by ColtonD719
Awesome. Glad it worked!

Re: Scattering Objects with a Density Map

Posted: Sat Feb 09, 2019 7:04 pm
by Kbrown
hey guys, I had a funny problem, my tree map was upside down

Image

and it may sound stupid, but I do not know how to get those spaces in the file
Image