-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrainsplash.cpp
69 lines (52 loc) · 1.77 KB
/
rainsplash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "rainsplash.h"
#include "texture.h"
#include "texturecache.h"
#include "particlematerial.h"
#include "terrain.h"
#include "drawcontext.h"
#include "camera.h"
RainSplash::RainSplash(float radius, bool mist, Terrain* terrain)
{
radius_ = radius;
terrain_ = terrain;
Texture* texture = NULL;
ParticleMaterial* material = static_cast<ParticleMaterial *>(material_);
if (mist) {
texture = TextureCache::getInstance()->acquire("rainmist", TextureType::Texture2D);
material->setHorizontal(false);
}
else {
texture = TextureCache::getInstance()->acquire("raindrop", TextureType::Texture2D);
material->setHorizontal(true);
}
setParticleTexture(texture);
setEmissionRate(8000);
setMaxParticleCount(40000);
}
void RainSplash::spawnParticle(Particle *particle)
{
float phi = randf(0, M_PI * 2);
float r = radius_ * randf(0, 1.0);
float x = cosf(phi) * r + cameraPosition_.x();
float z = sinf(phi) * r + cameraPosition_.z();
float y = terrain_->height(x, z) + 0.1;
particle->position = {x, y, z};
particle->maxLife = randf(0.4, 0.6);
particle->size = startSize_ = randf(0.2, 0.3);
float c = randf(0.6, 0.9);
particle->color = {c, c, c};
particle->opacity = startOpacity_ = randf(0.6, 0.8);
startSize_ = 0.2;
}
void RainSplash::updateParticle(Particle &particle, float dt)
{
float x = particle.life / particle.maxLife;
particle.size = linear_interpolate(startSize_, startSize_ + 0.15, x);
particle.opacity = startOpacity_ * (1 - x);
}
void RainSplash::renderGeometry(DrawContext &context)
{
ParticleSystem::renderGeometry(context);
transform().setToIdentity();
cameraPosition_ = context.camera->position();
}