360 Secure SkillHub
360 安全技能中心

v4-new-features

按 4.0 到 4.3 版本梳理 Phaser 4 新增渲染、图形、光照、纹理与物理等特性,给出 API 名称、关键行为、迁移注意点和可继续查阅的相关技能入口。

分类:知识
phaserjs
热度 0.00
最近更新 2026-12-04 23:28:21

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add phaserjs/phaser/v4-new-features
兼容性
通用兼容
联网要求
不联网
数据出网
数据不出本机
Metadata
name
v4-new-features
description
A map of what Phaser 4.0 to 4.3 added, a few lines each, with the skill which covers it: Filters, render nodes, lighting and cone lights, tint modes and setTint2, CaptureFrame, Gradient, the Noise objects, Sky and its stars, World3D, SpriteGPULayer, TilemapGPULayer, Stamp, Mesh2D, Stencil, CustomContext, setFPSLimit, SDF Graphics and Shapes, line joins, clean alpha, the Loader AbortSignal and the Phaser.AUTO fallback to Canvas. Use when asked what's new in Phaser 4 or a 4.x release, or when choosing a v4-only object for huge numbers of sprites, large tilemaps, procedural noise, gradients, banded skies, starfields or meshes. Triggers on: what's new, 4.1, 4.2, 4.3, Salusa, Giedi, Arrakis, changelog, SpriteGPULayer, TilemapGPULayer, Mesh2D, Stencil, noise, gradient, sky, starfield, raster bands.
metadata
{"phaser-version":"4.3.0"}

What's new in Phaser 4.0 to 4.3

A map of the features added since Phaser 3, by release. Each entry names the API and links to the skill with the detail. For code which must change when porting from v3, use v3-to-v4-migration.

Source: changelog/v4/4.0/CHANGELOG-v4.0.0.md, changelog/v4/4.1/, changelog/v4/4.2/, changelog/v4/4.2.1/, changelog/v4/4.3/CHANGELOG-v4.3.0.md, docs/Phaser 4 Rendering Concepts/. Related: phaser4-core, v3-to-v4-migration.

Most new rendering features are WebGL only. obj.filters is null until enableFilters(), which does nothing in Canvas, and Phaser.AUTO can fall back to Canvas (4.3). Guard them as phaser4-core shows.

Quick start: the v4-only objects with no other skill

create ()
{
    const { width, height } = this.scale;

    //  Gradient: a GPU gradient, as a Game Object. config, x, y, width, height.
    this.add.gradient({
        bands: [ { colorStart: 0x0b1d51, colorEnd: 0xff7e5f } ],
        shapeMode: 2,              //  0 LINEAR, 1 BILINEAR, 2 RADIAL, 3 CONIC_SYMMETRIC, 4 CONIC_ASYMMETRIC
        start: { x: 0.5, y: 0.5 }
    }, width / 2, height / 2, width, height);

    //  Sky (4.3): a vertical gradient in chunky bands, which fills the game and ignores the Camera.
    //  Colors run top to bottom: a preset name, or an array of colors and [ color, percent ] stops.
    const sky = this.add.sky([ 0x1b1446, [ 0xd9455f, 60 ], 0xffd166 ]).setBandHeight(24);

    //  Stars are drawn by the Sky. addStars returns the stars, not the Sky.
    const stars = sky.addStars({ count: 300, speedY: 40 });

    //  Noise: the factory names are all lower case.
    const clouds = this.add.noisesimplex2d({ noiseIterations: 4, noiseSeed: [ 1, 2 ] }, 200, 200, 256, 256);

    this.tweens.add({ targets: clouds, noiseFlow: Math.PI * 2, duration: 8000, repeat: -1 });

    //  SpriteGPULayer: up to millions of quads from one texture in one draw call.
    const layer = this.add.spriteGPULayer('stars', 10000);   //  texture, maximum members

    for (let i = 0; i < 10000; i++)
    {
        layer.addMember({ x: Math.random() * width, y: Math.random() * height, frame: 'star' });
    }
}
  • The noise objects are noise, noisecell2d, noisecell3d, noisecell4d, noisesimplex2d and noisesimplex3d. noiseSeed is an array of integers. Animate noiseOffset (an [x, y] array) to scroll, and noiseFlow (simplex) or the extra axes (noisecell3d, noisecell4d) to evolve the pattern. noiseNormalMap: true outputs a normal map for lighting.
  • The Gradient colors are a Phaser.Display.ColorRamp of ColorBands. Call gradient.ramp.encode() after changing them.
  • Sky: the config, x, y, width, height. The default size is the game, with an origin of 0 and a scroll factor of 0. Presets: day, dawn, sunset, dusk, night, rainbow (Phaser.GameObjects.Sky.Presets). setColors can be called every frame. bandHeight is in pixels, and 0 is a smooth gradient.
  • Sky colors and the Camera: setGradientHeight(worldHeight, 1) makes the colors cover the world, so the Sky shows the part the Camera's scrollY is at. Only vertical scroll changes the colors. scrollY, scrollSpeed and repeat scroll them by hand. scrollBands moves the bands, not just the colors.
  • Sky stars (SkyStars): count, layers, color / farColor, size / farSize, speedX / speedY in pixels per second (positive is right and down), cameraFactor (default 0.5), key / frame for a texture. is3D: true flies them out of originX, originY at speedZ. See src/gameobjects/sky/.
  • Gradient, Sky and the Noise objects extend Shader. See custom-rendering.
  • SpriteGPULayer: fill it once. Adding, editing or removing members rewrites the GPU buffer, which is expensive. Members can animate on the GPU (x, y, rotation, scaleX, scaleY, alpha and tintBlend take an animation config). It has setLighting. See src/gameobjects/spritegpulayer/.

4.0: the new renderer

Feature API Skill
Render nodes replace pipelines setRenderNodeRole, render.renderNodes config, addRenderStep custom-rendering
Filters replace preFX, postFX and FX enableFilters(), filters.internal, filters.external filters-and-postfx
The Mask filter replaces BitmapMask filters.internal.addMask(), Phaser.Actions.AddMaskShape masks-and-stencils
Effect Actions Phaser.Actions.AddEffectBloom, AddEffectShine, FitToRegion filters-and-postfx, actions-and-utilities
Lighting component setLighting(true), setSelfShadow(), lights have z lighting
Tint modes setTintMode(Phaser.TintModes.FILL) and others game-object-components
Vertex rounding vertexRoundMode, roundPixels defaults to false, smoothPixelArt phaser4-core, scale-and-responsive
CaptureFrame this.add.captureFrame(key): copies what's drawn so far to a texture filters-and-postfx
Gradient, Noise this.add.gradient, this.add.noise and the other five Quick start above
SpriteGPULayer this.add.spriteGPULayer(texture, size) Quick start above
TilemapGPULayer map.createLayer(id, tileset, x, y, true): one quad, one tileset, orthogonal maps tilemaps
Stamp this.add.stamp(x, y, texture, frame): a quad which ignores the Camera, for Dynamic Textures render-textures
Dynamic Texture changes render() runs the queued draws; preserve, callback, capture; Render Texture renderMode render-textures
TileSprite Uses atlas frames; tileRotation sprites-and-images
NineSlice tileX, tileY arguments tile the edges sprites-and-images
Compact texture atlas this.load.atlasPCT(key, url): a small text atlas format loading-assets
Hash noise in JS Phaser.Math.Hash, HashCell, HashSimplex geometry-and-math
Color ramps Phaser.Display.ColorBand, ColorRamp; Color helpers can write to an existing Color geometry-and-math
Destroyed flag gameObject.isDestroyed game-object-components

4.1 (Salusa)

  • import Phaser from 'phaser' works: the ESM build has a default export.
  • Layer is a true Game Object, so its filters work. See groups-and-containers.
  • render.mipmapRegeneration lets Dynamic Textures rebuild their mipmaps. Filters never use mipmaps.
  • Filter Controllers have getPaddingCeil(). Custom render nodes should call it rather than getPadding().

4.2 (Giedi)

Feature API Skill
Stencil this.add.stencil(x, y, children, options): a Container which writes to the stencil buffer, a fast lasting mask masks-and-stencils
StencilReference this.add.stencilreference(stencil, options): draws a Stencil again with other settings masks-and-stencils
CustomContext this.add.customcontext(x, y, children, callback): a Container which changes the DrawingContext, such as the scissor or stencil test masks-and-stencils
Alpha strategies render.alphaStrategy ('keep'), render.stencil (true), render.stencilAlphaStrategy ('dither'); strategies keep, dither, threshold masks-and-stencils
Mesh2D this.add.mesh2d(x, y, texture, vertices, indices): textured triangles which batch with sprites. Vertices are x, y, u, v; indices are a, b, c, page. Supports lighting src/gameobjects/mesh2d/Mesh2D.js
Cone lights this.lights.addConeLight(...), light.setCone(), setConeRotation, setConeAngles, disableCone lighting
Second tint TintModes.MULTIPLY_TWO, setTint2(), tint2TopLeft and the other corners game-object-components
FPS limit at runtime this.game.loop.setFPSLimit(30); 0 removes the limit game-setup-and-config
  • The filters render step now runs just before the object's own render step, so other render steps can run first.
  • inTintEffect in the tint shader changed from a float to a vec4. Only custom shaders which read it are affected.
  • The new BatchHandlerTri render node draws triangles; TransformerVertex has setupMatrix and transformVertex.
  • 4.2.1 fixed stencilInvert, and stencil clearing in framebuffers.

4.3 (Arrakis)

Graphics and Shapes, all in graphics-and-shapes:

  • SDF shapes. WebGL draws circles, ellipses, arcs, rounded rectangles, strokes and lines with signed distance fields. They're round and smooth at any zoom, and look different from 4.2. render.sdfShapes: false goes back to triangles.
  • Joins. Stroked paths now have pointed joins, as in Canvas. lineJoin ('miter', 'bevel', 'round') and miterLimit (default 10) on Graphics, setLineJoin and setMiterLimit on Shapes.
  • Gradients. fillLinearGradient, fillRadialGradient, lineLinearGradient and lineRadialGradient on Graphics, and setFillLinearGradient, setFillRadialGradient, setStrokeLinearGradient and setStrokeRadialGradient on Shapes. Any number of stops. Both renderers draw them.
  • Hard edges. setHardEdges(true) turns off the smoothed edge, for a crisp mask.
  • Clean alpha. setCleanAlpha() on Graphics and Shapes fades the whole object evenly, so overlapping parts don't show through each other. WebGL only, and it costs a framebuffer while alpha is below 1.

Other features:

Feature API Skill
Two-color tint TintModes.MULTIPLY_ADD, with setTint2() game-object-components
Sky and stars this.add.sky(colors), sky.addStars(config): banded backdrops and starfields Quick start above
World3D this.add.world3D(x, y, width, height): a small, fast 3D world in one Game Object, with models built from shapes, 3D lights, a 3D camera, terrain, particles and decals. WebGL only world3d
WebGL to Canvas fallback type: Phaser.AUTO falls back to Canvas if the WebGL Renderer can't be created. Phaser.WEBGL still throws game-setup-and-config
Abort loading this.load.xhr.signal = controller.signal, or signal in a file's XHR settings loading-assets
Loader prefix in config loader: { prefix: 'LEVEL1.' } loading-assets
TilemapGPULayer rotation Tiles rotated in multiples of 90 degrees tilemaps
Isometric shape queries getTilesWithinShape on isometric layers tilemaps
CSS-transformed canvas Pointer positions are right under a CSS rotate; this.scale.transformXY(pageX, pageY) input-keyboard-mouse-touch
Arcade wrap this.physics.world.wrap(object, padding, paddingY) physics-arcade
Matter fixed rotation setFixedRotation(false) lets a body turn again physics-matter
Spatial sound A Sound which follows an object also tracks its z audio-and-sound
TypeScript Event constants have literal types; tile lookups and Structs.Map.get are nullable events-system

4.3 changes which alter how existing code behaves:

  • Camera follow lerp is frame-rate independent: the value is the amount moved per frame at 60 fps. See cameras.
  • The Loader waits before each retry (100 ms, doubling, up to 5 s), and a file loads from the baseURL set when it was added. See loading-assets.
  • A Timeline completes once its Tweens have finished. TweenChain.remove is now TweenChain.removeTween. See time-and-timers and tweens.
  • dragend gives the same dragX and dragY as drag. See input-keyboard-mouse-touch.
  • Matter restingThresh defaults changed, and gravityScale works again. See physics-matter.
  • In Canvas, a Dynamic Texture's draw(entries, x, y) adds x and y to each object's position, as WebGL does.
  • Self-shadowing reads penumbra and diffuseFlatThreshold the right way round, and setSelfShadow affects only its own object. See lighting.
  • Display.Color.GetColor32 returns an unsigned integer.
  • Layer extends GameObject, so layer instanceof Phaser.Structs.List is false.
  • Arcade Physics separates some collisions in a different order, and a Group colliding with itself uses the dynamic tree.