I made this below shader which should simply get pixels from a specific area from a texture. The offset / scale works fine but the resulting RGB is very different to when i render the same texture with for example MeshBasicMaterial. I suspect it has to do with ‘mapTexelToLinear’ that i found in the default THREE shaders. But no idea what this does or how to duplicate that here.
I am aware that material.onBeforeCompile would be the best way but I cannot get this working . Can someone help me translate below into a onBeforeCompile apporach?
Thanks !!
getCutoutShaderMaterial(cutouttexture, offsetx, offsety, scalex, scaley) {
return new THREE.ShaderMaterial({
transparent: true,
side: SphereSide,
depthTest: false,
fog: false,
uniforms: {
cutouttexture: {
value: cutouttexture
},
offsetx: {
type: 'f',
value: offsetx
},
offsety: {
type: 'f',
value: offsety
},
scalex: {
type: 'f',
value: scalex
},
scaley: {
type: 'f',
value: scaley
}
},
vertexShader: `
varying vec2 vUv;
void main(){
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`,
fragmentShader: `
uniform sampler2D cutouttexture;
uniform float offsetx;
uniform float offsety;
uniform float scalex;
uniform float scaley;
varying vec2 vUv;
void main(){
gl_FragColor = texture2D( cutouttexture, vUv * vec2(scalex, scaley) + vec2(offsetx, offsety) * vec2(scalex, scaley) );
}
`
});
}