好的,所以我正在回答我自己的问题,所以没有其他人必须涉足这个烂摊子。这是解决方案:
首先,您需要在 PIXI 对其进行预处理之后,但在编译之前更改着色器代码。你这样做:
let shaderCode = eventShaderCode;
var simpleShader = new PIXI.Filter(eventVertexShaderCode, shaderCode, this.shaderUniforms);
if (!simpleShader.program.fragmentSrc.includes("#version 300 es")) {
simpleShader.program.vertexSrc = "#version 300 es \n" + simpleShader.program.vertexSrc;
simpleShader.program.fragmentSrc =
"#version 300 es \n" + simpleShader.program.fragmentSrc;
}
我将代码放在 if 语句中,因为我的程序中的多个对象使用此着色器,这可能导致多次添加 #version 行。我不是 100% 确定这是如何发生的,但我认为这与 PIXI 可能使用的某种缓存系统有关,因此它不必多次重新编译同一个着色器。无论如何:这很重要,即使它看起来没有必要。
接下来,您需要实际编写顶点和片段着色器。即使你只修改一个,你也需要重写两个。为了您的方便,我的如下:
顶点着色器:
in vec2 aVertexPosition;
uniform mat3 projectionMatrix;
out vec2 vTextureCoord;
uniform vec4 inputSize;
uniform vec4 outputFrame;
vec4 filterVertexPosition( void )
{
vec2 position = aVertexPosition * max(outputFrame.zw, vec2(0.)) + outputFrame.xy;
return vec4((projectionMatrix * vec3(position, 1.0)).xy, 0.0, 1.0);
}
vec2 filterTextureCoord( void )
{
return aVertexPosition * (outputFrame.zw * inputSize.zw);
}
void main(void)
{
gl_Position = filterVertexPosition();
vTextureCoord = filterTextureCoord();
}
片段着色器
precision highp float;
uniform sampler2D uSampler;
in vec2 vTextureCoord;
out vec4 color;
void main(){
color = texture(uSampler, vTextureCoord);
}
恭喜!您现在可以在您的 PIXI.JS 项目中使用#version 300 es!