godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.24k stars 20.23k forks source link

Get material from Surface Material Override #82496

Closed catafest closed 12 months ago

catafest commented 12 months ago

Godot version

v4.1.2.rc1.mono.official [58f0cae4a]

System information

Vulkan API 1.3.242 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce GTX 950

Issue description

I tried to get the Surface Material Override from a QuadMesh, to get a shader and use it in GDscript. I saw some issues about surface_get_material(0) but I'm not sure is fixed I used this GDScript source code :

func _ready():
    # get values from the shader 
    print(mesh)
    material = mesh.surface_get_material(0)
    print(material)
    noise = material.get_shader_parameter("wave").noise.get_semless_image(512,512)
...

The error is:

<QuadMesh#-9223372009524689752>
<Object#null>

Steps to reproduce

create a QuadMesh, add a shader, and try to get the Surface Material Override

Minimal reproduction project

Try this project to see the error GITHUB_Issues_catafest.zip

Calinou commented 12 months ago

You need to use get_surface_override_material(0) to get the Surface Material Override (instead of the per-mesh Material).

smix8 commented 12 months ago

What Calinou said. There are 3 material "levels".

The one on the mesh resource. -> Mesh.surface_get_material(surface_index)

The one on the mesh instance that replaces any material from the mesh in that surface slot -> MeshInstance3D.get_surface_override_material(surface_index)

The one on the visual instance that overrides everything -> GeometryInstance3D.get_material_override()

catafest commented 12 months ago

Error: Invalid call. Nonexistent function 'get_surface_override_material' in base 'QuadMesh' for this source code: ` func _ready():

get values from shader

print(mesh)
material = mesh.get_surface_override_material(0)

`

smix8 commented 12 months ago

As mentioned that function is part of the MeshInstance3D, not part of the Mesh.