CanvasItem

package
v0.0.0-...-a66c66c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package CanvasItem provides a canvas item shader pipeline used for shading 2D objects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fragment

type Fragment struct {
	SDF SDF

	Position  vec2.XY   `gd:"VERTEX"`     // Vertex, in local space.
	UV        vec2.XY   `gd:"UV"`         // Normalized texture coordinates. Range from 0 to 1.
	Color     vec4.RGBA `gd:"COLOR"`      // Color from vertex primitive.
	PointSize float.X   `gd:"POINT_SIZE"` // Point size for point drawing.
}

func (Fragment) AtLightPass

func (frag Fragment) AtLightPass() bool.X

AtLightPass returns true if the fragment is being processed at light pass.

func (Fragment) Fragcoord

func (frag Fragment) Fragcoord() vec4.XYZW

Fragcoord returns the coordinate of pixel center. In screen space. xy specifies position in viewport. Upper-left of the viewport is the origin, (0.0, 0.0).

func (Fragment) Normal

func (frag Fragment) Normal() vec3.XYZ

Normal read from NormalTexture.

func (Fragment) NormalTexture

func (frag Fragment) NormalTexture() texture.Sampler2D[vec4.XYZW]

NormalTexture returns the default 2D normal texture.

func (Fragment) PointCoord

func (frag Fragment) PointCoord() vec2.XY

PointCoord for drawing points.

func (Fragment) ScreenPixelSize

func (frag Fragment) ScreenPixelSize() vec2.XY

ScreenPixelSize of individual pixels. Equal to inverse of resolution.

func (Fragment) ScreenUV

func (frag Fragment) ScreenUV() vec2.XY

ScreenUV returns the screen UV coordinate for current pixel.

func (Fragment) SpecularShininess

func (frag Fragment) SpecularShininess() vec4.RGBA

SpecularShininess returns the specular shininess color, as sampled from the texture.

func (Fragment) SpecularShininessTexture

func (frag Fragment) SpecularShininessTexture() texture.Sampler2D[vec4.RGBA]

SpecularShininessTexture of this object.

func (Fragment) Texture

func (frag Fragment) Texture() texture.Sampler2D[vec4.RGBA]

Texture returns the default 2D texture.

func (Fragment) TexturePixelSize

func (frag Fragment) TexturePixelSize() vec2.XY

TexturePixelSize returns the normalized pixel size of default 2D texture. For a Sprite2D with a texture of size 64x32px, TEXTURE_PIXEL_SIZE = vec2(1/64, 1/32)

type Lighting

type Lighting struct {
	Color            vec4.RGBA `gd:"LIGHT"`           // Output color for this Light2D.
	ShadowModulation vec4.RGBA `gd:"SHADOW_MODULATE"` // Multiply shadows cast at this point by this color.
}

type Material

type Material struct {
	SDF SDF

	Normal             vec3.XYZ  `gd:"NORMAL"`           // Normal vector, in local space.
	NormalMap          vec3.XYZ  `gd:"NORMAL_MAP"`       // Configures normal maps meant for 3D for use in 2D. If used, overrides NORMAL.
	NormalMapDepth     float.X   `gd:"NORMAL_MAP_DEPTH"` // Normal map depth for scaling.
	PixelPosition      vec2.XY   `gd:"VERTEX"`           // Pixel position in screen space.
	ShadowPosition     vec2.XY   `gd:"SHADOW_VERTEX"`    // Same as VERTEX but can be written to alter shadows.
	LightPixelPosition vec3.XYZ  `gd:"LIGHT_VERTEX"`     // Same as VERTEX but can be written to alter lighting. Z component represents height.
	Color              vec4.RGBA `gd:"COLOR"`            // COLOR from the vertex() function multiplied by the TEXTURE color. Also output color value.
}

func (Material) Fragcoord

func (mat Material) Fragcoord() vec4.XYZW

Fragcoord returns the coordinate of pixel center in screen space. xy specifies position in window. Origin is lower left. z specifies fragment depth. It is also used as the output value for the fragment depth unless DEPTH is written to.

func (Material) Light

func (mat Material) Light() vec4.RGBA

Light output color for this Light2D.

func (Material) LightColor

func (mat Material) LightColor() vec4.RGBA

LightColor returns the color of the light, multiplied by the light's texture.

func (Material) LightDirection

func (mat Material) LightDirection() vec3.XYZ

LightDirection in screen space.

func (Material) LightEnergy

func (mat Material) LightEnergy() float.X

LightEnergy energy multiplier of the Light2D.

func (Material) LightIsDirectional

func (mat Material) LightIsDirectional() bool.X

LightIsDirectional returns true if this pass is a DirectionalLight2D.

func (Material) LightPosition

func (mat Material) LightPosition() vec3.XYZ

LightPosition in screen space. If using a DirectionalLight2D this is always (0.0, 0.0, 0.0).

func (Material) PointCoord

func (mat Material) PointCoord() vec2.XY

PointCoord returns the point coordinate for drawing points with POINT_SIZE.

func (Material) ScreenUV

func (mat Material) ScreenUV() vec2.XY

ScreenUV returns the screen UV coordinate for current pixel.

func (Material) SpecularShininess

func (mat Material) SpecularShininess() vec4.XYZW

SpecularShininess as set in the object's texture.

func (Material) Texture

func (mat Material) Texture() texture.Sampler2D[vec4.RGBA]

Texture returns the default 2D texture.

func (Material) TexturePixelSize

func (mat Material) TexturePixelSize() vec2.XY

TexturePixelSize returns the normalized pixel size of default 2D texture. For a Sprite2D with a texture of size 64x32px, TEXTURE_PIXEL_SIZE = vec2(1/64, 1/32)

func (Material) UV

func (mat Material) UV() vec2.XY

UV from the vertex() function, equivalent to the UV in the fragment() function.

type RenderMode

type RenderMode string
const (
	BlendMix            RenderMode = "blend_mix"             // Mix blend mode (alpha is transparency), default.
	BlendAdd            RenderMode = "blend_add"             // Additive blend mode.
	BlendSub            RenderMode = "blend_sub"             // Subtractive blend mode.
	BlendMul            RenderMode = "blend_mul"             // Multiplicative blend mode.
	BlendPremulAlpha    RenderMode = "blend_premul_alpha"    // Premultiplied alpha blend mode (fully transparent RenderMode = add, fully opaque RenderMode = mix).
	BlendDisabled       RenderMode = "blend_disabled"        // Disable blending, values (including alpha) are written as-is.
	Unshaded            RenderMode = "unshaded"              // Result is just albedo. No lighting/shading happens in material, making it faster to render.
	LightOnly           RenderMode = "light_only"            // Only draw on light pass.
	SkipVertexTransform RenderMode = "skip_vertex_transform" // VERTEX needs to be transformed manually in the vertex() function.
	WorldVertexCoords   RenderMode = "world_vertex_coords"   // VERTEX is modified in world coordinates instead of local.
)

type SDF

type SDF struct{}

SDF provides a few additional functions implemented to sample an automatically generated Signed Distance Field texture. These functions available for the fragment() and light() functions of CanvasItem shaders. Custom functions may also use them as long as they called from supported functions.

The signed distance field is generated from LightOccluder2D nodes present in the scene with the SDF Collision property enabled (which is the default). See the 2D lights and shadows documentation for more information.

func (SDF) FromScreenUV

func (mat SDF) FromScreenUV(uv vec2.XY) vec2.XY

FromScreenUV converts screen UV to an SDF.

func (SDF) Texture

func (mat SDF) Texture(pos vec2.XY) float.X

Texture performs an SDF texture lookup.

func (SDF) TextureNormal

func (mat SDF) TextureNormal(pos vec2.XY) vec3.XYZ

TextureNormal calculates a normal from the SDF texture.

func (SDF) ToScreenUV

func (mat SDF) ToScreenUV(pos vec2.XY) vec2.XY

ToScreenUV converts an SDF to screen UV.

type Shader

type Shader[T gdclass.Interface] struct {
	ShaderMaterial.Extension[T]
}

Shader used to draw all 2D elements in Godot. These include all nodes that inherit from CanvasItems, and all GUI elements.

CanvasItem shaders contain fewer built-in variables and functionality than Spatial shaders, but they maintain the same basic structure with vertex, fragment, and light processor functions.

func (*Shader[T]) Fragment

func (*Shader[T]) Fragment(vertex Vertex) Fragment

func (*Shader[T]) Lighting

func (*Shader[T]) Lighting(material Material) Lighting

func (*Shader[T]) Material

func (*Shader[T]) Material(fragment Fragment) Material

func (*Shader[T]) OnCreate

func (s *Shader[T]) OnCreate(value reflect.Value)

func (*Shader[T]) Pipeline

func (*Shader[T]) Pipeline() [3]string

func (*Shader[T]) RenderMode

func (*Shader[T]) RenderMode() []RenderMode

func (*Shader[T]) ShaderType

func (*Shader[T]) ShaderType() string

type Vertex

type Vertex struct {
	ModelMatrix mat4.ColumnMajor `gd:"MODEL_MATRIX"` // Model/local space to world space transform. World space is the coordinates you normally use in the editor.

	// World space to canvas space transform. In canvas space the origin is the upper-left corner of the
	// screen and coordinates ranging from (0, 0) to viewport size.
	CanvasMatrix mat4.ColumnMajor `gd:"CANVAS_MATRIX"`
	// Canvas space to clip space. In clip space coordinates ranging from (-1, -1) to (1, 1).
	ScreenMatrix mat4.ColumnMajor `gd:"SCREEN_MATRIX"`

	InstanceID     int.X     `gd:"INSTANCE_ID"`     // InstanceID for instancing.
	InstanceCustom vec4.XYZW `gd:"INSTANCE_CUSTOM"` // InstanceCustom data.
	AtLightPass    bool.X    `gd:"AT_LIGHT_PASS"`   // Always false.

	// Normalized pixel size of default 2D texture. For a Sprite2D with a texture of size 64x32px,
	// TEXTURE_PIXEL_SIZE = vec2(1/64, 1/32)
	TexturePixelSize vec2.XY `gd:"TEXTURE_PIXEL_SIZE"`

	Position vec2.XY `gd:"VERTEX"`    // Vertex, in local space.
	ID       float.X `gd:"VERTEX_ID"` // The index of the current vertex in the vertex buffer.

	UV        vec2.XY   `gd:"UV"`         // Normalized texture coordinates. Range from 0 to 1.
	Color     vec4.RGBA `gd:"COLOR"`      // Color from vertex primitive.
	PointSize float.X   `gd:"POINT_SIZE"` // Point size for point drawing.
	Custom0   vec4.XYZW `gd:"CUSTOM0"`    // Custom value from vertex primitive.
	Custom1   vec4.XYZW `gd:"CUSTOM1"`    // Custom value from vertex primitive.
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL