如何在Godot3中正确更改网格颜色?
extends MeshInstance
# class member variables go here, for example:
# var a = 2
# var b = "textvar"
var i=0
export(Color) var new_color = Color(1, 1, 1, 1)
func _ready():
var n = self
var mat=n.get_mesh().surface_get_material(0)
var mat2 = SpatialMaterial.new()
mat2.albedo_color = Color(0.8, 0.0, 0.0)
self.get_mesh().surface_set_material(0,mat2)
set_process(true)
# Called every time the node is added to the scene.
# Initialization here
func _process(delta):
randomize()
var mat2 = SpatialMaterial.new()
mat2.albedo_color = Color8(255, 0, 0)
var i = rand_range(0.0,100.0)
if i>50.0:
self.get_mesh().surface_set_material(0,mat2)
i=0
else:
mat2.albedo_color = Color8(0, 0, 255)
self.get_mesh().surface_set_material(0,mat2)我试着用这个简单的代码在godot3引擎中改变网格颜色。例如,在一些游戏中,这个想法可能有助于改变汽车的灯光颜色。
发布于 2019-11-06 03:39:23
可以使用MeshInstance的material_override属性和材质(SpatialMaterial)的albedo_color属性,将颜色设置为所需的任何颜色:
下面的示例将MeshInstance的颜色更改为橙色:
func _ready():
var newMaterial = SpatialMaterial.new() #Make a new Spatial Material
newMaterial.albedo_color = Color(0.92, 0.69, 0.13, 1.0) #Set color of new material
$"MeshInstance".material_override = newMaterial #Assign new material to material overrride首先,创建一个新的SpatialMaterial并为其指定一个名称。然后,设置该材质的颜色,并将该材质设置为MashInstance的替代材质。只需将"MeshIsntance“替换为MeshInstance的名称即可。例如,如果以下行包含在附加到MeshInstance本身的脚本中:
func _ready():
var newMaterial = SpatialMaterial.new()
newMaterial.albedo_color = Color(0.92, 0.69, 0.13, 1.0)
self.material_override = newMaterial如果要撤消替代:
self.material_override = null发布于 2018-08-27 17:34:07
您可以通过以下方式更改材质颜色:
extends MeshInstance
export(Color) var new_color = Color(1, 1, 1, 1)
func _ready():
randomize()
get_surface_material(0).albedo_color = new_color
set_process(true)还可以在检查器的"GeometryInstance“下添加材质覆盖(SpatialMaterial),并将其albedo属性设置为脚本中所需的颜色。
material_override.albedo_color = new_colorhttps://stackoverflow.com/questions/52028544
复制相似问题