Hello, I’ve searched almost everywhere I can think of for this but can’t find anything.
I’m looking for a script snippet that can detect a location on the terrain object and send back the texture from that location. I’m attempting to build better physics for a vehicle script and well, mud has different properties than grass. Is this possible? Any help is very appreciated.
First you will need to expose the splatmaps to your script
Texture2D splatmap;
you will need to attach this manually in the editor.
then you need to get a ratio between the splatmap size and the terrain size.
Vector2 ratio = new Vector2(splatmap.size.x/terrain.x,splatmap.size.y/terrain.z);
once you have done this you can read the the splat map color using a position in worldspace
you can :
Color posColor = splatmap.GetPixel( (int)(transform.x*ratio.x), (int)(transform.z*ratio.y));
cross reference this with the textures used for you terrain to return the texture
if(posColor==Color.red) mysurf = "mud";
if(posColor==Color.green) mysurf = "grass";
if(posColor==Color.blue) mysurf = "sand";
if(posColor==Color.black) mysurf = "rock";
Hi,
this sounds very interessting,
even when it not directly pointing to the question. is there a possibility to change now on runtime the texture of the terrain under that object?
Sure, but only with a texture already loaded into the terrainData
Just paint the the splat map
red, green, blue, or blk and alpha using SetPixel
make sure rgb has alpha=0 i think