Issue with shader and Particle Effects

I have a very basic shader that I got from the forums to test something non-related out. When applying it to render , a ParticleEffect 's uv coordinates completely resets and I believe flips upside down as well. Any ideas as to what might be happening here? Will provide particle code if needed, but I’ve tried it with a several particles and still get the same issue. FWIW, tried both setTextureFromNode and setTextureFromFile which yielded same results. I will provide the particle code if needed, but it is the same result for all the ones I have tried.

GLSL code
vshader = """#version 140
// Vertex inputs
in vec4 p3d_Vertex;
in vec2 p3d_MultiTexCoord0;
uniform float osg_FrameTime;
// Uniform inputs
uniform mat4 p3d_ModelViewProjectionMatrix;
uniform mat4 p3d_ModelViewMatrix;
uniform mat4 p3d_ProjectionMatrix;
// Output to fragment shader
out vec2 texcoord;
void main() {
  vec4 pos = p3d_Vertex;
  texcoord = p3d_MultiTexCoord0;
  gl_Position =p3d_ModelViewProjectionMatrix * pos;
}
"""

fshader = """#version 140
uniform sampler2D p3d_Texture0;
// Input from vertex shader
in vec2 texcoord;
void main() {
  vec4 color = texture2D(p3d_Texture0, texcoord.xy);
  gl_FragColor = color.rgba;
}
"""

My guess is that’s it’s simply that, by applying the shader to “render”, it affects all things parented below “render”–which includes your particle effect.

(Specifically, it may be that the particle system is applying a texture-transform to achieve the UV range shown in the “expected result” image–but your shader-code doesn’t seem to apply texture transforms, and so the one applied to the particles is having no effect.)

You might perhaps try either applying your shader to a node that is an ancestor of (or that is) your scene-node, but that is not an ancestor of your particle system, or explicitly disabling shaders on your particles via a call to “setShaderOff()” on the NodePath associated with your particle system.

The former might look something like this:

myScene = loader.loadModel("sceneModel")
myScene.reparentTo(render)

myParticles = ParticleEffect()
myParticles.loadConfig(fileName)
myParticles.start(parent = render, renderParent = render)

myScene.setShader(myShader)
# Note that the shader now applies to the scene--
#  but does >not< apply to the particles.

The latter might look something like this:

myScene = loader.loadModel("sceneModel")
myScene.reparentTo(render)

myParticles = ParticleEffect()
myParticles.loadConfig(fileName)
myParticles.start(parent = render, renderParent = render)

render.setShader(myShader)
myParticles.setShaderOff()
# Note that the shader now >does< in a sense apply to the particles--
#  but that this has been disabled by the call to "setShaderOff"

(You could also implement texture-transforms in your shader, of course–but as you say that the shader is only being applied as an experiment it may not be worth the bother.)

1 Like

Got it. Thank you!

1 Like