Skip to main content

Type System and Conversions

Scalar Types

Basic Scalar Types

TypeConstructorRangeGLSL/WGSL Equivalent
floatfloat(value)IEEE 754 32-bitfloat / f32
intint(value)-2³¹ to 2³¹-1int / i32
uintuint(value)0 to 2³²-1uint / u32
boolbool(value)true/falsebool / bool

Scalar Construction

// リテラル値から
const pi = float(3.14159)
const count = int(42)
const flag = bool(true)

// 他のノードから
const computed = float(sin(angle))
const index = int(position.x.mul(10))
const condition = bool(value.greaterThan(threshold))

Vector Types

Vector Constructors

TypeConstructor PatternsComponents
vec2vec2(x, y)x, y
vec2vec2(scalar)scalar, scalar
vec3vec3(x, y, z)x, y, z
vec3vec3(vec2, z)vec2.x, vec2.y, z
vec3vec3(scalar)scalar, scalar, scalar
vec4vec4(x, y, z, w)x, y, z, w
vec4vec4(vec3, w)vec3.x, vec3.y, vec3.z, w
vec4vec4(vec2, vec2)vec2a.x, vec2a.y, vec2b.x, vec2b.y

Vector Examples

// 基本的な構築
const position2D = vec2(100, 200)
const color = vec3(1, 0.5, 0.2)
const homogeneous = vec4(position2D, 0, 1)

// スカラーからの拡張
const white = vec3(1.0) // vec3(1.0, 1.0, 1.0)
const transparent = vec4(color, 0.8)

// 他のベクトルからの構築
const normal = vec3(tangent.xy, sqrt(1.0 - dot(tangent.xy, tangent.xy)))

Matrix Types

Matrix Constructors

TypeConstructorDimensionsElement Order
mat2mat2(...)2×2Column-major
mat3mat3(...)3×3Column-major
mat4mat4(...)4×4Column-major

Matrix Construction Patterns

// 配列からの構築
const transform2D = mat2([1, 0, 0, 1])

// Identity matrix
const identity3 = mat3([1, 0, 0, 0, 1, 0, 0, 0, 1])

// Column vector construction
const rotation = mat3(vec3(cos(angle), sin(angle), 0), vec3(-sin(angle), cos(angle), 0), vec3(0, 0, 1))

Type Conversion Methods

Explicit Conversions

MethodSource TypeTarget TypeBehavior
.toFloat()anyfloatExtract first component
.toInt()anyintTruncate to integer
.toUint()anyuintConvert to unsigned
.toBool()anyboolNon-zero → true
.toVec2()anyvec2Swizzle or broadcast
.toVec3()anyvec3Swizzle or broadcast
.toVec4()anyvec4Swizzle or broadcast

Conversion Examples

// スカラーからベクトルへ
const brightness = float(0.8)
const gray = brightness.toVec3() // vec3(0.8, 0.8, 0.8)

// ベクトルからスカラーへ
const position = vec3(1, 2, 3)
const x = position.toFloat() // 1.0

// ベクトル間の変換
const color3 = vec3(1, 0, 0)
const color4 = color3.toVec4() // vec4(1, 0, 0, 1)
const coord = color4.toVec2() // vec2(1, 0)

Automatic Type Promotion

Promotion Rules

OperationLeft TypeRight TypeResult Type
Arithmeticfloatvec3vec3
Arithmeticintfloatfloat
Arithmeticvec2vec3vec3
Comparisonfloatvec3bvec3
Logicalboolbvec2bvec2

Promotion Examples

// スカラーとベクトルの演算
const scale = float(2.0)
const position = vec3(1, 0, 0)
const scaled = position.mul(scale) // vec3(2, 0, 0)

// 異なる次元のベクトル演算
const offset2D = vec2(0.1, 0.2)
const position3D = vec3(1, 2, 3)
const result = position3D.add(offset2D) // vec3(1.1, 2.2, 3)

Swizzling System

Swizzling Patterns

PatternCoordinate SystemComponents
xyzwSpatialx, y, z, w
rgbaColorred, green, blue, alpha
stpqTextures, t, p, q

Swizzling Examples

const position = vec4(1, 2, 3, 4)

// Single component access
const x = position.x // float(1)
const g = position.g // float(2) - same as y
const s = position.s // float(1) - same as x

// Multi-component swizzling
const xy = position.xy // vec2(1, 2)
const rgb = position.rgb // vec3(1, 2, 3)
const bgr = position.bgr // vec3(3, 2, 1)

// Repeated components
const xx = position.xx // vec2(1, 1)
const xyxy = position.xyxy // vec4(1, 2, 1, 2)

Advanced Swizzling

// Complex reordering
const original = vec4(1, 2, 3, 4)
const reordered = original.wzyx // vec4(4, 3, 2, 1)
const mixed = original.xzyw // vec4(1, 3, 2, 4)

// Cross-pattern swizzling
const color = vec4(0.8, 0.4, 0.2, 1.0)
const luminance = color.rrr // vec3(0.8, 0.8, 0.8)
const position = color.stpq // Same as color.xyzw

Integer Vector Types

Integer Vector Constructors

TypeConstructorComponent Type
ivec2ivec2(x, y)int
ivec3ivec3(x, y, z)int
ivec4ivec4(x, y, z, w)int
uvec2uvec2(x, y)uint
uvec3uvec3(x, y, z)uint
uvec4uvec4(x, y, z, w)uint

Boolean Vector Types

TypeConstructorComponent Type
bvec2bvec2(x, y)bool
bvec3bvec3(x, y, z)bool
bvec4bvec4(x, y, z, w)bool

Specialized Vector Examples

// Integer vectors for indexing
const indices = ivec3(0, 1, 2)
const offset = uvec2(10, 20)

// Boolean vectors for masking
const mask = bvec3(true, false, true)
const condition = position.greaterThan(vec3(0)) // Returns bvec3

Type Inference Engine

Inference Rules

Type Inference Flow:
Expression

┌───▼───┐
│ Parse │ ──── Identify operators
│ Tree │ Identify operands
└───┬───┘

┌───▼───┐
│Infer │ ──── Apply promotion rules
│Types │ Resolve conflicts
└───┬───┘

┌───▼───┐
│Result │ ──── Final type
│ Type │ Error checking
└───────┘

Complex Type Inference

// 複雑な型推論の例
const a = float(2.0) // float
const b = vec3(1, 0, 0) // vec3
const c = a.add(b) // vec3 (float promoted to vec3)
const d = c.lessThan(vec3(1)) // bvec3 (comparison result)
const e = select(d, c, vec3(0)) // vec3 (conditional selection)

GLRE の型システムは、GLSL/WGSL の厳密な型規則を維持しながら、TypeScript の柔軟性を活用した直感的な型変換を提供します。