Jump to content
The Dark Mod Forums

Random texture offset per particle


rich_is_bored

Recommended Posts

I have a material with a scrolling texture. I have a particle effect. At present the texture scrolls across all the particles in unison but I need to offset the texture randomly on a per particle basis.

 

I thought shaderparms would be the solution here but it appears they are only applicable on a per entity basis. I tried looking through the source code to be sure but I don't see anything specific to particles that would help.

 

This experiment also happens to use a vertex/fragment program. I bring this up because if shaderparms are not an option perhaps there is some vertex or fragment properties I can use to derive a static value unique to each particle?

 

Link to comment
Share on other sites

Interesting problem. We fixed a similar issue for 2.04 in #4132, where adjacent weather patches looked identical. The problem is you want a different setting for each weather patch or particle quad, but it can't be random. It has to persist for that patch or quad from frame to frame. For weather patches, I took the centre of the particle effect's bounding box as part of the random number seed. That persists from frame to frame but will be different for each weather patch.

 

That won't fix your situation. You need each individual quad to have a different offset to its scroll. There's no way to fix that at the entity level as you found out. And you can't fix it in the material definition either, because all the quads for a particle stage are a single draw call -- so any vertexparms or scroll parameters that you set would be shared by all quads in the particle stage.

 

A fix in the vertex shader program would work, if we can think of a number that would (1) differ for each particle quad, and (2) be constant at every vertex of the quad. Tricky. You could take the vertex position and add it to the texcoord, but the vertex positions will differ across the quad and from frame to frame as the quad moves. You might get away with that approach depending on the texture and the quad movement speed, but it'd be a stroke of luck if you did.

 

The only vertex properties set by the engine for entity particles are: vertex position, vertex color, and texcoord. Texcoord is fixed at 0/1 unless you're using an animation strip. Position we've already talked about.

 

That leaves color. Vertex color is set by the engine and it's used to fade out particles over time. Good news: it's constant for all verts of any given quad. Since you're using your own fragment program, you could co-opt vertex color for your scrolling instead, using it in the vertex shader to scroll the texcoords, and ignoring it in the fragment shader. All particle quads of a given age would have the same vertex color, but as long as your particle quads are spawned one at a time, they will all have different ages.

  • Like 3
Link to comment
Share on other sites

Yep. I arrived at the same conclusion although my journey was more trial and error. I can confirm that vertex color works and your explanation improves my understanding a great deal.

 

On a side note, the textures loaded in my fragment program are cyan as if the red channel was missing. I've bypassed the issue for now by copying from a known working channel.

 

EDIT: It looks like the red channel is ignored once I call TEX a third time.

Edited by rich_is_bored
Link to comment
Share on other sites

EDIT: It looks like the red channel is ignored once I call TEX a third time.

Weird, I've never had that problem. I've no suggestions what could be causing it but if you want a second pair of eyes on it post or PM me your shader assembly code and I'll see whether I can spot anything awry.

 

On the scrolling fix, I had this in mind for your vertex shader:

MAD result.texcoord, vertex.color, 2.4, vertex.texcoord;
where the 2.4 is a constant you'd tweak to set the scrolling speed. You could use a standard vector of values instead of a single number if you needed to scroll in a different direction e.g. { 2.4, -1.5, 0, 0 }. You'd set your particle stage to fade from 1.0 to 0.0 over its entire lifetime. Is that similar to what you did?

 

You could still use vertex color in the fragment shader to fade out the particle, come to think of it, provided that you do want the particle to fade throughout its lifetime.

Link to comment
Share on other sites

This is the code for the particle effect.

 

 

!!ARBvp1.0
OPTION ARB_position_invariant;

MOV    result.texcoord[0], vertex.texcoord[0];
MOV    result.texcoord[1], program.local[0]; # scroll rate expression for x and y
MOV    result.texcoord[2], program.local[1]; # scale
MOV    result.texcoord[3], vertex.color;

END

!!ARBfp1.0
OPTION ARB_precision_hint_fastest;

# Texture 0 is tiling perlin
# Texture 2 is a mask

TEMP IMG, UV, TMP;

ADD UV, fragment.texcoord[0], fragment.texcoord[1];
TEX    IMG, UV, texture[0], 2D;
MOV TMP, IMG;

MUL UV, fragment.texcoord[0], fragment.texcoord[2];
ADD UV, UV, fragment.texcoord[1];
TEX    IMG, UV, texture[0], 2D;
MUL TMP, TMP, IMG;

ADD TMP, TMP, TMP;
TEX IMG, fragment.texcoord[0], texture[1], 2D;
MOV IMG.r, IMG.g;    # <---- FOR SOME REASON THE THIRD TIME I CALL TEX I GET A CYAN IMAGE.
MUL    TMP, TMP, IMG;
MOV TMP.a, TMP.g;

MOV    result.color, TMP;

END
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recent Status Updates

    • nbohr1more

      TDM 15th Anniversary Contest is now active! Please declare your participation: https://forums.thedarkmod.com/index.php?/topic/22413-the-dark-mod-15th-anniversary-contest-entry-thread/
       
      · 0 replies
    • JackFarmer

      @TheUnbeholden
      You cannot receive PMs. Could you please be so kind and check your mailbox if it is full (or maybe you switched off the function)?
      · 1 reply
    • OrbWeaver

      I like the new frob highlight but it would nice if it was less "flickery" while moving over objects (especially barred metal doors).
      · 4 replies
    • nbohr1more

      Please vote in the 15th Anniversary Contest Theme Poll
       
      · 0 replies
    • Ansome

      Well then, it's been about a week since I released my first FM and I must say that I was very pleasantly surprised by its reception. I had expected half as much interest in my short little FM as I received and even less when it came to positive feedback, but I am glad that the aspects of my mission that I put the most heart into were often the most appreciated. It was also delightful to read plenty of honest criticism and helpful feedback, as I've already been given plenty of useful pointers on improving my brushwork, level design, and gameplay difficulty.
      I've gotten back into the groove of chipping away at my reading and game list, as well as the endless FM catalogue here, but I may very well try my hand at the 15th anniversary contest should it materialize. That is assuming my eyes are ready for a few more months of Dark Radiant's bright interface while burning the midnight oil, of course!
      · 4 replies
×
×
  • Create New...