Skip to content

Commit 5e710a2

Browse files
committed
allow coordinate swap matrix for meshes
1 parent abac3ab commit 5e710a2

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

examples/meshCube.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ renderer = getRenderer(scene)
1414
canvas = scene.canvas
1515
gpuDevice = scene.gpuDevice
1616

17-
mesh = WGPUgfx.defaultWGPUMesh(joinpath(pkgdir(WGPUgfx), "assets", "cube.obj"))
17+
mesh = WGPUgfx.defaultWGPUMesh(
18+
joinpath(pkgdir(WGPUgfx), "assets", "cube.obj"),
19+
swapMat = [1 0 0 0; 0 0 1 0; 0 1 0 0; 0 0 0 1] .|> Float32
20+
)
1821

1922
addObject!(renderer, mesh)
2023

src/primitives/mesh.jl

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function readObj(path::String)
4949
push!(faceidxs, (p, u, n))
5050
end
5151
push!(indices, [idx for idx in faceidxs])
52-
else
52+
else
5353
indexList = [(Meta.parse(i), -1, -1) for i in s[2:end]]
5454
push!(indices, indexList)
5555
end
@@ -79,7 +79,14 @@ mutable struct WGPUMesh <: MeshSurface
7979
cshaders
8080
end
8181

82-
function defaultWGPUMesh(path::String; scale::Union{Vector{Float32}, Float32} = 1.0f0, color::Vector{Float64}=[0.5, 0.6, 0.7, 1.0], image::String="", topology="TriangleList")
82+
function defaultWGPUMesh(
83+
path::String;
84+
scale::Union{Vector{Float32}, Float32} = 1.0f0,
85+
color::Vector{Float64}=[0.5, 0.6, 0.7, 1.0],
86+
image::String="",
87+
topology="TriangleList",
88+
swapMat = Matrix(I, 4, 4) .|> Float32
89+
)
8390
meshdata = readObj(path) # TODO hardcoding Obj format
8491
vIndices = reduce(hcat, map((x)->broadcast(first, x), meshdata.indices)) .|> UInt32
8592
nIndices = reduce(hcat, map((x)->getindex.(x, 3), meshdata.indices))
@@ -91,49 +98,46 @@ function defaultWGPUMesh(path::String; scale::Union{Vector{Float32}, Float32} =
9198
else
9299
scale = scale |> diagm
93100
end
94-
# TODO blender conversion
95-
# swapMat = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1] .|> Float32;
96-
swapMat = [1 0 0 0; 0 0 -1 0; 0 1 0 0; 0 0 0 1] .|> Float32;
97-
101+
98102
vertexData = scale*swapMat*vertexData
99103
uvData = nothing
100104
textureData = nothing
101105
texture = nothing
102106
textureView = nothing
103-
107+
104108
if image != ""
105109
uvData = reduce(hcat, meshdata.uvs[uIndices[:]]) .|> Float32
106110
textureData = begin
107111
img = load(image)
108112
img = imresize(img, (256, 256)) # TODO hardcoded size
109113
img = RGBA.(img)
110-
imgview = channelview(img) |> collect
114+
imgview = channelview(img) |> collect
111115
end
112116
end
113-
117+
114118
indexData = 0:length(vIndices)-1 |> collect .|> UInt32
115119
unitColor = cat([
116120
color
117121
]..., dims=2) .|> Float32
118-
122+
119123
colorData = repeat(unitColor, inner=(1, length(vIndices)))
120-
124+
121125
normalData = scale*swapMat*reduce(hcat, meshdata.normals[nIndices[:]]) .|> Float32
122-
126+
123127
mesh = WGPUMesh(
124-
nothing,
128+
nothing,
125129
topology,
126130
vertexData,
127-
colorData,
128-
indexData,
129-
normalData,
130-
uvData,
131-
nothing,
131+
colorData,
132+
indexData,
133+
normalData,
134+
uvData,
135+
nothing,
132136
nothing,
133137
nothing,
134138
nothing,
135139
textureData,
136-
nothing,
140+
nothing,
137141
nothing,
138142
nothing,
139143
Dict(),

0 commit comments

Comments
 (0)