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]
102 # Add a particle at `center` with `scale`, living for `ttl` from `time_offset`
104 # `time_offset` is added to the creation time, it can be used to delay the
105 # apparition of a particle using a positive value.
107 # See the doc of the precise class of `program`, or the general `ParticleProgram`
108 # for information on the effect of these parameters.
109 fun add
(center
: Point3d[Float], scale
: Float, ttl
: Float, time_offset
: nullable Float)
111 var i
= total_particles
% n_particles
114 centers
[i
*3 ] = center
.x
115 centers
[i
*3+1] = center
.y
116 centers
[i
*3+2] = center
.z
121 time_offset
= time_offset
or else 0.0
122 ots
[i
] = clock
.total
.to_f
+ time_offset
125 # Draw all particles of this emitter
128 if ots
.is_empty
then return
130 var program
= program
133 var texture
= texture
134 if texture
!= null then
135 glActiveTexture gl_TEXTURE0
136 glBindTexture
(gl_TEXTURE_2D
, texture
.gl_texture
)
137 program
.use_texture
.uniform
true
138 program
.texture
.uniform
0
140 program
.use_texture
.uniform
false
143 program
.scale
.array_enabled
= true
144 program
.scale
.array
(scales
, 1)
146 program
.center
.array_enabled
= true
147 program
.center
.array
(centers
, 3)
149 program
.color
.array_enabled
= false
150 program
.color
.uniform
(1.0, 1.0, 1.0, 1.0)
152 program
.ot
.array_enabled
= true
153 program
.ot
.array
(ots
, 1)
155 program
.ttl
.array_enabled
= true
156 program
.ttl
.array
(ttls
, 1)
158 program
.t
.uniform clock
.total
.to_f
159 program
.mvp
.uniform app
.world_camera
.mvp_matrix
161 glDrawArrays
(gl_POINTS
, 0, ots
.length
)
165 # Particle drawing program using `gl_POINTS`
167 # This program should be subclassed to create custom particle effects.
168 # Either `vertex_shader_source` and `vertex_shader_core` can be refined.
169 class ParticleProgram
170 super GamnitProgramFromSource
172 redef var vertex_shader_source
= """
173 // Coordinates of particle effects
174 attribute vec4 center;
176 // Particles color tint
177 attribute vec4 color;
178 varying vec4 v_color;
180 // Per particle scaling
181 attribute float scale;
183 // Model view projection matrix
186 // Time-to-live of each particle
189 // Creation time of each particle
197 // Pass varyings to the fragment shader
203 // Discard expired or not yet created particles
204 if (dt > ttl || dt < 0.0) {
209 {{{vertex_shader_core}}}
213 # Core GLSL code for `vertex_shader_source`
215 # Refine this function to easily tweak the position, size and color of particles.
217 # Reminder: Each execution of the vertex shader applies to a single particle.
219 # ## Input variables:
220 # * `center`: reference coordinates of the particle effect.
221 # This if often the center of the particle itself,
222 # but it can also be reference coordinates for a moving particle.
223 # * `mvp`: model-view-projection matrix.
224 # * `color`: color tint of the particle.
226 # * `t`: global seconds counter since the creation of this particle emitter.
227 # * `ot`: creation time of the particle, in seconds, in reference to `t`.
228 # * `dt`: seconds since creation of the particle.
229 # * `ttl`: time-to-live of the particle, in seconds.
230 # * `pt`: advancement of this particle in its lifetime, in `[0.0 .. 1.0]`.
232 # ## Output variables:
233 # * `gl_Position`: position of the particle in camera coordinates.
234 # * `gl_PointSize`: size of the particle in camera coordinates.
235 # Set to `0.0` to discard the particle.
236 # * `v_color`: tint applied to the particle.
237 # Assigned by default to the value of `color`.
239 # ## Reference implementation
241 # The default implementation apply the model-view-projection matrix on the position
242 # and scales according to the distance from the camera.
243 # Most particle effects should apply the same base logic as the default implementation.
244 # Here it is for reference:
247 # gl_Position = center * mvp;
248 # gl_PointSize = scale / gl_Position.z;
250 fun vertex_shader_core
: String do return """
251 gl_Position = center * mvp;
252 gl_PointSize = scale / gl_Position.z;
255 redef var fragment_shader_source
= """
256 precision mediump float;
258 // Input from the vertex shader
259 varying vec4 v_color;
261 // Does this particle use a texture?
262 uniform bool use_texture;
264 // Texture to apply on this particle
265 uniform sampler2D texture0;
270 gl_FragColor = texture2D(texture0, gl_PointCoord) * v_color;
272 gl_FragColor = v_color;
275 """ @ glsl_fragment_shader
277 # Coordinates of particle effects
278 var center
= attributes
["center"].as(AttributeVec4) is lazy
280 # Should this program use the texture `texture`?
281 var use_texture
= uniforms
["use_texture"].as(UniformBool) is lazy
283 # Visible texture unit
284 var texture
= uniforms
["texture0"].as(UniformSampler2D) is lazy
286 # Color tint per vertex
287 var color
= attributes
["color"].as(AttributeVec4) is lazy
290 var scale
= attributes
["scale"].as(AttributeFloat) is lazy
292 # Model view projection matrix
293 var mvp
= uniforms
["mvp"].as(UniformMat4) is lazy
295 # Creation time of each particle
296 var ot
= attributes
["ot"].as(AttributeFloat) is lazy
299 var t
= uniforms
["t"].as(UniformFloat) is lazy
301 # Time-to-live of each particle
302 var ttl
= attributes
["ttl"].as(AttributeFloat) is lazy
305 # Graphics program to display blowing up particles
306 class ExplosionProgram
307 super ParticleProgram
309 redef fun vertex_shader_core
do return """
310 gl_Position = center * mvp;
311 gl_PointSize = scale / gl_Position.z * pt;
313 if (pt > 0.8) v_color *= (1.0-pt)/0.2;
317 # Graphics program to display particles slowly drifting upwards
319 super ParticleProgram
321 redef fun vertex_shader_core
do return """
326 gl_Position = c * mvp;
327 gl_PointSize = scale / gl_Position.z * (pt+0.1);
332 v_color *= 1.0 - pt*0.9;