(Solved) Image.set_pixel not applying change to the image in Godot 3.0. What did i miss?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cyanux
:warning: Old Version Published before Godot 3 was released.

Hello there :).

This question has been edited. The problem i had was not correctly diagnosed.

Original question was to correctly set the texture on a mesh with SpatialMaterial, but i saw later that it was the Image.set_pixel function that was not applying the changes.

So… Here is my question. Is there a magical function (and if yes, wich one?) to apply theses changes or is it a bug due to the active development of the master-branch? I Can’t find such function anywhere on the docs or other Q/A.

Here is my source code:

extends MeshInstance

func _ready():

	CreateQuad()
	pass
	
func CreateQuad():
	var sf = SurfaceTool.new()
	var m = Mesh.new()
	
	sf.begin(Mesh.PRIMITIVE_TRIANGLES)
	
	sf.add_uv(Vector2(0, 0))
	sf.add_vertex(Vector3(-1, 0, -1))
	
	sf.add_uv(Vector2(1, 0))
	sf.add_vertex(Vector3(1, 0, -1))
	
	sf.add_uv(Vector2(1, 1))
	sf.add_vertex(Vector3(1, 0, 1))
	
	sf.add_uv(Vector2(0, 0))
	sf.add_vertex(Vector3(-1, 0, -1))
	
	sf.add_uv(Vector2(1, 1))
	sf.add_vertex(Vector3(1, 0, 1))
	
	sf.add_uv(Vector2(0, 1))
	sf.add_vertex(Vector3(-1, 0, 1))
	
	sf.generate_normals()
	sf.index()
	sf.commit(m)
	
	self.mesh = m
	self.set_surface_material(0, CreateMaterial())
	
func CreateMaterial():
	var image = Image.new()
	image.create(16, 16, false, Image.FORMAT_RGH)
	for x in range(16):
		for y in range(16):
			randomize()
			var rand = randf()
			image.set_pixel(x, y, Color(0, rand, 0))
			
			
	var texture = ImageTexture.new()
	texture.create_from_image(image)
	var mat = SpatialMaterial.new()
	mat.set_texture(SpatialMaterial.TEXTURE_ALBEDO, texture)
	return mat

Don’t take care about my comments bellow, i was updating when i had fresh news. The essential is there.

Hello again :).

I have also tried with another piece of code to create my meshes from scratch… Still doesn’t work. I have an ugly dark blue instead of the pixelized green that I want.

Here is the code:

tool
extends MeshInstance

func _ready():

	CreateQuad()
	
func CreateQuad():
	var sf = SurfaceTool.new()
	var m = Mesh.new()
	
	sf.begin(Mesh.PRIMITIVE_TRIANGLES)
	sf.add_uv(Vector2(0, 0))
	sf.add_vertex(Vector3(-1, 0, -1))
	sf.add_uv(Vector2(1, 0))
	sf.add_vertex(Vector3(1, 0, -1))
	sf.add_uv(Vector2(1, 1))
	sf.add_vertex(Vector3(1, 0, 1))
	sf.add_uv(Vector2(0, 0))
	sf.add_vertex(Vector3(-1, 0, -1))
	sf.add_uv(Vector2(1, 1))
	sf.add_vertex(Vector3(1, 0, 1))
	sf.add_uv(Vector2(0, 1))
	sf.add_vertex(Vector3(-1, 0, 1))
	sf.generate_normals()
	sf.index()
	sf.commit(m)

	self.mesh = m
	self.set_surface_material(0, CreateMaterial())

func CreateMaterial():
	var image = Image.new()
	image.create(16, 16, false, Image.FORMAT_RGH)
	for x in range(16):
		for y in range(16):
			randomize()
			var rand = randf()
			image.set_pixel(x, y, Color(0, rand, 0))
	var texture = ImageTexture.new()
	texture.create_from_image(image)
	var mat = SpatialMaterial.new()
	mat.set_texture(SpatialMaterial.TEXTURE_ALBEDO, texture)
	return mat

Here is the result:
result

maybe my UVs are wrong, i don’t know much about them so i made what seems to be logic for me.

Cyanux | 2017-08-22 07:05

Mhmmm… The problem seems to be the image itself. Looks like it doesn’t apply the set_pixel function… Looking to fix that… If i can find how to save the changes. Knowing that, i’m gonna adapt the topic title.

Cyanux | 2017-08-22 08:16

:bust_in_silhouette: Reply From: Zylann

You need to lock the image before setting its pixels:

image.lock()
...setting pixels...
image.unlock()

Oh, I tried image.unlock() first and then image.lock() after the set_pixel(). But yes, I just saw this morning this message in error logs, i forgot to check them, my bad, I was only reading the debugs:

" Image must be locked with ‘lock()’ before using set_pixel() "

Aaaaaah… Dear Cyanux… Where do you put your eyes… (Yes I talk to myself :D)

Thanks for the answer :).

And as you are there, also thanks for your modules, i’m in love with opensimplex :).

Cyanux | 2017-08-24 08:35

That does not change anything :(. Look over here.

MaaaxiKing | 2021-02-04 09:41

MaaaaxiKing, you are mixing up Texture and Image.

Zylann | 2021-02-04 15:56