try write material system

This commit is contained in:
Dmitriy Dergachev 2026-02-11 18:56:49 +03:00
parent 72b8118c60
commit 6019de4159
7 changed files with 204 additions and 2 deletions

View File

@ -2,12 +2,14 @@ package main
import ( import (
"log" "log"
"runtime"
"git.influ.su/artmares/art3de/pkg/engine" "git.influ.su/artmares/art3de/pkg/engine"
"git.influ.su/artmares/art3de/pkg/engine/renderer" "git.influ.su/artmares/art3de/pkg/engine/renderer"
) )
func main() { func main() {
runtime.LockOSThread()
// Создаем движок // Создаем движок
eng := engine.NewEngine("My Game", 1280, 1024) eng := engine.NewEngine("My Game", 1280, 1024)

5
go.mod
View File

@ -3,6 +3,7 @@ module git.influ.su/artmares/art3de
go 1.25.6 go 1.25.6
require ( require (
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 // indirect github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20250301202403-da16c1255728 // indirect github.com/go-gl/glfw/v3.3/glfw v0.0.0-20250301202403-da16c1255728
github.com/go-gl/mathgl v1.2.0
) )

2
go.sum
View File

@ -2,3 +2,5 @@ github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 h1:5BVwOaUSBTlVZowGO6VZGw
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw= github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20250301202403-da16c1255728 h1:RkGhqHxEVAvPM0/R+8g7XRwQnHatO0KAuVcwHo8q9W8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20250301202403-da16c1255728 h1:RkGhqHxEVAvPM0/R+8g7XRwQnHatO0KAuVcwHo8q9W8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20250301202403-da16c1255728/go.mod h1:SyRD8YfuKk+ZXlDqYiqe1qMSqjNgtHzBTG810KUagMc= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20250301202403-da16c1255728/go.mod h1:SyRD8YfuKk+ZXlDqYiqe1qMSqjNgtHzBTG810KUagMc=
github.com/go-gl/mathgl v1.2.0 h1:v2eOj/y1B2afDxF6URV1qCYmo1KW08lAMtTbOn3KXCY=
github.com/go-gl/mathgl v1.2.0/go.mod h1:pf9+b5J3LFP7iZ4XXaVzZrCle0Q/vNpB/vDe5+3ulRE=

View File

@ -0,0 +1,55 @@
package material
// Material - интерфейс материала
type Material interface {
// ID возвращает уникальный идентификатор материала
ID() uint32
// Name возвращает имя материала
Name() string
// Type возвращает тип материала
Type() Type
// Shader возвращает шейдерную программу
Shader() uint32
// Apply применяет материал для рендеринга
Apply(instance Instance)
// Update обновляет состояние материала (для анимированных)
Update(deltaTime float32)
// CreateInstace создает экземпляр материала
CreateInstance() Instance
// Destroy освобождает ресурсы материала
Destroy()
}
// Instance - экземпляр материала с переопределенными параметрами
type Instance interface {
// Material возвращает базовый материал
Material() Material
// SetParameter устанавливает параметр
SetParameter(name string, value any) error
// GetParameter возвращает параметер
GetParameter(name string) (any, error)
// SetTexture переопределяет текстуру
SetTexture(textureType TextureType, texture Texture) error
// GetTexture возвращает текстуру
GetTexture(textureType TextureType) Texture
// Clone создает копию экземпляра
Clone() Instance
}
// Texture - интерфейс текстуры
type Texture interface {
// ID возвращает OpenGL ID текстуры
ID() uint32
// Type возвращает тип текстуры
Type() TextureType
// Path возвращает путь к файлу
Path() string
// Width возвращает ширину
Width() int
// Height возвращает высоту
Height() int
// Format возвращает формат
Format() uint32
// Destroy освобождает ресурсы
Destroy()
}

View File

@ -0,0 +1,45 @@
package manager
import "git.influ.su/artmares/art3de/pkg/render/material"
// Manager - Интерфейс менеджера материалов
type Manager interface {
// CreatePBRMaterial создает PBR материал
CreatePBRMaterial(name string, shader uint32, config *PBRConfig) (material.Material, error)
// GetMaterial возвращает материал по имени
GetMaterial(name string) material.Material
// HasMaterial проверяет существование материала
HasMaterial(name string) bool
// DestroyMaterial уничтожает материал
DestroyMaterial(name string) error
// LoadTexture загружает текстуру
LoadTexture(path string, textureType material.TextureType) (material.Texture, error)
// GetTexture возвращает текстуру из кэша
GetTexture(path string) material.Texture
// PreloadTextures предзагружает текстуры из директории
PreloadTextures(dir string) error
// Update обновляет все материалы
Update(deltaTime float32)
// Cleanup освобождает все ресурсы
Cleanup()
// Stats возвращает статистику
Stats() Stats
}
// PBRConfig - конфигурация PBR материала
type PBRConfig struct {
Name string
Shader uint32
Format material.TextureFormat
TextureDir string
TexturePrefix string
Params *material.PBRParams
}
// Stats - статистика менеджера
type Stats struct {
Materials int
Textures int
TextureMemory int64
Instances int
}

View File

@ -0,0 +1,34 @@
package manager
import (
"fmt"
"sync"
"git.influ.su/artmares/art3de/pkg/render/material"
)
type materialManager struct {
materials map[string]material.Material
textureCache *textureCache
boundShader uint32
mu sync.RWMutex
stats Stats
}
// New создает новый менеджер материалов
func New() Manager {
return &materialManager{
materials: make(map[string]material.Material),
textureCache: newTextureCache(),
}
}
func (m *materialManager) CreatePBRMaterial(name string, shader uint32, config *PBRConfig) (material.Material, error) {
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.materials[name]; exists {
return nil, fmt.Errorf("material %s already exists", name)
}
}

View File

@ -0,0 +1,63 @@
package material
import (
"github.com/go-gl/mathgl/mgl32"
)
// Type - тип материала
type Type int
const (
TypeUnknown Type = iota
TypePBR
TypeUnlit
TypeLiquid
TypeGas
)
// TextureType - тип текстуры
type TextureType int
const (
TextureUnknown TextureType = iota
TextureAlbedo
TextureNormal
TextureORM
TextureEmissive
TextureMetallic
TextureRoughness
TextureAO
TextureHeight
TextureSpecular
)
// TextureFormat - формат текстурного набора
type TextureFormat int
const (
FormatSeparate TextureFormat = iota // Отдельные текстуры
FormatORM // ORM _ Albedo + Normal
FormatPBRSet // Полный PBR набор
)
// PBRParams - параметры PBR материала
type PBRParams struct {
Albedo mgl32.Vec3
Metallic float32
Roughness float32
AO float32
Emissive mgl32.Vec3
Alpha float32
}
// DefaultPBRParams возвращает параметры по уполчанию
func DefaultPBRParams() PBRParams {
return PBRParams{
Albedo: mgl32.Vec3{1.0, 1.0, 1.0},
Metallic: 0.0,
Roughness: 0.5,
AO: 1.0,
Emissive: mgl32.Vec3{0.0, 0.0, 0.0},
Alpha: 1.0,
}
}