外观
纹理
Phaser 4 指南 · 英文原文
纹理管理器负责创建和存储 Phaser 的纹理(Texture)。纹理包含引擎使用它所需的数据。精灵等游戏对象持有对纹理及其所用帧的引用,因此多个对象可以共享同一张纹理。
一张纹理由一帧或多帧组成。每帧都对应纹理中的一块矩形区域。为精灵指定纹理时,可以进一步选择其中某一帧,例如第 5 帧。
在 Phaser 中,一张精灵表(Sprite Sheet)或一个纹理图集(Texture Atlas)对应一个纹理实例,其中包含多个帧。纹理管理器的解析器根据图像与数据文件创建这些帧。
动画通过按指定的顺序和帧率显示各帧来实现。后文还会介绍动态纹理和画布纹理的用法。
纹理
纹理 拥有键, 第一个帧名称, 帧映射, 一个或多个源图像( 扫描、 图像、 视频) , 以及 0 或多个数据源图像( 法线贴图 ) 。 有三个纹理类, CanvasTexture、 DynamicTexture和 Texture 。
最大纹理尺寸取决于设备;您可以检查renderer.getMaxTextureSize(). 移动的2048px和桌面的4096px应该安全.
默认纹理
- 默认 :
'__DEFAULT' - 失踪:
'__MISSING' - 4x4 白色:
'__WHITE'
获取纹理管理器中的纹理键
js
const keys = this.textures.getTextureKeys(); // → ['mummy', 'bat', 'torch', …]从纹理管理器获取纹理
textures.get() ** 总是** 返回一个纹理; 如果没有这样的键, 它将是 __MISSING 纹理。 所以您应该先使用 textures.exists() 。
js
const texture = this.textures.exists("mummy")
? this.textures.get("mummy")
: null;从数组生成纹理
js
var config = {
data: data,
// 3x3:
// [ '...',
// '...',
// '...' ]
pixelWidth: 1, // pixel width of each data
pixelHeight: 1, // pixel height of each data
preRender: null, // callback, function(canvas, ctx) {}
postRender: null, // callback, function(canvas, ctx) {}
canvas: null, // create a canvas if null
resizeCanvas: true,
clearCanvas: true,
};
var texture = this.textures.generate(key, config);校验现有纹理
js
var hasKey = this.textures.exists(key);获取 Base64 数据
js
var s = this.textures.getBase64(key); // type= 'image/png', encoderOptions= 0.92
// var s = this.textures.getBase64(key, frame, type, encoderOptions);获取像素颜色
js
var color = this.textures.getPixel(x, y, key);
// var color = this.textures.getPixel(x, y, key, frame);color 属性
r: 0 ~ 255g: 0 ~ 255b: 0 ~ 255a: 0 ~ 255
js
var alpha = this.textures.getPixelAlpha(x, y, key);
// var alpha = this.textures.getPixelAlpha(x, y, key, frame);alpha : 0 ~ 255
如果坐标超出范围,则返回null。
删除纹理
删除存储在纹理缓存中的纹理 。
js
this.textures.remove(key);加载纹理所用的图像
通常您不会直接创建纹理。 Phaser 会在您装入图像时为您创建纹理 。
load.image()用单帧__BASE创建纹理.load.spritesheet()创建了一种纹理,框架命名为整数,从0开始,加上__BASE.load.atlas()或load.unityAtlas()创建了带有图集数据中命名的帧的纹理,加上__BASE.load.multiatlas()创建相同, 并有多个源图像
在Phaser术语中,"Spritesheet"在行或列中具有统一的单元格,而"地图册"则在任意大小和位置上都有帧. Phaser可以加载由Texture Packer(任何"Phaser 3"格式)或Unity创建的地图册.
Phaser可以使用浏览器可以显示的any image format. SVG在造型时被光栅化(通过浏览器). Phaser v3.60支持WebGL compressed textures.
用法
装入图像纹理
jsthis.load.image(key, url);通过基数64 装入图像纹理
jsthis.textures.addBase64(key, data);获取图像纹理
jsvar texture = this.textures.get(key); var image = texture.getSourceImage(); // var width = image.width; // var height = image.height;从框架对象获取图像纹理
jsvar texture = this.textures.get(frameObject);
使用完整图像作为纹理
如果您已经有一个完整的 image 或 canvas , 您可以直接使用 addImage() 、 addSpriteSheet() 、 addAtlas() 等方法将其添加到纹理管理器中。 这些方法与相应的负载方法非常相似, 但是它们会采用 sourceImage 参数( 图像或画布) 而不是 URL 。
您可以用这种方式从同一个来源做第二个纹理, 如果您想创建不同的帧集 :
js
this.textures.addImage(
"mummyCopy",
this.textures.get("mummy").getSourceImage()
);画布纹理
罐装纹理有以2d rendering context为源的canvas。您可以使用其中的任何一种Canvas API。您可以绘制其上的纹理帧,但不能绘制游戏对象(参见动态纹理) 。
您可以用 createCanvas() 创建空白画布纹理 :
const texture = this.textures.createCanvas('key', width, height);
或使用已有的画布 :
const texture = this.textures.addCanvas('key', canvas);
使用 drawFrame() 在 Canvas 纹理上绘制另一个纹理帧 :
texture.drawFrame('mummy', 1, x, y);
或 draw() 如果您有源图像( 不寻常) :
texture.draw(sourceImage, x, y);
如果您直接在画布上下文上工作, 完成时刷新纹理 :
js
const ctx = texture.getContext();
// CanvasTexture has its own `width` and `height`.
// You could also read these from the base frame, as with the Texture class.
const { width, height } = texture;
ctx.fillStyle = "ghostwhite";
ctx.fillRect(0, 0, width, height);
texture.refresh();refresh() 需要更新纹理以显示于 WebGL 渲染模式。不要调用 refresh() 之后 draw() 或 或 时 间 drawFrame()已经包括了
若在绘制后需要使用getPixel()或getPixels(),请拨打update()而不是refresh().
用法
创建画布纹理
jsvar texture = scene.textures.createCanvas(key, width, height);获取画布元素
jsvar canvas = texture.getCanvas(); var context = texture.getContext();绘制框架
jstexture.drawFrame(key, frame, x, y); // texture.drawFrame(key, frame, x, y, update);update:更新内部的图像数据缓冲器和阵列。默认值为true。
绘制图像
jstexture.draw(x, y, source); // texture.draw(x, y, source, update);source: HTML 图像元素,或 HTML 画幅的 Canvas 元素 。update:更新内部的图像数据缓冲器和阵列。默认值为true。
Clear
jstexture.clear();or
jstexture.clear(x, y, width, height); // // texture.clear(x, y, width, height, update);update:更新内部的图像数据缓冲器和阵列。默认值为true。
刷新纹理
jstexture.refresh();颜色纹理
获取像素颜色
jsvar color = texture.getPixel(x, y); // var color = texture.getPixel(x, y, color);jsvar colors = texture.getPixels(x, y, width, height);colors:[{x, y, color, alpha}, ...]
设置像素颜色
jstexture.setPixel(x, y, red, green, blue); // texture.setPixel(x, y, red, green, blue, alpha);
图像数据
获取图像数据
jsvar imageData = texture.getData(x, y, width, height);设定图像数据
jstexture.putData(imageData, x, y);
添加帧
jstexture.add(name, sourceIndex, x, y, width, height);name: 此框架的名称。 名称在纹理内是独一无二的 。sourceIndex:这个框架是其一部分的纹理源索引.x: 此框架左上角的 x 坐标 。y: 此框架左上角的y坐标 。width: 此框架的宽度 。height: 此框架的高度 。
动态纹理
动态纹理(Dynamic Texture)是一种特殊的纹理,它允许您直接绘制纹理,帧和最类游戏对象.
用法
创建动态纹理
jsvar texture = scene.textures.addDynamicTexture(key, width, height);如果此纹理不是Sprite游戏对象的基础纹理, 则禁用
texture.isSpriteTexture。jstexture.setIsSpriteTexture(false); // texture.isSpriteTexture = false;设定大小
jstexture.setSize(width, height);填充颜色
jstexture.fill(rgb); // texture.fill(rgb, alpha, x, y, width, height);rgb: 以数字颜色填充此动态纹理 。alpha:填充时使用的α值。默认值为1。x,y,width,height:填充矩形的区域,默认行为是填充整块大小.
Clear
jstexture.clear();jstexture.clear(x, y, width, height);绘制游戏对象
jstexture.draw(entries); // texture.draw(entries,x, y); // texture.draw(entries, x, y, alpha, tint);entries:- 任意可渲染的游戏对象,例如Sprite,Text,Graphics或TileSprite.
- 平面图层.
- A组,内容将依次加以修改和绘制。
- 容器,内装物将按顺序完全悬挂和抽取。
- 一个场景显示列表,通过
Scene.children来绘制整个列表. - 另一种动态纹理,或称Render纹理.
- 纹理帧实例 。
- 字符串。 用于从纹理管理器中查找纹理 。
x,y: 绘制框架的 x/y 位置,或对对象应用的偏移.- 如果物体是团体、容器或显示清单,则坐标为_在儿童位置上加_。
- 对于所有其他类型的天体,坐标是准确的.
alpha,tint:只有纹理帧使用.- 游戏对象在绘制时使用自己的α和锡值.
Erase
jstexture.erase(entries); // texture.erase(entries, x, y);entries:- 任意可渲染的游戏对象,例如Sprite,Text,Graphics或TileSprite.
- 平面图层.
- A组,内容将依次加以修改和绘制。
- 容器,内装物将按顺序完全悬挂和抽取。
- 一个场景显示列表,通过
Scene.children来绘制整个列表. - 另一种动态纹理,或称Render纹理.
- 纹理帧实例 。
- 字符串。 用于从纹理管理器中查找纹理 。
x,y: 绘制框架的 x/y 位置,或对对象应用的偏移.- 如果物体是团体、容器或显示清单,则坐标为_在儿童位置上加_。
- 对于所有其他类型的天体,坐标是准确的.
绘制框架
jstexture.stamp(key, frame, x, y, { alpha: 1, tint: 0xffffff, angle: 0, rotation: 0, scale: 1, scaleX: 1, scaleY: 1, originX: 0.5, originY: 0.5, blendMode: 0, erase: false, skipBatch: false, });or
jstexture.drawFrame(key, frame, x, y); // texture.drawFrame(key, frame, x, y, alpha, tint);x,y: 左上方位置
绘制重复框架
重复装满大小的帧
jstexture.repeat(key, frame);在区域中重复
jstexture.repeat(key, frame, x, y, width, height); // texture.repeat(key, frame, x, y, width, height, alpha, tint, skipBatch);
添加帧
jstexture.add(name, sourceIndex, x, y, width, height);name: 此框架的名称。 名称在纹理内是独一无二的 。sourceIndex:这个框架是其一部分的纹理源索引.x: 此框架左上角的 x 坐标 。y: 此框架左上角的y坐标 。width: 此框架的宽度 。height: 此框架的高度 。
批量绘图
Begin
jstexture.beginDraw();Draw
绘制游戏对象
jstexture.batchDraw(entries, x, y, alpha, tint);entries:- 任意可渲染的游戏对象,例如Sprite,Text,Graphics或TileSprite.
- 平面图层.
- A组,内容将依次加以修改和绘制。
- 容器,内装物将按顺序完全悬挂和抽取。
- 一个场景显示列表,通过
Scene.children来绘制整个列表. - 另一种动态纹理,或称Render纹理.
- 纹理帧实例 。
- 字符串。 用于从纹理管理器中查找纹理 。
绘制框架
jstexture.batchDrawFrame(key, frame, x, y, alpha, tint);绘制图像
jstexture.stamp(key, frame, x, y, { // ... skipBatch: true, });绘制重复图像
jstexture.repeat(key, frame, x, y, width, height, alpha, tint, true);
End
jstexture.endDraw();
内部摄像机
内部摄像机
texture.camera滚动( 抵销)
jstexture.camera.setScroll(x, y);缩放( 缩放)
jstexture.camera.setZoom(zoom);Rotate
jstexture.camera.setAngle(angle); // angle in degrees
Snapshot
抓图区域
jstexture.snapshot(callback); // texture.snapshot(callback, type, encoderOptions);or
jstexture.snapshotArea(x, y, width, height, callback, type, encoderOptions);callback:在快照图像创建后要引用的函数 。jsfunction(imageElement) { }imageElement: HTML ImageElement. 维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维基月球在线解说-维尔日报-维尔日报-维尔德·维尔曼.
type:图像创建的格式,通常为'image/png'或'image/jpeg'. 默认值为'image/png'.encoderOptions: 图像质量,介于0和1之间,用于图像格式,具有失压缩,如'image/jpeg',默认值为0.92.x,y,width,height:快照区。
获取像素的颜色
jstexture.snapshotPixel(x, y, callback);x,y:像素的x/y坐标来得到.callback:在快照图像创建后要引用的函数 。jsfunction(colorObject) { }colorObject: 或 被抓取一个像素的颜色对象, 或 包含画布内容的快照的新图像 。
渲染纹理
成形纹理本质上是握有动态纹理的Image.
事件
纹理管理器准备好了
jsthis.textures.on("ready", function () {});添加纹理
jsthis.textures.on("addtexture", function (key) {});or
jsthis.textures.on("addtexture-" + key, function () {});添加纹理时出错
jsthis.textures.on("onerror", function (key) {});删除纹理
jsthis.textures.on("removetexture", function (key) {});or
jsthis.textures.on("removetexture-" + key, function () {});
帧
框架 框架有一个名称、位置、几个维度( realWidth和 realHeight 最重要) , 以及一个可选的自定义枢轴。 文件称框架名称为“ 名称”(用于地图集纹理) 和“ 索引”(用于图示表纹理) , 但它们都是相同的。 所有纹理都有一个特殊的框架, 名为“ __BASE”, 代表整个纹理 。
纹理储存在纹理管理器中, this.textures 现场或 game.textures。纹理游戏对象在它们中保留其当前纹理和框 texture 财务报告和已审计财务报表 frame 属性。
内置纹理有三种: 内置纹理: 内置纹理: __DEFAULT (32×32透明), __MISSING (32×32绿色斜箱),和 __WHITE (4×4白). 杭州市志·人物志·人物志·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物·人物
获取纹理的帧名称
js
const frameNames = this.textures.get("mummy").getFrameNames(); // → [0, 1, 2, …]获取所有纹理键与帧名称
js
for (const textureKey of this.textures.getTextureKeys()) {
console.info(textureKey, this.textures.get(textureKey).getFrameNames(true));
}从纹理中获取一帧
用法
- 从纹理中获取框架
js
var frame = this.textures.getFrame(key, frame);框架属性
frame.source.image:纹理源的图像.frame.cutX:在源图像中X位置切除.frame.cutY:Y在源图像中的位置从剪切.frame.cutWidth:要剪切的源图像中区域宽度.frame.cutHeight:要剪切的源图像中区域高度.
Examples:
jsconst mummyFrame1 = this.textures.getFrame("mummy", 1); // OR const mummyFrame1 = this.textures.get("mummy").get(1);纹理本身在技术上没有维度;对于您想要从基框读取的:
jsconst { realWidth, realHeight } = this.textures.getFrame("mummy", "__BASE");
设置纹理过滤模式
js
// Nearest-neighbor filter (pixelated)
this.textures.get("mummy").setFilterMode(Phaser.Textures.FilterMode.NEAREST);
// Linear filter (antialiased)
this.textures.get("mummy").setFilterMode(Phaser.Textures.FilterMode.LINEAR);从游戏对象访问纹理
游戏对象的texture和frame 持有其当前纹理和帧,所以你可以在那里访问它们而不是从纹理管理器进入它们。只需记住你正在和共享对象合作.
js
const mummy = this.add.sprite(0, 0, "mummy", 1);
console.log(mummy.texture.key); // → 'mummy'
console.log(mummy.frame.name); // → 1
const mummyFrame1 = mummy.texture.get(1);设置自定义枢轴点(原点)
js
for (const frame of Object.values(this.textures.get("sprites").frames)) {
if (frame.name === "__BASE") {
continue;
}
frame.customPivot = true;
frame.pivotX = 0.5;
frame.pivotY = 1;
console.log(frame.texture.key, frame.name, frame.pivotX, frame.pivotY);
}添加帧
texture.add(frameName, sourceIndex, x, y, width, height);
您可以使用数字或字符串框名. sourceIndex是单源纹理的 0.
框架可以被克隆,但您必须手动添加新框架对象 :
js
// Clone frame 0 of texture "asp".
const aspFrame = this.textures.cloneFrame("asp", 0);
// Add it as frame 0 of the "bat" texture.
const batTexture = this.textures.get("bat");
batTexture.frames[aspFrame.name] = aspFrame;
batTexture.frameTotal += 1;您可以在任意纹理中添加框架。 您可以在这里将单个框架的纹理“ 转换为多框架的纹理表 :
js
this.load.image("example", "example.png");
this.load.once("filecomplete-image-example", () => {
const texture = this.textures.get("image");
texture.firstFrame = 0;
texture.add(0 /* … */);
texture.add(1 /* … */);
texture.add(2 /* … */);
texture.getFrameNames(); // -> [0, 1, 2]
});在实践中,您通常会添加帧来创建多帧的Canvas纹理或动态纹理(见下文).
添加纹理图集
js
this.textures.addAtlas(key, HTMLImageElement, data);
// this.textures.addAtlas(key, HTMLImageElement, data, dataSource);key:纹理中独特的基于字符串的键.HTMLImageElement: HTML 图像元素/s.data:纹理图集数据/s.js{ frames: [ { // Location of frame image frame: { x, y, w, h }, // trimmed trimmed: sourceSize: { w, h }, spriteSourceSize: { x, y, w, h }, rotated: // Custom origin anchor: pivot: { x, y }, // Other custom properties of this frame ... } ], // Other custom properties of this texture ... }dataSource:一个可选的数据图像元素(法线贴图).
or
js
this.textures.addAtlas(undefined, texture, data);
// this.textures.addAtlas(undefined, texture, data, dataSource);texture: Phaser 纹理.
添加精灵表
js
this.textures.addSpriteSheet(key, HTMLImageElement, config);
// this.textures.addAtlas(key, HTMLImageElement, config, dataSource);key:纹理中独特的基于字符串的键.HTMLImageElement: HTML 图像元素/s.config: 此精灵表的配置对象 。js{ frameWidth: , frameHeight: , startFrame: 0, endFrame: -1, margin: 0, spacing: 0 }dataSource:一个可选的数据图像元素(法线贴图).
or
js
this.textures.addSpriteSheet(undefined, texture, config);
// this.textures.addSpriteSheet(undefined, texture, config, dataSource);texture: Phaser 纹理.
作者署名
本页包含以下作者贡献的内容。