Skip to content

Commit

Permalink
Merge pull request ikemen-engine#1555 from Eiton/3d-stage
Browse files Browse the repository at this point in the history
fix: 3d stage animation looping fix
  • Loading branch information
K4thos authored Dec 11, 2023
2 parents 867c530 + 527cd05 commit 0154f61
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/render_gl.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,15 @@ func (r *Renderer) SetModelPipeline(eq BlendEquation, src, dst BlendFunc, depthM
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.indexBuffer)
gl.EnableVertexAttribArray(uint32(r.modelShader.aPos))
gl.EnableVertexAttribArray(uint32(r.modelShader.aUv))
gl.EnableVertexAttribArray(uint32(r.modelShader.aColor))
gl.VertexAttribPointerWithOffset(uint32(r.modelShader.aPos), 3, gl.FLOAT, false, 0, uintptr(offset1))
gl.VertexAttribPointerWithOffset(uint32(r.modelShader.aUv), 2, gl.FLOAT, false, 0, uintptr(offset2))
gl.VertexAttribPointerWithOffset(uint32(r.modelShader.aColor), 4, gl.FLOAT, false, 0, uintptr(offset3))
if offset3 > 0 {
gl.EnableVertexAttribArray(uint32(r.modelShader.aColor))
gl.VertexAttribPointerWithOffset(uint32(r.modelShader.aColor), 4, gl.FLOAT, false, 0, uintptr(offset3))
} else {
gl.VertexAttrib4f(uint32(r.modelShader.aColor), 1, 1, 1, 1)
}

}
func (r *Renderer) ReleaseModelPipeline() {
gl.DisableVertexAttribArray(uint32(r.modelShader.aPos))
Expand Down
17 changes: 12 additions & 5 deletions src/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,7 @@ type Primitive struct {
vertexBufferOffset uint32
elementBufferOffset uint32
materialIndex *uint32
useVertexColor bool
mode PrimitiveMode
}

Expand Down Expand Up @@ -1860,6 +1861,7 @@ func loadglTFStage(filepath string) (*Model, error) {
}
primitive.numIndices = uint32(len(indices))
if idx, ok := p.Attributes[gltf.COLOR_0]; ok {
primitive.useVertexColor = true
switch doc.Accessors[idx].ComponentType {
case gltf.ComponentUbyte:
if doc.Accessors[idx].Type == gltf.AccessorVec3 {
Expand Down Expand Up @@ -1923,9 +1925,7 @@ func loadglTFStage(filepath string) (*Model, error) {
}
}
} else {
for i := 0; i < int(primitive.numVertices); i++ {
mdl.vertexBuffer = append(mdl.vertexBuffer, 1, 1, 1, 1)
}
primitive.useVertexColor = false
}

if p.Material != nil {
Expand Down Expand Up @@ -2057,7 +2057,11 @@ func drawNode(mdl *Model, n *Node, proj, modelview mgl.Mat4, drawBlended bool) {
return
}
color := mdl.materials[*p.materialIndex].baseColorFactor
gfx.SetModelPipeline(blendEq, src, dst, true, mdl.materials[*p.materialIndex].doubleSided, int(p.vertexBufferOffset), int(p.vertexBufferOffset+12*p.numVertices), int(p.vertexBufferOffset+20*p.numVertices))
if p.useVertexColor {
gfx.SetModelPipeline(blendEq, src, dst, true, mdl.materials[*p.materialIndex].doubleSided, int(p.vertexBufferOffset), int(p.vertexBufferOffset+12*p.numVertices), int(p.vertexBufferOffset+20*p.numVertices))
} else {
gfx.SetModelPipeline(blendEq, src, dst, true, mdl.materials[*p.materialIndex].doubleSided, int(p.vertexBufferOffset), int(p.vertexBufferOffset+12*p.numVertices), -1)
}

gfx.SetModelUniformMatrix("projection", proj[:])
gfx.SetModelUniformMatrix("modelview", modelview[:])
Expand Down Expand Up @@ -2121,7 +2125,7 @@ func (s *Stage) drawModel(pos [2]float32, yofs float32, scl float32) {
func (model *Model) step() {
for _, anim := range model.animations {
anim.time += sys.turbo / 60
for anim.time > anim.duration && anim.duration > 0 {
for anim.time >= anim.duration && anim.duration > 0 {
anim.time -= anim.duration
}
time := 60 * float64(anim.time)
Expand All @@ -2130,6 +2134,9 @@ func (model *Model) step() {
} else if math.Abs(float64(anim.time)-math.Ceil(float64(anim.time))) < 0.001 {
anim.time = float32(math.Ceil(time) / 60)
}
for anim.time >= anim.duration && anim.duration > 0 {
anim.time = anim.duration
}
for _, channel := range anim.channels {
node := model.nodes[channel.nodeIndex]
sampler := anim.samplers[channel.samplerIndex]
Expand Down

0 comments on commit 0154f61

Please sign in to comment.