CPShader
A programmable shader for scene object.
Shader is a piece of user-defined code that is executed on each frame pass for each scene object that has one or more shaders attached to it.
Different Ways To Animate
In CosPlay there are different ways one could implement animated scene objects. In the end, all of these approaches deliver similar results but each individual technique is tailor-made for a specific animation type:
Animated Sprites
This is a classic sprite animation technique ideally suited for animations that can be defined as a sequence of individual similarly or identically sized images. When played, each individual images is shown for a short period of time one after another delivering animation effect.
Lets consider an animation of the airplane in a top-down view game play. The basic animations for the airplane banking left or right, taking off or landing are ideally suited for sprite-based animation as they can easily be defined as a short sequence of individual images.
Particle Effects
Particle effect animation is based on the concept of a pixel-based particle and particle emitter. Particle emitter emits particles. Each particle and its emitter have a fully programmable behavior. The key characteristic of particle effect animation is the randomness over the large number of individual elements that you can easily implement using fully programmable particles and emitters.
In our airplane example, lets consider how one could implement the explosion when the airplane is hit with the missile. One could potentially implement such animated explosion as a long sequence of images but such process would be very tidies and lack the desired randomness. Particle effect-based animation fits the bill perfectly in such cases allowing to implement such random explosion animation in just a few lines of code.
Canvas Drawing
Sometime, a simple drawing on the canvas is all that's needed for a desired animation. Consider how one could implement a laser strike in our airplane example. A laser strike can be defined as a variable length line of pixel shown for a split second. The best way to implement it is with one line of code using many of the drawing functions in CPCanvas class and putting this logic into CPSceneObject.render method.
Video Sprites
Video sprite is a variation of sprite-based animation. In case of video, there are typically a lot more frames (often 1000s of frames) and all these frames have the same dimension. CPVideo and CPVideoSprite provide easy-to-use mechanism to implement it. Back to our airplane example, the video-based animation would be ideal choice for the cutscenes, entry video, etc.
Shaders
CPShader Shader is a piece of user-defined code that is executed on each frame for each scene object that has one or more shaders attached to it. There are types of animations that simply don't fit any previous type. The typical example of shader-based animation is the various lighting effect: flash-lite, sun shadows, transitions, highlighting, etc. These types of animation simply cannot be reasonably implemented using sprites, or particles, or canvas drawing. In such cases, shaders provide simple and effective contract to implement this behavior. Yet another unique characteristic of shaders is their application reusability. In fact, the same shader can be added to multiple scene objects to provide its effect.
In our airplane example, shaders can be used for shadow effect or "flashing" the airplane when it is hit by the enemy fire.
Shader is an asset. Just like other assets such as fonts, images, animations or videos they are not managed or governed by the CosPlay game engine unlike scenes and scene objects that are managed and governed by the game engine. Assets are typically created outside the game loop and managed by the developer, they can be freely shared between scenes or scene objects as any other standard Scala objects.
Full-Screen Shaders
Shader can work with either a scene object it is attached to or with full screen. Typically, a full screen shader will be attached to an off-screen sprite (since the particular scene object such shader is attached to is irrelevant). In more elaborate games, there could be multiple off-screen sprites with multiple full-screen shaders - where all these shaders work with the same screen real estate. In such cases it can be non-trivial to control the order in which shaders are executed, if required. The recommended technique for such cases is to have only one off-screen sprite that attaches all full-screen shaders so that their order can be easily defined and controlled.
Order Of Processing
Note that shader pass consists of two phases:
- On the 1st phase shaders for all visible scene objects are processed
- On the 2nd phase shaders for all invisible scene objects are processed. This allows to minimize the interference between object shaders and full-screen shaders that are typically attached to the off-screen sprite that is invisible. In other words, full-screen shaders will be execute after all object shaders in a given scene.
Attributes
- See also
- Example
-
See CPShaderExample class for the example of using shaders.
- Source
- CPShader.scala
- Graph
-
- Supertypes
- Known subtypes
-
class CPBeatShaderclass CPBorderShaderclass CPDurationShaderclass CPFadeInShaderclass CPFadeOutShaderclass CPRandomFadeInShaderclass CPShimmerShaderclass CPSlideInShaderclass CPSlideOutShaderclass CPSparkleShaderclass CPFlashlightShaderclass CPStarStreakShader