Particle effects

Particles are managed by instances of ParticleSystem that are configured for a specific kind of particle. For instance, a particle system can be created for a max of 100 particles, with a smoke effect and a precise texture, as in:

var smoke = new ParticleSystem(100, app.smoke_program, new Texture("smoke.png"))

The system must be registered in app.particle_systems to be drawn on screen.

Particles are added to a system with their configuration, as in:

var position = new Point3d[Float](0.0, 0.0, 0.0)
var scale = 2.0
var time_to_live = 1.0 # in seconds
smoke.add(position, scale, time_to_live)

Introduced classes

class ExplosionProgram

gamnit :: ExplosionProgram

Graphics program to display blowing up particles
class ParticleProgram

gamnit :: ParticleProgram

Particle drawing program using gl_POINTS
class ParticleSystem

gamnit :: ParticleSystem

Particle system using program and texture to draw each particles
class SmokeProgram

gamnit :: SmokeProgram

Graphics program to display particles slowly drifting upwards

Redefined classes

redef class App

gamnit :: particles $ App

App subclasses are cross-platform applications

All class definitions

redef class App

gamnit :: particles $ App

App subclasses are cross-platform applications
class ExplosionProgram

gamnit $ ExplosionProgram

Graphics program to display blowing up particles
class ParticleProgram

gamnit $ ParticleProgram

Particle drawing program using gl_POINTS
class ParticleSystem

gamnit $ ParticleSystem

Particle system using program and texture to draw each particles
class SmokeProgram

gamnit $ SmokeProgram

Graphics program to display particles slowly drifting upwards
package_diagram gamnit::particles particles gamnit::depth_core depth_core gamnit::particles->gamnit::depth_core gamnit::flat_core flat_core gamnit::depth_core->gamnit::flat_core ...gamnit::flat_core ... ...gamnit::flat_core->gamnit::flat_core gamnit::depth depth gamnit::depth->gamnit::particles gamnit::cardboard cardboard gamnit::cardboard->gamnit::depth gamnit::stereoscopic_view stereoscopic_view gamnit::stereoscopic_view->gamnit::depth gamnit::cardboard... ... gamnit::cardboard...->gamnit::cardboard gamnit::stereoscopic_view... ... gamnit::stereoscopic_view...->gamnit::stereoscopic_view

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module angles

geometry :: angles

Angle related service using Float to represent an angle in radians
module app

app :: app

app.nit is a framework to create cross-platform applications
module app_base

app :: app_base

Base of the app.nit framework, defines App
module array

core :: array

This module introduces the standard array structure.
module assets

app :: assets

Portable services to load resources from the assets folder
module aware

android :: aware

Android compatibility module
module bitset

core :: bitset

Services to handle BitSet
module boxes

geometry :: boxes

Provides interfaces and classes to represent basic geometry needs.
module bytes

core :: bytes

Services for byte streams and arrays
module c

c :: c

Structures and services for compatibility with the C language
module caching

serialization :: caching

Services for caching serialization engines
module cameras

gamnit :: cameras

Camera services producing Model-View-Projection matrices
module cameras_cache

gamnit :: cameras_cache

Cache the Matrix produced by Camera::mvp_matrix
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module display

gamnit :: display

Abstract display services
module dynamic_resolution

gamnit :: dynamic_resolution

Virtual screen with a resolution independent from the real screen
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module flat_core

gamnit :: flat_core

Core services for the flat API for 2D games
module gamnit

gamnit :: gamnit

Game and multimedia framework for Nit
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module geometry

geometry :: geometry

Provides interfaces and classes to represent basic geometry needs.
module glesv2

glesv2 :: glesv2

OpenGL graphics rendering library for embedded systems, version 2.0
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module input

mnit :: input

Defines abstract classes for user and general inputs to the application.
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module matrix

matrix :: matrix

Services for matrices of Float values
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module performance_analysis

performance_analysis :: performance_analysis

Services to gather information on the performance of events by categories
module points_and_lines

geometry :: points_and_lines

Interfaces and classes to represent basic geometry needs.
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module programs

gamnit :: programs

Services for graphical programs with shaders, attributes and uniforms
module projection

matrix :: projection

Services on Matrix to transform and project 3D coordinates
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module realtime

realtime :: realtime

Services to keep time of the wall clock time
module ropes

core :: ropes

Tree-based representation of a String.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module textures

gamnit :: textures

Load textures, create subtextures and manage their life-cycle
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module depth_core

gamnit :: depth_core

Base entities of the depth 3D game framework

Children

module depth

gamnit :: depth

Framework for 3D games in Nit

Descendants

module a_star-m

a_star-m

module cardboard

gamnit :: cardboard

Update the orientation of world_camera at each frame using the head position given by android::cardboard
module stereoscopic_view

gamnit :: stereoscopic_view

Refine EulerCamera and App::frame_core_draw to get a stereoscopic view
module vr

gamnit :: vr

VR support for gamnit depth, for Android only
# Particle effects
#
# Particles are managed by instances of `ParticleSystem` that
# are configured for a specific kind of particle.
# For instance, a particle system can be created for a max of 100 particles,
# with a smoke effect and a precise texture, as in:
#
# ~~~
# var smoke = new ParticleSystem(100, app.smoke_program, new Texture("smoke.png"))
# ~~~
#
# The system must be registered in `app.particle_systems` to be drawn on screen.
#
# Particles are added to a system with their configuration, as in:
#
# ~~~
# var position = new Point3d[Float](0.0, 0.0, 0.0)
# var scale = 2.0
# var time_to_live = 1.0 # in seconds
# smoke.add(position, scale, time_to_live)
# ~~~
module particles

import depth_core

redef class App

	# Graphics program to display static non-moving particles
	var static_program = new ParticleProgram

	# Graphics program to display blowing up particles
	var explosion_program = new ExplosionProgram

	# Graphics program to display particles slowly drifting upwards
	var smoke_program = new SmokeProgram

	# Enabled particle emitters
	#
	# To be populated by the client program.
	var particle_systems = new Array[ParticleSystem]
end

# Particle system using `program` and `texture` to draw each particles
#
# Each instance draws a maximum of `n_particles`.
# If full, new particles replace the oldest particles.
# Expired particle are still sent to the CPU but should be discarded by the vertex shader.
class ParticleSystem

	# Maximum number of particles
	var n_particles: Int

	private var total_particles = 0

	# Program to draw the particles
	var program: ParticleProgram

	# Texture to apply on particles, if any
	var texture: nullable Texture

	# Clock used to set `ots` and `program::t`
	#
	# TODO control this value from the game logic to allow pausing and slowing time.
	private var clock = new Clock

	# Coordinates of each particle effects
	private var centers = new Array[Float]

	# Creation time of each particles
	private var ots = new Array[Float]

	# Scale of each particles
	private var scales = new Array[Float]

	# Time-to-live of each particle
	private var ttls = new Array[Float]

	# Clear all particles
	fun clear
	do
		centers.clear
		ots.clear
		scales.clear
		ttls.clear
		total_particles = 0
	end

	# Add a particle at `center` with `scale`, living for `ttl` from `time_offset`
	#
	# `time_offset` is added to the creation time, it can be used to delay the
	# apparition of a particle using a positive value.
	#
	# See the doc of the precise class of `program`, or the general `ParticleProgram`
	# for information on the effect of these parameters.
	fun add(center: Point3d[Float], scale: Float, ttl: Float, time_offset: nullable Float)
	do
		var i = total_particles % n_particles
		total_particles += 1

		centers[i*3  ] = center.x
		centers[i*3+1] = center.y
		centers[i*3+2] = center.z

		ttls[i] = ttl
		scales[i] = scale

		time_offset = time_offset or else 0.0
		ots[i] = clock.total.to_f + time_offset
	end

	# Draw all particles of this emitter
	fun draw
	do
		if ots.is_empty then return

		var program = program
		program.use

		var texture = texture
		if texture != null then
			glActiveTexture gl_TEXTURE0
			glBindTexture(gl_TEXTURE_2D, texture.gl_texture)
			program.use_texture.uniform true
			program.texture.uniform 0
		else
			program.use_texture.uniform false
		end

		program.scale.array_enabled = true
		program.scale.array(scales, 1)

		program.center.array_enabled = true
		program.center.array(centers, 3)

		program.color.array_enabled = false
		program.color.uniform(1.0, 1.0, 1.0, 1.0)

		program.ot.array_enabled = true
		program.ot.array(ots, 1)

		program.ttl.array_enabled = true
		program.ttl.array(ttls, 1)

		program.t.uniform clock.total.to_f
		program.mvp.uniform app.world_camera.mvp_matrix

		glDrawArrays(gl_POINTS, 0, ots.length)
	end
end

# Particle drawing program using `gl_POINTS`
#
# This program should be subclassed to create custom particle effects.
# Either `vertex_shader_source` and `vertex_shader_core` can be refined.
class ParticleProgram
	super GamnitProgramFromSource

	redef var vertex_shader_source = """
		// Coordinates of particle effects
		attribute vec4 center;

		// Particles color tint
		attribute vec4 color;
		varying vec4 v_color;

		// Per particle scaling
		attribute float scale;

		// Model view projection matrix
		uniform mat4 mvp;

		// Time-to-live of each particle
		attribute float ttl;

		// Creation time of each particle
		attribute float ot;

		// Current time
		uniform float t;

		void main()
		{
			// Pass varyings to the fragment shader
			v_color = color;

			float dt = t - ot;
			float pt = dt/ttl;

			// Discard expired or not yet created particles
			if (dt > ttl || dt < 0.0) {
				gl_PointSize = 0.0;
				return;
			}

			{{{vertex_shader_core}}}
		}
		"""

	# Core GLSL code for `vertex_shader_source`
	#
	# Refine this function to easily tweak the position, size and color of particles.
	#
	# Reminder: Each execution of the vertex shader applies to a single particle.
	#
	# ## Input variables:
	# * `center`: reference coordinates of the particle effect.
	#   This if often the center of the particle itself,
	#   but it can also be reference coordinates for a moving particle.
	# * `mvp`: model-view-projection matrix.
	# * `color`: color tint of the particle.
	#
	# * `t`: global seconds counter since the creation of this particle emitter.
	# * `ot`: creation time of the particle, in seconds, in reference to `t`.
	# * `dt`: seconds since creation of the particle.
	# * `ttl`: time-to-live of the particle, in seconds.
	# * `pt`: advancement of this particle in its lifetime, in `[0.0 .. 1.0]`.
	#
	# ## Output variables:
	# * `gl_Position`: position of the particle in camera coordinates.
	# * `gl_PointSize`: size of the particle in camera coordinates.
	#   Set to `0.0` to discard the particle.
	# * `v_color`: tint applied to the particle.
	#   Assigned by default to the value of `color`.
	#
	# ## Reference implementation
	#
	# The default implementation apply the model-view-projection matrix on the position
	# and scales according to the distance from the camera.
	# Most particle effects should apply the same base logic as the default implementation.
	# Here it is for reference:
	#
	# ~~~glsl
	# gl_Position = center * mvp;
	# gl_PointSize = scale / gl_Position.z;
	# ~~~
	fun vertex_shader_core: String do return """
			gl_Position = center * mvp;
			gl_PointSize = scale / gl_Position.z;
	"""

	redef var fragment_shader_source = """
		precision mediump float;

		// Input from the vertex shader
		varying vec4 v_color;

		// Does this particle use a texture?
		uniform bool use_texture;

		// Texture to apply on this particle
		uniform sampler2D texture0;

		void main()
		{
			if (use_texture) {
				gl_FragColor = texture2D(texture0, gl_PointCoord) * v_color;
			} else {
				gl_FragColor = v_color;
			}
		}
		""" @ glsl_fragment_shader

	# Coordinates of particle effects
	var center = attributes["center"].as(AttributeVec4) is lazy

	# Should this program use the texture `texture`?
	var use_texture = uniforms["use_texture"].as(UniformBool) is lazy

	# Visible texture unit
	var texture = uniforms["texture0"].as(UniformSampler2D) is lazy

	# Color tint per vertex
	var color = attributes["color"].as(AttributeVec4) is lazy

	# Scaling per vertex
	var scale = attributes["scale"].as(AttributeFloat) is lazy

	# Model view projection matrix
	var mvp = uniforms["mvp"].as(UniformMat4) is lazy

	# Creation time of each particle
	var ot = attributes["ot"].as(AttributeFloat) is lazy

	# Current time
	var t = uniforms["t"].as(UniformFloat) is lazy

	# Time-to-live of each particle
	var ttl = attributes["ttl"].as(AttributeFloat) is lazy
end

# Graphics program to display blowing up particles
class ExplosionProgram
	super ParticleProgram

	redef fun vertex_shader_core do return """
		gl_Position = center * mvp;
		gl_PointSize = scale / gl_Position.z * pt;

		if (pt > 0.8) v_color *= (1.0-pt)/0.2;
	"""
end

# Graphics program to display particles slowly drifting upwards
class SmokeProgram
	super ParticleProgram

	redef fun vertex_shader_core do return """
		vec4 c = center;
		c.y += dt * 1.0;
		c.x += dt * 0.1;

		gl_Position = c * mvp;
		gl_PointSize = scale / gl_Position.z * (pt+0.1);

		if (pt < 0.1)
			v_color *= pt / 0.1;
		else
			v_color *= 1.0 - pt*0.9;
	"""
end
lib/gamnit/depth/particles.nit:15,1--334,3