Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Cyanux | |
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:
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