1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 # Particles are managed by instances of `ParticleSystem` that
18 # are configured for a specific kind of particle.
19 # For instance, a particle system can be created for a max of 100 particles,
20 # with a smoke effect and a precise texture, as in:
23 # var smoke = new ParticleSystem(100, app.smoke_program, new Texture("smoke.png"))
26 # The system must be registered in `app.particle_systems` to be drawn on screen.
28 # Particles are added to a system with their configuration, as in:
31 # var position = new Point3d[Float](0.0, 0.0, 0.0)
33 # var time_to_live = 1.0 # in seconds
34 # smoke.add(position, scale, time_to_live)
42 # Graphics program to display static non-moving particles
43 var static_program
= new ParticleProgram
45 # Graphics program to display blowing up particles
46 var explosion_program
= new ExplosionProgram
48 # Graphics program to display particles slowly drifting upwards
49 var smoke_program
= new SmokeProgram
51 # Enabled particle emitters
53 # To be populated by the client program.
54 var particle_systems
= new Array[ParticleSystem]
57 # Particle system using `program` and `texture` to draw each particles
59 # Each instance draws a maximum of `n_particles`.
60 # If full, new particles replace the oldest particles.
61 # Expired particle are still sent to the CPU but should be discarded by the vertex shader.
64 # Maximum number of particles
67 private var total_particles
= 0
69 # Program to draw the particles
70 var program
: ParticleProgram
72 # Texture to apply on particles, if any
73 var texture
: nullable Texture
75 # Clock used to set `ots` and `program::t`
77 # TODO control this value from the game logic to allow pausing and slowing time.
78 private var clock
= new Clock
80 # Coordinates of each particle effects
81 private var centers
= new Array[Float]
83 # Creation time of each particles
84 private var ots
= new Array[Float]
86 # Scale of each particles
87 private var scales
= new Array[Float]
89 # Time-to-live of each particle
90 private var ttls
= new Array[Float]
92 # Add a particle at `center` with `scale`, living for `ttl` from `time_offset`
94 # `time_offset` is added to the creation time, it can be used to delay the
95 # apparition of a particle using a positive value.
97 # See the doc of the precise class of `program`, or the general `ParticleProgram`
98 # for information on the effect of these parameters.
99 fun add
(center
: Point3d[Float], scale
: Float, ttl
: Float, time_offset
: nullable Float)
101 var i
= total_particles
% n_particles
104 centers
[i
*3 ] = center
.x
105 centers
[i
*3+1] = center
.y
106 centers
[i
*3+2] = center
.z
111 time_offset
= time_offset
or else 0.0
112 ots
[i
] = clock
.total
.to_f
+ time_offset
115 # Draw all particles of this emitter
118 if ots
.is_empty
then return
120 var program
= program
123 var texture
= texture
124 if texture
!= null then
125 glActiveTexture gl_TEXTURE0
126 glBindTexture
(gl_TEXTURE_2D
, texture
.gl_texture
)
127 program
.use_texture
.uniform
true
128 program
.texture
.uniform
0
130 program
.use_texture
.uniform
false
133 program
.scale
.array_enabled
= true
134 program
.scale
.array
(scales
, 1)
136 program
.center
.array_enabled
= true
137 program
.center
.array
(centers
, 3)
139 program
.color
.array_enabled
= false
140 program
.color
.uniform
(1.0, 1.0, 1.0, 1.0)
142 program
.ot
.array_enabled
= true
143 program
.ot
.array
(ots
, 1)
145 program
.ttl
.array_enabled
= true
146 program
.ttl
.array
(ttls
, 1)
148 program
.t
.uniform clock
.total
.to_f
149 program
.mvp
.uniform app
.world_camera
.mvp_matrix
151 glDrawArrays
(gl_POINTS
, 0, ots
.length
)
155 # Particle drawing program using `gl_POINTS`
157 # This program should be subclassed to create custom particle effects.
158 # Either `vertex_shader_source` and `vertex_shader_core` can be refined.
159 class ParticleProgram
160 super GamnitProgramFromSource
162 redef var vertex_shader_source
= """
163 // Coordinates of particle effects
164 attribute vec4 center;
166 // Particles color tint
167 attribute vec4 color;
168 varying vec4 v_color;
170 // Per particle scaling
171 attribute float scale;
173 // Model view projection matrix
176 // Time-to-live of each particle
179 // Creation time of each particle
187 // Pass varyings to the fragment shader
193 // Discard expired or not yet created particles
194 if (dt > ttl || dt < 0.0) {
199 {{{vertex_shader_core}}}
203 # Core GLSL code for `vertex_shader_source`
205 # Refine this function to easily tweak the position, size and color of particles.
207 # Reminder: Each execution of the vertex shader applies to a single particle.
209 # ## Input variables:
210 # * `center`: reference coordinates of the particle effect.
211 # This if often the center of the particle itself,
212 # but it can also be reference coordinates for a moving particle.
213 # * `mvp`: model-view-projection matrix.
214 # * `color`: color tint of the particle.
216 # * `t`: global seconds counter since the creation of this particle emitter.
217 # * `ot`: creation time of the particle, in seconds, in reference to `t`.
218 # * `dt`: seconds since creation of the particle.
219 # * `ttl`: time-to-live of the particle, in seconds.
220 # * `pt`: advancement of this particle in its lifetime, in `[0.0 .. 1.0]`.
222 # ## Output variables:
223 # * `gl_Position`: position of the particle in camera coordinates.
224 # * `gl_PointSize`: size of the particle in camera coordinates.
225 # Set to `0.0` to discard the particle.
226 # * `v_color`: tint applied to the particle.
227 # Assigned by default to the value of `color`.
229 # ## Reference implementation
231 # The default implementation apply the model-view-projection matrix on the position
232 # and scales according to the distance from the camera.
233 # Most particle effects should apply the same base logic as the default implementation.
234 # Here it is for reference:
237 # gl_Position = center * mvp;
238 # gl_PointSize = scale / gl_Position.z;
240 fun vertex_shader_core
: String do return """
241 gl_Position = center * mvp;
242 gl_PointSize = scale / gl_Position.z;
245 redef var fragment_shader_source
= """
246 precision mediump float;
248 // Input from the vertex shader
249 varying vec4 v_color;
251 // Does this particle use a texture?
252 uniform bool use_texture;
254 // Texture to apply on this particle
255 uniform sampler2D texture;
260 gl_FragColor = texture2D(texture, gl_PointCoord) * v_color;
261 if (gl_FragColor.a <= 0.01) discard;
263 gl_FragColor = v_color;
266 """ @ glsl_fragment_shader
268 # Coordinates of particle effects
269 var center
= attributes
["center"].as(AttributeVec4) is lazy
271 # Should this program use the texture `texture`?
272 var use_texture
= uniforms
["use_texture"].as(UniformBool) is lazy
274 # Visible texture unit
275 var texture
= uniforms
["texture"].as(UniformSampler2D) is lazy
277 # Color tint per vertex
278 var color
= attributes
["color"].as(AttributeVec4) is lazy
281 var scale
= attributes
["scale"].as(AttributeFloat) is lazy
283 # Model view projection matrix
284 var mvp
= uniforms
["mvp"].as(UniformMat4) is lazy
286 # Creation time of each particle
287 var ot
= attributes
["ot"].as(AttributeFloat) is lazy
290 var t
= uniforms
["t"].as(UniformFloat) is lazy
292 # Time-to-live of each particle
293 var ttl
= attributes
["ttl"].as(AttributeFloat) is lazy
296 # Graphics program to display blowing up particles
297 class ExplosionProgram
298 super ParticleProgram
300 redef fun vertex_shader_core
do return """
301 gl_Position = center * mvp;
302 gl_PointSize = scale / gl_Position.z * pt;
304 if (pt > 0.8) v_color.a = (1.0-pt)/0.2;
308 # Graphics program to display particles slowly drifting upwards
310 super ParticleProgram
312 redef fun vertex_shader_core
do return """
317 gl_Position = c * mvp;
318 gl_PointSize = scale / gl_Position.z * (pt+0.1);
321 v_color.a = pt / 0.1;
323 v_color.a = 1.0 - pt*0.9;