Skip to main content
Version: Phaser v4.2.1

Mesh2D

A Mesh2D Game Object.

A Mesh2D Game Object is used for the display of 2D meshes. It is a WebGL only Game Object. It contains a number of textured triangles. Each triangle is defined by a set of three vertices, with a position and texture coordinate; and a reference to a texture.

Because the triangles define their own texture coordinates, Mesh2D does not directly use frame data from the texture. You should set up your own texture coordinates, either by hand or using a texture atlas.

The Mesh2D game object can batch together with quads from game objects like Image, Sprite, and Text. It supports several rendering strategies, and it's important to use the correct one.

By default, it does not combine triangles into quads. Each triangle is rendered as a single quad. This is inefficient, so consider switching to one of the other strategies.

Use the buildOrderedIndices method to precompute an optimized index list, which arranges triangles into quad-forming pairs, synthesizing degenerate triangles where a triangle has no edge-sharing partner. You choose the optimization strategy (0 fast, 1 medium, 2 high), paying the cost once when the topology is stable. Use useOrderedIndices (and setUseOrderedIndices) to toggle between the ordered and unordered lists without rebuilding. This strategy is best for static topology, where the triangles do not change. (The vertices can change, but the triangles do not.)

Use the renderAsTriangles method to render the mesh as individual triangles. This is suitable for dynamic topology that cannot be optimized into quads ahead of time. However, it uses a separate render node designed for textured triangles, so it doesn't batch with quads. This strategy is best for dynamic topology, where the arrangement of triangles itself changes frequently.

Prefer the ordered index list strategy where possible. It needs to do less work at render time.

If you can guarantee that the index list is already ordered, you can set useOrderedIndices to true and generate indicesOrdered yourself.

Mesh2D supports lighting. You should be careful not to distort the mesh too far, or normal maps will look weird. In particular, rotating texture coordinates will rotate the apparent light direction.

This is intended to be used as a base for dealing with 2D meshes.

Constructor

new Mesh2D(scene, x, y, texture, vertices, indices, [flipV])

Parameters

nametypeoptionaldefaultdescription
scenePhaser.SceneNoThe Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
xnumberNoThe horizontal position of this Game Object in the world.
ynumberNoThe vertical position of this Game Object in the world.
texturestring | Phaser.Textures.TextureNoThe key, or instance of the Texture this Game Object will use to render with, as stored in the Texture Manager.
verticesArray.<number>NoThe vertices of the mesh. Each vertex is a sequence within the array: x, y, u, v. The array has a step of 4.
indicesArray.<number>NoThe indices of the mesh. Each index is a sequence: a, b, c, page. The abc values index to vertices in the vertices array. The page value is the index of the texture source in the texture atlas to use for this triangle. Typically 0. The array has a step of 4.
flipVbooleanYesfalseWhether to flip the texture coordinates vertically. This affects texture coordinates, not the vertices. Set this property if your geometry provides texture coordinates that are opposite to GL texture expectations (which are bottom-up).

Scope: static

Extends

Phaser.GameObjects.GameObject
Phaser.GameObjects.Components.AlphaSingle
Phaser.GameObjects.Components.BlendMode
Phaser.GameObjects.Components.ComputedSize
Phaser.GameObjects.Components.Depth
Phaser.GameObjects.Components.Flip
Phaser.GameObjects.Components.GetBounds
Phaser.GameObjects.Components.Lighting
Phaser.GameObjects.Components.Origin
Phaser.GameObjects.Components.RenderNodes
Phaser.GameObjects.Components.ScrollFactor
Phaser.GameObjects.Components.TextureCrop
Phaser.GameObjects.Components.Transform
Phaser.GameObjects.Components.Visible

Source: src/gameobjects/mesh2d/Mesh2D.js#L14
Since: 4.2.0

Inherited Members​

From Phaser.GameObjects.Components.AlphaSingle:

From Phaser.GameObjects.Components.BlendMode:

From Phaser.GameObjects.Components.ComputedSize:

From Phaser.GameObjects.Components.Depth:

From Phaser.GameObjects.Components.Filters:

From Phaser.GameObjects.Components.Flip:

From Phaser.GameObjects.Components.Lighting:

From Phaser.GameObjects.Components.Origin:

From Phaser.GameObjects.Components.RenderNodes:

From Phaser.GameObjects.Components.ScrollFactor:

From Phaser.GameObjects.Components.TextureCrop:

From Phaser.GameObjects.Components.Transform:

From Phaser.GameObjects.Components.Visible:

From Phaser.GameObjects.GameObject:


Public Members​

flipV​

flipV: boolean​

Description:

Whether to flip the texture coordinates vertically.

This affects texture coordinates, not the vertices. Set this property if your geometry provides texture coordinates that are opposite to GL texture expectations (which are bottom-up).

Source: src/gameobjects/mesh2d/Mesh2D.js#L210
Since: 4.2.0


indices​

indices: Array.<number>​

Description:

The indices of the mesh. Each index is a sequence: a, b, c, page. These index to vertices in the vertices array. The array has a step of 4.

  • a (offset 0): The index of the first vertex.

  • b (offset 1): The index of the second vertex.

  • c (offset 2): The index of the third vertex.

  • page (offset 3): The page of the triangle: which texture source

    in the texture atlas is used for this triangle. Typically 0.

Source: src/gameobjects/mesh2d/Mesh2D.js#L138
Since: 4.2.0


indicesOrdered​

indicesOrdered: Array.<number>​

Description:

An optimized copy of the indices list, built by buildOrderedIndices. It uses the same internal pattern as indices (a sequence of a, b, c, page with a step of 4), but it may be longer, because it can contain synthesized degenerate triangles which pad single triangles out to complete quads.

Triangles in this list are arranged in pairs. Each pair is intended to be consumed as a single quad: the first triangle is p, q, r and the second is q, r, s, where q, r is the shared edge, and p, s are the corners unique to each triangle. When a triangle has no partner, s repeats r to form a degenerate second triangle.

This is null until buildOrderedIndices is called. Use useOrderedIndices to control whether it is used.

Source: src/gameobjects/mesh2d/Mesh2D.js#L156
Since: 4.2.0


renderAsTriangles​

renderAsTriangles: boolean​

Description:

Whether to render this mesh as individual triangles, rather than combining triangles into quads.

When true, the renderer routes the mesh to a batch handler optimized for individual triangles (gl.TRIANGLES). This is suitable for dynamic topology which cannot be optimized into quads ahead of time. When false, the mesh is rendered as quads, which batches with regular sprites.

Source: src/gameobjects/mesh2d/Mesh2D.js#L193
Since: 4.2.0


useOrderedIndices​

useOrderedIndices: boolean​

Description:

Whether to use indicesOrdered instead of indices when rendering.

This has no effect unless indicesOrdered has been populated by buildOrderedIndices. It is safe to toggle at any time, allowing you to switch between the ordered and unordered lists without rebuilding.

Source: src/gameobjects/mesh2d/Mesh2D.js#L179
Since: 4.2.0


vertices​

vertices: Array.<number>​

Description:

The vertices of the mesh. Each vertex is a sequence within the array: x, y, u, v. The array has a step of 4.

  • x (offset 0): The x position of the vertex.

  • y (offset 1): The y position of the vertex.

  • u (offset 2): The u texture coordinate of the vertex.

  • v (offset 3): The v texture coordinate of the vertex.

Source: src/gameobjects/mesh2d/Mesh2D.js#L121
Since: 4.2.0


Inherited Methods​

From Phaser.Events.EventEmitter:

From Phaser.GameObjects.Components.AlphaSingle:

From Phaser.GameObjects.Components.BlendMode:

From Phaser.GameObjects.Components.ComputedSize:

From Phaser.GameObjects.Components.Depth:

From Phaser.GameObjects.Components.Filters:

From Phaser.GameObjects.Components.Flip:

From Phaser.GameObjects.Components.GetBounds:

From Phaser.GameObjects.Components.Lighting:

From Phaser.GameObjects.Components.Origin:

From Phaser.GameObjects.Components.RenderNodes:

From Phaser.GameObjects.Components.RenderSteps:

From Phaser.GameObjects.Components.ScrollFactor:

From Phaser.GameObjects.Components.TextureCrop:

From Phaser.GameObjects.Components.Transform:

From Phaser.GameObjects.Components.Visible:

From Phaser.GameObjects.GameObject:


Public Methods​

buildOrderedIndices​

<instance> buildOrderedIndices([strategy], [useOrderedIndices])​

Description:

Builds indicesOrdered, an optimized copy of indices in which triangles are arranged into quad-forming pairs. Each pair consists of two triangles p, q, r and q, r, s: where two triangles share an edge, q, r is that shared edge and p, s are their unique corners; where a triangle has no partner, the second triangle of the pair is degenerate (s repeats r), padding the single triangle out to a full quad.

Because this processes the entire indices list, you should only call it when the topology is stable. The cost depends on the chosen strategy.

Parameters:

nametypeoptionaldefaultdescription
strategynumberYes0The level of optimization to use. - 0: Fast. Each triangle forms its own quad with a synthesized degenerate triangle. No edge sharing is detected. This is quick to build but the least memory efficient at render time. - 1: Medium. Each triangle checks only the next triangle for a shared edge, forming a quad if one is found, otherwise padding with a degenerate triangle. - 2: High. Every triangle is checked against every other triangle for a shared edge, using an edge lookup to keep this tractable. This is the slowest to build but the most memory efficient at render time.
useOrderedIndicesbooleanYesIf defined, also sets the useOrderedIndices property.

Returns: Phaser.GameObjects.Mesh2D - This Game Object instance.

Source: src/gameobjects/mesh2d/Mesh2D.js#L322
Since: 4.2.0


setRenderAsTriangles​

<instance> setRenderAsTriangles([value])​

Description:

Sets whether to render this mesh as individual triangles.

Parameters:

nametypeoptionaldefaultdescription
valuebooleanYesfalseWhether to render the mesh as individual triangles.

Returns: Phaser.GameObjects.Mesh2D - This Game Object instance.

Source: src/gameobjects/mesh2d/Mesh2D.js#L308
Since: 4.2.0


setUseOrderedIndices​

<instance> setUseOrderedIndices([value])​

Description:

Sets whether to use indicesOrdered instead of indices when rendering.

This has no side effects other than setting the property. It does not rebuild indicesOrdered, so you may toggle freely between the ordered and unordered lists. Call buildOrderedIndices to populate the ordered list.

Parameters:

nametypeoptionaldefaultdescription
valuebooleanYesfalseWhether to use the ordered index list.

Returns: Phaser.GameObjects.Mesh2D - This Game Object instance.

Source: src/gameobjects/mesh2d/Mesh2D.js#L289
Since: 4.2.0