Effect Materials

  Edit on GitHub

These docs are for 0.13.0-SNAPSHOT, an unreleased SNAPSHOT. See the latest release.

An EffectMaterial is an unlit, transparent material for mesh based effects, such as sword swing crescents, shockwave rings, auras, and magic shells. It never writes depth or casts shadows.

Pipeline

Add an EffectMaterialPipelineProvider. Its shader only reads the camera, so it can be rendered with an UnlitEnvironment.

val modelBatch =
    ModelBatch(device).apply {
        addPipelineProvider(EffectMaterialPipelineProvider())
        colorFormat = hdrTarget.format
        depthFormat = depthTarget.format
    }
val environment = UnlitEnvironment(device)

Material

The maskTexture shapes the effect: its red times alpha is the mask intensity. The rampTexture is sampled left to right by that intensity and multiplied by color times intensity. Go over 1 for bloom. Vertex colors are multiplied in.

val slash =
    EffectMaterial(
            device,
            maskTexture = crescentMask,
            rampTexture = fireRamp,
            color = Color(1f, 0.9f, 0.8f, 1f),
        )
        .apply { intensity = 4f }

val node = MeshNode(listOf(MeshPrimitive(crescentMesh, slash)))

Use a REPEAT sampler on the mask when tiling or scrolling it, and a clamped one on the ramp.

Scrolling and Distortion

Advance time each frame to scroll the textures. The red channel of the noiseTexture bends the mask by distortion, in UVs.

slash.apply {
    maskTiling.set(1f, 1f)
    maskScroll.set(0.5f, 0f) // uvs per second
    noiseTiling.set(3f, 1f)
    noiseScroll.set(-1.5f, 0f)
    distortion = 0.04f
}

onUpdate { dt -> slash.time += dt.seconds }

Erosion

The effect dissolves where the noise is below erosion, with a soft erosionSoftness falloff. edgeColor is drawn along the dissolving edge, edgeWidth wide, and edgeIntensity makes it glow.

slash.apply {
    erosion = 0.25f
    erosionSoftness = 0.08f
    edgeColor.set(1f, 0.5f, 0.15f, 1f)
    edgeIntensity = 6f
    edgeWidth = 0.06f
}

Per Instance Life

Each instance’s color tints the effect with its rgb, and its alpha is the life left: 1 adds no erosion and 0 erodes it fully. Many instances can share one material and still dissolve on their own.

val life = MutableColor(Color.WHITE)

onUpdate {
    life.a = 1f - progress
    node.color(life)
}

Fresnel

fresnelMode fades the effect by the view angle, tightened by fresnelPower. FADE_EDGES softens the silhouette and BRIGHTEN_EDGES leaves a bright rim, such as for auras and shields.

val aura =
    EffectMaterial(device, color = Color(0.2f, 0.4f, 1f, 1f)).apply {
        fresnelMode = EffectFresnelMode.BRIGHTEN_EDGES
        fresnelPower = 2.5f
    }

Blending

EffectBlendMode.ADDITIVE, the default, adds light, such as for slashes, fire, and magic. EffectBlendMode.ALPHA is standard transparency, such as for smoke and dust, and is drawn back to front.

val dust =
    EffectMaterial(
        device,
        maskTexture = ringMask,
        color = Color(0.45f, 0.4f, 0.35f, 0.8f),
        blendMode = EffectBlendMode.ALPHA,
    )

Noise

The noiseTexture defaults to Textures.textureNoise, a 128x128 tiling fbm noise in each color channel. TileableNoise creates others. The same arguments always give the same noise.

val streaks = TileableNoise.createTexture(device, size = 128, frequency = 6, octaves = 4, seed = 7)
val slash = EffectMaterial(device, maskTexture = crescentMask, noiseTexture = streaks)

It can also be sampled on the CPU. fbm repeats every 1 unit.

val n = TileableNoise.fbm(u, v, frequency = 4, octaves = 4)
val tile = TileableNoise.fbm(size = 64) // 64x64 values, row by row

Releasing

The material doesn’t own its textures.

onRelease {
    slash.release()
    streaks.release()
}

Example

See the EffectMaterialExample (-effectMaterial) for dissolving sword swings that share one material, a shockwave ring over an alpha blended dust ring, and a fresnel aura.