Skip to content

package terrain

import "kaijuengine.com/engine/terrain"

Constants

AssetVersion

2

HeightEncodingUint16

HeightEncoding

"uint16-normalized"

WeightEncodingUint16

WeightEncoding

"uint16-normalized"

Types

AutoMaterialRule

struct

type AutoMaterialRule struct {
    Name          string
    Layer         int
    TargetWeight  matrix.Float
    Constraints   TexturePaintConstraints
    NoiseStrength matrix.Float
    NoiseScale    matrix.Float
    NoiseSeed     int
}

TerrainAutoMaterialRules

func TerrainAutoMaterialRules(preset TerrainAutoMaterialPreset) []AutoMaterialRule

BrushFalloff

int

type BrushFalloff int

const ( FalloffLinear BrushFalloff = iota FalloffSmooth FalloffConstant )

BrushMode

int

type BrushMode int

const ( BrushRaise BrushMode = iota BrushLower BrushSmooth )

DirtyRegion

struct

type DirtyRegion struct {
    MinX, MinZ int
    MaxX, MaxZ int
    Valid      bool
}

DirtyRegion.Expand

func (r DirtyRegion) Expand(padding, resolution int) DirtyRegion

DirtyRegion.Intersects

func (r DirtyRegion) Intersects(other DirtyRegion) bool

HeightEncoding

string

type HeightEncoding string

HeightField

struct

type HeightField struct {
    Resolution int
    Heights    []matrix.Float
    MinHeight  matrix.Float
    MaxHeight  matrix.Float

    // Has unexported fields.
}

NewHeightField

func NewHeightField(resolution int, minHeight, maxHeight, initialHeight matrix.Float) (*HeightField, error)

HeightField.AddHeight

func (h *HeightField) AddHeight(x, z int, delta matrix.Float) bool

HeightField.ClearDirty

func (h *HeightField) ClearDirty()

HeightField.CopyRegion

func (h *HeightField) CopyRegion(region DirtyRegion) []matrix.Float

HeightField.DirtyRegion

func (h *HeightField) DirtyRegion() DirtyRegion

HeightField.Height

func (h *HeightField) Height(x, z int) matrix.Float

HeightField.Paint

func (h *HeightField) Paint(stroke PaintStroke) DirtyRegion

HeightField.Sample

func (h *HeightField) Sample(x, z matrix.Float) matrix.Float

HeightField.SetHeight

func (h *HeightField) SetHeight(x, z int, height matrix.Float) bool

HeightField.SetRegion

func (h *HeightField) SetRegion(region DirtyRegion, heights []matrix.Float) DirtyRegion

PaintStroke

struct

type PaintStroke struct {
    Mode     BrushMode
    Center   matrix.Vec2
    Radius   matrix.Float
    Strength matrix.Float
    Falloff  BrushFalloff
    Spacing  matrix.Float
}

SplatLayerChannel

struct

type SplatLayerChannel struct {
    Texture int
    Channel int
}

Terrain

struct

type Terrain struct {
    Config        TerrainConfig
    Entity        *engine.Entity
    Transform     *matrix.Transform
    HeightField   *HeightField
    LayerSet      *TerrainLayerSet
    SplatTextures []TerrainSplatTexture
    MeshChunks    []TerrainChunk
    Material      *rendering.Material
    ShaderData    []rendering.DrawInstance

    // Has unexported fields.
}

Load

func Load(host *engine.Host, id string) (*Terrain, error)

LoadForEntity

func LoadForEntity(host *engine.Host, id string, entity *engine.Entity) (*Terrain, error)

New

func New(host *engine.Host, config TerrainConfig) (*Terrain, error)

NewFromAsset

func NewFromAsset(host *engine.Host, asset TerrainAsset) (*Terrain, error)

NewFromAssetForEntity

func NewFromAssetForEntity(host *engine.Host, asset TerrainAsset, entity *engine.Entity) (*Terrain, error)

NewModel

func NewModel(config TerrainConfig) (*Terrain, error)

NewModelFromAsset

func NewModelFromAsset(asset TerrainAsset) (*Terrain, error)

Terrain.AddLayer

func (t *Terrain) AddLayer(layer TerrainLayer) int

Terrain.ApplyAutoMaterialRules

func (t *Terrain) ApplyAutoMaterialRules(rules []AutoMaterialRule) DirtyRegion

Terrain.ApplyDirty

func (t *Terrain) ApplyDirty()

Terrain.ApplyHeightRegion

func (t *Terrain) ApplyHeightRegion(region DirtyRegion, heights []matrix.Float) DirtyRegion

Terrain.ApplyLayerSetState

func (t *Terrain) ApplyLayerSetState(state TerrainLayerSetState) bool

Terrain.ApplyTextureDirty

func (t *Terrain) ApplyTextureDirty(region DirtyRegion)

Terrain.ApplyTextureWeightRegion

func (t *Terrain) ApplyTextureWeightRegion(region DirtyRegion, weights []matrix.Float) DirtyRegion

Terrain.ClearBrushPreview

func (t *Terrain) ClearBrushPreview()

Terrain.ClearLayer

func (t *Terrain) ClearLayer(layer int) DirtyRegion

Terrain.Collision

func (t *Terrain) Collision() (*graviton.TerrainCollision, error)

Terrain.CollisionBounds

func (t *Terrain) CollisionBounds() graviton.AABB

CollisionBounds returns the local-space bounds used by terrain collision.

Terrain.CopyTextureWeightRegion

func (t *Terrain) CopyTextureWeightRegion(region DirtyRegion) []matrix.Float

Terrain.Destroy

func (t *Terrain) Destroy(host *engine.Host)

Terrain.EraseLayer

func (t *Terrain) EraseLayer(layer int, stroke PaintStroke) DirtyRegion

Terrain.FillLayer

func (t *Terrain) FillLayer(layer int) DirtyRegion

Terrain.HeightAtLocal

func (t *Terrain) HeightAtLocal(localXZ matrix.Vec2) matrix.Float

Terrain.HeightAtWorld

func (t *Terrain) HeightAtWorld(point matrix.Vec3) matrix.Float

Terrain.LayerCount

func (t *Terrain) LayerCount() int

Terrain.LayerSetState

func (t *Terrain) LayerSetState() TerrainLayerSetState

Terrain.LayerWeightAt

func (t *Terrain) LayerWeightAt(layer, x, z int) matrix.Float

Terrain.MarkTextureDirty

func (t *Terrain) MarkTextureDirty(layer int, region DirtyRegion)

Terrain.MarkTextureRegionDirty

func (t *Terrain) MarkTextureRegionDirty(region DirtyRegion)

Terrain.MoveLayer

func (t *Terrain) MoveLayer(from, to int) bool

Terrain.NewCollision

func (t *Terrain) NewCollision() *graviton.TerrainCollision

NewCollision creates a Graviton terrain collider backed by the terrain height storage. Height edits are visible to the collider immediately; do not mutate terrain heights concurrently with a physics step.

Terrain.NormalizeWeightsAt

func (t *Terrain) NormalizeWeightsAt(x, z int) bool

Terrain.Paint

func (t *Terrain) Paint(stroke PaintStroke) DirtyRegion

Terrain.PaintLayer

func (t *Terrain) PaintLayer(layer int, stroke PaintStroke) DirtyRegion

Terrain.PaintLine

func (t *Terrain) PaintLine(from, to matrix.Vec2, stroke PaintStroke) DirtyRegion

Terrain.PaintTextureLayer

func (t *Terrain) PaintTextureLayer(layer int, stroke TexturePaintStroke) TexturePaintResult

Terrain.PaintTextureLine

func (t *Terrain) PaintTextureLine(layer int, from, to matrix.Vec2, stroke TexturePaintStroke) TexturePaintResult

Terrain.RayHit

func (t *Terrain) RayHit(ray graviton.Ray) (TerrainRayHit, bool)

Terrain.RayHitLocal

func (t *Terrain) RayHitLocal(ray graviton.Ray) (TerrainRayHit, bool)

Terrain.RefreshTexturePreview

func (t *Terrain) RefreshTexturePreview()

Terrain.RemoveLayer

func (t *Terrain) RemoveLayer(layer int) bool

Terrain.SampleLayerWeightAtLocal

func (t *Terrain) SampleLayerWeightAtLocal(layer int, localXZ matrix.Vec2) matrix.Float

Terrain.SampleTextureLayerAtLocal

func (t *Terrain) SampleTextureLayerAtLocal(localXZ matrix.Vec2) TexturePaintResult

Terrain.SetBrushPreview

func (t *Terrain) SetBrushPreview(centerXZ matrix.Vec2, radius, ringWidth matrix.Float, color matrix.Color)

Terrain.SetLayer

func (t *Terrain) SetLayer(layer int, value TerrainLayer) bool

Terrain.SplatLayerChannel

func (t *Terrain) SplatLayerChannel(layer int) (SplatLayerChannel, bool)

Terrain.SplatTextureCount

func (t *Terrain) SplatTextureCount() int

Terrain.SplatTextureWriteRequest

func (t *Terrain) SplatTextureWriteRequest(texture int, region DirtyRegion) rendering.GPUImageWriteRequest

Terrain.StrokeRegion

func (t *Terrain) StrokeRegion(stroke PaintStroke) DirtyRegion

Terrain.TextureStrokeRegion

func (t *Terrain) TextureStrokeRegion(stroke TexturePaintStroke) DirtyRegion

Terrain.VisitPaintLineStamps

func (t *Terrain) VisitPaintLineStamps(from, to matrix.Vec2, stroke PaintStroke, visit func(PaintStroke) bool)

Terrain.VisitTexturePaintLineStamps

func (t *Terrain) VisitTexturePaintLineStamps(from, to matrix.Vec2, stroke TexturePaintStroke, visit func(TexturePaintStroke) bool)

Terrain.WeightDebugRGBA

func (t *Terrain) WeightDebugRGBA(layer int) []byte

TerrainAsset

struct

type TerrainAsset struct {
    Version             int
    Config              TerrainConfig
    Heights             []uint16
    Layers              []TerrainLayer
    WeightMapResolution int
    Weights             []uint16
}

DeserializeAsset

func DeserializeAsset(data []byte) (TerrainAsset, error)

LoadAsset

func LoadAsset(assetDb assets.Database, id string) (TerrainAsset, error)

NewAsset

func NewAsset(config TerrainConfig, heights []matrix.Float) (TerrainAsset, error)

NewAssetFromHeightField

func NewAssetFromHeightField(config TerrainConfig, field *HeightField) (TerrainAsset, error)

NewAssetFromTerrain

func NewAssetFromTerrain(model *Terrain) (TerrainAsset, error)

NewAssetWithLayerSet

func NewAssetWithLayerSet(config TerrainConfig, heights []matrix.Float, layerSet *TerrainLayerSet) (TerrainAsset, error)

TerrainAsset.FloatHeights

func (a TerrainAsset) FloatHeights() []matrix.Float

TerrainAsset.Height

func (a TerrainAsset) Height(x, z int) matrix.Float

TerrainAsset.LayerSet

func (a TerrainAsset) LayerSet() (*TerrainLayerSet, error)

TerrainAsset.Serialize

func (a TerrainAsset) Serialize() ([]byte, error)

TerrainAutoMaterialPreset

struct

type TerrainAutoMaterialPreset struct {
    GrassLayer    int
    RockLayer     int
    SnowLayer     int
    FlatSlopeMax  matrix.Float
    CliffSlopeMin matrix.Float
    SnowHeightMin matrix.Float
    NoiseStrength matrix.Float
    NoiseScale    matrix.Float
    NoiseSeed     int
}

TerrainChunk

struct

type TerrainChunk struct {
    Key        string
    StartX     int
    StartZ     int
    EndX       int
    EndZ       int
    Mesh       *rendering.Mesh
    Drawing    rendering.Drawing
    ShaderData rendering.DrawInstance
    Indexes    []uint32
}

TerrainConfig

struct

type TerrainConfig struct {
    Resolution      int
    PaintResolution int
    WorldSize       matrix.Vec2
    MinHeight       matrix.Float
    MaxHeight       matrix.Float
    InitialHeight   matrix.Float
    ChunkSize       int
    Material        string
    Textures        []TerrainTexture
    ShaderData      string
}

TerrainLayer

struct

type TerrainLayer struct {
    Name               string
    TextureContentID   string
    NormalContentID    string
    RoughnessContentID string
    Filter             rendering.TextureFilter
    Tiling             matrix.Vec2
    Offset             matrix.Vec2
    Rotation           matrix.Float
    Tint               matrix.Color
    TextureWorldSize   matrix.Vec2
    Locked             bool
    Hidden             bool
    Solo               bool
    TriplanarCliffs    bool
    TriplanarSlope     matrix.Float
}

TerrainLayer describes one paintable material layer on a terrain.

TextureContentID is the albedo texture currently consumed by the terrain material. NormalContentID and RoughnessContentID are stored for authoring and future material expansion. Locked layers preserve their current contribution during painting, while Hidden and Solo only affect preview packing.

NewTerrainLayer

func NewTerrainLayer(textureContentID string) TerrainLayer

TerrainLayerSet

struct

type TerrainLayerSet struct {
    Layers    []TerrainLayer
    WeightMap *TextureWeightMap
    // Has unexported fields.
}

TerrainLayerSet owns the ordered TerrainLayer list and the normalized weight map with matching layer indexes.

Mutate layers through this type when possible. Add, remove, move, fill, clear, and paint operations keep the TextureWeightMap shape aligned with Layers and preserve per-texel normalization.

NewTerrainLayerSet

func NewTerrainLayerSet(resolution int) (*TerrainLayerSet, error)

TerrainLayerSet.AddLayer

func (s *TerrainLayerSet) AddLayer(layer TerrainLayer) int

TerrainLayerSet.ClearLayer

func (s *TerrainLayerSet) ClearLayer(layer int) DirtyRegion

TerrainLayerSet.EffectiveWeightMapForPreview

func (s *TerrainLayerSet) EffectiveWeightMapForPreview() *TextureWeightMap

TerrainLayerSet.EraseLayer

func (s *TerrainLayerSet) EraseLayer(layer int, stroke PaintStroke) DirtyRegion

TerrainLayerSet.FillLayer

func (s *TerrainLayerSet) FillLayer(layer int) DirtyRegion

TerrainLayerSet.LayerCount

func (s *TerrainLayerSet) LayerCount() int

TerrainLayerSet.LayerWeightAt

func (s *TerrainLayerSet) LayerWeightAt(layer, x, z int) matrix.Float

TerrainLayerSet.MoveLayer

func (s *TerrainLayerSet) MoveLayer(from, to int) bool

TerrainLayerSet.NormalizeWeightsAt

func (s *TerrainLayerSet) NormalizeWeightsAt(x, z int) bool

TerrainLayerSet.PaintLayer

func (s *TerrainLayerSet) PaintLayer(layer int, stroke PaintStroke) DirtyRegion

TerrainLayerSet.PaintTextureLayer

func (s *TerrainLayerSet) PaintTextureLayer(layer int, stroke TexturePaintStroke) TexturePaintResult

TerrainLayerSet.RemoveLayer

func (s *TerrainLayerSet) RemoveLayer(layer int) bool

TerrainLayerSet.SetLayer

func (s *TerrainLayerSet) SetLayer(layer int, value TerrainLayer) bool

TerrainLayerSet.SetLayerWeightAt

func (s *TerrainLayerSet) SetLayerWeightAt(layer, x, z int, weight matrix.Float) bool

TerrainLayerSet.WeightDebugRGBA

func (s *TerrainLayerSet) WeightDebugRGBA(layer int) []byte

TerrainLayerSetState

struct

type TerrainLayerSetState struct {
    Layers              []TerrainLayer
    WeightMapResolution int
    Weights             []matrix.Float
}

TerrainLayerTextureDiagnostic

struct

type TerrainLayerTextureDiagnostic struct {
    Layer            int
    Name             string
    TextureContentID string
}

MissingTerrainLayerTextures

func MissingTerrainLayerTextures(layers []TerrainLayer, textureExists func(string) bool) []TerrainLayerTextureDiagnostic

TerrainRayHit

struct

type TerrainRayHit struct {
    Point      matrix.Vec3
    LocalPoint matrix.Vec3
    Normal     matrix.Vec3
    Distance   matrix.Float
}

TerrainShaderData

shader_data_registry.ShaderDataTerrain

type TerrainShaderData = shader_data_registry.ShaderDataTerrain

TerrainSplatTexture

struct

type TerrainSplatTexture struct {
    Key        string
    Texture    *rendering.Texture
    LayerStart int
    LayerCount int
    Pixels     []byte
    Dirty      DirtyRegion

    // Has unexported fields.
}

TerrainTexture

struct

type TerrainTexture struct {
    Key    string
    Filter rendering.TextureFilter
}

TextureBrushMode

int

type TextureBrushMode int

const ( TextureBrushPaint TextureBrushMode = iota TextureBrushErase TextureBrushSmoothWeights TextureBrushFill TextureBrushReplace TextureBrushSample )

TextureBrushStamp

struct

type TextureBrushStamp struct {
    Resolution int
    Alpha      []matrix.Float
}

TextureBrushStamp.Sample

func (s *TextureBrushStamp) Sample(x, z, scale, rotation matrix.Float) matrix.Float

TexturePaintConstraints

struct

type TexturePaintConstraints struct {
    UseSlope     bool
    SlopeMin     matrix.Float
    SlopeMax     matrix.Float
    UseHeight    bool
    HeightMin    matrix.Float
    HeightMax    matrix.Float
    AngleCutoff  matrix.Float
    NormalFacing matrix.Vec3
}

TexturePaintResult

struct

type TexturePaintResult struct {
    Dirty         DirtyRegion
    Sampled       bool
    SampledLayer  int
    SampledWeight matrix.Float
}

TexturePaintStroke

struct

type TexturePaintStroke struct {
    Mode                TextureBrushMode
    Center              matrix.Vec2
    Radius              matrix.Float
    Strength            matrix.Float
    Falloff             BrushFalloff
    Opacity             matrix.Float
    TargetWeight        matrix.Float
    Spacing             matrix.Float
    ReplaceLayer        int
    PreserveOtherLayers bool
    Constraints         TexturePaintConstraints
    NoiseStrength       matrix.Float
    NoiseScale          matrix.Float
    NoiseSeed           int
    Jitter              matrix.Float
    Stamp               *TextureBrushStamp
    StampScale          matrix.Float
    StampRotation       matrix.Float
}

TextureWeightMap

struct

type TextureWeightMap struct {
    Resolution int
    Layers     int
    Weights    []matrix.Float

    // Has unexported fields.
}

TextureWeightMap stores terrain texture blend weights in cell-major order.

The backing layout is ((x + z*Resolution) * Layers) + layer. Every texel is expected to normalize across all layers. Use SetWeightAt followed by NormalizeWeightsAt for direct edits, or prefer PaintTextureLayer for editor brush behavior with falloff, locks, smoothing, replace, sampling, and fill modes.

NewTextureWeightMap

func NewTextureWeightMap(resolution, layers int) (*TextureWeightMap, error)

TextureWeightMap.AddLayer

func (m *TextureWeightMap) AddLayer(defaultWeight matrix.Float)

TextureWeightMap.ClearLayer

func (m *TextureWeightMap) ClearLayer(clearLayer int) DirtyRegion

TextureWeightMap.ClearLayerWithLocks

func (m *TextureWeightMap) ClearLayerWithLocks(clearLayer int, locked []bool) DirtyRegion

TextureWeightMap.CopyRegion

func (m *TextureWeightMap) CopyRegion(region DirtyRegion) []matrix.Float

TextureWeightMap.EraseLayer

func (m *TextureWeightMap) EraseLayer(layer int, stroke PaintStroke) DirtyRegion

TextureWeightMap.FillLayer

func (m *TextureWeightMap) FillLayer(fillLayer int) DirtyRegion

TextureWeightMap.FillLayerWithLocks

func (m *TextureWeightMap) FillLayerWithLocks(fillLayer int, locked []bool) DirtyRegion

TextureWeightMap.MoveLayer

func (m *TextureWeightMap) MoveLayer(from, to int) bool

TextureWeightMap.NormalizeAll

func (m *TextureWeightMap) NormalizeAll()

TextureWeightMap.NormalizeWeightsAt

func (m *TextureWeightMap) NormalizeWeightsAt(x, z int) bool

TextureWeightMap.PaintLayer

func (m *TextureWeightMap) PaintLayer(layer int, stroke PaintStroke) DirtyRegion

TextureWeightMap.PaintTextureLayer

func (m *TextureWeightMap) PaintTextureLayer(layer int, stroke TexturePaintStroke) TexturePaintResult

TextureWeightMap.PaintTextureLayerWithLocks

func (m *TextureWeightMap) PaintTextureLayerWithLocks(layer int, stroke TexturePaintStroke, locked []bool) TexturePaintResult

TextureWeightMap.RemoveLayer

func (m *TextureWeightMap) RemoveLayer(removeLayer int) bool

TextureWeightMap.Sample

func (m *TextureWeightMap) Sample(layer int, x, z matrix.Float) matrix.Float

TextureWeightMap.SetRegion

func (m *TextureWeightMap) SetRegion(region DirtyRegion, weights []matrix.Float) DirtyRegion

TextureWeightMap.SetWeightAt

func (m *TextureWeightMap) SetWeightAt(layer, x, z int, weight matrix.Float) bool

TextureWeightMap.WeightAt

func (m *TextureWeightMap) WeightAt(layer, x, z int) matrix.Float

WeightEncoding

string

type WeightEncoding string