我构建了一个简单的PIXI.js应用程序来实现PIXI.RenderTexture,但它不起作用.

它应该渲染两个正方形的精灵,黑色和白色.黑色的加上常规的stage.addChild:

const sprite1 = new PIXI.Sprite(PIXI.Texture.from('bc-sq-200.png'));
sprite1.x = 500;
app.stage.addChild(sprite1);

白色的应该渲染为renderTexture:

const sprite2 = new PIXI.Sprite(PIXI.Texture.from('wt-sq-200.png'));
// app.stage.addChild(sprite2);

const renderer = PIXI.autoDetectRenderer();
const renderTexture = PIXI.RenderTexture.create({ width: 700, height: 700 });
renderer.render(sprite2, { renderTexture });

const mainSprite = PIXI.Sprite.from(renderTexture);
app.stage.addChild(mainSprite);

然而,只能看到黑色的正方形.

这可能是什么问题?到底出了什么问题?

Github回购的最小示例:https://github.com/poludnev/test-pixi-render-texture

推荐答案

修好了.我认为这是自动检测渲染器选项的问题,你可以简单地使用应用程序中的渲染器,这应该会起作用:

let app = new PIXI.Application({
  width: 500,
  height: 256,
  antialiasing: true,
  transparent: false,
  resolution: 1
});

document.body.appendChild(app.view);

//To change the background color
app.renderer.backgroundColor = "0xff0000";
const sprite1 = new PIXI.Sprite(PIXI.Texture.from("public/bc-sq-200.png"));

app.stage.addChild(sprite1);
const sprite2 = new PIXI.Sprite(PIXI.Texture.from("public/wt-sq-200.png"));

sprite2.position.x = 0;
sprite2.position.y = 0;
sprite2.anchor.x = 0;
sprite2.anchor.y = 0;

setTimeout(() => {
  const renderTexture = PIXI.RenderTexture.create({ width: 200, height: 200 });
  console.log(renderer);

  app.renderer.render(sprite2, {
    renderTexture
  });
  const mainSprite = new PIXI.Sprite(renderTexture);
  mainSprite.x = 200;
  mainSprite.y = 0;
  mainSprite.width = 200;
  mainSprite.height = 200;
  app.stage.addChild(mainSprite);
  console.log(mainSprite);
}, 2000);

这是一张demo美元的钞票

我更改了其他一些东西,以最大限度地减少潜在的问题:

  1. SetTimeout表示PNG有足够的时间加载.您应该确保预先加载assets资源 ,setTimeout只是因为它很容易.

  2. mainSprite的宽度和高度很重要

  3. 添加了红色背景,以便您可以更容易地看到这两个框.

我真的以为这会是10秒的修复,但这个合法的修复花了我一两个小时.肯定需要更好的文档来更清楚地说明AutoDetectReneller和RenderTexture之间的关系,因为OPPS的原始代码几乎直接来自文档.

Javascript相关问答推荐

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

IMDB使用 puppeteer 加载更多按钮(nodejs)

Snowflake JavaScript存储过程返回成功,尽管预期失败

TypeScript索引签名模板限制

Mongoose post hook在使用await保存时不返回Postman响应

处理时间和字符串时MySQL表中显示的日期无效

Regex结果包含额外的match/group,只带一个返回

如何在JavaScript文件中使用Json文件

单个HTML中的多个HTML文件

Web Crypto API解密失败,RSA-OAEP

在我的index.html页面上找不到我的Java脚本条形图

使用父标签中的Find函数查找元素

如何通过Axios在GraphQL查询中发送数组

Reaction即使在重新呈现后也会在方法内部保留局部值

固定动态、self 调整的优先级队列

在Puppeteer中使用promise进行日志(log)记录时出现TargetCloseError

对不同目录中的Angular material 表列进行排序

如何缩小函数中联合返回类型的范围

JS:inline date子串.

按下键盘上的空格键后,单词就会变色.我想在最后一封信之后立即给它们涂上 colored颜色