zworld/engine/render/pass/material.go

69 lines
2.0 KiB
Go
Raw Permalink Normal View History

2024-01-14 22:56:06 +08:00
package pass
import (
"zworld/engine/object/light"
"zworld/engine/object/mesh"
"zworld/engine/render/uniform"
"zworld/engine/renderapi"
"zworld/engine/renderapi/cache"
"zworld/engine/renderapi/command"
"zworld/engine/renderapi/material"
"zworld/engine/renderapi/texture"
"zworld/plugins/math/vec2"
"zworld/plugins/math/vec4"
)
type MaterialCache cache.T[*material.Def, []Material]
// Material implements render logic for a specific material.
type Material interface {
ID() material.ID
Destroy()
// Begin is called prior to drawing, once per frame.
// Its purpose is to clear object buffers and set up per-frame data such as cameras & lighting.
Begin(uniform.Camera, []light.T)
// BeginGroup is called just prior to recording draw calls.
// Its called once for each group, and may be called multiple times each frame.
// The primary use for it is to bind the material prior to drawing each group.
Bind(command.Recorder)
// Draw is called for each mesh in the group.
// Its purpose is to set up per-draw data such as object transforms and textures
// as well as issuing the draw call.
Draw(command.Recorder, mesh.Mesh)
// End is called after all draw groups have been processed.
// It runs once per frame and is primarily responsible for flushing uniform buffers.
End()
}
func AssignMeshTextures(samplers cache.SamplerCache, msh mesh.Mesh, slots []texture.Slot) [4]uint32 {
textureIds := [4]uint32{}
for id, slot := range slots {
ref := msh.Texture(slot)
if ref != nil {
handle, exists := samplers.TryFetch(ref)
if exists {
textureIds[id] = uint32(handle.ID)
}
}
}
return textureIds
}
func CameraFromArgs(args renderapi.Args) uniform.Camera {
return uniform.Camera{
Proj: args.Projection,
View: args.View,
ViewProj: args.VP,
ProjInv: args.Projection.Invert(),
ViewInv: args.View.Invert(),
ViewProjInv: args.VP.Invert(),
Eye: vec4.Extend(args.Position, 0),
Forward: vec4.Extend(args.Forward, 0),
Viewport: vec2.NewI(args.Viewport.Width, args.Viewport.Height),
}
}