How to add a shader to 3D model?
Asked Answered
C

5

0

How to add a shader to 3D model? (Godot 4)


extends Node3D

@onready var headShader = preload("res://3d/Mesh/head.gdshader")
@onready var headMesh = $Head_Mesh_1

func _ready():

	pass 
Coimbatore answered 6/1, 2023 at 17:29 Comment(0)
C
0

Here's a simple solution.
You need to create and save a material (name_material.tres)

extends MeshInstance3D


@onready var shaderA = preload("res://3d/Shader/material_shader_1.tres")

func _ready():

	self.material_override = shaderA
	
	pass
Coimbatore answered 7/1, 2023 at 13:43 Comment(0)
H
0

You mean in code or via GUI? Either way you have to create a material first then assign the shader to the material.

Hydraulics answered 6/1, 2023 at 19:21 Comment(0)
C
0

The shader is already created("res://3d/Mesh/head.gdshader"), me necessary to attach it through a script.
Perhaps the mesh will be replaced many times. I need to automate the attachment of material.

Coimbatore answered 6/1, 2023 at 19:38 Comment(0)
H
0

You create a new ShaderMaterial resource then load and attach your shader to it's shader property then apply that material to the GeometryInstance material override property.

https://docs.godotengine.org/en/latest/classes/class_geometryinstance3d.html#class-geometryinstance3d-property-material-override
https://docs.godotengine.org/en/latest/classes/class_shadermaterial.html

Hydraulics answered 7/1, 2023 at 1:56 Comment(0)
C
0

Godo load shader in 2018
https://godotengine.org/qa/35114/how-can-i-load-a-shader-file-through-script

onready var WaterShader = preload("res://Water.shader")

func _ready():
    self.material.shader = WaterShader

How do I upload material I've already created to Godo 4?
I looked at the documentation MeshInstance3D but could not find the desired function.

Script

extends MeshInstance3D

@onready var shaderA = preload("res://3d/Shader/Head_shader_1.tres")

func _ready():
#	self.  ??
	pass
Coimbatore answered 7/1, 2023 at 12:24 Comment(0)
C
0

Here's a simple solution.
You need to create and save a material (name_material.tres)

extends MeshInstance3D


@onready var shaderA = preload("res://3d/Shader/material_shader_1.tres")

func _ready():

	self.material_override = shaderA
	
	pass
Coimbatore answered 7/1, 2023 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.