Getting incorrect texture rotation in mesh component

Using resource.create_buffer to uniquely create different mesh. But getting texture correctly only in one mesh and in others getting texture in rotated.


image

local uuid = require("game.uuid.src.uuid")

go.property("Type", hash("WALL"))
go.property("Width", 0)
go.property("Height", 0)
go.property("Left", 0)
go.property("Right", 0)
go.property("Top", 0)
go.property("Bottom", 0)
go.property("Adjacent_1", hash("adjacent wall id"))
go.property("Adjacent_2", hash("adjacent wall id"))
-- go.property("my_texture", resource.texture("/assets/background-elements-redux-fix/Backgrounds/backgroundCastles.png"))

-- wall types

-- WALL / FLOOR / LEFT_WALL / RIGHT_WALL / LEFT_ROOM_DIVIDER / RIGHT_ROOM_DIVIDER / LEFT_WALL_DIVIDER / RIGHT_WALL_DIVIDER


local function fill_positions(self, v0, v1, v2, v3)
	-- first triangle
	self.positions[1] = v0.x
	self.positions[2] = v0.y
	self.positions[3] = v0.z

	self.positions[4] = v1.x
	self.positions[5] = v1.y
	self.positions[6] = v1.z

	self.positions[7] = v2.x
	self.positions[8] = v2.y
	self.positions[9] = v2.z

	-- second triangle
	self.positions[10] = v0.x
	self.positions[11] = v0.y
	self.positions[12] = v0.z

	self.positions[13] = v2.x
	self.positions[14] = v2.y
	self.positions[15] = v2.z

	self.positions[16] = v3.x
	self.positions[17] = v3.y
	self.positions[18] = v3.z
end

local function vertex_position(self)
	--for wall
	-- right 640  top 960
	local v0, v1, v2, v3 = vmath.vector3()
	if self.Type == hash("WALL") then
		v3 = vmath.vector3(self.origin.x, self.Height, 0)
		v2 = vmath.vector3(self.Width, self.Height, 0)
		v1 = vmath.vector3(self.Width, self.origin.y, 0)
		v0 = vmath.vector3(self.origin.x, self.origin.y, 0)
	elseif self.Type == hash("FLOOR") then
		v0 = vmath.vector3(self.Left- self.Left, self.Bottom, 0)
		v1 = vmath.vector3(self.Left, self.Top, 0)
		v2 = vmath.vector3(self.Right, self.Top, 0)
		v3 = vmath.vector3(self.Right + self.Left, self.Bottom, 0)
	end
	return v0, v1, v2, v3
end

function init(self)
	self.origin = go.get_world_position()

	local res = go.get("#front", "vertices")
	local buf = resource.get_buffer(res)

	-- -- create a cloned buffer resource from another resource buffer
	local res_path = "buf-" .. uuid() .. ".bufferc"
	local cloned_res = resource.create_buffer(res_path, { buffer = buf})
	local new_buffer = resource.get_buffer(cloned_res)

	self.positions = buffer.get_stream(new_buffer, "position")

	self.v0, self.v1, self.v2, self.v3 = vertex_position(self)

	fill_positions(self, self.v0, self.v1, self.v2, self.v3)

	local data = sys.load_resource('/assets/background-elements-redux-fix/Backgrounds/backgroundColorForest.png') 
	imageloader.load{
		data = data,
		listener = function(self, image_resource)
			pprint(image_resource)
			resource.set_texture( go.get("#front", "texture0"), image_resource.header, image_resource.buffer )
		end
	}

	-- assign cloned buffer to a mesh component
	go.set("#front", "vertices", cloned_res)
end

function update(self, dt)
end

function on_message(self, message_id, message, sender)
end

I think you need to printout the values from WALL and FLOOR and check that the result is what you expect it to be?
It seems like you flip the Y coordinates.

Yes, vertex positions are changing for different walls instances here. But, all three wall go instance is having same script all are type wall so only “WALL” if block is running. I’m nowhere manipulating and changing vertex position for other mesh. So I’m not getting it what causing it.


Do you have a repro for this you could share?

Here test project for above.
Screenshot from 2023-02-18 21-06-05

https://drive.google.com/drive/folders/15hm_aEgcMwkt96QAj0ekud1Pln4C9t5B?usp=share_link

Well, if the red text is the correct mesh, then you notice that the v3 is in the top left corner. For the other colors, v3 is not in the top ledt corner.
And if all use WALL, the I suggest looking at the math calculating v3 et al.

Looking further, you seem to mix world space and local space. You should choose one or the other.

Yes, math calculation for vertex position was wrong. Now, its properly showing.

2 Likes