2 回答

TA貢獻1906條經驗 獲得超3個贊
您的著色器正在嘗試讀取名為的紋理uSampler,但您從未創建過該統一名稱。相反,您命名了您的 uniform texture,它永遠不會在您的著色器代碼中調用。看起來簡單地重命名你的制服就可以解決你的問題:
uniforms = {
uResolution: new PIXI.Point(800, 600),
uSampler: new PIXI.Texture.from('img link here')
}

TA貢獻1856條經驗 獲得超11個贊
這是 Pixi.js 在著色器內渲染紋理的示例
// Create a pixi instance
const app = new PIXI.Application({ width: 800, height: 800 });
document.body.appendChild(app.view);
// Create the container that we will apply the shader to
var container = new PIXI.Container();
app.stage.addChild(container);
// Bring in some images
const spriteForShader = new PIXI.Sprite.from('https://assets.codepen.io/292864/internal/avatars/users/default.png?fit=crop&format=auto&height=512&version=1&width=512')
// This image is a random image from imgur (which has CORS enabled so Codepen can grab it)
const skyrimComic = new PIXI.Sprite.from('https://i.imgur.com/6BheBL1.jpeg')
// Note: The container must be rendering something in order for the shader to show,
// which is why we add this sprite to it. It is a different sprite than spriteForShader
// to prove that the shader is rendering the spriteForShader ONLY via the texture uniform
// and not because it's a child. Try removing the filter to see what it looks like without the
// shader applied
container.addChild(skyrimComic);
var shaderCode = `
varying vec2 vTextureCoord;
uniform sampler2D uTexture;
void main(void) {
gl_FragColor = texture2D(uTexture, vTextureCoord);
// Set the red to 0 just to show that the shader is having an effect
// this will make the texture render without any red
gl_FragColor.r = 0.0;
}
`;
var uniforms = {
// Pass the texture to the shader uniform
// "uTexture" could be named whatever you want
uTexture: spriteForShader.texture
}
var simpleShader = new PIXI.Filter('',shaderCode,uniforms);
container.filters = [simpleShader]
看到它在 Codepen 上工作
你的問題是你傳遞的是一個內部有紋理的對象,而不是紋理。
分享
編輯
跟隨
2022 年 7 月 25 日 15:44編輯
這是 Pixi.js 在著色器內渲染紋理的示例
// Create a pixi instance
const app = new PIXI.Application({ width: 800, height: 800 });
document.body.appendChild(app.view);
// Create the container that we will apply the shader to
var container = new PIXI.Container();
app.stage.addChild(container);
// Bring in some images
const spriteForShader = new PIXI.Sprite.from('https://assets.codepen.io/292864/internal/avatars/users/default.png?fit=crop&format=auto&height=512&version=1&width=512')
// This image is a random image from imgur (which has CORS enabled so Codepen can grab it)
const skyrimComic = new PIXI.Sprite.from('https://i.imgur.com/6BheBL1.jpeg')
// Note: The container must be rendering something in order for the shader to show,
// which is why we add this sprite to it. It is a different sprite than spriteForShader
// to prove that the shader is rendering the spriteForShader ONLY via the texture uniform
// and not because it's a child. Try removing the filter to see what it looks like without the
// shader applied
container.addChild(skyrimComic);
var shaderCode = `
varying vec2 vTextureCoord;
uniform sampler2D uTexture;
void main(void) {
gl_FragColor = texture2D(uTexture, vTextureCoord);
// Set the red to 0 just to show that the shader is having an effect
// this will make the texture render without any red
gl_FragColor.r = 0.0;
}
`;
var uniforms = {
// Pass the texture to the shader uniform
// "uTexture" could be named whatever you want
uTexture: spriteForShader.texture
}
var simpleShader = new PIXI.Filter('',shaderCode,uniforms);
container.filters = [simpleShader]
看到它在 Codepen 上工作
你的問題是你傳遞的是一個內部有紋理的對象,而不是紋理。
分享
編輯
跟隨
2022 年 7 月 25 日 15:44編輯
這是 Pixi.js 在著色器內渲染紋理的示例
// Create a pixi instance
const app = new PIXI.Application({ width: 800, height: 800 });
document.body.appendChild(app.view);
// Create the container that we will apply the shader to
var container = new PIXI.Container();
app.stage.addChild(container);
// Bring in some images
const spriteForShader = new PIXI.Sprite.from('https://assets.codepen.io/292864/internal/avatars/users/default.png?fit=crop&format=auto&height=512&version=1&width=512')
// This image is a random image from imgur (which has CORS enabled so Codepen can grab it)
const skyrimComic = new PIXI.Sprite.from('https://i.imgur.com/6BheBL1.jpeg')
// Note: The container must be rendering something in order for the shader to show,
// which is why we add this sprite to it. It is a different sprite than spriteForShader
// to prove that the shader is rendering the spriteForShader ONLY via the texture uniform
// and not because it's a child. Try removing the filter to see what it looks like without the
// shader applied
container.addChild(skyrimComic);
var shaderCode = `
varying vec2 vTextureCoord;
uniform sampler2D uTexture;
void main(void) {
gl_FragColor = texture2D(uTexture, vTextureCoord);
// Set the red to 0 just to show that the shader is having an effect
// this will make the texture render without any red
gl_FragColor.r = 0.0;
}
`;
var uniforms = {
// Pass the texture to the shader uniform
// "uTexture" could be named whatever you want
uTexture: spriteForShader.texture
}
var simpleShader = new PIXI.Filter('',shaderCode,uniforms);
container.filters = [simpleShader]
看到它在 Codepen 上工作
你的問題是你傳遞的是一個內部有紋理的對象,而不是紋理。
分享
編輯
跟隨
2022 年 7 月 25 日 15:44編輯
這是 Pixi.js 在著色器內渲染紋理的示例
// Create a pixi instance
const app = new PIXI.Application({ width: 800, height: 800 });
document.body.appendChild(app.view);
// Create the container that we will apply the shader to
var container = new PIXI.Container();
app.stage.addChild(container);
// Bring in some images
const spriteForShader = new PIXI.Sprite.from('https://assets.codepen.io/292864/internal/avatars/users/default.png?fit=crop&format=auto&height=512&version=1&width=512')
// This image is a random image from imgur (which has CORS enabled so Codepen can grab it)
const skyrimComic = new PIXI.Sprite.from('https://i.imgur.com/6BheBL1.jpeg')
// Note: The container must be rendering something in order for the shader to show,
// which is why we add this sprite to it. It is a different sprite than spriteForShader
// to prove that the shader is rendering the spriteForShader ONLY via the texture uniform
// and not because it's a child. Try removing the filter to see what it looks like without the
// shader applied
container.addChild(skyrimComic);
var shaderCode = `
varying vec2 vTextureCoord;
uniform sampler2D uTexture;
void main(void) {
gl_FragColor = texture2D(uTexture, vTextureCoord);
// Set the red to 0 just to show that the shader is having an effect
// this will make the texture render without any red
gl_FragColor.r = 0.0;
}
`;
var uniforms = {
// Pass the texture to the shader uniform
// "uTexture" could be named whatever you want
uTexture: spriteForShader.texture
}
var simpleShader = new PIXI.Filter('',shaderCode,uniforms);
container.filters = [simpleShader]
看到它在 Codepen 上工作
你的問題是你傳遞的是一個內部有紋理的對象,而不是紋理。
添加回答
舉報