Search Unity

How do i get albedo informatian from some material, using script?

Discussion in 'General Discussion' started by sammtan, May 8, 2021.

?

Which do u prefer?

  1. Unity Default Terrain

    66.7%
  2. Mesh as Terrain

    33.3%
  1. sammtan

    sammtan

    Joined:
    Jul 2, 2019
    Posts:
    14
    Hi guys, im a noob at scripting.
    How do i get albedo information from some material?
    Basic material have albedo, normal, metallic, specular, anything else base on the shader. Then i want access that albedo, so my script can read that albedo data, for to use with my own purposes.

    For example, i have a mesh, and a terrain. I want to lerp my terrain albedo, with my mesh albedo. Then i create a script, at my mesh, to get the albedo from my terrain. How to do that?

    Also, i need another help.
    Which do you (guys) prefer, unity default terrain or terrain using mesh? Vote at the poll below. Plz

    Every solution and advice are acceptable. Built-in RP.
    Sorry for bad English
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    It would be better if you said what you're trying to achieve.

    You can't do that easily, because albedo changes for each pixel, if the material is textured.

    If you use Raycast, then you can determine individual triangle you hit from the hit result, and coordinates within the triangle.
    https://docs.unity3d.com/ScriptReference/RaycastHit.html
    Based on that data you could try determining which material you hit, and then you could manually sample texture, which will only work if the data is readable by the script in the first place.
    https://docs.unity3d.com/ScriptReference/Texture2D.GetPixel.html

    You could also try rendering the part of the terrain/mesh you want with a tiny render target and a replacement shader, and read the data from there.

    And in case of built-in terrain, you'd be using this:
    https://docs.unity3d.com/ScriptReference/TerrainData.GetAlphamaps.html

    Which will give you access to blend values used by terrain, and then derive the values from there.

    That's why I said it would be a good idea to know what you're trying to do...
     
  3. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    I can't tell you how but I know this is done with microsplat/megasplat. I think you can get a free version from the asset store - you could reverse engineer that.