このページはまだ日本語訳がないため、英語の原文を表示しています。
3Drender3d
game/render3d provides mesh buffers, textures and samplers, metallic-roughness PBR materials, directional, spot and point light shadow maps, camera uniforms, offscreen render-to-texture cameras and multi-viewport split-screen rendering, distance and height fog, and an HDR render target with a tone-mapping pass for the NSGo GPU view alongside the GPU-agnostic, zero-steady-state-allocation CPU 3D render pipeline and developer tools: frustum culling, opaque/transparent draw sorting, automatic instancing, LOD selection with hysteresis, camera-facing billboards and 3D ribbon trails, 3D debug wireframes and transform gizmos, frame profiling, and mobile/desktop GPU budgets.
import "github.com/nsgo-dev/game/pkg/render3d"
All matrices, bounding boxes, rays, and frustums follow math3d conventions. To composite 2D sprite batches and screen-space layers over a 3D gpu.View, see Overlay3D in game/scene.
Frustum culling, draw sorting, instancing, and LOD (Queue)
Reuse a Queue across frames and pass your frame's []DrawItem into Queue.Build(viewProj, camPos, items):
queue := &render3d.Queue{
LODHysteresis: 0.1, // 10% deadband prevents LOD popping at thresholds
}
plan := queue.Build(cam.ViewProj(), cam.Pos, drawItems)
// 1. Submit instanced opaque batches (contiguous Mat4 transform slices):
for _, batch := range plan.Batches {
submitInstanced(batch.PipelineID, batch.MaterialID, batch.MeshID, batch.Transforms)
}
// 2. Submit transparent draws strictly back-to-front:
for _, draw := range plan.Transparent {
submitDraw(draw)
}
In one allocation-free steady-state pass, Queue.Build performs:
- Frustum culling: extracts six planes via
math3d.FrustumFromMat4NOand skips off-screenWorldBounds. The[-1, 1]form is exact for OpenGL-style projections (math3d.PerspectiveNO,math3d.OrthoNO) and conservative for[0, 1]ones, so one queue serves both;Queue.FrustumNOis kept for source compatibility and ignored. - Occlusion culling (optional): when
Queue.Occluderis set, frustum-visible items whoseWorldBoundsfailOcclusionBuffer.TestAABBare skipped and counted inRenderStats.Occluded. See Occlusion culling. - LOD selection with hysteresis: evaluates each item's
LODs([]LODLevel, finest to coarsest) using distance andProjectScreenRatio, remembering the prior LOD perDrawItem.ID(SelectLOD). Callqueue.ResetLODHistory()on camera cuts. - Opaque sorting & instancing: orders
BlendOpaqueandBlendAlphaMaskitems by(PipelineID, MaterialID, MeshID, Blend, front-to-back view depth)to minimize state switches and early-Z overdraw, then groups adjacent matching items intoInstanceBatches (unlessDisableInstancingis set). - Transparent sorting: places
BlendAlphaBlenddraws intoplan.Transparent, ordered strictly back-to-front by view depth. Both sorts are stable, so draws whose keys tie keep their submission order from frame to frame.
Occlusion culling (OcclusionBuffer)
OcclusionBuffer is a small CPU depth buffer with a max-Z pyramid. Rasterize large static occluders (walls, floors, buildings) into it once per view, then let the queue skip items hidden behind them. It never culls a visible item: a box that crosses the near plane, or one tested against a view other than the one rasterized, is always reported visible.
occ := render3d.NewOcclusionBuffer(0, 0) // 128x72 by default
walls := []render3d.Occluder{
render3d.NewBoxOccluder(wallBounds, wallTransform),
render3d.NewQuadOccluder(a, b, c, d, true),
render3d.OccluderFromMesh(vertices, indices, transform),
}
// Every frame, with the same viewProj the queue uses:
occ.Clear()
occ.Rasterize(viewProj, walls...)
occ.BuildPyramid()
plan := queue.BuildWithOcclusion(viewProj, camPos, drawItems, occ)
Set OcclusionBuffer.DepthNO for an OpenGL-style [-1, 1] projection so the near plane matches. Occluders must be opaque and closed or double-sided; only meshes that fully hide what is behind them belong here.
Camera uniform on the GPU (CameraBuffer)
CameraBuffer keeps one camera's uniform buffer on an NSGo gpu.Device, with the bind group layout and bind group that expose it to vertex and fragment shaders at CameraGroup (group 0). CameraWGSL declares the matching block, so a 3D shader prepends it and places vertices with camera.view_proj:
struct Camera {
view: mat4x4<f32>,
proj: mat4x4<f32>,
view_proj: mat4x4<f32>,
eye: vec4<f32>,
}
@group(0) @binding(0) var<uniform> camera: Camera;
CameraUniform is the block's data (NewCameraUniform(cam) fills it from a scene3d.Camera) and Encode writes its CameraUniformSize (208) bytes without allocating. Create the buffer once the device is ready, upload the frame's camera before recording draws, and list Layout() at CameraGroup in every 3D pipeline's Layouts:
camBuf, err := render3d.NewCameraBuffer(cx, dev)
// ...
layouts := []*gpu.BindGroupLayout{render3d.CameraGroup: camBuf.Layout()}
pipe, err := dev.CreatePipeline(cx, gpu.PipelineDesc{Layouts: layouts /* shaders, vertex buffers, depth */})
// Each frame, in game.Loop.Draw:
camBuf.WriteCamera(&drawn)
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
The reference tests render the same scene headless through gpu.NewSoftwareRenderer with a perspective and an orthographic camera orbiting on game.Loop, and compare each frame with render3d/testdata/golden/camera_perspective.png and camera_orthographic.png; UPDATE_GOLDEN=1 rewrites them.
Directional, point and spot lights in PBR shading (LightRig, GPULightBuffer)
LightRig holds a scene's linear-RGB lighting: an AmbientLight, up to MaxDirectionalLights (4) DirectionalLights, up to MaxPointLights (16) PointLights, and up to MaxSpotLights (8) SpotLights. One pass shades all of them together, so a scene can mix a sun, lamps and a flashlight.
DirectionalLight: parallel rays travelling alongDirection(for example{Y: -1}shines straight down onto a+Ysurface; normalized when shaded or packed), with linearColorandIntensity.PointLight: omnidirectional emitter atPositionwith linearColor,Intensity, and optional cutoffRange. WhenRange > 0, distance falloff uses the glTFKHR_lights_punctualsmooth windowing curvemax(min(1 - (d/Range)^4, 1), 0)^2 / max(d^2, 1e-4)so contribution reaches zero smoothly atRange;Range == 0uses pure clamped inverse-square1 / max(d^2, 1e-4)(PointLightAttenuation).SpotLight: a glTFKHR_lights_punctualcone atPositionpointing alongDirection(normalized when shaded or packed), with linearColor,IntensityandRangefalloff exactly as for aPointLight, multiplied by the angular falloff between the half-anglesInnerConeAngleandOuterConeAngle(radians,0 <= inner < outer <= pi/2): full intensity inside the inner cone,clamp(cos(angle) * angleScale + angleOffset, 0, 1)^2between the cones, and zero outside the outer cone or behind the light (SpotConeAttenuation, withangleScaleandangleOffsetfromSpotLight.ConeScaleOffset, andSpotLight.Attenuationfor the combined distance and cone factor).Validaterefuses a zero outer cone, an outer cone pastpi/2and an inner cone that is not smaller than the outer one.AmbientLight: uniform isotropic linear fill applied asAmbient.Color * Ambient.Intensity * BaseColor.
GPULightBuffer uploads a validated LightRig into a 1056-byte (LightUniformSize), 16-byte-aligned std140/WGSL uniform buffer bound at LightGroup (group 1), scoped to *core.Scope with optional LightBufferOptions.Retained survival across GPU device loss:
rig := render3d.LightRig{
Ambient: render3d.AmbientLight{Color: math3d.Vec3{X: 0.16, Y: 0.22, Z: 0.32}, Intensity: 0.22},
Directional: []render3d.DirectionalLight{
{Direction: math3d.Vec3{X: 0.55, Y: -0.8, Z: -0.45}, Color: math3d.Vec3{X: 1, Y: 0.96, Z: 0.9}, Intensity: 2.6},
},
Point: []render3d.PointLight{
{Position: math3d.Vec3{X: 1.6, Y: 1.15, Z: 1.5}, Color: math3d.Vec3{X: 0.25, Y: 0.72, Z: 1}, Intensity: 9, Range: 6.5},
},
Spot: []render3d.SpotLight{
{
Position: math3d.Vec3{X: -1.8, Y: 3.6, Z: 1.2}, Direction: math3d.Vec3{X: 0.7, Y: -3.6, Z: -1.1},
Color: math3d.Vec3{X: 1, Y: 0.72, Z: 0.38}, Intensity: 28, Range: 9,
InnerConeAngle: math3d.DegToRad(14), OuterConeAngle: math3d.DegToRad(26),
},
},
}
lightBuf, err := render3d.NewLightBuffer(cx, dev, rig, render3d.LightBufferOptions{Label: "scene", Retained: true})
drawBuf, err := render3d.NewPBRDrawBuffer(cx, dev, nil, render3d.NewPBRDrawUniform(model, render3d.PBRMaterial{
BaseColor: math3d.Vec3{X: 0.82, Y: 0.14, Z: 0.12},
Metallic: 0.0,
Roughness: 0.22,
}), true)
pipe, err := render3d.NewLitPBRPipeline(cx, dev, camBuf.Layout(), lightBuf.Layout(), drawBuf.Layout(), gpu.FormatDepth24Plus)
// Each frame:
_ = lightBuf.Update(frame.Device(), rig)
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
lightBuf.Bind(pass)
pass.SetBindGroup(render3d.PBRDrawGroup, drawBuf.BindGroup())
mesh.Draw(pass)
Custom 3D shaders can prepend CameraWGSL + LightWGSL to read @group(1) @binding(0) var<uniform> lights: LightRigUniform and call the portable WGSL helpers point_light_attenuation, spot_cone_attenuation, pbr_brdf, shade_pbr, and linear_to_srgb. LitPBRWGSL combines them with MeshVertexLayout and a per-draw PBRDrawUniform at PBRDrawGroup (group 2).
For more local lights than the rig holds, bind a ClusteredLightSystem at LightGroup instead of a GPULightBuffer: it uploads the same rig and adds any number of clustered point and spot lights, shaded with the same BRDF (Clustered forward lighting).
The shading model follows glTF 2.0 metallic-roughness PBR (dielectric F0 = 0.04, Schlick Fresnel, Trowbridge-Reitz GGX normal distribution, height-correlated Smith-GGX visibility, energy-conserving Lambertian diffuse, perceptual roughness floored at 0.045, and IEC 61966-2-1 linear-to-sRGB output). EvaluatePBR, ShadePoint, PointLightAttenuation, SpotConeAttenuation, and LinearToSRGB implement the exact same equations on the CPU; LightRig.AppendUniformData and LightUniforms.Encode pack the uniform block without allocating. Reference tests verify GPU output pixel-for-pixel against CPU ShadePoint, exercise both full and WebGL 2 / GLES 3.0 (nsgotest.NoGPUCompute) profiles plus Retained device loss, and compare lit scenes against render3d/testdata/golden/lights_pbr_scene.png, lights_point_falloff.png and spot_lights_scene.png.
Distance and height fog (Fog, GPUFogBuffer)
Fog configures distance fog (FogOff, FogLinear, FogExp, FogExp2), analytical exponential height fog along the view ray (HeightEnabled, HeightBase, HeightFalloff, HeightDensity), or both. Color is linear RGB (math3d.Vec3) and MaxOpacity caps the final blend factor in (0, 1] (zero means 1.0). With both kinds enabled, their transmittances multiply:
factor = clamp(1 - (1 - f_dist) * (1 - f_height), 0, MaxOpacity)
Height fog integrates the vertical extinction HeightDensity * exp(-HeightFalloff * (y - HeightBase)) analytically along the segment from the ray's start to the surface, switching to a four-term Taylor expansion near horizontal rays, so grazing and vertical rays stay smooth and a high camera looking down never overflows.
GPUFogBuffer holds the 48-byte FogUniforms block (FogUniformSize) with optional Retained survival across device loss. Both lit pipelines take it:
- Lights:
NewLitPBRFogPipelinecompilesLitPBRFogWGSL, theLitPBRWGSLlighting above with the fog buffer atFogGroup(group 3) after the camera, lights and per-draw groups.GPUFogBuffer.Bindbinds it there. - Materials: set
MaterialPipelineOptions.FogLayoutandNewMaterialPipelinecompilesMaterialFogWGSLwith the fog buffer atMaterialFogGroup(group 2), after the camera and material groups. Fog blends the linear radiance before the material shader's tone mapping, so the fog color is tone-mapped with the scene.
fog := render3d.Fog{
Mode: render3d.FogExp,
Color: math3d.Vec3{X: 0.62, Y: 0.72, Z: 0.86},
Density: 0.03,
MaxOpacity: 0.9,
HeightEnabled: true,
HeightBase: 2,
HeightFalloff: 0.25,
HeightDensity: 0.05,
}
fogBuf, err := render3d.NewGPUFogBuffer(cx, dev, fog, render3d.FogBufferOptions{Label: "fog", Retained: true})
pipe, err := render3d.NewLitPBRFogPipeline(cx, dev, camBuf.Layout(), lightBuf.Layout(), drawBuf.Layout(), fogBuf.Layout(), gpu.FormatDepth24Plus)
matPipe, err := render3d.NewMaterialPipeline(cx, dev, camBuf.Layout(), defaults.Layout(), render3d.MaterialPipelineOptions{
DepthFormat: gpu.FormatDepth24Plus,
FogLayout: fogBuf.Layout(),
})
// Each frame:
_ = fogBuf.Update(frame.Device(), fog)
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
lightBuf.Bind(pass)
fogBuf.Bind(pass)
pass.SetBindGroup(render3d.PBRDrawGroup, drawBuf.BindGroup())
mesh.Draw(pass)
pass.SetPipeline(matPipe)
pass.SetBindGroup(render3d.MaterialFogGroup, fogBuf.BindGroup())
gpuMat.Bind(pass)
gltfMesh.Draw(pass)
The view ray starts at the eye for a perspective camera. For an orthographic camera it starts on the camera's plane, at the point the surface projects from, so fog grows with the distance from that plane rather than from the camera's position. FogCameraWGSL's fog_camera_pos(world_pos) computes that start point in shaders, and FogCameraPos(cameraUniform, worldPos) computes the same point on the CPU.
- CPU reference:
Fog.Factor(camPos, worldPos)andFog.Apply(color, camPos, worldPos)evaluate the shader's formulas, withcamPosthe ray's start fromFogCameraPos.Fog.Validaterejects non-finite or out-of-range fields withErrInvalidFog, andFog.Sanitizedreplaces them with safe defaults.AppendFogUniformspacks the block without allocating. - Custom shaders: prepend
FogWGSLfor theFogstruct,fog_factor(cam_pos, world_pos, fog)andapply_fog(color, cam_pos, world_pos, fog), thenFogBindingWGSL(group 3) orMaterialFogBindingWGSL(group 2) for the binding, andFogCameraWGSLafterCameraWGSLfor the ray start.FogWGSLdeclares no binding of its own, so a shader can also embedFogin its own uniform block.
Reference tests check rendered pixels against ShadePoint and EvaluateMaterial blended by Fog.Apply on the software GPU and in nsgotest (full and nsgotest.NoGPUCompute profiles), cover orthographic cameras, Update, Retained device loss and scope disposal, and compare against render3d/testdata/golden/fog_linear_distances.png and fog_height.png.
Skybox and image-based lighting (Cubemap, SkyboxPipeline, GPUIBLBuffer)
render3d provides 6-face cubemaps (Cubemap, GPUCubemap), a fullscreen-triangle background skybox (SkyboxPipeline), and split-sum image-based lighting (IrradianceSH, PrefilterEnvironment, GenerateBRDFLUT, EnvBRDFApprox, GPUIBLBuffer, IBLWGSL, LitPBRIBLWGSL) that run on every NSGo GPU backend, including WebGL 2 and OpenGL ES 3.0 (NoGPUCompute).
Build a Cubemap from six faces (CubemapFromImages), an equirectangular panorama (CubemapFromEquirectangular; pass srgb = true for a typical PNG or JPEG), or a procedural three-band sky (GradientSkyCubemap), then bake diffuse L2 spherical harmonic irradiance and a GGX-prefiltered specular mip chain on the CPU before uploading:
env, err := render3d.GradientSkyCubemap(zenith, horizon, ground, 64)
sh, err := render3d.ComputeIrradianceSH(env)
prefilter, err := render3d.PrefilterEnvironment(env, render3d.PrefilterOptions{
BaseSize: 64,
MipLevels: 5,
SampleCount: 64,
})
skyCube, err := render3d.UploadCubemap(cx, dev, env, render3d.CubemapUpload{Label: "sky", Retained: true})
prefCube, err := render3d.UploadCubemap(cx, dev, prefilter, render3d.CubemapUpload{Label: "ibl", Retained: true})
skyPipe, err := render3d.NewSkyboxPipeline(cx, dev, render3d.SkyboxPipelineDesc{
ColorFormat: dev.SurfaceFormat(),
DepthFormat: gpu.FormatDepth24Plus,
Retained: true,
})
skyGroup, err := skyPipe.BindCubemap(cx, skyCube)
iblBuf, err := render3d.NewGPUIBLBuffer(cx, dev, prefCube, render3d.NewIBLUniform(sh, prefCube, 1.0), render3d.IBLBufferOptions{Label: "ibl", Retained: true})
pipe, err := render3d.NewLitPBRIBLPipeline(cx, dev, camBuf.Layout(), lightBuf.Layout(), drawBuf.Layout(), iblBuf.Layout(), gpu.FormatDepth24Plus)
// Each frame: draw opaque lit meshes with the IBL buffer at IBLGroup (group 3), then the skybox, which skips the pixels they cover:
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
lightBuf.Bind(pass)
iblBuf.Bind(pass)
pass.SetBindGroup(render3d.PBRDrawGroup, drawBuf.BindGroup())
mesh.Draw(pass)
skyPipe.WriteCamera(cam)
skyPipe.Draw(pass, skyGroup)
- Depth and translation:
NewSkyboxUniformstrips camera translation so the sky stays infinitely distant as the viewer moves.SkyboxWGSLdraws one fullscreen triangle from@builtin(vertex_index)with no vertex buffer, at depth1.0, the far plane, withCompareLessEqualandDepthWrite: false, so drawn after opaque geometry it shades only uncovered pixels and paints over nothing nearer than the far plane. - Lit PBR with environment lighting:
LitPBRIBLWGSLisLitPBRWGSLplusevaluate_ibl: it adds diffuse spherical harmonic irradiance and prefiltered specular to the direct lighting before encoding to sRGB. Set theLightRig's ambient light to zero when the environment supplies the ambient term. IBL and fog both use bind group 3 (IBLGroupandFogGroup), the last of the four groups every backend guarantees, so a lit pipeline has one or the other. - Custom shaders: prepend
IBLWGSL(group 3) orIBLWGSLForGroup(group)and callevaluate_ibl(normal, view_dir, base_color, metallic, roughness, ao). Diffuse lighting evaluates the 9 pre-convolved L2 coefficients (evaluate_irradiance_sh), and specular lighting samples the prefiltered cubemap atroughness * ibl.max_lodscaled by Karis's analyticalenv_brdf_approx(EnvBRDFApprox). Shaders that prefer a 2D lookup texture can bake the split-sum GGX table withGenerateBRDFLUTorGenerateBRDFLUTWithSamples. - Buffer, validation and memory:
GPUIBLBufferfollowsGPULightBufferandGPUFogBuffer:Update(dev, u)writes new parameters,Bindsets it atIBLGroup, andByteSizereports its uniform buffer forRenderStats, whileGPUCubemap.ByteSizereports each cubemap.IBLUniform.Validaterefuses non-finite spherical harmonic coefficients and non-finite or negative intensity or max LOD withErrInvalidIBL. - High dynamic range environments and
.hdrloading (HDRCubemap,UploadCubemapHDR):Cubemapstores 8 bits per channel (RGBA8UnormorRGBA8UnormSRGB), clamping radiance above 1. For physically based highlights above 1.0, useHDRCubemap, which stores linearfloat32RGB faces and mips: decode a Radiance.hdr(RGBE) equirectangular panorama directly withCubemapFromHDR(orDecodeHDR/EncodeHDR+CubemapFromHDREquirectangular), generate a procedural HDR sky withGradientSkyHDRCubemap, or promote an LDR cubemap withHDRCubemapFromCubemap. Bake HDR diffuse irradiance and prefiltered specular withComputeIrradianceSHHDRandPrefilterEnvironmentHDR, then upload withUploadCubemapHDR, which produces aGPUCubemapingpu.FormatRGBA16Float(clamping above65504to stay finite in half-float, and falling back toRGBA8Unormonly if the device lacksFormatRGBA16Float). Draw HDR scenes and skyboxes into anHDRTargetresolved byToneMapPass(for exampleToneMapNeutral), or setSkyboxPipelineDesc.ToneMap(SkyboxToneMapWGSL) to apply Reinhard + gamma 2.2 when drawing an HDR skybox directly to an LDR surface. - Textured glTF PBR materials with IBL (
NewMaterialIBLPipeline,MaterialIBLWGSL):NewMaterialIBLPipelinepairsCameraGroup(group 0), a textured glTFGPUMaterialatMaterialGroup(group 1 — base color, metallic-roughness, tangent-space normal, occlusion, and emissive maps, plus alpha masking/blending and double-sided normal flipping), and aGPUIBLBufferatMaterialIBLGroup(group 2). SetMaterialIBLPipelineOptions.LinearOutput(MaterialIBLLinearWGSL) to write unclamped linear radiance into anHDRTargetforToneMapPass;EvaluateMaterialIBLis the matching pure-Go CPU reference. - CPU reference and device loss:
EvaluateIBLandEvaluateIBLHDRmatchevaluate_iblon the CPU,ShadePointplusEvaluateIBLmatchesLitPBRIBLWGSL, andEvaluateMaterialIBLmatchesMaterialIBLWGSL. WithRetained: trueonCubemapUpload,SkyboxPipelineDesc,IBLBufferOptions, andMaterialIBLPipelineOptions, NSGo restores textures, samplers, pipelines and uniform buffers after a device loss; callSkyboxPipeline.SetDevice(dev)inOnReadyand pass the new device toGPUIBLBuffer.Update. - Reference renders: headless software-GPU tests compare a skybox behind a mesh and 2x2 metallic/roughness spheres with
render3d/testdata/golden/skybox_scene.pngandibl_spheres.png, and render the KhronosDamagedHelmet(render3d/testdata/damagedhelmet/reference_ibl.png, skipped when the CC-BY-NC-4.0 fixture directory is absent) with all five glTF PBR maps under an.hdr-decodedFormatRGBA16FloatHDR environment and skybox resolved throughToneMapNeutral(UPDATE_GOLDEN=1rewrites them), verifying measurably brighter specular highlights than an LDR-clamped environment, CPU reference parity, far-plane visibility, retained device-loss recovery, and both the full andnsgotest.NoGPUComputeprofiles.
Reflection probes and light probes (ReflectionProbeSet, LightProbeVolume, GPUProbeIBLBuffer)
render3d extends its IBL pipeline with spatially varying local reflections and interpolated diffuse indirect lighting built directly on Cubemap, GPUCubemap, PrefilterEnvironment, and IrradianceSH — plugging into LitPBRWGSL at IBLGroup (group 3) so metallic, roughness, emissive, LightRig direct lights, inverse-transpose normal matrices, and sRGB output work in one pass across every backend including WebGL 2 and OpenGL ES 3.0 (NoGPUCompute).
var probes render3d.ReflectionProbeSet
room := &render3d.ReflectionProbe{
ID: "hall",
Position: math3d.Vec3{X: 0, Y: 1.5, Z: 0},
BoxMin: math3d.Vec3{X: -6, Y: 0, Z: -6},
BoxMax: math3d.Vec3{X: 6, Y: 4, Z: 6},
BlendDistance: 1.5,
BoxProjection: true,
}
probes.Add(room)
// Bake on load or on request (runtime-only; re-baking closes any previous GPUCubemap):
render3d.BakeReflectionProbeGPU(cx, dev, room, render3d.ProbeBakeOptions{
Size: 64,
ClearColor: core.RGB(0x101824),
SRGB: false, // RGBA8Unorm target; set true for RGBA8UnormSRGB
Prefilter: render3d.PrefilterOptions{BaseSize: 64, MipLevels: 5, SampleCount: 64},
}, func(pass *gpu.RenderPass, face gpu.CubeFace, faceCam render3d.CameraUniform) {
camBuf.Write(faceCam)
drawStaticRoom(pass)
}, func(err error) {
// Invoked asynchronously on the engine goroutine once all six face readbacks finish.
})
// Bake a 3D light probe grid of IrradianceSH for dynamic objects:
grid, _ := render3d.NewLightProbeGrid(roomBounds, 4, 2, 4)
_ = grid.Bake(16, sampleRoomRadiance)
lightProbes := &render3d.LightProbeVolume{Grid: grid, FallbackSH: skySH}
// Bind per-object probe IBL at IBLGroup (group 3) alongside LitPBRWGSL:
probeIBL, _ := render3d.NewGPUProbeIBLBuffer(cx, dev, render3d.ProbeIBLBufferOptions{Label: "hero-ibl"})
pipe, _ := render3d.NewLitPBRProbeIBLPipeline(cx, dev, camBuf.Layout(), lightBuf.Layout(), drawBuf.Layout(), probeIBL.Layout(), gpu.FormatDepth24Plus)
// Each frame for a moving object:
_ = probeIBL.UpdateForPoint(dev, heroPos, &probes, lightProbes, skyPrefCube, 1.0)
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
lightBuf.Bind(pass)
pass.SetBindGroup(render3d.PBRDrawGroup, drawBuf.BindGroup())
probeIBL.Bind(pass)
heroMesh.Draw(pass)
- Local reflection selection, parallax and blending:
ReflectionProbeSet.SelectReflectionProbespicks up to two ready (enabled, baked, non-dirty) probes covering a world position byPriority, smoothstepInfluence(overBlendDistance), and volume, returning normalized(Weight0, Weight1, SkyWeight)summing to 1. Probes withBoxProjection: trueapplyBoxProjectDirection(box_project_dirin WGSL) to correct reflections against[BoxMin, BoxMax]. Unbaked, dirty, or disabled probes are skipped during selection, and any missing probe ornilsky binds a 1x1 black(0, 0, 0, 255)fallback cubemap so CPUEvaluateProbeIBLand GPULitPBRProbeIBLWGSLmatch identically. - Light probes (
LightProbe,LightProbeGrid,LightProbeVolume): storeIrradianceSHcoefficients (baked viaComputeIrradianceSHorComputeIrradianceSHFromFuncand combined withAdd,Scale,Lerp).LightProbeGrid.Sampleinterpolates trilinearly andLightProbeVolume.SampleLightProbesblends scatteredLightProbepoints (with optional finiteRadius) smoothly over the grid orFallbackSH. Both validateCountsagainstlen(Probes)and non-finite coordinates without panicking. - Asynchronous GPU baking and target formats:
BakeReflectionProbeGPUrenders the six faces withProbeFaceCamerainto temporaryFormatRGBA8Unorm(whenSRGBis false) orFormatRGBA8UnormSRGB(whenSRGBis true) plusFormatDepth24Plusoffscreen render targets, reads back all six faces asynchronously viagpu.Device.ReadTexture, flips rows to canonical cubemap orientation, prefilters roughness mips withPrefilterEnvironment, uploads viaUploadCubemap, closes any previousprobe.GPUCubemap, and invokesonDoneon the engine goroutine.BakeReflectionProbeprovides the matching CPU sampler bake. - Runtime-only bakes: reflection probe cubemaps and light probe SH data are runtime-only caches generated on load or on request rather than persisted in text scene files.
- Custom shaders and HDR: prepend
ProbeIBLWGSL(IBLGroup) orProbeIBLWGSLForGroup(group)and callevaluate_probe_ibl(world_pos, normal, view_dir, base_color, metallic, roughness, ao). Share oneProbeIBLBindGroupLayoutacross multiple per-objectGPUProbeIBLBufferinstances viaProbeIBLBufferOptions.Layout, and useNewLitPBRProbeIBLPipelineWithOptionswithLinearOutput: trueforHDRTarget.ShadePointplusEvaluateProbeIBL(followed byLinearToSRGB) is the exact CPU reference forLitPBRProbeIBLWGSL.
Mesh buffers (Mesh, GPUMesh)
A Mesh is triangle-list geometry on the CPU: Positions (required), Normals and UVs (each empty or one per position) and Indices (empty for a non-indexed mesh). The fields have the same types as a glTF primitive's Positions, Normals, TexCoords0 and Indices, and front faces wind counter-clockwise as in glTF and math3d. UploadMesh validates the mesh and creates its vertex buffer and, when indexed, its index buffer on the NSGo GPU view's gpu.Device. The buffers belong to the scope you pass and are released when it is disposed or when you call GPUMesh.Close:
gpu.View(cx, gpu.ViewProps{
DepthFormat: gpu.FormatDepth24Plus,
OnReady: func(dev *gpu.Device) {
if mesh == nil { // OnReady runs again after a device loss; NSGo has already restored the retained buffers
mesh, err = render3d.UploadMesh(cx, dev, render3d.Mesh{
Positions: prim.Positions,
Normals: prim.Normals,
UVs: prim.TexCoords0,
Indices: prim.Indices,
}, render3d.MeshUpload{Label: "crate", Retained: true})
}
pipe, err = dev.CreatePipeline(cx, gpu.PipelineDesc{
Vertex: gpu.ShaderStage{Module: shader, EntryPoint: "vs_main"},
Fragment: gpu.ShaderStage{Module: shader, EntryPoint: "fs_main"},
VertexBuffers: []gpu.VertexBufferLayout{render3d.MeshVertexLayout()},
Primitive: gpu.PrimitiveState{Topology: gpu.TopologyTriangleList, FrontFace: gpu.FrontCCW, CullMode: gpu.CullBack},
DepthStencil: &gpu.DepthStencilState{Format: gpu.FormatDepth24Plus, DepthWrite: true, DepthCompare: gpu.CompareLess},
Targets: []gpu.ColorTargetState{{Format: dev.SurfaceFormat()}},
})
},
OnFrame: func(frame *gpu.Frame) {
pass := frame.BeginRenderPass(gpu.RenderPassDesc{})
pass.SetPipeline(pipe)
mesh.Draw(pass) // or mesh.Bind(pass) and your own DrawIndexedInstanced
pass.End()
frame.Submit()
},
})
Every mesh uses one interleaved little-endian layout of 32 bytes per vertex, so one pipeline draws any of them; a missing normal or UV is written as zeros:
| Attribute | @location |
Format | Offset |
|---|---|---|---|
| position | 0 (MeshPositionLocation) |
float32x3 |
0 |
| normal | 1 (MeshNormalLocation) |
float32x3 |
12 |
| UV | 2 (MeshUVLocation) |
float32x2 |
24 |
Indices are written as uint16 while the mesh has at most 65,535 vertices and as uint32 beyond that, so the index 0xFFFF, which WebGL 2 always treats as a primitive restart, is never used in a uint16 buffer; index data is zero-padded to a multiple of four bytes as WebGPU requires. Only formats that every GPU view backend accepts are used: Metal on macOS and iOS, Direct3D 11 on Windows, OpenGL 3.3 on Linux, WebGPU or WebGL 2 on the web and OpenGL ES 3.0 on Android. The package's tests draw meshes on the software device of headless nsgotest, with both the full capabilities and those of the WebGL 2 and OpenGL ES 3.0 backends (nsgotest.NoGPUCompute); drawing on each hardware backend is not yet covered by an automated test.
Mesh.Validaterejects an empty mesh, attribute counts that do not match the positions, non-finite values, partial triangles and out-of-range indices; every error wrapsErrInvalidMesh, andUploadMeshcreates nothing for an invalid mesh.Mesh.AppendVertexDataandMesh.AppendIndexDatawrite the buffer bytes into a slice you own without allocating when it has capacity, for streaming geometry throughDevice.WriteBufferor a ring buffer. They do not validate: callMesh.Validatefirst, since the bytes written for an invalid mesh are unspecified.MeshUpload.Retainedkeeps a CPU copy so NSGo recreates the buffers after a device loss and the sameGPUMeshdraws again, so upload a retained mesh only once, as in the example; leave it off whenOnReadyuploads the meshes again.GPUMesh.DrawandGPUMesh.Bindrecord nothing for a mesh that is notValid(nil after a failed upload, closed, its scope disposed, or not retained across a device loss), so it never draws with buffers an earlier mesh left bound.GPUMesh.Boundsis the object-space box to transform intoDrawItem.WorldBounds, andGPUMesh.ByteSizethe GPU memory forRenderStatsand budgets.
Textures and samplers (GPUTexture, GPUSampler)
UploadTexture uploads any Go image.Image (including non-zero-origin sub-images) to a gpu.Device as a scope-owned GPUTexture, converting it to straight-alpha RGBA8 (PixelBufferFromImage; *image.NRGBA bytes are copied verbatim even when alpha is zero so packed data maps stay intact). UploadTexturePixels uploads raw PixelBuffer bytes in FormatRGBA8Unorm, FormatRGBA8UnormSRGB, FormatRG8Unorm, or FormatR8Unorm, with optional Stride row padding:
texLayout, err := render3d.TextureBindingLayout(cx, dev)
albedo, err := render3d.UploadTexture(cx, dev, img, render3d.TextureUpload{
Label: "crate-albedo",
SRGB: true, // color maps decode sRGB to linear on sample; leave false for normal/ORM data maps
Mipmaps: true, // CPU area-weighted box chain (in linear light for sRGB) uploaded per level
Retained: true, // survives device loss alongside retained samplers
})
samp, err := render3d.NewGPUSampler(cx, dev, render3d.SamplerLinearRepeat())
bg, err := render3d.BindTexture(cx, dev, texLayout, albedo, samp)
// In OnFrame, alongside CameraGroup (group 0):
pass.SetBindGroup(1, bg)
mesh.Draw(pass)
-
Mipmaps: setting
TextureUpload.Mipmapsbuilds a CPU area-weighted box mipmap chain (in linear light whenSRGBis true, avoiding dark halos) and uploads every level viaDevice.WriteTextureMip. This works identically on every NSGo backend without depending on GPU mipmap generation, and every uploaded level is replayed automatically whenRetainedis true. Cap the level count withTextureUpload.MaxMipLevels. -
KTX2 textures:
LoadKTX2uploads a 2D KTX2 file such asgame assetsandgame texwrite, with every mip level it holds, in its sRGB or linear format, and returns the sampler to draw it with:KTX2Upload.Samplerwith its filters set from the file'sgame.filterkey (KTX2FilterKey), which thefilterof the image's import settings sidecar writes, sonearestdraws pixel art sharp andlinearblends. BasisLZ, Zstandard and ZLIB supercompressed levels are decompressed first. BC1, BC3, BC7, ETC2 RGB8 and RGBA8, and ASTC 4x4 levels (including multi-partition, dual-plane and BISE-coded blocks written byastcenc) are uploaded compressed whereDevice.SupportsCompressedFormatallows and decoded to RGBA8 on the CPU elsewhere, so the texture loads on every backend;KTX2Samplerapplies the filter to sampler options without a device. A file that is not such a texture, or names an unknown filter, returns an error wrappingErrInvalidTexture:data, err := fs.ReadFile(assets, "albedo.ktx2") albedo, opts, err := render3d.LoadKTX2(cx, dev, data, render3d.KTX2Upload{Label: "albedo", Retained: true, Sampler: render3d.SamplerLinearRepeat()}) samp, err := render3d.NewGPUSampler(cx, dev, opts) -
Basis Universal (UASTC and BasisLZ/ETC1S):
LoadKTX2takes both UASTC 4x4 (high-quality) and BasisLZ/ETC1S (basisu's default mode, including alpha slices) KTX2 files, and transcodes them to the best block format the device supports: BC7, then BC1 for an opaque texture, BC3, ETC2 RGB8 or RGBA8 (linear only, as NSGo has no sRGB ETC2), then ASTC 4x4. It decodes to RGBA8 when the device has none of them, when the base level's width or height is not a multiple of 4, or whenKTX2Upload.ForceRGBA8is set. Transcoding decodes every block and encodes it again on the goroutine that callsLoadKTX2: on a desktop CPU (Ryzen 9 3900X) a 1024×1024 level takes about 0.5 s to BC7, 0.8 s to ETC2 and 0.16 s to ASTC, and four times as long at 2048×2048 (BenchmarkTranscodeUASTCintooling/ktx2), so load large universal textures during a loading screen. BC1 without alpha (VK_FORMAT_BC1_RGB_*) is refused, because an RGBA BC1 upload would draw its punch-through texels transparent. -
Dynamic updates:
GPUTexture.UpdateImageandGPUTexture.UpdatePixelsreplace texels in place for a matching dimension and format, regenerating and re-uploading the mip chain when the texture was created with mipmaps. -
Sampler presets and validation:
SamplerLinearRepeat(),SamplerLinearClamp(),SamplerLinearMirror(),SamplerNearestRepeat(), andSamplerNearestClamp()return customizableSamplerOptions.SamplerOptions.Validaterejects invalid filter/address modes, non-finite or inverted LOD bounds, andMaxAnisotropy > 1with non-linear filters (matching WebGPU's validation rule) with errors wrappingErrInvalidSampler. -
Lifecycle and memory:
GPUTexture.ByteSizesums every uploaded mip level forRenderStats.TextureBytesandBudgetchecks. BothGPUTextureandGPUSamplerare released when their owningcore.Scopeis disposed or earlier viaClose(), andValid()reports whether they belong to a live device. -
Reference renders:
render3d's tests verify textured meshes, filtering, wrapping, sRGB/linear sampling, and mipmaps against golden reference PNGs (render3d/testdata/golden/textured_mesh_scene.pngandtextured_filtering_wrapping.png) as well as headlessgpu.Viewtests on both the full andnsgotest.NoGPUCompute(WebGL 2 / GLES 3.0) profiles;UPDATE_GOLDEN=1rewrites the reference images.
Morph target meshes (MorphMesh, GPUMorphMesh)
MorphMesh pairs a base Mesh with per-vertex MorphTarget position and normal deltas (matching gltf.MorphTarget) and optional initial Weights. UploadMorphMesh validates the base mesh, targets, and weights (wrapping ErrInvalidMesh on malformed data) and creates a GPUMorphMesh backed by the same 32-byte MeshVertexLayout vertex buffer and optional index buffer as GPUMesh, so any existing 3D pipeline draws it without shader changes on every backend—including WebGL 2 and OpenGL ES 3.0 without compute shaders:
morph, err := render3d.UploadMorphMesh(cx, dev, render3d.MorphMesh{
Base: render3d.Mesh{
Positions: prim.Positions,
Normals: prim.Normals,
UVs: prim.TexCoords0,
Indices: prim.Indices,
},
Targets: targets,
Weights: gltf.ResolveWeights(doc, nodeIdx),
}, render3d.MeshUpload{Label: "character", Retained: true})
// Each frame before recording draws:
if sampled, ok := gltf.SampleMorphWeights(anim, nodeIdx, animTime, weights); ok {
weights = sampled
}
_ = morph.SetWeights(weights) // marks dirty only when a weight actually changes
morph.Update(dev) // blends on CPU and calls dev.WriteBuffer only when dirty
morph.Draw(pass)
- Dirty tracking:
SetWeightscompares against active weights and marks the mesh dirty only when at least one weight changes;Updateskips both CPU blending andDevice.WriteBufferwhen clean. - Live object-space bounds:
GPUMorphMesh.Boundsevaluates pending CPU blends on demand (caching the result soUpdatedoes not blend twice) and keeps the underlyingGPUMesh.Boundsin sync for frustum culling. - Device-loss restore: with
MeshUpload.Retained, NSGo recreates the vertex buffer with the last uploaded morphed bytes after a device loss; ifSetWeightsis called while the device is lost, the mesh staysDirtyso the firstUpdateafter restore uploads the pending pose.
GPU skinning (SkinnedMesh, GPUSkinnedMesh, GPUSkinPalette)
SkinnedMesh extends Mesh with per-vertex glTF skinning attributes (Joints [][4]uint16 and Weights []math3d.Vec4) and optional JointCount. SkinnedMeshFromGLTF and SkinnedMeshesFromGLTFNode extract and validate triangle primitives from a loaded gltf.Document, checking joint bounds and normalizing each vertex's four weights so they sum to 1 (falling back to (1, 0, 0, 0) when all four weights are zero). UploadSkinnedMesh uploads the interleaved 64-byte vertex buffer and optional index buffer (uint16 up to 65,535 vertices, uint32 beyond) as a scope-owned GPUSkinnedMesh with optional Retained device-loss restoration.
Every skinned mesh shares one interleaved little-endian layout (SkinnedVertexLayout), extending MeshVertexLayout at locations 0..2:
| Attribute | @location |
Format | Offset |
|---|---|---|---|
| position | 0 (SkinnedPositionLocation) |
float32x3 |
0 |
| normal | 1 (SkinnedNormalLocation) |
float32x3 |
12 |
| UV | 2 (SkinnedUVLocation) |
float32x2 |
24 |
| joints | 3 (SkinnedJointsLocation) |
uint32x4 |
32 |
| weights | 4 (SkinnedWeightsLocation) |
float32x4 |
48 |
GPUSkinPalette stores up to MaxSkinJoints (128) joint matrices in a portable uniform buffer (SkinUniformSize = 8,192 bytes, fitting comfortably below WebGL 2 and OpenGL ES 3.0's 16 KiB MAX_UNIFORM_BLOCK_SIZE minimum). Unused joint slots are padded with identity matrices. AppendJointMatrices and SkinJointMatrices / SkinJointMatricesWithTransform (which compute glTF 2.0 jointNode.WorldMatrix * InverseBindMatrices[i] from doc.Nodes[].WorldMatrix) reuse caller storage with zero allocations.
Three pipelines draw a GPUSkinnedMesh, each binding the palette at its own group, because WebGPU and WebGL 2 guarantee only four bind groups and the lit and material pipelines already use groups 0 to 2:
| Pipeline | Shader | Palette group | Not combinable with |
|---|---|---|---|
NewSkinnedMeshPipeline (unlit, colored by normal) |
SkinnedMeshWGSL |
SkinGroup (1) |
|
NewLitPBRPipelineWithOptions with LitPBRPipelineOptions.SkinLayout (scene lights) |
LitPBRSkinnedWGSL |
LitPBRSkinGroup (3) |
FogLayout, IBLLayout, ShadowLayout, SpotShadowLayout, PointShadowLayout |
NewMaterialPipeline with MaterialPipelineOptions.SkinLayout (glTF material with textures) |
MaterialSkinnedWGSL |
MaterialSkinGroup (2) |
FogLayout |
SkinWGSL declares the uniform block at group 1 and the portable skin_position / skin_normal vertex helpers matching CPU SkinVertex, for custom shaders that bind nothing else at group 1.
Joint matrices come in two conventions. SkinJointMatrices returns world-space joints, so the skinned vertex is already in world space. anim3d's Player.SkinMatricesForNode returns mesh-local joints, inverse(meshWorld) * jointWorld * IBM, and the Player keeps its own world matrices instead of writing doc.Nodes. Upload mesh-local joints with GPUSkinPalette.UpdateWithTransform and the mesh node's world matrix (Player.WorldTransform), or pass that matrix as the lit PBR draw's model; otherwise the mesh node's own transform (CesiumMan's Z-up rotation) is lost. A lit CesiumMan animated by anim3d:
doc, _ := gltf.LoadFS(assetsFS, "CesiumMan.gltf")
const meshNode = 2
meshes, _ := render3d.SkinnedMeshesFromGLTFNode(doc, meshNode)
player, _ := anim3d.NewPlayer(doc)
_ = player.Play(0)
// In gpu.View OnReady:
camBuf, _ := render3d.NewCameraBuffer(cx, dev)
lights, _ := render3d.NewLightBuffer(cx, dev, rig, render3d.LightBufferOptions{})
draw, _ := render3d.NewPBRDrawBuffer(cx, dev, nil, render3d.NewPBRDrawUniform(math3d.Mat4Identity(), mat), true)
palette, _ := render3d.NewGPUSkinPalette(cx, dev, render3d.SkinPaletteOptions{Label: "hero", Retained: true})
gpuMesh, _ := render3d.UploadSkinnedMesh(cx, dev, meshes[0], render3d.MeshUpload{Label: "hero", Retained: true})
pipe, _ := render3d.NewLitPBRPipelineWithOptions(cx, dev, camBuf.Layout(), lights.Layout(), draw.Layout(), render3d.LitPBRPipelineOptions{
DepthFormat: gpu.FormatDepth24Plus,
SkinLayout: palette.Layout(),
})
// Each frame:
player.Step(dt)
joints, _ := player.SkinMatricesForNode(meshNode) // mesh-local
_ = palette.Update(frame.Device(), joints)
draw.Write(frame.Device(), render3d.NewPBRDrawUniform(player.WorldTransform(meshNode), mat))
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
lights.Bind(pass)
pass.SetBindGroup(render3d.PBRDrawGroup, draw.BindGroup())
pass.SetBindGroup(render3d.LitPBRSkinGroup, palette.BindGroup())
gpuMesh.Draw(pass)
Without anim3d, pose the document with doc.Animations[i].Apply(doc, t) and doc.ComputeWorldMatrices(), then call palette.UpdateFromGLTF(dev, doc, skinIndex) (zero allocations) and draw with an identity model.
- Bounds:
SkinnedMesh.BoundsandGPUSkinnedMesh.Boundscover the unskinned positions in the mesh's own space, not the drawn mesh. For culling and camera framing useSkinnedBounds(gpuMesh.Bounds(), joints), the union of that box under every joint matrix, which contains the skinned mesh in any pose. - Weights: weights are divided by their largest component before they are summed, so large finite weights normalize instead of overflowing.
- Tests: CesiumMan and RiggedSimple are drawn on the full and
nsgotest.NoGPUCompute(WebGL 2 / GLES 3.0) profiles and compared with CPUSkinVertexrenders; CesiumMan's bind pose is checked against the mesh node's world matrix, and the lit and material pipelines are checked with both joint conventions.
Metallic-roughness materials (Material, MaterialDefaults, GPUMaterial)
Material (aliased as MetallicRoughnessMaterial) represents a glTF 2.0 metallic-roughness PBR surface with base color (BaseColorFactor, BaseColorTexture), metallic-roughness (MetallicFactor, RoughnessFactor, MetallicRoughnessTexture using the glTF G = roughness, B = metallic channel packing), tangent-space normal mapping (NormalTexture, NormalScale), ambient occlusion (OcclusionTexture, OcclusionStrength), emissive (EmissiveFactor, EmissiveTexture), alpha modes (AlphaOpaque, AlphaMask with AlphaCutoff, AlphaBlend), and DoubleSided back-face normal flipping.
Create one shared MaterialDefaults per gpu.Device to own the MaterialGroup (group 1) bind group layout (plus the unlit one, UnlitLayout, described under Unlit materials and vertex colors), default linear-repeat sampler, and 1×1 fallback textures (white sRGB for missing base color, white linear for missing metallic-roughness/occlusion, flat (128, 128, 255, 255) for missing normal maps, and black for missing emissive maps). Because every GPUMaterial binds those fallbacks for omitted slots, all materials share one bind group layout with zero pipeline permutations:
defaults, err := render3d.NewMaterialDefaults(cx, dev, render3d.MaterialDefaultsOptions{Retained: true})
pipe, err := render3d.NewMaterialPipeline(cx, dev, camBuf.Layout(), defaults.Layout(), render3d.MaterialPipelineOptions{
DepthFormat: gpu.FormatDepth24Plus,
AlphaMode: render3d.AlphaOpaque,
})
mat := render3d.DefaultMaterial()
mat.BaseColorFactor = [4]float32{0.95, 0.78, 0.34, 1}
mat.MetallicFactor = 1.0
mat.RoughnessFactor = 1.0
mat.MetallicRoughnessTexture = mrTex // G = roughness, B = metallic
mat.NormalTexture = normalTex // tangent-space normal map (+X right, +Y up, +Z out)
gpuMat, err := render3d.NewGPUMaterial(cx, dev, defaults, mat, render3d.MaterialUpload{
Label: "brushed-gold",
Retained: true,
})
// In OnFrame, alongside CameraGroup (group 0):
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
gpuMat.Bind(pass) // binds at MaterialGroup (group 1)
mesh.Draw(pass)
- Zero-allocation uniform encoding and dynamic updates:
MaterialUniforms.AppendUniformDataandEncodewrite the 16-byte-aligned 64-byte uniform block (MaterialUniformSize) with zero allocations when capacity is available.GPUMaterial.Update(dev, m)updates scalar factors and flags in place without recreating the bind group; passingdevexplicitly lets retained materials stay updatable after a device loss and restore. - Portable normal mapping and Cook-Torrance GGX shading:
MaterialWGSL(built fromCameraWGSL+MaterialBindingsWGSL) derives an API-independent tangent frame from screen-spacedpdx/dpdyderivatives (Cramer's rule across Metal, D3D11, OpenGL, WebGPU, WebGL 2, and GLES 3.0) and falls back to a deterministic orthonormal frame when derivatives or UVs are degenerate, so standard 32-byteMeshVertexLayoutmeshes get accurate normal mapping without extra vertex attributes. - Vertex colors (
COLOR_0) on metallic-roughness & lit PBR pipelines: setMaterialPipelineOptions.VertexColors: true(compilesMaterialColoredWGSL, or its fog / linear-output variant) orLitPBRPipelineOptions.VertexColors: true(compilesLitPBRColoredWGSL, or its fog, IBL, shadow, spot/point shadow, CSM, or linear-output variant) to draw aGPUColoredMesh(ColoredVertexLayout,@location(3)linear RGBA). Per glTF 2.0 §3.7.2.1, the shader multipliesbase_color(RGB and alpha) by the linear vertex color beforeAlphaMaskcutoff and PBR shading.VertexColorscannot be combined withSkinLayout(SkinnedVertexLayouthas no vertex color attribute). - CPU reference and golden tests:
EvaluateMaterial(withMaterialEvalInput.VertexColorandHasVertexColor, defaulting to[1, 1, 1, 1]when unset) implements the exact same Cook-Torrance GGX BRDF, hemisphere ambient, normal perturbation, alpha cutoff, and Reinhard + sRGB tone mapping in pure Go;PBRMaterial.WithVertexColorfolds a linear vertex color intoPBRMaterialforShadePoint/EvaluatePBR/EvaluateIBL.render3d's tests verify GPU pixel readback directly againstEvaluateMaterialandShadePoint, render the KhronosVertexColorTestsample end-to-end, compare golden reference renders (pbr_metallic_roughness_ramp.pngandpbr_textured_normal_maps.png), and checkgpu.Viewexecution on both the full andnsgotest.NoGPUCompute(WebGL 2 / GLES 3.0) profiles;UPDATE_GOLDEN=1rewrites the golden PNGs.
glTF 2.0 materials and textures (UploadGLTFMaterials, GLTFMaterialSet)
UploadGLTFMaterials maps every material, texture, image, and sampler in a loaded *gltf.Document onto render3d's Material, GPUMaterial, GPUTexture, and GPUSampler types:
doc, err := gltf.LoadFS(assetsFS, "DamagedHelmet.gltf")
matSet, err := render3d.UploadGLTFMaterials(cx, dev, defaults, doc, render3d.GLTFMaterialUpload{
Label: "DamagedHelmet",
Retained: true, // recovers uploaded textures, samplers, and materials across device loss
})
defer matSet.Close()
// Resolve per-primitive material (with optional KHR_materials_variants support and -1 default fallback):
gpuMat := matSet.MaterialForVariant(prim, "NightOps") // or matSet.MaterialForPrimitive(prim, -1)
gpuMat.Bind(pass)
gpuMesh.Draw(pass)
- Automatic color-space separation & deduplication:
baseColorTextureandemissiveTextureupload withSRGB: true(FormatRGBA8UnormSRGB), whilemetallicRoughnessTexture,normalTexture, andocclusionTextureupload as linear (FormatRGBA8Unorm). Referenced glTF images are decoded once per image index and deduplicated on the GPU by(imageIndex, sRGB), so packed ORM maps shared betweenocclusionTextureandmetallicRoughnessTexture(or shared across multiple materials) upload only once; if the same image is used as both a color map and a data map, each color space receives its own GPU texture. - glTF 2.0 samplers & mipmaps:
SamplerOptionsFromGLTFmaps all six glTF filter modes and three wrap modes (Repeat,ClampToEdge,MirroredRepeat), defaulting omitted wrap modes toAddressRepeat(required by assets whose UVs wrap outside[0, 1], such asDamagedHelmet'sV ∈ [1, 2]) and omitted filters to trilinear filtering with CPU-generated mipmaps. A texture without a sampler gets the same glTF default sampler as an empty sampler object. The non-mipmappedNEARESTandLINEARmin filters clamp the level of detail to the base level, so they stay sharp when the same image is also uploaded with mips for another sampler.GLTFMaterialUpload.MaxAnisotropyapplies anisotropy only to all-linear samplers so nearest samplers stay WebGPU-valid;DisableMipmapsturns off mip chains when desired. - Extensions, metadata & default material:
GLTFMaterialSetalways uploads a glTF 2.0 default material returned byDefaultMaterial()andMaterial(-1)for unassigned primitives.KHR_materials_emissive_strengthis folded intoEmissiveFactor, and a negativenormalTexture.scaleflips the normal map's X and Y as the specification allows.Info(i)/InfoForVariant(prim, name)exposeGLTFMaterialInfowith theKHR_materials_unlitflag (Unlit) and per-slotGLTFTextureSlotUV metadata (EffectiveTexCoord,TransformforKHR_texture_transform). - What
MaterialWGSLdoes not draw: the shader lights every material and samples every texture with the mesh'sTEXCOORD_0and no transform. Unlit materials, texture transforms and other UV sets are recorded but not applied, so such assets draw wrong unless you handle them.GLTFMaterialInfo.Unsupported()lists these cases for a material andGLTFTextureSlot.NeedsUVBake()reports a slot that needs its UVs baked into the mesh (for example withDocument.TransformTexCoords) or a custom shader. - Khronos reference verification:
render3d's tests render the KhronosDamagedHelmetsample and check it against the asset's own base color image: a probe texture reads back the UV of every pixel, and the render must match the decoded JPEG under glTF's UV origin, sRGB color and channel order far better than under a flipped V, a linear upload or swapped channels. The tests also compare the render withrender3d/testdata/damagedhelmet/reference.pngto catch regressions and verifygpu.Viewparity across both the full andnsgotest.NoGPUCompute(WebGL 2 / GLES 3.0) profiles as well asRetaineddevice-loss restoration. The model is licensed CC-BY-NC-4.0, so its directory is a separate module that is left out of the publishedgithub.com/nsgo-dev/gamemodule, and these tests skip when it is absent.
Extended PBR materials: emissive strength, clearcoat, and transmission (GPUExtendedMaterial, TransmissionBackground)
Material also carries fields for KHR_materials_clearcoat (ClearcoatFactor, ClearcoatRoughnessFactor, ClearcoatTexture, ClearcoatSampler, ClearcoatRoughnessTexture, ClearcoatRoughnessSampler, ClearcoatNormalTexture, ClearcoatNormalSampler, ClearcoatNormalScale), KHR_materials_transmission (TransmissionFactor, TransmissionTexture, TransmissionSampler), and dielectric index of refraction (IOR, defaulting to DefaultMaterialIOR = 1.5), while KHR_materials_emissive_strength folds directly into HDR EmissiveFactor values (> 1.0, rendered cleanly into an HDRTarget with LinearOutput: true, BloomPass, and ToneMapPass).
Plain GPUMaterial and MaterialDefaults keep their lean 11-binding layout unchanged for ordinary metallic-roughness assets. For surfaces that use clearcoat, transmission, or custom IOR, use ExtendedMaterialDefaults, GPUExtendedMaterial (or UploadGLTFExtendedMaterials), and NewExtendedMaterialPipeline:
extDefs, err := render3d.NewExtendedMaterialDefaults(cx, dev, render3d.MaterialDefaultsOptions{Retained: true})
transBG, err := render3d.NewTransmissionBackground(cx, dev, render3d.TransmissionBackgroundOptions{
Retained: true,
SRGB: true, // true when sampling an sRGB-encoded RGBA8Unorm opaque pass; false for HDRTarget
})
pipe, err := render3d.NewExtendedMaterialPipeline(cx, dev, camBuf.Layout(), extDefs.Layout(), render3d.ExtendedMaterialPipelineOptions{
DepthFormat: gpu.FormatDepth24Plus,
LightLayout: lightBuf.Layout(), // optional LightRig at ExtendedLightGroup (2)
TransmissionLayout: transBG.Layout(), // optional per-frame background at TransmissionBackgroundGroup (3)
})
// Upload glTF materials (base PBR + clearcoat + transmission + emissiveStrength + IOR):
matSet, err := render3d.UploadGLTFExtendedMaterials(cx, dev, extDefs, doc, render3d.GLTFMaterialUpload{
Label: "Sunglasses",
Retained: true,
})
// Each frame: render opaque scene to opaqueRT, update the single per-frame TransmissionBackground, then draw transmissive draws:
_ = transBG.SetTexture(dev, opaqueRT.Color, true) // or transBG.SetDynamicResolution(dev, dynTarget, nil, 0, 0, true)
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
pass.SetBindGroup(render3d.ExtendedLightGroup, lightBuf.BindGroup())
transBG.Bind(pass) // @group(3) TransmissionBackgroundGroup (or BindAt(..., MaterialTransmissionBackgroundGroup) without LightLayout)
matSet.ExtendedMaterial(prim.Material).Bind(pass) // @group(1) MaterialGroup
mesh.Draw(pass)
- UV-aligned clearcoat normal mapping:
ExtendedMaterialBindingsWGSLreuses the same Cramer's-ruledpdx/dpdycotangent-frame normal perturbation helper (with deterministic orthonormal fallback) on the geometric normal forClearcoatNormalTextureindependently of the baseNormalTexture, honoring positive and negativeClearcoatNormalScale. - Per-frame transmission background and dynamic resolution:
TransmissionBackgroundkeeps the opaque scene color texture out of per-material bind groups so resizing the swapchain or scaling aDynamicResolutionTargetupdates only one pass-level bind group. Screen UVs are derived fromfrag_pos.xy / vec2<f32>(textureDimensions(transmission_bg_tex)), scaled and half-texel clamped bySetViewport/SetDynamicResolutionso unused padding texels never bleed. - CPU parity and Khronos golden tests:
EvaluateMaterial,EvaluatePBR,ShadePoint, andShadePointWithBackgroundevaluate clearcoat, transmission, and IOR on the CPU to match the GPU shaders. Tests verify GPU-vs-CPU parity,DynamicResolutionTargetresizes across 0.5×–1.0× scales,gpu.Viewexecution on both full andnsgotest.NoGPUCompute(WebGL 2 / GLES 3.0) profiles,Retaineddevice-loss restoration, and golden renders of the KhronosEmissiveStrengthTest(khronos_emissive_strength.png),TextureTransformMultiTestclearcoat panels (khronos_clearcoat.png), andSunglassesKhronos(khronos_transmission.png).
Unlit materials and vertex colors (UnlitMaterial, ColoredMesh)
ColoredMesh and GPUColoredMesh extend MeshVertexLayout (locations 0–2) with a linear RGBA vertex color attribute at @location(3) (ColoredVertexLayout, stride 48 bytes) for glTF COLOR_0. When Colors is empty, AppendVertexData writes linear opaque white (1, 1, 1, 1) per vertex so colored and uncolored meshes share one pipeline. ColoredMeshFromGLTF extracts triangle primitives from a gltf.Document including COLOR_0 (expanding VEC3 RGB to alpha 1.0) and KHR_texture_transform UVs on baseColorTexture.
UnlitMaterial, GPUUnlitMaterial, UnlitWGSL, and NewUnlitPipeline implement glTF KHR_materials_unlit surfaces unaffected by scene lighting (baseColorFactor * baseColorTexture * vertexColor in linear space, followed by alpha handling and IEC 61966-2-1 linear-to-sRGB encoding). They share the metallic-roughness types where the two overlap: AlphaMode, MaterialGroup (group 1), MaterialUpload, GLTFMaterialUpload, SamplerOptionsFromGLTF, and the per-device MaterialDefaults, which also owns the unlit bind group layout (MaterialDefaults.UnlitLayout):
defaults, err := render3d.NewMaterialDefaults(cx, dev, render3d.MaterialDefaultsOptions{Retained: true})
cm, err := render3d.ColoredMeshFromGLTF(doc, 0, 0)
gpuMesh, err := render3d.UploadColoredMesh(cx, dev, cm, render3d.MeshUpload{Label: "sign", Retained: true})
gpuMat, err := render3d.UploadGLTFUnlitMaterial(cx, dev, defaults, doc, 0, render3d.GLTFMaterialUpload{
Label: "sign-unlit",
Retained: true,
})
pipe, err := render3d.NewUnlitPipeline(cx, dev, camBuf.Layout(), defaults.UnlitLayout(), render3d.UnlitPipelineOptions{
AlphaMode: gpuMat.Material().AlphaMode,
DoubleSided: gpuMat.Material().DoubleSided,
DepthFormat: gpu.FormatDepth24Plus,
Retained: true,
})
// In OnFrame:
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
gpuMat.Bind(pass) // binds @group(1) (MaterialGroup)
gpuMesh.Draw(pass)
- Fallback texture and sampler: when
UnlitMaterial.BaseColorTextureorBaseColorSampleris nil,NewGPUUnlitMaterialbinds the sharedMaterialDefaults.WhiteSRGB()texture andMaterialDefaults.Sampler(), so textureless unlit materials allocate nothing beyond their 32-byte uniform buffer. - Alpha modes and culling:
AlphaOpaqueforces output alpha to1.0with depth writes enabled;AlphaMaskdiscards fragments with combined alpha< AlphaCutoff(default0.5) and outputs1.0;AlphaBlendenables straight-alpha blending (gpu.BlendAlpha) with depth writes disabled.DoubleSidedswitches culling fromgpu.CullBacktogpu.CullNone.BaseColorFactormust lie in[0, 1], as forMaterial. - Target color space and CPU parity:
NewUnlitPipelineselectsfs_main(linear-to-sRGB in shader) for non-sRGB targets such asFormatRGBA8Unorm, andfs_linear(linear output) for sRGB targets (ColorFormat.IsSRGB(), where the hardware encodes on write) or whenLinearOutputis set for anHDRTarget.NewMaterialPipelineand the lit PBR pipelines always encode in the shader and do not switch on an sRGB format, so when unlit and PBR draws share a pass, give them a non-sRGB target.EvaluateUnlitprovides the matching pure-Go CPU reference (Linear,SRGB,Discarded,RGBA8(),BlendOver()). - Vertex colors in both unlit and PBR pipelines:
ColoredVertexLayoutandGPUColoredMeshwork withNewUnlitPipeline, withNewMaterialPipelinewhenMaterialPipelineOptions.VertexColorsis set (MaterialColoredWGSL), and withNewLitPBRPipelineWithOptionswhenLitPBRPipelineOptions.VertexColorsis set (LitPBRColoredWGSLand its fog, IBL, shadow, and CSM variants). WithoutVertexColors, the metallic-roughness and lit PBR pipelines keepMeshVertexLayoutunchanged. - Zero-allocation uniform updates and device-loss recovery:
AppendUnlitUniformswrites the 32-byte uniform block with zero allocations when given capacity;GPUUnlitMaterial.Update(dev, mat)updates parameters in place without caching a*gpu.Devicepointer, so materials created withRetained: truesurvive device loss and remain updatable after restoration. NSGo restores a bind group layout only when a restored bind group uses it, soMaterialDefaults.UnlitLayout()comes back only while a retainedGPUUnlitMaterialexists;MaterialDefaults.Validdoes not cover it. - glTF helpers:
UnlitMaterialFromGLTFvalidates that a glTF material declaresKHR_materials_unlit(returningErrInvalidUnlitMaterialotherwise).UploadGLTFUnlitMaterialdecodes and uploads embedded or externalbaseColorTextureimages in sRGB with a sampler fromSamplerOptionsFromGLTF(so a non-mipmappedminFilterstays on level 0 even when mipmaps exist) and appliesGLTFMaterialUpload's mipmap, anisotropy andRetainedoptions asUploadGLTFMaterialsdoes. AbaseColorTexturewhose image comes only through an extension such asKHR_texture_basisureturnsErrInvalidUnlitMaterialinstead of drawing white. - Reference renders:
render3d's tests draw the KhronosUnlitTestsample end to end (loader,ColoredMeshFromGLTF,UploadGLTFUnlitMaterial,NewUnlitPipeline) and require every covered pixel to be the sample's flat#FF7F00or#007FFF, compare a multi-mode unlit + vertex-color scene againstrender3d/testdata/golden/unlit_vertex_colors_scene.png, and verifygpu.Viewpixel parity againstEvaluateUnliton both the full andnsgotest.NoGPUCompute(WebGL 2 / GLES 3.0) profiles.
Toon shading (ToonMaterial, ToonRenderer)
ToonMaterial and ToonRenderer draw cel/toon shading: quantized diffuse light bands (or a 1D color ramp texture), hard stepped Blinn-Phong highlights, Fresnel rim light, and inverted-hull outlines. The shaders (ToonWGSL, ToonOutlineWGSL) use only vertex and fragment stages, so they run on WebGPU, Metal, Vulkan, Direct3D 11, OpenGL 3.3, OpenGL ES 3.0, WebGL 2, and NSGo's software renderer:
defaults, err := render3d.NewMaterialDefaults(cx, dev, render3d.MaterialDefaultsOptions{Retained: true})
renderer, err := render3d.NewToonRenderer(cx, dev, render3d.ToonRendererOptions{
CameraLayout: camBuf.Layout(),
DepthFormat: gpu.FormatDepth24Plus,
Retained: true,
})
spec := render3d.DefaultToonMaterial()
spec.BaseColorFactor = [4]float32{0.94, 0.34, 0.24, 1}
spec.OutlineWidth = 0.045
heroMat, err := renderer.NewMaterial(cx, dev, defaults, spec, "hero")
// Hard-edged meshes (boxes, low-poly props) need shared corner normals for a closed outline:
boxOutlineCPU, err := render3d.SmoothOutlineNormals(boxMesh)
boxOutline, err := render3d.UploadMesh(cx, dev, boxOutlineCPU, render3d.MeshUpload{Label: "box-outline"})
// In OnFrame, inside a render pass with a depth attachment:
light, err := render3d.ToonLightFromRig(rig) // or render3d.DefaultToonLight()
err = renderer.Draw(dev, pass, camBuf.BindGroup(), light,
render3d.ToonDraw{Mesh: sphereMesh, Material: heroMat, Transform: sphereXform},
render3d.ToonDraw{Mesh: boxGPU, OutlineMesh: boxOutline, Material: boxMat, Transform: boxXform},
)
- Bands and ramps:
Bandsselects 2 to 8 diffuse steps (0means3), blending fromShadowColorto fully lit acrossN·L;BandSmoothnessin[0, 0.5]softens the band edges (0gives hard steps). ARampTexture, sampled atu = clamp(N·L, 0, 1), replaces the bands with an artist-drawn color ramp; without its ownRampSamplerthe material creates a linear-clamp sampler so the ramp's ends do not wrap. - Highlights:
SpecularColor,SpecularPower(0means32),SpecularThreshold, andSpecularSmoothnessadd a stepped Blinn-Phong highlight on lit surfaces;RimColor,RimThreshold, andRimSmoothnessadd a Fresnel rim based on1 - N·V. - Lights:
ToonLightholds oneDirectionalLightkey light, whoseN·Lis banded, and anAmbientLightfill, with the same conventions as aLightRig:Directionis the way the rays travel, a zero direction is an error, intensity0turns a light off, and errors wrapErrInvalidLight.ToonLightFromRigtakes the first directional light and the ambient fill of a scene's rig; point lights are not used. - Bind groups:
CameraGroup(0),ToonMaterialGroup(1, the same slot asMaterialGroup) andToonSceneGroup(2, the per-draw model matrix and the key light, with a dynamic offset inToonRenderer). - Materials:
NewGPUToonMaterialandToonRenderer.NewMaterialtake a sharedMaterialDefaultsand bind its white textures and sampler for omitted ones, asNewGPUMaterialdoes.GPUToonMaterial.Updaterewrites the uniform block without allocating but cannot swap textures or samplers. - Outlines: the outline pipeline (
NewToonOutlinePipeline,gpu.CullFront) pushes back faces out along their normals byOutlineWidthand fills them withOutlineColor; an outline alpha of0or a width of0turns it off. The width is in world units, or withOutlineClipSpacein NDC y units, which givesOutlineWidth × viewport height / 2pixels in every direction at every distance as long as the camera's aspect ratio matches the viewport's.SmoothOutlineNormalsaverages the normals of vertices at the same position (coordinates equal after rounding to 1e-5), each corner weighted by its angle, and returns an error wrappingErrInvalidMeshfor an invalid mesh. - Drawing:
ToonRenderer.Drawdraws the outlines first and the cel-shaded surfaces second, opaque and with depth writes. It can run any number of times before a submission, into one pass or several: each call writes its per-draw uniforms after the slots earlier calls of the frame used. Inside agpu.Framethe slots rewind on their own whendev.FrameIndex()advances; code that submits withdev.Submitdirectly callsToonRenderer.Beginonce per frame (at mostMaxToonDrawsPerFramedraws between two calls).Drawreturns agpu.Errorfor a nil or closed renderer, device, pass, or camera bind group. For blended toon surfaces, build pipelines withNewToonPipelineandNewToonOutlinePipelineand aToonPipelineOptions.Blend. - CPU reference and tests:
EvaluateToonandToonQuantizeBandevaluateToonWGSLin Go.render3d's tests compare a multi-object scene withrender3d/testdata/golden/toon_scene.png(UPDATE_GOLDEN=1rewrites it), draw in a headlessgpu.Viewon both the full andnsgotest.NoGPUComputeprofiles, restore aRetainedrenderer after a device loss, draw twice before one submission, and measure the clip-space outline on a wide viewport.
Custom shader materials (CustomMaterial)
NewCustomMaterial compiles a custom 3D material written in WGSL (ShaderWGSL) or Kage (ShaderKage) with declared, strongly-typed parameters (ShaderParamDesc) onto an NSGo gpu.Device, managing its std140 uniform buffer, @group(1) bind group (CustomMaterialGroup, the same slot as MaterialGroup), 1×1 fallback textures, and render pipeline inside the owning core.Scope. Where GPUMaterial and MaterialBindingsWGSL give a custom shader the fixed glTF metallic-roughness bindings, CustomMaterial lets the shader declare its own parameters and textures:
mat, err := render3d.NewCustomMaterial(cx, dev, render3d.CustomShaderDesc{
Label: "hologram",
Language: render3d.ShaderWGSL, // or render3d.ShaderKage
CameraLayout: camBuf.Layout(), // binds CameraWGSL at @group(0); omit for clip-space shaders
DrawLayout: draw.Layout(), // optional PBRDrawBuffer at @group(2) for a per-draw model matrix
DepthFormat: gpu.FormatDepth24Plus,
Cull: gpu.CullBack,
Retained: true, // survives GPU device loss and restore
Params: []render3d.ShaderParamDesc{
{Name: "intensity", Kind: render3d.ParamFloat, Default: float32(1.0)},
{Name: "light_dir", Kind: render3d.ParamVec3, Default: math3d.Vec3{X: 0.4, Y: 0.85, Z: 0.35}},
{Name: "tint", Kind: render3d.ParamColor, Default: core.RGB(0xf0c878)},
{Name: "albedo", Kind: render3d.ParamTexture},
},
Source: myWGSLSource,
})
// Update parameters at runtime and flush dirty bytes before drawing:
_ = mat.SetFloat("intensity", 1.5)
_ = mat.SetColor("tint", core.RGB(0x55d0ff))
_ = mat.SetTexture("albedo", tex, samp) // nil tex restores 1x1 fallback; nil samp uses default linear-repeat
_ = mat.Sync(dev) // 0 allocations when unchanged
// In OnFrame:
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
pass.SetBindGroup(render3d.PBRDrawGroup, draw.BindGroup())
mat.Draw(pass, mesh) // sets pipeline, binds @group(1), and draws mesh
- Declared parameter types and std140 layout:
ParamFloat(f32),ParamVec2(vec2<f32>),ParamVec3(vec3<f32>),ParamVec4/ParamColor(vec4<f32>),ParamInt(i32),ParamBool(u32,0/1for portable uniform structs), andParamTexture(texture_2d<f32>+<name>_sampler). Non-texture parameters are packed automatically in declared order with WGSL/std140 alignment (ComputeUniformLayout,ParamOffset,UniformSize) at@group(1) @binding(0)(material), followed by each texture/sampler pair (ParamBinding). A float, int or bool parameter cannot directly follow aParamVec3, because NSGo's Metal backend padsvec3struct members to 16 bytes where WGSL packs the next scalar into the last 4; declare it before the vec3 or after a vec2/vec4, or useParamVec4. Unset texture slots receive an automatic 1×1 fallback texture (opaque white by default, customizable viaShaderParamDesc.Default). - WGSL and Kage shaders:
ShaderWGSLprependsCameraWGSLwhenCameraLayoutis set and the source declares nothing in@group(0), and prepends the generatedMaterialParams/ texture bindings unlessManualBindings: true(then the source declares@group(1)itself).ShaderKageparses Kagevar <Name> float|vec2|vec3|vec4|int|booldeclarations, helper functions, andfunc Surface(uv vec2, normal vec3, worldPos vec3) vec4(orfunc Fragment(dst vec4, pos vec2) vec4) and translates them into portable WGSL supportingsample(<tex>, uv)andimage0At(uv). Unlike Ebitengine's 2D Kage,Fragment'sposis the mesh's normalized UV rather than a texel position,dstis the fragment's framebuffer position, andimageNAtsamples with the texture's sampler (linear-repeat by default), so it wraps instead of returning transparent outside the image. - Placement and lights: without
DrawLayout, the Kage vertex stage passes positions and normals through unchanged, soworldPosandnormalare in object space and the mesh draws where its vertices are. SetDrawLayoutto aPBRDrawBuffer's layout and bind its bind group atPBRDrawGroup(group 2): a Kage shader then transforms positions by thePBRDrawUniformmodel matrix and normals by its normal matrix, soworldPosandnormalare in world space, while a WGSL shader declares the block itself asLitPBRWGSLdoes.ExtraLayoutadds group 3 for bindings the shader declares, such as aGPULightBufferdeclared withLightWGSLForGroup(3)(LightWGSLuses group 1, which holds the material) or aDirectionalShadowMapwithShadowWGSL; it requiresDrawLayout.BindandDrawbind only group 1, so the caller binds groups 0, 2 and 3. - Texture swaps:
Synckeeps the bind groups of the 8 most recently bound texture/sampler combinations, so alternating between a few textures (a flipbook, ping-pong render targets) reuses them without allocating; a material that cycles through more distinct textures than that creates a bind group per change. - Strict validation and cross-backend checks: Typed setters (
SetFloat,SetVec2,SetVec3,SetVec4,SetColor,SetInt,SetBool,SetTexture) and atomicApplyMapreject unknown parameter names, type mismatches, non-finite values (NaN/Inf), and out-of-range values against optionalMin/Maxbounds with*CustomMaterialError, which matches bothErrInvalidCustomMaterialandErrInvalidMaterial. ItsLineis the source line, also for WGSL errors in code translated from Kage.ApplyMapbinds a texture with the default sampler, asSetTexturewith a nil sampler does.CompileCheck/CompileCheckDescverifies shader lowering and cross-backend pipeline compilation across MSL, HLSL, GLSL 3.30, GLSL ES 3.00 (WebGL 2 / Android), and WGSL without an activegpu.View.ByteSizereports the uniform buffer and fallback textures forRenderStatsandBudget. - Reference renders:
render3d's tests verify both WGSL and Kage custom materials against golden reference PNGs (render3d/testdata/golden/custom_material_wgsl.pngandcustom_material_kage.png), cross-backendPipelineCacheartifacts for all five targets, headlessgpu.Viewrendering on bothfullandnsgotest.NoGPUCompute(WebGL 2 / GLES 3.0) profiles,Retaineddevice-loss restoration, and zero-allocation steady-state updates;UPDATE_GOLDEN=1rewrites the reference images.
Budgeted async GPU uploads (Uploader)
Uploader runs the decoding and CPU packing of meshes, textures and raw GPU payloads on background worker goroutines, and Uploader.Step, called once per frame, writes the results to the gpu.Device in chunks within a per-frame byte budget, so loading a large 3D model does not stall a frame:
gpu.View(cx, gpu.ViewProps{
DepthFormat: gpu.FormatDepth24Plus,
OnReady: func(dev *gpu.Device) {
uploader = render3d.NewUploader(cx, dev, render3d.UploaderOptions{
BytesPerFrame: 2 * 1024 * 1024, // 2 MiB per frame (default 4 MiB)
ChunkBytes: 256 * 1024, // 256 KiB per write
Workers: 2, // background decode goroutines
})
meshHandle = uploader.QueueMesh(ctx, render3d.PriorityHigh, func(ctx context.Context) (render3d.Mesh, error) {
return loadAndDecodeMesh(ctx, "hero.glb")
}, func(g *render3d.GPUMesh, err error) {
if err == nil {
heroMesh = g
}
}, render3d.MeshUpload{Label: "hero", Retained: true})
texHandle = uploader.QueueTexture(ctx, render3d.PriorityNormal, func(ctx context.Context) (image.Image, error) {
return decodeTexturePNG(ctx, "hero_albedo.png")
}, func(tex *render3d.GPUTexture, err error) {
if err == nil {
heroAlbedo, _ = render3d.BindTexture(cx, dev, texLayout, tex, sampler)
}
}, render3d.TextureUpload{Label: "hero_albedo", SRGB: true, Mipmaps: true, Retained: true})
},
OnFrame: func(frame *gpu.Frame) {
stats := uploader.Step() // writes at most BytesPerFrame on the engine goroutine
_ = stats
// draw with whichever meshes and textures are ready...
},
})
- Drop-in for the synchronous uploads:
QueueMeshtakes the sameMeshUploadoptions asUploadMeshand delivers aGPUMesh;QueueTextureandQueueTexturePixelstake the sameTextureUploadoptions asUploadTextureandUploadTexturePixelsand deliver aGPUTexturewith the same texels, the same straight-alpha conversion (PixelBufferFromImage) and the same mip chain (box-filtered in linear light for sRGB), so it works withBindTextureand counts inGPUTexture.ByteSize. EachQueue*call also takes anUploadPriority. - Decode off-thread, upload on the engine goroutine: decode callbacks, mesh validation and packing, image conversion and mip generation run on the workers. GPU resource creation, the chunked writes (
WriteBuffer,WriteTextureRegion,WriteTexture) and theonReadycallbacks run on the goroutine that callsStep. Errors aQueue*call can report at once (a nil decode function, a closed uploader, a full queue) callonReadybefore the call returns, on the caller's goroutine. - Per-frame byte budget:
UploaderOptions.BytesPerFrame(defaultDefaultUploadBytesPerFrame, 4 MiB) andChunkBytes(defaultDefaultUploadChunkBytes, 256 KiB) cap the bytes written perStep, andSetBytesPerFrameandSetChunkByteschange them at runtime, for example raising the budget on a loading screen and lowering it during play. The budget bounds the upload work in each frame; it is not a time limit. Buffers split at 4-byte aligned offsets; uncompressed textures split by mip level, row band and, when a row exceeds the remaining budget, part of a row; block-compressed textures split by block row. - Raw buffers and textures:
QueueRawBufferstreams custom vertex, storage or uniform buffers (RawBufferUpload), andQueueRawTexturestreams pre-encoded or block-compressed mip chains, array layers and cube faces (RawTextureUpload,TextureMipData) and delivers thegpu.Texture. - Priority and cancellation:
PriorityHigh,PriorityNormalandPriorityLoworder both the decode and the upload queue (FIFO within a priority), so a high-priority asset preempts a low-priority one that is partway through its upload at the next chunk. EachQueue*call returns an*UploadHandlewhoseState(),Progress(),Err(),Done()andCancel()are safe from any goroutine. - Lifecycle: passing a
*core.ScopetoNewUploadercloses the uploader when the scope is disposed.Closecancels pending decodes, joins every worker goroutine, releases partially uploaded GPU resources and calls theonReadyof each unfinished upload withErrUploaderClosed; call it on the goroutine that callsStep, as scope disposal does. Passnilin headless tests and callCloseyourself.UploaderOptions.MaxPendingbounds the unfinished uploads, failing further ones withErrUploadQueueFull.
HDR rendering, tone mapping, and gamma (HDRTarget, ToneMapPass)
Render the scene's linear radiance into an HDRTarget (gpu.FormatRGBA16Float, falling back to gpu.FormatRGBA8Unorm on devices where SupportsHDR(dev) is false), then resolve it to the display with ToneMapPass. render3d's own shaders tone-map and encode to sRGB by default, so draw into the HDR target with their linear output mode: LitPBRPipelineOptions.LinearOutput for lit PBR (with or without fog or IBL, through NewLitPBRPipelineWithOptions) and MaterialPipelineOptions.LinearOutput for materials. Without it the image is tone-mapped and gamma-encoded twice and the HDR target only stores values already clamped to [0, 1].
var (
hdr *render3d.HDRTarget
pipe *gpu.Pipeline
tonePass *render3d.ToneMapPass
setupErr error
)
gpu.View(cx, gpu.ViewProps{
OnReady: func(dev *gpu.Device) {
if tonePass != nil {
tonePass.SetDevice(dev) // retained resources come back in place after a device loss
return
}
w, h := dev.SurfaceSize()
hdr, setupErr = render3d.NewHDRTarget(cx, dev, render3d.HDRTargetDesc{
Label: "scene-hdr",
Width: uint32(w),
Height: uint32(h),
DepthFormat: gpu.FormatDepth24Plus,
Retained: true,
})
if setupErr != nil {
return
}
pipe, setupErr = render3d.NewLitPBRPipelineWithOptions(cx, dev, camBuf.Layout(), lightBuf.Layout(), drawBuf.Layout(), render3d.LitPBRPipelineOptions{
ColorFormat: hdr.Format(),
DepthFormat: gpu.FormatDepth24Plus,
LinearOutput: true,
})
if setupErr != nil {
return
}
tonePass, setupErr = render3d.NewToneMapPass(cx, dev, render3d.ToneMapPassDesc{
Label: "tonemap",
TargetFormat: dev.SurfaceFormat(),
Retained: true,
})
},
OnFrame: func(frame *gpu.Frame) {
if setupErr != nil || tonePass == nil {
return
}
w, h := frame.Size()
if err := hdr.Resize(frame.Device(), uint32(w), uint32(h)); err != nil {
return
}
// 1. Render the scene in linear HDR radiance (values above 1.0 are kept):
scenePass := frame.BeginRenderPass(hdr.RenderPassDesc(core.RGB(0x0a0e18), 1))
scenePass.SetPipeline(pipe)
scenePass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
lightBuf.Bind(scenePass)
scenePass.SetBindGroup(render3d.PBRDrawGroup, drawBuf.BindGroup())
mesh.Draw(scenePass)
scenePass.End()
// 2. Tone-map and encode to sRGB onto the swapchain:
opts := render3d.DefaultToneMapOptions()
opts.Operator = render3d.ToneMapAgX
opts.Exposure = render3d.ExposureFromEV(0.5)
displayPass := frame.BeginRenderPass(gpu.RenderPassDesc{Label: "tonemap"})
err := tonePass.DrawTarget(displayPass, frame.Device(), hdr, opts)
displayPass.End()
if err != nil {
return
}
frame.Submit()
},
})
ToneMapOptions.Operator chooses the highlight compression curve:
| Operator | Curve | Best for |
|---|---|---|
ToneMapNone |
Linear clamp to [0, 1] |
Debugging linear inputs |
ToneMapReinhard |
Per-channel c / (1 + c) |
Simple soft highlight roll-off |
ToneMapACES (DefaultToneMapOptions) |
Narkowicz ACES filmic fit | High-contrast cinematic look |
ToneMapNeutral |
Khronos PBR Neutral | Faithful mid-tone hues and PBR asset viewing |
ToneMapAgX |
Troy Sobotka AgX default look | Smooth highlight desaturation without hue skew |
- Exposure:
ToneMapOptions.Exposureis a linear multiplier; derive it from photographic stops withExposureFromEV(ev)(0 -> 1.0,+1 -> 2.0,-1 -> 0.5, clamped to[-126, 126]stops so it is always positive). Exposure0renders black, so a fade to black can interpolate it down to0; negative, NaN and infinite values count as1.0. Start fromDefaultToneMapOptions()so the field's zero value does not leave the image black. - Gamma and sRGB: when
ToneMapOptions.Gamma <= 0(the default) andLinearOutputis false,ToneMapPassapplies the exact IEC 61966-2-1 linear-to-sRGB transfer function (LinearToSRGBChannel/LinearToSRGBon the CPU, withSRGBToLinearChannel/SRGBToLinearas their inverse); a positiveGamma(such as2.2) appliespow(c, 1/Gamma). WhenToneMapPassDesc.TargetFormat.IsSRGB()is true, the pass writes linear tone-mapped RGB so the sRGB framebuffer encodes it once, without double gamma. A material drawn withLinearOutputand resolved withToneMapReinhardandGamma: 2.2looks the same as the material pipeline's own output. - Overflow: an
RGBA16Floattarget stores values above 65504, such as strong specular highlights, as+Inf. The pass clamps exposed radiance to[0, 65504]and maps NaN to0before the curve (ApplyToneMapdoes the same), so blown highlights stay white instead of turning black. - Dynamic resolution: create the
DynamicResolutionTargetwith an HDRColorFormat(PreferredHDRFormat(dev)), draw the scene withBegin3D, and callToneMapPass.DrawDynamicResolutionin place ofUpscale. It tone-maps only the scaled sub-viewport and upscales it with the pass's sampler in the same draw; the sharpen filter is not applied. - One set of options per submission: each
ToneMapPasshas one uniform buffer, and buffer writes are applied before the submission's commands, so every draw one pass records into a frame uses the options of its lastDraw. To tone-map several views with different options in one frame (split screen, a picture-in-picture camera), create oneToneMapPassper view. - Allocations and device loss:
AppendToneMapUniformsencodes theToneMapUniformSize(48-byte) uniform block without allocating, and the pass reuses its bind group while the source texture stays the same; each draw still uploads the block withgpu.Device.WriteBuffer, which copies it. WithRetained: trueonHDRTargetDescandToneMapPassDesc, NSGo recreates the textures, sampler, uniform buffer, layout and pipeline after a device loss, and the pass rebuilds its bind group on the next draw. - CPU reference and portability:
ApplyToneMapmirrorsToneMapWGSLon the CPU.ToneMapWGSLavoids theexpandlog2builtins (AgX uses a range-reduced polynomiallog2), because NSGo v0.3.0's software GPU returns 0 for them, and runs on both the full GPU profile andnsgotest.NoGPUCompute(WebGL 2 / OpenGL ES 3.0). - Reference renders: the tests draw a scene through the lit PBR pipeline's linear output mode, with emissive boxes and highlights well above 1.0, and compare it with
render3d/testdata/golden/tonemap_aces.png,tonemap_agx.pngandtonemap_neutral.png;UPDATE_GOLDEN=1rewrites them. Other tests check the lit PBR and material linear modes againstShadePointandEvaluateMaterialthroughApplyToneMap.
Anti-aliasing (MSAATarget, FXAAPass)
render3d supports hardware multisample anti-aliasing (MSAA) where the GPU device offers it and a full-screen FXAA 3.11 post-processing pass for single-sampled paths, post-FX chains, or devices without MSAA:
Mode (AAMode) |
Behavior |
|---|---|
AANone |
Single-sampled (1x), no post-processing. |
AAFXAA |
Single-sampled render into an offscreen target followed by FXAAPass. |
AAMSAA2x |
Requests 2x MSAA; ClampSampleCount promotes it to 4x when Features.MSAA4x is true (WebGPU and NSGo expose 1x and 4x sample counts) and falls back to 1x otherwise. |
AAMSAA4x |
Requests 4x MSAA; resolves to 4x when Features.MSAA4x is true and falls back to 1x otherwise. |
MSAATarget wraps an offscreen color (and optional depth) gpu.RenderTarget whose sample count is clamped via ClampSampleCount. Call Resize on viewport size changes (redundant calls at the same dimensions are zero-allocation no-ops), pass ColorAttachment / DepthStencilAttachment (or RenderPassDesc) into BeginRenderPass, and read the single-sampled result from OutputTexture (the resolved texture when multisampled, or the color texture when single-sampled):
target, err := render3d.NewMSAATarget(cx, dev, render3d.MSAATargetDesc{
Label: "scene",
Width: uint32(w),
Height: uint32(h),
ColorFormat: gpu.FormatRGBA8Unorm,
DepthFormat: gpu.FormatDepth24Plus,
SampleCount: mode.SampleCount(), // clamped automatically via ClampSampleCount
Retained: true,
})
// In OnFrame:
desc, err := target.RenderPassDesc(clearColor, 1.0)
if err != nil {
return // the target is closed or its device is lost
}
pass := frame.BeginRenderPass(desc)
// ... draw with a pipeline using SampleCount: target.SampleCount() ...
pass.End()
resolved := target.OutputTexture()
When MSAA is multisampled, ColorAttachment sets ResolveTarget to the resolve texture and StoreOp to gpu.StoreDiscard so tile GPUs avoid writing back the transient multisample buffer; when clamped to 1x, it stores directly into the color texture with gpu.StoreStore. ColorAttachment and RenderPassDesc return an error for a closed target or a lost device rather than an attachment with a nil texture, which NSGo would draw into the view's swapchain.
- Depth: the depth attachment is transient by default: it is cleared at the start of the pass and discarded at the end (and so is the stencil of a depth-stencil format). Set
KeepDepth: trueonMSAATargetDescto store it, so later passes can readDepthTexture(); the depth texture of a4xtarget is multisampled. - Sample count across devices: the target keeps the requested sample count, and
Resizeclamps it against the current device, reallocating even at the same size when the clamped count changes. AfterSetDeviceto a device with different MSAA support, callResize, compareSampleCount()with its previous value, and rebuild the pipelines built with the old count.
For post-process anti-aliasing, FXAAPass compiles FXAAWGSL (a bufferless full-screen triangle FXAA 3.11 Quality shader using only textureSampleLevel(..., 0.0), so it runs without compute shaders on both full and NoGPUCompute / WebGL 2 / OpenGL ES 3.0 profiles) and keeps one bind group per source texture, so steady-state draws do not allocate. The shader reads the texel size from the source with textureDimensions, so one pass can filter sources of different sizes in the same frame:
fxaa, err := render3d.NewFXAAPass(cx, dev, render3d.FXAAPassDesc{
TargetFormat: dev.SurfaceFormat(),
Options: render3d.DefaultFXAAOptions(),
Retained: true,
})
// Inside a single-sampled render pass (e.g. swapchain) or via fxaa.Encode(enc, src, dst):
err = fxaa.Draw(swapchainPass, target.OutputTexture())
FXAAOptionstunesSubpixelQuality(default0.75, setDisableSubpixel: truefor edge-only smoothing),EdgeThreshold(default0.166), andEdgeThresholdMin(default0.0833); non-finite or out-of-range fields are sanitized viaSanitize().- One set of options per submission: the options are one uniform block, and buffer writes are applied before the submission's commands, so every draw one
FXAAPassrecords into a frame uses the options of its last draw. To filter several views with different options in one frame, create oneFXAAPassper option set. ApplyFXAA(src, opts)runs the matching pure-Go CPU FXAA 3.11 reference on an*image.RGBA, leaving flat interiors below the contrast threshold untouched while smoothing diagonal edges.- If a
RetainedMSAATargetorFXAAPasssurvives a device loss, callSetDevice(dev)inOnReady/OnDeviceRestoredso subsequentResizeor uniform/bind-group updates target the restored device. - Color space: FXAA's thresholds are tuned for perceptual, gamma-encoded luma, and
ApplyFXAAworks on the stored bytes. Feed the pass a non-sRGB source holding gamma-encoded (for example tone-mapped LDR) color, and draw into a target of the same kind. An sRGB source is sampled as linear values, so edge detection no longer matchesApplyFXAA, and a gamma-encoded source drawn into an sRGB target is encoded twice.TargetFormatdefaults todev.SurfaceFormat(), which is sRGB on some platforms, so set it explicitly. - Reference renders: the software-renderer tests compare the scene with
aa_baseline.png,aa_msaa4x.png, andaa_fxaa.pngunderrender3d/testdata/golden/(UPDATE_GOLDEN=1rewrites them) and checkFXAAPassagainstApplyFXAA. On both the full andNoGPUComputeprofiles, the tests check the MSAA output against the software renderer's reference render and theFXAAPassoutput againstApplyFXAA.
Directional light shadows (DirectionalShadowMap, DirectionalShadowConfig)
DirectionalShadowMap manages an offscreen FormatDepth32Float shadow map render target, a depth-only caster pipeline (ShadowDepthWGSL) with a dynamic-offset caster transform buffer, a 96-byte ShadowUniforms buffer, and both nearest (hard) and linear (PCF) comparison samplers on an NSGo gpu.Device. ShadowWGSL declares the matching bindings at ShadowGroup (group 3, after CameraGroup, LightGroup and PBRDrawGroup) together with a portable sample_directional_shadow(world_pos, world_normal) helper. NewLitPBRShadowPipeline (or LitPBRPipelineOptions.ShadowLayout) builds the lit PBR pipeline with the shadow (LitPBRShadowWGSL): it shades a LightRig exactly like LitPBRWGSL and multiplies the rig's first directional light, Directional[0], by the shadow, leaving the other lights, ambient and emission unshadowed. Group 3 is also where fog, IBL and skinning go, so a lit PBR pipeline takes one of them.
sm, err := render3d.NewDirectionalShadowMap(cx, dev, render3d.ShadowMapOptions{
Resolution: 1024,
PCFKernel: render3d.ShadowFilterPCF3x3, // or ShadowFilterHard
DepthBias: 2,
DepthBiasSlopeScale: 2.0,
CullMode: gpu.CullBack,
Retained: true, // survives GPU device loss automatically
})
litPipe, err := render3d.NewLitPBRShadowPipeline(cx, dev, camBuf.Layout(), lightBuf.Layout(), drawBuf.Layout(), sm.Layout(), gpu.FormatDepth24Plus)
// Each frame in OnFrame(frame *gpu.Frame):
dev := frame.Device()
cfg := render3d.DefaultDirectionalShadowConfig()
cfg.Direction = rig.Directional[0].Direction // the light the shadow attenuates
cfg.Strength = 1
if !preset.Shadows { // quality.Preset from the quality package
cfg.Strength = 0 // the shader then skips the shadow map
}
_, err = sm.UpdateBounds(dev, cfg, sceneBounds) // fits the orthographic frustum, texel-snapped
// 1. Depth-only shadow pass (nil while the device is lost or after Close):
if preset.Shadows {
if shadowPass := sm.BeginShadowPass(dev, frame); shadowPass != nil {
sm.DrawCaster(shadowPass, pillarMesh, pillarModel)
sm.DrawCaster(shadowPass, crateMesh, crateModel)
shadowPass.End()
}
}
// 2. Main lit pass:
mainPass := frame.BeginRenderPass(gpu.RenderPassDesc{})
mainPass.SetPipeline(litPipe)
mainPass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
mainPass.SetBindGroup(render3d.LightGroup, lightBuf.BindGroup())
mainPass.SetBindGroup(render3d.ShadowGroup, sm.BindGroup())
mainPass.SetBindGroup(render3d.PBRDrawGroup, drawBuf.BindGroup())
floorMesh.Draw(mainPass)
mainPass.End()
- Framing and texel snapping:
DirectionalShadowConfig.LightViewProj()builds an orthographic frustumOrthoSizewide andDepthRangedeep, centred onTarget(which sits at depth 0.5).FitBounds(lightDir, sceneBounds)encloses anAABB, extending toward the light byCasterPadding. WithTexelSnap: truethe shadow map's texel grid stays fixed in world space: the frustum is sized from the bounds' bounding sphere, rounded up to a coarse step, so the texel size does not change as the bounds move or the light turns, and the light-space center snaps to whole texels, so moving the framing (for exampleUpdateBoundsfollowing the camera) shifts static shadows by whole texels and their edges do not shimmer. WithoutTexelSnap,FitBoundshugs the bounds' light-space extent for the most texels per world unit, at the cost of shimmering when the bounds move. - Hard 1-tap vs 3x3 PCF filtering:
ShadowFilterHarduses a nearest comparison sampler with a singletextureSampleCompareLeveltap;ShadowFilterPCF3x3averages 9 comparison taps with a linear comparison sampler (combining hardware 2x2 PCF per tap with a 3x3 neighborhood) for soft penumbras. SwitchingPCFKernelviaWriteUniforms,Update, orUpdateBoundsswapsBindGroup()dynamically without allocating or recreating GPU resources. - Acne control and portability:
DepthBias(slope-scaled in the shader by1 + 2*(1 - NdotL)), world-spaceNormalBias, and hardware rasterizer bias (ShadowMapOptions.DepthBias/DepthBiasSlopeScale) suppress shadow acne across flat and grazing surfaces. Custom shaders can prependShadowWGSL(orShadowWGSLForGroup(n)when group 3 holds something else) and callsample_directional_shadow(world_pos, world_normal);LightWGSL'sshade_pbr_shadowedtakes that visibility for directional light 0. No compute shaders or storage buffers are used, so directional shadows run on every NSGo target including WebGL 2 and OpenGL ES 3.0. - Quality presets: to honour
quality.Preset.Shadows, skip the shadow pass and setStrengthto 0 when it is off, as in the example; withStrength0 the shader returns full visibility without sampling the map. - Zero-allocation uniforms and CPU reference:
AppendShadowUniformsandShadowUniforms.Encodepack the 96-byte uniform block with zero heap allocations; a NaN or infinite matrix orStrengthdisables the shadow, non-finite biases become 0 and an unknownPCFKernelis treated asShadowFilterHard, so uniforms built by hand cannot put NaN on screen.EvaluateDirectionalShadowis the matching pure-Go CPU reference over aShadowDepthGrid(decoded from GPU depth readback viaShadowDepthGridFromBytes), andShadePointShadowedis the CPU reference of the shadowed lighting.render3d's tests check every open floor pixel of hard and 3x3 PCF renders, penumbras included, against these references, check where the shadow falls by projecting a caster along the light, and compare the renders with golden PNGs (render3d/testdata/golden/shadow_directional_hard.pngandshadow_directional_pcf.png) on bothgpu.Viewprofiles (fullandnsgotest.NoGPUCompute), acrossRetaineddevice-loss restoration, and through scope disposal.
Render-to-texture cameras and multiple viewports (RenderTexture, ViewportRenderer)
RenderTexture allocates a scope-owned offscreen gpu.RenderTarget (color, optional depth/stencil, and optional 4x MSAA with automatic resolve) whose sampleable color output is exposed directly as a *GPUTexture via RenderTexture.GPUTexture(). Pass that handle to BindTexture (or any material texture slot) so mirrors, minimaps, security monitors, and portals render from a secondary camera and appear as textures on 3D geometry in the main pass:
rt, err := render3d.NewRenderTexture(cx, dev, render3d.RenderTextureDesc{
Label: "mirror",
Width: 256,
Height: 256,
DepthFormat: gpu.FormatDepth24Plus,
Retained: true, // survives device loss alongside retained samplers and bind groups
})
mirrorBG, err := render3d.BindTexture(cx, dev, texLayout, rt.GPUTexture(), samp)
ViewportRenderer orchestrates frames with any mix of offscreen RenderTexture cameras and multiple viewports on the main surface (side-by-side split screen, stacked split screen, 4-player quad split, or overlapping picture-in-picture overlays):
renderer, err := render3d.NewViewportRenderer(cx, dev) // list renderer.CameraLayout() at CameraGroup
halves := render3d.SplitScreenHorizontal(2) // left [0..0.5], right [0.5..1]
views := []render3d.CameraView{
{Name: "mirror", Camera: mirrorCam, Target: rt, LayerMask: layerWorld, ClearColor: core.RGB(0x182030)},
{Name: "player1", Camera: p1Cam, Viewport: halves[0], LayerMask: layerWorld | layerMirror, ClearColor: core.RGB(0x0c1018)},
{Name: "player2", Camera: p2Cam, Viewport: halves[1], LayerMask: layerWorld | layerMirror},
}
err = renderer.RenderFrame(frame, render3d.SurfaceDesc{HasDepth: true}, views, func(ctx render3d.ViewportContext) {
visible = render3d.FilterLayeredItems(sceneItems, ctx.LayerMask, visible[:0])
plan := queue.Build(ctx.Uniform.ViewProj, ctx.Camera.Pos, visible)
// ctx.Pass already has SetViewport, SetScissor, and CameraGroup bound for this view.
drawPlan(ctx.Pass, plan)
})
- Execution order:
ViewportRendereralways renders offscreen views (Target != nil) before main-surface views (Target == nil), ordering each phase stably by ascendingCameraView.Priority(OrderCameraViews), so offscreen textures are up to date before any main-pass material samples them. - Per-view camera uniforms & aspect ratio: each active view gets its own retained uniform buffer slot at
CameraGroupuploaded before pass recording so multiple views in onegpu.Frameorgpu.Encodernever clobber each other's matrices. UnlessCameraView.PreserveAspectis set, a per-view copy ofCameraadjustsProjection.Aspectto the resolvedPixelViewportwithout mutating the caller's*scene3d.Camera. The slots are reused by every call, so callRenderFrameorRenderEncoder(which takes the device the encoder was begun on) at most once per submitted frame or command buffer, and use a separateViewportRendererfor each call that shares one submission. - Per-camera layer masks:
CameraView.LayerMaskmatchesscene3d.Node.Layerbitmasks (zero defaults toscene3d.LayerAll).LayerMatches,FilterLayeredItems, andFilterNodesByLayerfilterLayeredDrawItemand*scene3d.Nodeslices with zero steady-state allocations when reusing the destination buffer. In those functions, as inscene3d.Decal.LayerMask, a zero mask matches nothing, so passViewportContext.LayerMask(already the effective mask); setCameraView.Disabledfor a camera that renders nothing.FilterNodesByLayertests the layer only and keeps hidden or disabled nodes. - Split-screen seams & overlays:
SplitScreenHorizontal(SplitScreenSideBySide),SplitScreenVertical(SplitScreenStacked),SplitScreenQuad, andViewportFromPixelsconstruct normalized[0, 1]ViewportRectvalues.ViewportRect.Resolverounds both edges independently so adjacent split-screen viewports tile with zero pixel gaps or overlap, andPixelViewport.Applysets bothSetViewportandSetScissorso geometry never bleeds across seams.PixelViewportis in physical pixels;PixelViewport.Bounds(pixelsPerPoint)divides by the surface's content scale (its pixel width over the view's width in points) to give the logical-pointcore.BoundsthatDebugDraw.Flush,Gizmo.DrawGizmoandBillboardRenderer.Drawtake. Consecutive views on the same target share one GPU render pass, and only the view that opens a pass clears it, so a later view in that pass ignores its ownClearColor(draw a full-viewport quad for a distinct background); setCameraView.ResetDepthon an overlapping picture-in-picture view to preserve the underlying color while clearing depth. A view that returns to an offscreen target after views on another target (for example two halves of one atlas with a different target between them byPriority) loads that target's color and depth instead of clearing them, so the earlier views' output is kept. - Resize & device loss:
RenderTexture.Resizeis a no-op when dimensions match and swaps atomically on change, incrementingGeneration()so callers know when to rebindBindTexturebind groups. WithRetained: true,RenderTextureandViewportRendererrestore automatically after GPU device loss and are released with their owningcore.ScopeorClose().
3D debug draw and transform gizmos
DebugDraw projects 3D wireframes with near-plane clipping directly into an NSGo *core.Drawing overlay:
var dbg render3d.DebugDraw
dbg.Grid(math3d.Vec3Zero(), 10, 20, core.RGBA(120, 130, 150, 0.4))
dbg.AABB(enemyBounds, core.RGB(255, 80, 80))
dbg.Sphere(alertCenter, 4, core.RGB(255, 200, 60), 24)
dbg.Axes(selectedPos, 1.5)
dbg.Flush(d, cam.ViewProj(), viewportBounds)
dbg.Clear()
For level editors and inspectors, Gizmo handles translate, rotate, and scale axes (GizmoTranslate, GizmoRotate, GizmoScale):
var gizmo render3d.Gizmo
axis, _ := gizmo.PickAxis(ray, objPos, gizmoScale, render3d.GizmoTranslate)
if axis != render3d.AxisNone {
delta := gizmo.DragTranslate(objPos, axis, startRay, currentRay, 0.25) // 0.25m snap
objPos = objPos.Add(delta)
}
gizmo.DrawGizmo(d, cam.ViewProj(), viewportBounds, objPos, gizmoScale, render3d.GizmoTranslate, axis)
Debug lines and gizmos clip at the near plane of a [0, 1] depth projection, which math3d.Perspective and math3d.Ortho produce. For an OpenGL-style [-1, 1] projection set DebugDraw.DepthNO and Gizmo.DepthNO, or call ProjectSegmentNO, so the near half of its depth range is not hidden. Each planar translate handle is drawn as the square that PickAxis tests. DrawGizmo allocates nothing; to read the segments yourself without allocating, reuse a slice with gizmo.AppendSegments(buf[:0], objPos, gizmoScale, mode).
Billboards and ribbon trails
A Billboard is a quad that turns toward the camera, and a Trail is a ribbon through a moving object's recent positions that twists about its centerline to face the viewer. Both take a CameraBasis, the camera's position and unit axes, built from matrices or filled from a scene3d.Camera:
basis, _ := render3d.NewCameraBasis(cam.ViewMatrix(), cam.ProjMatrix())
// or: render3d.CameraBasis{Pos: cam.Pos, Right: cam.Right(), Up: cam.Up(), Forward: cam.Forward(), Orthographic: cam.Projection.Kind == scene3d.ProjectionOrthographic}
Billboard.Mode picks how the quad turns:
| Mode | Faces | Use for |
|---|---|---|
BillboardViewPlane (default) |
the view plane, along the camera's right and up | particles, sprites, impostors |
BillboardViewPoint |
the camera's position, rolled by the camera's up | large quads near the screen edge |
BillboardAxial |
the camera, rotating only about Axis (+Y when zero) |
trees, grass, flames, beams |
The view-plane and view-point modes face the camera at every angle, including looking straight up and down; an axial quad faces it as far as its axis allows and turns edge-on when the camera looks along the axis. Pivot places the quad relative to Center ({Y: -0.5} stands it on its bottom edge) and Rotation rolls it counter-clockwise on screen.
smoke := render3d.Billboard{Center: pos, Width: 1, Height: 1, Rotation: spin, Color: core.RGBA(200, 200, 200, 0.6)}
tree := render3d.Billboard{Center: base, Width: 2, Height: 4, Pivot: math3d.Vec2{Y: -0.5}, Mode: render3d.BillboardAxial, Texture: treeImage}
quad, ok := smoke.Quad(basis) // corners BL, BR, TR, TL, counter-clockwise toward the camera
item := quad.DrawItem(render3d.DrawItem{ID: 7, MeshID: unitQuad}) // Transform maps the unit quad; Blend defaults to BlendAlphaBlend
Until GPU submission lands, BillboardRenderer fills billboards into a *core.Drawing back to front, clipping them against the near plane. Textured quads are mapped with an affine transform, which is exact under an orthographic camera and close for quads that are small on screen; a textured quad that crosses the near plane is skipped.
var billboards render3d.BillboardRenderer // reuse across frames
billboards.Draw(d, []render3d.Billboard{smoke, tree}, basis, cam.ViewProj(), viewportBounds)
Trail mirrors the point-recording, lifetime, width and color options of particles.Trail in world units and reuses its particles.Curve and particles.ColorCurve for width and color from head (u = 0) to tail (u = 1). It has no Texture and no Attach, Detach or Follow: call AddPoint with the object's position each frame. Each point's edge runs along tangent × toCamera, and a trail heading straight at the camera keeps its previous edge direction instead of collapsing:
sword := render3d.NewTrail(render3d.TrailConfig{
Width: 0.3,
Lifetime: 250 * time.Millisecond,
WidthCurve: particles.Curve{{T: 0, Value: 1}, {T: 1, Value: 0}},
ColorGradient: particles.ColorCurve{{T: 0, Color: core.RGB(0xffe080)}, {T: 1, Color: core.RGBA(255, 80, 0, 0)}},
})
// In Update:
sword.AddPoint(bladeTip)
sword.Update(dt)
// In Draw, or hand Build's triangle strip (two vertices per point, tail to head) to a GPU:
verts := sword.Build(basis)
sword.Draw(d, basis, cam.ViewProj(), viewportBounds)
Build reuses its storage and does not allocate; Bounds and DrawItem give the transparent queue a camera-independent box for sorting.
Render stats, profiler, and GPU budgets
plan.Stats (RenderStats) reports exact culled/visible counts, draw calls, instanced batches, triangles, vertices, material/pipeline switches, and GPU memory. Track rolling frame timing with NewFrameProfiler(120) (Record, AverageFrameTime, P95FrameTime, FPS, PeakDrawCalls, PeakTriangles) and assert budgets in dev builds or tests with CheckBudget:
| Preset | MaxDrawCalls | MaxTriangles | MaxVertices | MaxMaterialSwitches | MaxPipelineSwitches | MaxTextureBytes | MaxBufferBytes |
|---|---|---|---|---|---|---|---|
MobileBudget() |
150 | 150,000 | 300,000 | 60 | 32 | 128 MiB | 64 MiB |
DesktopBudget() |
2,000 | 2,000,000 | 4,000,000 | 500 | 200 | 1 GiB | 512 MiB |
if violations := render3d.CheckBudget(plan.Stats, render3d.MobileBudget()); len(violations) > 0 {
for _, v := range violations {
log.Println("budget exceeded:", v)
}
}
Hot-reloading shaders and materials
Run the game with nsgo game dev and attach hotreload.ShaderReloader and hotreload.MaterialReloader (from github.com/nsgo-dev/game/pkg/tooling/hotreload) to hotreload.NewDevWatcher so edited WGSL (.wgsl), Kage (.kage), and material YAML (.material.yaml, .mat.yaml, .material.yml, .mat.yml) files reload within a second without restarting:
w := hotreload.NewDevWatcher(cx) // nil outside game dev and in release builds
dir := hotreload.DevAssetsDir()
shaders := hotreload.NewShaderReloader(cx, dev,
hotreload.WithShaderDir(dir),
hotreload.WithOnShaderError(func(re hotreload.ReloadError) {
log.Printf("shader error (%s): %v", re.Name, re)
}),
)
materials := hotreload.NewMaterialReloader(cx, shaders,
hotreload.WithMaterialDir(dir),
hotreload.WithOnMaterialError(func(re hotreload.ReloadError) {
log.Printf("material error (%s): %v", re.Name, re)
}),
)
w.WatchMaterials(materials) // watches both material YAML and referenced .wgsl/.kage shaders
_, _, err := materials.Register("crystal", "materials/crystal.material.yaml", buildPipeline)
Every reload compiles a candidate *gpu.ShaderModule and builds a candidate *gpu.Pipeline before swapping:
- Safe fallback on errors: if a shader edit fails to compile, misses an entry point, or fails pipeline creation — or if a material YAML has invalid syntax, an unknown field, or an out-of-range value — the previous working shader, material, and
*gpu.Pipelineremain active without crashing. The failure is recorded as aReloadError(unwrapping to*loaderr.Errorwith file, line, and column when reported) accessible viaLastErrorandErrors, and clears automatically once a subsequent save compiles cleanly. When debouncing is enabled,Watcheralso gives 0-byte reads extra time to settle so editors that save by truncating before writing do not emit a spurious empty-source error, while a file that genuinely stays empty still reports it. - Shader-to-material dependencies: editing a shader referenced by one or more materials automatically rebuilds and swaps every dependent material's pipeline, closing replaced pipelines cleanly.
- Text material format (
MaterialDef): a versioned YAML document (version: 1, refusing unknown fields and round-tripping deterministically viaParseMaterial/EncodeMaterial) declaringshader(or splitvertexShader/fragmentShader), entry points (vertexEntry,fragmentEntry), GPU render state (blend,cull,frontFace,depthWrite,depthCompare, applied viaApplyToPipelineDesc), and typed uniform parameters (scalars,vec2,vec3,vec4,colors,textures). For shared 2D/PBRresource.Materialfiles (type: material), usew.WatchResources(resource). - Direct shader watching: for standalone pipelines without material YAML, use
ShaderReloader.RegisterPipelinewithw.WatchShaders(shaders), orw.WatchShader(relPath, onReload)for a single.wgslor.kagefile. - Release builds: under
-tags release,NewDevWatcherreturns nil andWatchShader,WatchShaders, andWatchMaterialscompile to no-ops whileShaderReloaderandMaterialReloadercontinue serving initial loads.
Dynamic resolution scaling (DynamicResolutionScaler, DynamicResolutionTarget)
DynamicResolutionScaler adapts a linear 3D render scale in [MinScale, MaxScale] (default [0.5, 1.0]) to hold TargetFrameTime (default ~16.67 ms for 60 FPS), and DynamicResolutionTarget renders the 3D scene into a scaled sub-viewport of an offscreen gpu.RenderTarget and upscales it onto the native surface while keeping 2D HUD overlays at full native resolution:
target, err := render3d.NewDynamicResolutionTarget(cx, dev, render3d.DynamicResolutionTargetOptions{
TimingLabel: "world-3d",
Config: render3d.DynamicResolutionConfig{
TargetFrameTime: time.Second / 60,
MinScale: 0.5,
MaxScale: 1.0,
Filter: render3d.DynamicResolutionBilinear, // or DynamicResolutionNearest / DynamicResolutionSharpen
},
})
// In gpu.ViewProps.OnFrame (with scene.Overlay3D mounted over gpu.View for a native-res HUD):
w, h := frame.Size()
pass3d, err := target.Begin3D(frame, w, h, core.RGB(0x10141c))
pass3d.SetPipeline(scenePipe)
pass3d.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
mesh.Draw(pass3d)
pass3d.End()
_ = target.Upscale(frame, nil, w, h) // blits scaled 3D sub-viewport to native surface
frame.Submit()
target.RecordPassTimings(frame.PassTimings()) // or target.RecordFrameTime(gpuDuration)
- Proportional fill-rate controller with hysteresis: because fragment work scales with
scale²,DynamicResolutionScaler.RecordFrameTimesmooths GPU timings with an EMA (SmoothingAlpha), ignores loading/resume spikes aboveHitchLimit, holds steady inside±Deadband, steps down promptly on overload (CooldownDown,StepDown), and recovers smoothly under sustained headroom (CooldownUp,StepUp). UseSetFixedScale,ClearFixedScale,SetEnabled, andSetScaleBounds(for example cappingMaxScaletoquality.Preset.ResolutionScale) to override or bound adaptation. - Zero-reallocation sub-viewport scaling:
DynamicResolutionTargetallocates its offscreen color+depthgpu.RenderTargetat native capacity and setsSetViewportandSetScissorto[0, 0, scaledW, scaledH]on eachBegin3Dcall, so scale changes never churn GPU textures.Upscaleclamps UVs to half-texel inset bounds of the active sub-rectangle so unused padding texels never bleed into the edges. - Native-resolution HUD: mount
scene.Overlay3Dovergpu.Viewso 2D HUD layers draw in a crisp 1:1 nativecore.Canvas, record a follow-upgpu.LoadLoadnative HUD pass afterUpscale, or callUpscaleIntoPassinside a single native color pass and draw GPU HUD elements right after it. - Upscale filters: switch between
DynamicResolutionBilinear,DynamicResolutionNearest, andDynamicResolutionSharpen(a 5-tap contrast-adaptive sharpening pass clamped to the local neighborhood min/max) at runtime viaSetFilterandSetSharpnesswithout reallocating pipelines or textures.
Baked lightmaps (LightmapMesh, LightmapRenderer)
LightmapMesh and GPULightmapMesh add a dedicated secondary UV channel (UV1, matching glTF TEXCOORD_1) alongside primary material UVs (UV0, TEXCOORD_0) in a 40-byte interleaved vertex layout (LightmapVertexLayout, LightmapVertex). When UV1 is empty, UploadLightmapMesh falls back cleanly to UV0 and reports GPULightmapMesh.UsedUV0Fallback(). In game/gltf, Document.MaterialLightmap extracts baked lightmap bindings from MOZ_lightmap, EXT_lightmap, or extras.lightmap (including KHR_texture_transform / atlas uvScale and uvOffset), and Document.LightmapTexCoords / PrimitiveLightmapUVs resolve TEXCOORD_1 with TEXCOORD_0 fallback.
lmRef, err := doc.MaterialLightmap(prim.Material)
lmUVs, _, err := doc.LightmapTexCoords(prim, lmRef)
lmMesh, err := render3d.UploadLightmapMesh(cx, dev, render3d.LightmapMeshFromAttributes(
prim.Positions, prim.Normals, prim.TexCoords0, lmUVs, prim.Indices,
), render3d.MeshUpload{Label: "room", Retained: true})
renderer, err := render3d.NewLightmapRenderer(cx, dev, camBuf.Layout(), render3d.LightmapRendererOptions{
DepthFormat: gpu.FormatDepth24Plus,
Retained: true,
})
// Each frame inside OnFrame:
renderer.BeginFrame()
camBuf.WriteCamera(cam)
mat := render3d.DefaultLightmapMaterial()
mat.BaseColorTexture = albedoTex
mat.Lightmap = lightmapTex // RGBM / RGBD: upload with TextureUpload{SRGB: false}
mat.Encoding, err = render3d.ParseLightmapEncoding(lmRef.Encoding) // "" is LightmapLinear
mat.Intensity = lmRef.Intensity
mat.RGBMMaxRange = lmRef.RGBMMaxRange // 0 uses DefaultRGBMMaxRange
mat.SetUVTransform(render3d.UVTransform{Scale: lmRef.UVScale, Offset: lmRef.UVOffset})
mat.DirectLight = &rig.Directional[0] // optional: a LightRig light, Direction is the travel direction
_ = renderer.Draw(pass, camBuf, lmMesh, worldMat, mat)
- HDR and LDR encodings: supports linear 8-bit or float16 HDR textures (
LightmapLinear, withUploadLightmapRGBA16Ffor float16 upload), sRGB LDR textures (LightmapSRGB, avoiding double-decode when uploaded withTextureUpload{SRGB: true}), and 8-bit packed HDR formats (LightmapRGBMviaEncodeRGBM/EncodeRGBMImageandLightmapRGBDviaEncodeRGBD/EncodeRGBDImage) for full WebGL 2 and mobile compatibility. - Shared lightmap atlases: set
LightmapMaterial.UVScaleandUVOffset(orSetUVTransform) so multiple meshes sample distinct sub-rectangles of one baked lightmap atlas within a single render pass using dynamic uniform offsets. - Fallback and dynamic light blend: leave
LightmapMaterial.Lightmapnil to shade withAmbientFallback, and optionally setLightmapMaterial.DirectLightto a*DirectionalLight(the same type as aLightRig's, withDirectionthe way the light travels) to add its Lambertian N·L term to the baked irradiance. Normals go through the normal matrix, so non-uniformly scaled level geometry is lit correctly.Intensity0 means no baked light;DefaultLightmapMaterialsets 1. - Output encoding: the shader encodes its linear result to sRGB, as
LitPBRWGSLand the material pipeline do, so lightmapped geometry and PBR-lit props drawn into the same pass match. For an sRGB color format (LightmapRendererOptions.ColorFormator the surface format) the encode is left to the hardware. - Atlas transform:
LightmapRef.UVScale/UVOffsetare the authoritative atlas transform;LightmapRef.Texturecarries no scale or offset, so do not also apply it withTransformTexCoords. See Baked lightmaps for where the values come from. - Texture streaming:
BeginFramedrops cached bind groups of lightmap and albedo textures that have been closed, so streaming levels or hot-reloading lightmaps does not grow the cache. - Scope:
LightmapRendereris a standalone Lambertian path, albedo times (baked irradiance plus one optionalDirectionalLight). It has no metallic-roughness, normal, occlusion, or emissive maps and does not read aLightRig.
PBR lightmap path (GPUPBRLightmap, NewPBRLightmapPipeline, UploadGLTFPBRLightmaps)
To keep full glTF 2.0 metallic-roughness PBR materials (base color, metallic-roughness, normal, occlusion, emissive, alpha modes, double-sided faces) and dynamic LightRig lighting (directional, point, and spot lights) on baked geometry, use NewPBRLightmapPipeline together with GPUMaterial and GPUPBRLightmap:
matSet, err := render3d.UploadGLTFMaterials(cx, dev, defaults, doc, render3d.GLTFMaterialUpload{Retained: true})
lmSet, err := render3d.UploadGLTFPBRLightmaps(cx, dev, doc, render3d.GLTFPBRLightmapUpload{Retained: true})
lmMeshData, err := render3d.LightmapMeshFromGLTF(doc, prim)
lmMesh, err := render3d.UploadLightmapMesh(cx, dev, lmMeshData, render3d.MeshUpload{Retained: true})
pipe, err := render3d.NewPBRLightmapPipeline(
cx, dev,
camBuf.Layout(), lightBuf.Layout(), defaults.Layout(), lmSet.Layout(),
render3d.PBRLightmapPipelineOptions{DepthFormat: gpu.FormatDepth24Plus, Retained: true},
)
// Inside OnFrame:
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
lightBuf.Bind(pass)
pass.SetBindGroup(render3d.PBRLightmapMaterialGroup, matSet.Material(prim.Material).BindGroup())
lmSet.Lightmap(prim.Material).Bind(pass) // or a per-node instance from lmSet.NewInstance(...)
lmMesh.Draw(pass)
- Bind groups & vertex layout: reuses
LightmapVertexLayout(40 bytes:Position,Normal,UV0,UV1) withCameraGroup(0),LightGroup(1,GPULightBuffer),PBRLightmapMaterialGroup(2,GPUMaterialfromNewGPUMaterialorUploadGLTFMaterials), andPBRLightmapGroup(3,GPUPBRLightmapcarrying model/normal matrices, atlasUVTransform,LightmapEncoding,Intensity,RGBMMaxRange, and the lightmap texture/sampler). - Shading equation: the decoded lightmap replaces the ambient / indirect diffuse term as
(1 - metallic) * baseColor * decodedLightmap * intensity * occlusion(soLightRig.Ambientis not double-counted on baked geometry), whileLightRig's directional, point, and spot lights supply the Cook-Torrance GGX direct term plus emissive, encoded withlinear_to_srgb(or unclamped linear whenLinearOutputis set orColorFormatis sRGB). For a dielectric (metallic = 0), rough (roughness = 1), untextured material with no dynamic lights it matchesLightmapRenderer; adding aLightRiglight adds the exact direct term ofLitPBRWGSL.EvaluatePBRLightmapis the CPU reference. - glTF integration:
UploadGLTFPBRLightmapsreadsMOZ_lightmap,EXT_lightmap, andextras.lightmap(includingKHR_texture_transform,scaleOffset,uvScale,uvOffset,intensity,encoding,rgbmMaxRange), deduplicates lightmap textures/samplers, and returns aGLTFPBRLightmapSetmatchingGLTFMaterialSet(useNewInstancefor multiple nodes sharing one material at different transforms).
When to use LightmapRenderer vs the PBR lightmap path
- Use
LightmapRendererfor lightweight static architecture, low-end/mobile fill-rate budgets, or diffuse-only baked geometry that only needs an albedo texture (UV0), a baked lightmap (UV1), and at most one dynamic Lambertian directional sun light — it binds only 2 textures per draw and handles per-frame dynamic uniform ring offsets automatically viaBeginFrame/Draw. - Use the PBR lightmap path (
NewPBRLightmapPipeline+GPUPBRLightmap) whenever baked geometry also needs metallic-roughness maps, tangent-space normal maps, ambient occlusion maps, emissive maps, alpha masking/blending, double-sided surfaces, HDRLinearOutput, or dynamic directional/point/spot specular and diffuse lighting from aLightRigin a single pipeline.
Point and spot light shadows (SpotShadowMap, PointShadowMap)
SpotShadowMap and PointShadowMap shadow the first spot light (rig.Spot[0]) and the first point light (rig.Point[0]) of a LightRig. They render depth-only FormatDepth32Float maps from the rig's own SpotLight and PointLight, and the lit PBR pipelines multiply that light's contribution by the shadow, so a shadowed light is shaded exactly like the same light in LitPBRWGSL (Cook-Torrance GGX, energy-conserving Lambert, PointLightAttenuation, glTF cone falloff). Everything runs on every NSGo backend, the WebGL 2 / OpenGL ES 3.0 profile (nsgotest.NoGPUCompute) and the software renderer included:
spot, err := render3d.NewSpotShadowMap(cx, dev, render3d.DefaultSpotShadowConfig())
litPipe, err := render3d.NewLitPBRSpotShadowPipeline(cx, dev, camBuf.Layout(), lightBuf.Layout(), drawBufs[0].Layout(), spot.Layout(), gpu.FormatDepth24Plus)
// Each frame in OnFrame:
_ = spot.Update(dev, rig.Spot[0]) // the light the shadow attenuates
if sp := spot.BeginShadowPass(dev, frame); sp != nil {
spot.DrawCaster(sp, pillarMesh, pillarModel) // false when the pass is full
sp.End()
}
mp := frame.BeginRenderPass(gpu.RenderPassDesc{ /* color + depth */ })
mp.SetPipeline(litPipe)
mp.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
mp.SetBindGroup(render3d.LightGroup, lightBuf.BindGroup())
mp.SetBindGroup(render3d.LocalShadowGroup, spot.BindGroup())
for i, mesh := range meshes {
mp.SetBindGroup(render3d.PBRDrawGroup, drawBufs[i].BindGroup()) // per-draw model matrix and material
mesh.Draw(mp)
}
mp.End()
For a point light, update from rig.Point[0], draw with NewLitPBRPointShadowPipeline, and render the cube faces:
_ = point.Update(dev, rig.Point[0])
for face := range gpu.CubeFace(render3d.PointShadowFaceCount) {
if fp := point.BeginFacePass(dev, frame, face); fp != nil {
point.DrawCaster(fp, blockerMesh, blockerModel)
fp.End()
}
}
- Spot projection: a square
[0, 1]-depth perspective from the light'sPositionalong itsDirectionwith a field of view of2 * OuterConeAngle(SpotShadowViewProj). The far plane is the light'sRange, orSpotShadowConfig.Far(defaultDefaultLocalShadowFar, 50) for a light withRange0;Neardefaults to 0.1. A cone wider thanMaxSpotShadowConeAngle(just under 90°) cannot be projected andUpdatereturnsErrInvalidLocalShadow. - Point atlas: the six 90° cube faces are packed into one
(3 * FaceResolution) × (2 * FaceResolution)2D depth atlas, because NSGo cannot attach a single face of a depth cube map.sample_point_shadowpicks the dominant face, computes the perspective depth(far/(far-near)) * (1 - near/axisDist)directly, and clamps every tap inside its tile so 3×3 PCF never bleeds into a neighboring face.BeginFacePasswith face 0 clears the whole atlas and faces 1..5 keep what is there, so a partial refresh either includes face 0 with every other face or skips it. - Casters:
DrawCasterwrites each caster's(lightViewProj, model)into its own 256-byte dynamic-offset slot, so many casters draw in one pass.MaxCasters(default 256, at mostMaxLocalShadowCasters) applies per spot pass and per point face; each face has its own slots, so faces can be refreshed in any order.DrawCasterreturns false when the pass or face is full orUpdatehas not run yet. - Filtering and bias:
LocalShadowFilterHard(one nearest comparison tap) orLocalShadowFilterPCF3x3(nine linear taps), with a constantDepthBiasand a world-spaceNormalBias. - Bind group 3:
LocalShadowGroupisShadowGroup(3), the group the directional shadow, fog, IBL and skinning also use, so a lit PBR pipeline takes one of them (LitPBRPipelineOptions.SpotShadowLayoutorPointShadowLayout). A custom shader that needs both, or group 3 for something else, places the bindings withSpotShadowWGSLForGroup/PointShadowWGSLForGroupand scales its lights withshade_pbr_visibility(…, directional0_vis, point0_vis, spot0_vis)fromLightWGSL. - Device loss and CPU references: with
Retained: truethe depth target, sampler, buffers, bind groups and depth pipeline come back after a device loss.EvaluateSpotShadow,EvaluatePointShadowandPointShadowSelectFacemirror the WGSL, andShadePointVisibilitywith aLightVisibilityreproduces the lit pixel; the tests compare them with the GPU and with the golden rendersrender3d/testdata/golden/local_shadow_spot.pngandlocal_shadow_point.png(UPDATE_GOLDEN=1rewrites them).
Cascaded shadow maps (CascadedShadowMap, CSMConfig)
CascadedShadowMap shadows the first directional light of a LightRig (rig.Directional[0]) with up to four cascades (MaxCascades), so shadows stay sharp near the camera and still reach the horizon. It is the large-scene alternative to DirectionalShadowMap: the lit PBR pipelines shade the rig exactly like LitPBRWGSL and multiply that one light by the cascaded shadow, leaving the other lights, ambient and emission unshadowed. Everything runs on every NSGo backend, the WebGL 2 / OpenGL ES 3.0 profile (nsgotest.NoGPUCompute) and the software renderer included:
cfg := render3d.DefaultCSMConfig() // 4 cascades, 1024 texels each, 3x3 PCF, texel snapping
cfg.MaxShadowDistance = 80
cfg.Retained = true
csm, err := render3d.NewCascadedShadowMap(cx, dev, cfg)
litPipe, err := render3d.NewLitPBRCSMPipeline(cx, dev, camBuf.Layout(), lightBuf.Layout(), drawBufs[0].Layout(), csm.Layout(), gpu.FormatDepth24Plus)
// Each frame in OnFrame(frame *gpu.Frame):
shadowFrame := render3d.FitCascadesFromCamera(cam, rig.Directional[0].Direction, csm.Config()) // the light the cascades shadow
_ = csm.Update(dev, shadowFrame)
for i := range csm.CascadeCount() {
if pass := csm.BeginCascadePass(frame, i); pass != nil {
for j, mesh := range casterMeshes {
csm.DrawCaster(pass, mesh, casterModels[j]) // false once MaxCasters distinct models are taken
}
pass.End()
}
}
mp := frame.BeginRenderPass(gpu.RenderPassDesc{ /* color + depth */ })
mp.SetPipeline(litPipe)
mp.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
mp.SetBindGroup(render3d.LightGroup, lightBuf.BindGroup())
mp.SetBindGroup(render3d.CSMGroup, csm.BindGroup())
for i, mesh := range meshes {
mp.SetBindGroup(render3d.PBRDrawGroup, drawBufs[i].BindGroup())
mesh.Draw(mp)
}
mp.End()
- Splits (
ComputeCSMSplits):[near, min(far, MaxShadowDistance)]is divided with the practical split scheme, blending logarithmic and uniform splits bySplitLambda(default 0.75). - Fitting (
FitCascades,FitCascadesFromCamera): each cascade's slice of the camera frustum is cut along the projection's own near and far planes, so perspective and orthographic cameras both work.FitCascadesFromCamerauses the near and far planesCamera.ProjMatrixuses; shadows start at view depth 0.1 when an orthographic camera's near plane is at or behind it. WithTexelSnap(the default) each cascade is sized from a bounding sphere and snapped to whole texels, so shadow edges do not shimmer as the camera moves or turns.CasterPaddingpulls each cascade's near plane toward the light so casters outside the view still cast into it. Fitting andCSMFrame.EncodeUniformsallocate nothing. - One 2D depth atlas: the cascades are tiles of one
FormatDepth32Floattexture (1x1 for one cascade, 2x1 for two, 2x2 for three or four), so no depth texture arrays are needed.Updaterefuses, withErrInvalidCSM, a frame fitted with another cascade count, resolution or filter than the atlas, because its tiles, texel size and filter would not match the atlas and its sampler. The firstBeginCascadePassafterUpdate, whichever cascade it draws, clears the whole atlas; render every cascade after eachUpdate, in any order. - Casters:
DrawCasterwrites each distinct model matrix into its own 256-byte dynamic-offset slot once per frame and reuses it in the other cascades, soMaxCasters(default 256) counts distinct models between twoUpdatecalls, not draws. When the slots run out it draws nothing and returns false.DrawCasterGroupdraws with caller-owned transforms instead. - Filtering and blending:
ShadowFilterHard(one nearest comparison tap) orShadowFilterPCF3x3(nine linear taps clamped inside the cascade's tile), the sameShadowFilterthe single directional shadow map uses.CascadeBlendRangecross-fades into the next cascade over that fraction of each cascade and fades the last one out towardMaxShadowDistance;DebugCascadestints surfaces by cascade (red, green, blue, yellow). - Bind group 3:
CSMGroupisShadowGroup(3), so a lit PBR pipeline takes the cascaded shadow map instead of the directional, spot or point shadow, fog, IBL or skinning (LitPBRPipelineOptions.CSMLayout). Custom shaders prependCameraWGSLandCSMWGSL(orCSMWGSLForGroup(n)when group 3 holds something else) and passsample_csm_shadow(world_pos, world_normal)toshade_pbr_shadowedfromLightWGSL. - Device loss and CPU references: with
Retained: truethe atlas, sampler, buffers, bind groups and depth pipeline come back after a device loss.EvaluateCSMShadowmirrors the WGSL over an atlas read back withReadCSMDepthAtlas/ReadCSMDepthAtlasAsync, andShadePointShadowedwith that visibility, timesCSMFrame.DebugTint, reproduces the lit pixel; the tests compare them with the GPU on both profiles and with the golden renderrender3d/testdata/golden/csm_large_scene.png(UPDATE_GOLDEN=1rewrites it).
3D post-processing (PostStack3D, BloomPass, SSAOPass, DepthOfFieldPass)
PostStack3D composes screen-space ambient occlusion (SSAOPass), depth-aware disc bokeh Depth of Field (DepthOfFieldPass), and HDR Bloom (BloomPass) over an offscreen 3D color+depth gpu.RenderTarget (FormatRGBA16Float + FormatDepth32Float by default) and writes the final image to the swapchain or a chosen destination texture:
stack, err := render3d.NewPostStack3D(cx, dev, render3d.PostStack3DOptions{
Width: uint32(w),
Height: uint32(h),
OutputFormat: dev.SurfaceFormat(),
Retained: true,
EnableSSAO: true,
SSAO: render3d.SSAOConfigFromCamera(cam),
EnableDoF: true,
DoF: render3d.DepthOfFieldConfigFromCamera(cam, 5.0, 2.0, 5.0),
EnableBloom: true,
Bloom: render3d.DefaultBloomConfig(),
})
// In OnFrame:
pass := stack.BeginScenePass(frame, core.RGB(0x0c1018))
// ... draw 3D meshes into stack.SceneTarget() ...
pass.End()
_ = stack.ApplyScene(frame.Device(), frame, nil) // SSAO -> Depth of Field -> Bloom -> swapchain
frame.Submit()
// When the view resizes (or the camera zooms), refresh the projection SSAO and DoF depend on:
_ = stack.Resize(dev, uint32(w), uint32(h))
cam.SetViewport(float32(w), float32(h))
_ = stack.SetCamera(cam)
- Composable order and standalone passes:
PostStack3Druns enabled stages in fixed order — SSAO -> Depth of Field -> Bloom — routing intermediate results through ping-pong offscreen targets and writing the final stage directly inOutputFormat(or blitting cleanly when all stages are disabled).SetSSAO,SetDepthOfFieldandSetBloomtoggle a stage; a zero config keeps its current settings, sostack.SetSSAO(false, render3d.SSAOConfig{})only turns SSAO off. Each pass (SSAOPass,DepthOfFieldPass,BloomPass) can also be constructed and applied standalone against any caller-ownedgpu.Texturecolor and depth attachments viaRenderPassRecorder(satisfied by both*gpu.Frameand*gpu.Encoder). - Formats and destinations:
ColorFormat(scene and intermediate targets) isFormatRGBA16Float(default),FormatRGBA8Unorm,FormatRGBA8UnormSRGB,FormatBGRA8UnormorFormatBGRA8UnormSRGB: four filterable channels, because Depth of Field stores its Circle of Confusion in alpha. A nil destination writes to the swapchain inOutputFormat; a destination texture must be inColorFormatorOutputFormat, and the last stage draws with the pipeline built for that format. Every input and destination must have the pass or stack size, and the destination cannot also be an input; otherwiseApplyreturnsErrInvalidPostProcess. - Camera projection:
SSAOConfigFromCameracapturescam.ProjMatrix()andDepthOfFieldConfigFromCamerareads the near and far planes and projection kind from that matrix, so the defaultsscene3d.Projectionfills in for zero fields (and an orthographic near plane at or behind the camera) match the depth buffer. Both are captured once; callPostStack3D.SetCamera(orSetConfigon a standalone pass) whenever the projection changes. With a zeroProjMatrix, SSAO falls back to a 60° perspective at the pass aspect ratio with the config'sNearandFar. - SSAO (
SSAOConfig,SSAOPass): reconstructs view-space positions and edge-preserving normals from the 3D depth buffer, evaluates a deterministic 4–16-sample hemisphere kernel with stratified 4×4 tangent rotation, applies an optional depth-aware bilateral blur (BlurRadius,DepthThreshold), and multiplies ambient occlusion into the scene color (AOTextureexposes the AO map for inspection). - Depth of Field (
DepthOfFieldConfig,DepthOfFieldPass): linearizes perspective or orthographic[0, 1]depth to compute a signed Circle of Confusion aroundFocusDistance±FocusRange, then gathers an 18-tap two-ring disc bokeh blur that prevents blurred background pixels from bleeding across sharp in-focus silhouettes. - Bloom (
BloomConfig,BloomPass): extracts bright LDR or HDR (> 1.0) pixels with a quadraticThreshold/SoftKneecurve, runsBlurPassesseparable 9-tap Gaussian horizontal and vertical passes (optionally at half resolution viaHalfRes), and composites the glow additively withIntensity. - Portability, lifecycle, and reference renders: every pass uses fragment-only portable WGSL with explicit
textureSampleLeveland algebraic/precomputed weights (no compute shaders orexp/log2instructions), running on both full andnsgotest.NoGPUCompute(WebGL 2 / OpenGL ES 3.0) profiles, supportingResize(dev, w, h)andRetaineddevice-loss recovery. Golden reference renders (render3d/testdata/golden/post3d_bloom.png,post3d_ssao.png,post3d_dof.png, andpost3d_combined.png) verify each pass and the combined stack;UPDATE_GOLDEN=1rewrites them.
Clustered forward lighting (ClusteredLightSystem, ClusterGrid)
A LightRig shades at most 16 point and 8 spot lights. For scenes with hundreds of local lights, ClusteredLightSystem takes the place of GPULightBuffer at LightGroup: it uploads the same LightRig (ambient, directional, and the rig's own point and spot lights) at binding 0, and bins any number of extra local lights (ClusterLight) into a 3D grid of screen tiles × logarithmic depth slices (default 16 × 9 × 24, up to 512 visible lights and 64 per cluster). Each fragment then shades only the lights in its own cluster, with the same Cook-Torrance BRDF and glTF KHR_lights_punctual falloff as shade_pbr, so clustered and rig lights look identical. Because cluster assignment runs on the CPU, only fragment storage buffers (Features.StorageBuffers and Limits.MaxStorageBufferBindingSize) are required — compute shaders are not needed, so Apple Metal devices use the full clustered path. On targets without storage buffers (WebGL 2, OpenGL ES 3.0, Linux GtkGLArea, and headless tests using nsgotest.NoGPUCompute) it falls back to one uniform buffer holding the FallbackMaxLights (default 16) most important visible lights:
sys, err := render3d.NewClusteredLightSystem(cx, dev, render3d.DefaultClusterConfig())
pipe, err := render3d.NewClusteredLitPBRPipeline(cx, dev, camBuf.Layout(), sys, drawBuf.Layout(), gpu.FormatDepth24Plus)
// Convert scene lights once; a Range of 0 (unbounded) gets a derived cutoff range.
local := make([]render3d.ClusterLight, 0, len(lamps))
for _, l := range lamps {
cl, err := render3d.ClusterLightFromPoint(l)
if err != nil {
return err
}
local = append(local, cl)
}
// Each frame, before recording draws:
camBuf.WriteCamera(cam)
stats, err := sys.UpdateCamera(frame.Device(), cam, viewW, viewH, rig, local)
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
sys.Bind(pass)
pass.SetBindGroup(render3d.PBRDrawGroup, drawBuf.BindGroup())
mesh.Draw(pass)
- Lights:
ClusterLightis a point or spot record whoseRangemust be positive, because clustering needs a bounding sphere.ClusterLightFromPointandClusterLightFromSpotconvert aPointLightorSpotLightand, forRange == 0, derive the distance at whichIntensity * max(Color) / d^2falls toClusterRangeCutoff(1/256).Updatereturns an error wrappingErrInvalidClusterLightfor a light that failsValidate(includingRange == 0) orErrInvalidLightfor a malformed rig, instead of dropping it.EvaluateLightAttenuationreusesPointLightAttenuationandSpotConeAttenuation. - Viewport size:
viewWandviewHmust be the size of the area the pass renders, starting at its origin, because the shader finds a fragment's tile from@builtin(position). Under aDynamicResolutionTarget, passScaledSize(), not the native size. - Shaders:
NewClusteredLitPBRPipelinecompilesClusteredLitWGSLorClusteredFallbackWGSL(whichever matchessys.WGSL()), theLitPBRWGSLmodule withshade_pbr_clusteredin place ofshade_pbr, writing sRGB. Custom shaders prependCameraWGSLandsys.BindingsWGSL()(ClusteredBindingsWGSLorClusteredFallbackBindingsWGSL, both of which includeLightWGSL) and callshade_pbr_clustered(world_pos, normal, view_dir, base_color, metallic, roughness, frag_pos), which has the same signature on both paths. - CPU clustering (
ClusterGrid): view-space cluster AABBs are cached by(proj, near, far)and reused across camera motion.AssignLightsculls off-frustum lights, computes tight screen-tile and depth-slice bounds, tests sphere-vs-AABB (plus conservative cone-vs-AABB rejection for spot lights), and packsOffsetsandLightIndiceswith zero steady-state allocations. Lights beyondMaxLights, and lights beyondMaxLightsPerClusterin one cell, are dropped in input order (TruncatedLights,TruncatedClusters), so put important lights first when a scene can exceed either cap. - Fallback selection:
SelectFallbackLightsdrops lights whose bounding sphere is outside the view frustum, then ranks the rest by power, range and distance to the camera. In fallback modeCulledLightscounts lights outside the frustum or without power, andTruncatedLightsthe visible lights the cap left out. - Depth-slice lookup: the shader finds a fragment's depth slice with a 6-step binary search over a uniform split-depth table rather than
log2andexp, because NSGo v0.3.0's software GPU renderer evaluates those builtins to zero. - CPU reference:
ShadeClusteredPointisShadePointplus every clustered light, andShadeClusteredGridPointuses only the lights of the fragment's cluster, exactly as the GPU does. Tests compare GPU pixels against them on the full andNoGPUComputeprofiles, including a 256-light scene. - Limits:
ValidatecapsMaxLightsatMaxClusterLights(65,536), keepsMaxLightsPerClusterat or belowMaxLights, and caps index slots (TilesX * TilesY * SlicesZ * MaxLightsPerCluster) atMaxClusterIndexEntries;NewClusteredLightSystemreturnsErrInvalidClusterConfigwhen a storage buffer would exceed the device'sMaxStorageBufferBindingSize. - Desktop GPU performance: measured on a MacBook Pro (Apple M4 Pro, Metal 4, macOS arm64,
2560 × 1440drawable, 120 Hz display) with 256 animated clustered point lights (~250 visible, ~475 active clusters): sustained120.0 fpson the clustered storage-buffer path, with CPU cluster assignment and buffer upload taking ~145 µs/frame. - Lifecycle: set
ClusterConfig.Retainedto restore buffers across GPU device loss;ByteSize()reports GPU buffer bytes forRenderStatsand budgets, andClose()releases resources eagerly.
Splat-map terrain materials (TerrainMaterial, GPUTerrainMaterial)
Render multi-layer heightmap terrain (world3d) blending up to 8 PBR layers from 1 or 2 RGBA weight (splat) maps with per-layer UV tiling, optional height-based contrast blending, optional slope-driven triplanar projection on steep cliffs, and full scene lighting plus shadows:
gpuTerrain, err := render3d.NewGPUTerrainMaterial(cx, dev, render3d.TerrainMaterial{
Layers: layers, // 1..8 TerrainLayer values (albedo, normal, metallic-roughness, height, UVScale, HeightBias)
SplatMap0: splatImg0,
SplatMap1: splatImg1, // optional for <= 4 layers
HeightBlendContrast: 0.65,
Triplanar: render3d.DefaultTerrainTriplanar(),
}, render3d.TerrainMaterialUpload{Label: "alpine", Mipmaps: true, Retained: true})
pipe, err := render3d.NewTerrainMaterialPipeline(cx, dev, camBuf.Layout(), lightBuf.Layout(), gpuTerrain.Layout(), render3d.TerrainPipelineOptions{
DepthFormat: gpu.FormatDepth24Plus,
ShadowLayout: shadowMap.Layout(), // or CSMLayout / FogLayout at group 3
})
pass.SetPipeline(pipe)
pass.SetBindGroup(render3d.CameraGroup, camBuf.BindGroup())
pass.SetBindGroup(render3d.LightGroup, lightBuf.BindGroup())
gpuTerrain.Bind(pass) // TerrainMaterialGroup (group 2)
pass.SetBindGroup(render3d.ShadowGroup, shadowMap.BindGroup())
terrainMesh.Draw(pass)
- Texture-array layout:
GPUTerrainMaterialpacks layer albedo (sRGB), tangent-space normal, and height/roughness/metallic maps into threegpu.Dim2DArraytextures (TerrainAlbedoArrayBinding,TerrainNormalArrayBinding,TerrainMRHeightArrayBinding) plus the two RGBA splat maps atTerrainMaterialGroup(group 2). Total fragment texture bindings stay at 5 (6 with shadows), well within WebGL 2 / GLES 3 limits with zero pipeline permutations across 1–8 layers. - Lighting, shadows, fog, and HDR:
NewTerrainMaterialPipelinecompilesTerrainMaterialWGSL,TerrainMaterialShadowWGSL(ShadowLayout),TerrainMaterialCSMWGSL(CSMLayout), orTerrainMaterialFogWGSL(FogLayout), plusLinearOutputvariants forHDRTarget/ToneMapPass. - Live updates & CPU reference: call
UpdateUniformsto tweak transforms, blend contrast, triplanar settings, or per-layer factors/tiling without reallocating textures,UpdateSplatMapsto upload repainted splat maps in place, andEvaluateTerrainMaterialas the pure-Go CPU reference matching the WGSL shader. - Loading directly from
*.terrain.yaml:TerrainMaterialFromDoc(fsys, doc)decodes either packed RGBAsplatMapsor per-layer grayscaleweightMapPNGs, resolves optionalresource.Materialfiles alongside explicit PBR overrides, and validates aTerrainMaterialready forNewGPUTerrainMaterial;MeshFromTerrainMeshconvertsworld3d.TerrainMeshchunks directly intorender3d.Meshgeometry.