Site Logo


Sparen's Danmakufu ph3 Tutorials Lesson 14 - Utilizing Blend Types in Danmakufu

The video for this lesson is Conarnar's Youmu! The second nonspell takes full advantage of one of the blend properties of lasers to create a very unique nonspell experience, and this is just one of the many ways in which it is possible to use blend types for graphical effect.

Part 1: What will be Covered in this Lesson?

Back in Lesson 8, I talked about blend types in terms of shotsheets. Today I will go significantly more in depth and explain various uses of the various blend types, as well as what they do.

Part 2: What are the Blend Types in Danmakufu ph3?

Firstly, what are the blend types present in Danmakufu? Well, there is, of course, the default - Alpha. Then there are two forms of Additive Blending - BLEND_ADD_RGB and BLEND_ADD_ARGB. There are also Multiplicative and Subtractive blends. Also available is Invert, which is commonly abused due to its effects.

You can use ObjRender_SetBlendType() to change blend types.

As of ph3 [.1 pre6a], there is also a BLEND_SHADOW, which is not documented. For more information, see the following thread on Maidens of the Kaleidoscope: Figuring out BLEND_SHADOW (Note: MotK is currently a read-only archive).

Part 3: What is Alpha Compositing and How is it Used?

I'll begin with the simplest 'blend' - Alpha. This is the default and renders the image the exact same way as any graphical editor. Transparency is maintained if present.

From a technical standpoint, Alpha Compositing involves taking the image in question and merging it with the background below (for Danmakufu, that means anything with a lower render priority or any object with the same render priority that was created prior).

The applications of BLEND_ALPHA are numerous. Almost everything in Danmakufu is rendered with it. Most graphics will be rendered using Alpha, and the usage of transparency allows for extremely detailed effects that meld with the backgrounds they are on.

Part 4: What is Additive Blending and How is it Used?

Of the simple arithmetic blend modes, Additive Blending sees the most use in Danmakufu. It adds the pixel values of one layer with the other. This always results in a brighter image. For example, you are rendering one image on top of a background. The pixel values of the image and background underneath will add, resulting in a shinier image.

In Danmakufu, there are two ways to use Additive Blending. One involves a black background - BLEND_ADD_RGB. Everything that is pure black becomes transparent, and everything else becomes shiny. Most bubble bullets and fireball bullets are rendered with ADD because it makes them very shiny and nice to look at, especially when contrasted with a dark background or playing field. Additionally, lasers are rendered by default using BLEND_ADD_RGB.

The other type of Additive Blending is BLEND_ADD_ARGB. Instead of black, you use a partially transparent object. The level of transparency will dictate the level of shininess that results.

Note that when using ObjRender_SetAlpha(), ADD_RGB ignores it and will always display the image the same way, while with ADD_ARGB, the alpha of the image in Danmakufu will change the level of shininess of the image.

EXERCISE: Render some images with Additive Blending. What kind of images work best? Does ADD_ARGB work better? Worse?

Part 5: What is Subtractive Blending and How is it Used?

In contrast to Additive Blending, Subtractive Blending sees limited usage in Danmakufu, being used mainly for graphical effect. It does the exact opposite as Additive Blending, always darkening the image. If the values end up being negative, black is displayed instead. Subtractive Blending is excellent when used on fog and partially transparent images.

Part 6: What is Multiplicative Blending and How is it Used?

Multiplicative blending is great for darkening images, but has limited use in Danmakufu - mainly for graphical effect. It multiplies the pixel value of the top layer with that of the bottom layer, darkening the image. Note that Multiply Blend in Danmakufu does not work correctly with regards to opaque/partially or wholly transparent pixels.

Part 7: What is Invert and How is it Used?

To close off the list of standard blend types in Danmakufu, we will discuss Invert, which is technically not a blend type. Black turns to white, white turns to black. Usually, this is done using a shader. But in Danmakufu, we use a specific method to control invert.

Most of the time, we use a pure white circle or square on a transparent background. We set the blend type of this circle or square to BLEND_INV_DESTRGB. Now, anything rendered below this circle will have its color inverted. Applying two objects with invert, one on top of the other, will negate the effect.

The invert effect is pretty cool, but is also used in situations where it is not optimal. For best usage, a high contrast background or scene should be used to give maximum effect to the invert.

For example:

    task Invert{
        let invert = GetCurrentScriptDirectory() ~ "./img/inv_circle.png"; //Define path
        let objInv = ObjPrim_Create(OBJ_SPRITE_2D);
        let objcount = 0;
        ObjPrim_SetTexture(objInv, invert);
        ObjSprite2D_SetSourceRect(objInv, 0, 0, 512, 512);
        ObjSprite2D_SetDestCenter(objInv);
        ObjRender_SetScaleXYZ(objInv, 0.25, 0.25, 1);
        ObjRender_SetPosition(objInv, 384/2, 448/2, 1);
        ObjRender_SetBlendType(objInv, BLEND_INV_DESTRGB);
        while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
            ObjRender_SetScaleXYZ(objInv, 0.25 + 0.1*sin(objcount*3), 0.25 + 0.1*sin(objcount*3), 1);
            objcount++;
            yield;
        }
        Obj_Delete(objInv);
    }

The above code creates an invert object at the center of the screen whose size oscillates with time. Depending on how you want to use it, Invert can be a powerful tool.

Quiz: Blend Types

1) For a shiny image, which blend type would we use?

A. BLEND_ADD_ARGB
B. BLEND_SHADOW
C. BLEND_SUBTRACT

2) Seija wants to invert everything. Specifically, she wants to invert the entire STG_Frame. How would she do this?

The best way to do this is to have a white square texture, and create a 2D Sprite of this object. The render priority would be 99 in order to cover everything on the STG_Frame, such as the text. The blend would be BLEND_INV_DESTRGB, and the object would be centered at (320, 240), stretched to fit the screen.

    task Invert{
	let invert = GetCurrentScriptDirectory~"./img/inv_square.png"; //Define path
	let objInv = ObjPrim_Create(OBJ_SPRITE_2D);
	let objcount = 0;
	ObjPrim_SetTexture(objInv, invert);
	ObjSprite2D_SetSourceRect(objInv, 0, 0, 512, 512);
	ObjSprite2D_SetDestRect(objInv, -320, -240, 320, 240);
	ObjRender_SetPosition(objInv, 640/2, 480/2, 1);
	ObjRender_SetBlendType(objInv, BLEND_INV_DESTRGB);
	Obj_SetRenderPriorityI(objInv, 99);
    }

Alternatively, we could use a render target of the entire game screen with a pixel shader, but that's beyond the scope of this lesson.

Summary

  • Additive Blending is good for making graphics shiny
  • Subtractive Blending is good for making graphics dark
  • Invert can be used to flip the RGB values of whatever is rendered below an INV_DESTRGB object

Sources and External Resources

N/A