Customising an animated material

Software: Away3D 4.x

seanClusta, Jr. Member
Posted: 22 October 2014 02:31 PM   Total Posts: 38

Hi, me again!

Lately I’ve been experimenting with writing custom materials using shaders written in AGAL. I used the advanced level tutorials here http://away3d.com/tutorials/ as a starting point and must say they are very helpful. I now have control over custom vertex and fragment operations and am starting to do some nice creative 3D work with Away3D.

For my next step I want to combine the control I have over the custom shaders but also add SpriteSheet animation into my custom material. I’ve followed the Basic_SpriteSheetAnimation example from the repo and understand how to create and apply the texture, create the SpriteSheetAnimator and apply it to the mesh. I’m not sure though of how to combine the 2 processes.

Do I have to implement the whole texture/spritesheet sampling into my own custom MaterialPass? Or can I override some methods of the TextureMaterial and update the getVertexCode() or getFragmentCode() that way? Or is there a totally different better way?

I wouldn’t ask but I just can’t find any more documentation on this. I’m sure with enough hours of reverse engineering I’ll figure it out but would really appreciate an answer even just a quick pointer to push me (and others that may face this request in the future) in the right direction.

Thanks

   

Avatar
Fabrice Closier, Administrator
Posted: 22 October 2014 07:19 PM   Total Posts: 1265   [ # 1 ]

That’s a tough one… I wrote this animator long ago, but on top of my head, if you try change just the animator class so it accepts your material as input type you should be good.
If not recreating similar animator is simple, all you need is 5 things: constants for offsetu,v and scaleu,v, assuming you default uvs values of your input to 0-1. and an optional one to toggle sources if you plan to have “long” animations spreaded over more maps. That’s it.

The animator keeps track of each frame and has an internal update for these that measure time and increases frame index based on duration. On increase it update the offsets/scales constants. That’s is what you would need to (re)create. The shader on itself is simple as you could see, it makes a tmp uv, multiply by scale and adds the offsets.

Not sure my answer is really what you expect, but thats all I have for now (without diving in code on a machine that isn’t near me atm)

 

   

seanClusta, Jr. Member
Posted: 23 October 2014 09:34 AM   Total Posts: 38   [ # 2 ]

Thanks for the reply Fabrice,

I did look into it myself before noticing this reply and came up with a solution - I think it’s the second one you outlined of re-creating the animator?

Basically I went about extending a MaterialBase and a MaterialPassBase (as in the materials tutorials above). I pass the spritesheet texture into the custom material and onto the pass. Within the custom pass, on the render method I am accessing the Mesh’s animator (SpriteSheetAnimator) and frame (SpriteSheetAnimationFrame) using the below references. This gives me access to the currentFrameNumber and therefore the UV scales and offsets (via the frame).

var renderable:SubMesh irenderable as SubMesh;
var 
animator:SpriteSheetAnimator renderable.animator as SpriteSheetAnimator;

var 
animSet:SpriteSheetAnimationSet animationSet as SpriteSheetAnimationSet;
var 
spriteSheetClipNode:SpriteSheetClipNode animSet.animations[0] as SpriteSheetClipNode;
var 
frame:SpriteSheetAnimationFrame spriteSheetClipNode.frames[animator.currentFrameNumber] as SpriteSheetAnimationFrame;
   
_uvScale[0] frame.scaleU;
_uvScale[1] frame.scaleV;
_uvOffset[0] frame.offsetU;
_uvOffset[1] frame.offsetV

I can then pass these UV scales and offsets into the fragment shader as Fragment constants, and use them in the shader code to choose which part of the texture to render.

I’ve attached a lightweight project which demos this code in action.
It just creates a cube out of 6 planes. Each plane has this custom material attached to it. The material uses a spritesheet of a single pixel outline which pulses to give the effect of the cube having a pulsing wireframe.
Nothing fancy happens in the vertex or fragment shaders (apart from setting UV values) but it’s a base from where you could alter it.

It’s a FlashDevelop project, and should you want to compile it you will have to change the path to the SDK (in bat/SetupSDK.bat

Hopefully someone else will find it useful

 

 

File Attachments
AnimatedSpriteSheet.zip  (File Size: 1023KB - Downloads: 255)
   

seanClusta, Jr. Member
Posted: 19 November 2014 04:59 PM   Total Posts: 38   [ # 3 ]

Hi Fabrice, or anyone else - I wonder if you can help?

I’m now looking into modifying/mixing custom vertex shader code with the built in ParticleAnimator.

The Particle systems included with Away3D seem very powerful so i want to use the existing API but really need to add some ‘warp code’ to the vertex shader - it’s just a few lines of Vx shader code which take in some params from the render method

The problem is when I use this custom pass (extending MaterialPassBase again) the particles don’t animate / the animator code isn’t called.

The above solution worked OK for the spritesheet animations because there was only a few bits i needed to re-implement, so I added them into my custom pass - but the particles would be much more work. I’m wondering if there’s a better way? i.e. if I could add my shader code to the existing particle animation code?

Do you have any examples or could point me in the right direction of merging different shader programs? Is that possible?

I see in the ParticleNodes (that extend ParticleNodeBase) there are methods to return the AGAL codes. I assume that these then get combined with other nodes to create a single shader program? If so, would it be possible to implement and add my own routine?

I hope that makes sense. I can post some code if it helps?

 

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X