diff options
author | Matt Ebb <matt@mke3.net> | 2008-10-06 16:25:22 +0400 |
---|---|---|
committer | Matt Ebb <matt@mke3.net> | 2008-10-06 16:25:22 +0400 |
commit | 25ece3ba2f60ec4271828bb4ab1c083c6fa36761 (patch) | |
tree | 53124d534e2714095a6f214cced5cc04675c1f0a | |
parent | 67a9d4154d520a98e572e98f680995fca701f728 (diff) |
* New point density update: Turbulence
This addition allows you to perturb the point density with noise, to give
the impression of more resolution. It's a quick way to add detail, without
having to use large, complex, and slower to render particle systems.
Rather than just overlaying noise, like you might do by adding a secondary
clouds texture, it uses noise to perturb the actual coordinate looked up
in the density evaluation. This gives a much better looking result, as it
actually alters the original density.
Comparison of the particle cloud render without, and with added turbulence
(the render with turbulence only renders slightly more slowly):
http://mke3.net/blender/devel/rendering/volumetrics/pd_turbulence.jpg
Using the same constant noise function/spatial coordinates will give a
static appearance. This is fine (and quicker) if the particles aren't
moving, but on animated particle systems, it looks bad, as if the
particles are moving through a static noise field. To overcome this, there
are additional options for particle systems, to influence the turbulence
with the particles' average velocity, or average angular velocity. This
information is only available for particle systems at the present.
Here you can see the (dramatic) difference between no turbulence, static
turbulence, and turbulence influenced by particle velocity:
http://mke3.net/blender/devel/rendering/volumetrics/turbu_compare.mov
-rw-r--r-- | source/blender/blenkernel/intern/texture.c | 4 | ||||
-rw-r--r-- | source/blender/blenloader/intern/readfile.c | 13 | ||||
-rw-r--r-- | source/blender/makesdna/DNA_texture_types.h | 18 | ||||
-rw-r--r-- | source/blender/render/intern/source/pointdensity.c | 83 | ||||
-rw-r--r-- | source/blender/src/buttons_shading.c | 34 |
5 files changed, 131 insertions, 21 deletions
diff --git a/source/blender/blenkernel/intern/texture.c b/source/blender/blenkernel/intern/texture.c index 7b558008a92..33b824623fa 100644 --- a/source/blender/blenkernel/intern/texture.c +++ b/source/blender/blenkernel/intern/texture.c @@ -878,6 +878,10 @@ PointDensity *BKE_add_pointdensity(void) pd->source = TEX_PD_PSYS; pd->point_tree = NULL; pd->point_data = NULL; + pd->noise_size = 0.5f; + pd->noise_depth = 1; + pd->noise_fac = 1.0f; + pd->noise_influence = TEX_PD_NOISE_STATIC; return pd; } diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index 568513650e0..249459bf3a4 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -7882,6 +7882,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main) if (main->versionfile <= 247) { Material *ma; + Tex *tex; for(ma=main->mat.first; ma; ma= ma->id.next) { /* trigger for non-volumetric file */ @@ -7893,6 +7894,18 @@ static void do_versions(FileData *fd, Library *lib, Main *main) ma->vol_absorption_col[0] = ma->vol_absorption_col[1] = ma->vol_absorption_col[2] = 0.0f; } } + + for(tex=main->tex.first; tex; tex= tex->id.next) { + if (tex->pd == NULL) + tex->pd = BKE_add_pointdensity(); + else if (tex->pd->noise_size < 0.0001f) { + tex->pd->noise_size = 0.5f; + tex->pd->noise_depth = 1; + tex->pd->noise_fac = 1.0f; + tex->pd->noise_influence = TEX_PD_NOISE_STATIC; + } + } + } /* set the curve radius interpolation to 2.47 default - easy */ diff --git a/source/blender/makesdna/DNA_texture_types.h b/source/blender/makesdna/DNA_texture_types.h index 95bacbbd4c6..95f61e73c79 100644 --- a/source/blender/makesdna/DNA_texture_types.h +++ b/source/blender/makesdna/DNA_texture_types.h @@ -144,8 +144,13 @@ typedef struct PointDensity { short pdpad2; void *point_tree; /* the acceleration tree containing points */ - void *point_data; /* dynamically allocated extra for extra information, like particle age */ - int pdpad3[2]; + float *point_data; /* dynamically allocated extra for extra information, like particle age */ + + float noise_size; + short noise_depth; + short noise_influence; + float noise_fac; + float pdpad4; } PointDensity; @@ -429,6 +434,15 @@ typedef struct TexMapping { #define TEX_PD_OBJECTSPACE 1 #define TEX_PD_WORLDSPACE 2 +/* flag */ +#define TEX_PD_TURBULENCE 1 + + +/* noise_influence */ +#define TEX_PD_NOISE_STATIC 0 +#define TEX_PD_NOISE_VEL 1 +#define TEX_PD_NOISE_ANGVEL 2 +#define TEX_PD_NOISE_TIME 3 #endif diff --git a/source/blender/render/intern/source/pointdensity.c b/source/blender/render/intern/source/pointdensity.c index 7998fa82836..e7e2d4fc26e 100644 --- a/source/blender/render/intern/source/pointdensity.c +++ b/source/blender/render/intern/source/pointdensity.c @@ -30,6 +30,7 @@ #include "MEM_guardedalloc.h" #include "BLI_arithb.h" +#include "BLI_blenlib.h" #include "BLI_kdopbvh.h" #include "BKE_DerivedMesh.h" @@ -45,13 +46,13 @@ #include "renderdatabase.h" #include "texture.h" - static void pointdensity_cache_psys(Render *re, PointDensity *pd, Object *ob, ParticleSystem *psys) { DerivedMesh* dm; ParticleKey state; float cfra=bsystem_time(ob,(float)G.scene->r.cfra,0.0); int i, childexists; + int total_particles; float partco[3]; float obview[4][4]; @@ -74,12 +75,16 @@ static void pointdensity_cache_psys(Render *re, PointDensity *pd, Object *ob, Pa /* in case ob->imat isn't up-to-date */ Mat4Invert(ob->imat, ob->obmat); - pd->point_tree = BLI_bvhtree_new(psys->totpart+psys->totchild, 0.0, 2, 6); + total_particles = psys->totpart+psys->totchild; + + pd->point_tree = BLI_bvhtree_new(total_particles, 0.0, 2, 6); + if (pd->noise_influence != TEX_PD_NOISE_STATIC) + pd->point_data = MEM_mallocN(sizeof(float)*3*total_particles, "point_data"); if (psys->totchild > 0 && !(psys->part->draw & PART_DRAW_PARENT)) childexists = 1; - for (i = 0; i < psys->totpart + psys->totchild; i++) { + for (i = 0; i < total_particles; i++) { state.time = cfra; if(psys_get_particle_state(ob, psys, i, &state, 0)) { @@ -97,6 +102,16 @@ static void pointdensity_cache_psys(Render *re, PointDensity *pd, Object *ob, Pa } BLI_bvhtree_insert(pd->point_tree, i, partco, 1); + + if (pd->noise_influence == TEX_PD_NOISE_VEL) { + pd->point_data[i*3 + 0] = state.vel[0]; + pd->point_data[i*3 + 1] = state.vel[1]; + pd->point_data[i*3 + 2] = state.vel[2]; + } else if (pd->noise_influence == TEX_PD_NOISE_ANGVEL) { + pd->point_data[i*3 + 0] = state.ave[0]; + pd->point_data[i*3 + 1] = state.ave[1]; + pd->point_data[i*3 + 2] = state.ave[2]; + } } } @@ -131,7 +146,6 @@ static void pointdensity_cache_object(Render *re, PointDensity *pd, ObjectRen *o VecSubf(ver_co, ver_co, obr->ob->loc); } else { /* TEX_PD_WORLDSPACE */ - } BLI_bvhtree_insert(pd->point_tree, i, ver_co, 1); @@ -232,6 +246,7 @@ typedef struct PointDensityRangeData float *density; float squared_radius; float *point_data; + float *vec; short falloff_type; } PointDensityRangeData; @@ -239,17 +254,26 @@ void accum_density(void *userdata, int index, float squared_dist) { PointDensityRangeData *pdr = (PointDensityRangeData *)userdata; const float dist = pdr->squared_radius - squared_dist; + float density; if (pdr->falloff_type == TEX_PD_FALLOFF_STD) - *pdr->density += dist; + density = dist; else if (pdr->falloff_type == TEX_PD_FALLOFF_SMOOTH) - *pdr->density+= 3.0f*dist*dist - 2.0f*dist*dist*dist; + density = 3.0f*dist*dist - 2.0f*dist*dist*dist; else if (pdr->falloff_type == TEX_PD_FALLOFF_SHARP) - *pdr->density+= dist*dist; + density = dist*dist; else if (pdr->falloff_type == TEX_PD_FALLOFF_CONSTANT) - *pdr->density+= pdr->squared_radius; + density = pdr->squared_radius; else if (pdr->falloff_type == TEX_PD_FALLOFF_ROOT) - *pdr->density+= sqrt(dist); + density = sqrt(dist); + + if (pdr->point_data) { + pdr->vec[0] += pdr->point_data[index*3 + 0];// * density; + pdr->vec[1] += pdr->point_data[index*3 + 1];// * density; + pdr->vec[2] += pdr->point_data[index*3 + 2];// * density; + } + + *pdr->density += density; } #define MAX_POINTS_NEAREST 25 @@ -259,6 +283,9 @@ int pointdensitytex(Tex *tex, float *texvec, TexResult *texres) PointDensity *pd = tex->pd; PointDensityRangeData pdr; float density=0.0f; + float vec[3] = {0.0, 0.0, 0.0}; + float tv[3]; + float turb; if ((!pd) || (!pd->point_tree)) { texres->tin = 0.0f; @@ -269,16 +296,40 @@ int pointdensitytex(Tex *tex, float *texvec, TexResult *texres) pdr.density = &density; pdr.point_data = pd->point_data; pdr.falloff_type = pd->falloff_type; + pdr.vec = vec; - BLI_bvhtree_range_query(pd->point_tree, texvec, pd->radius, accum_density, &pdr); + if (pd->flag & TEX_PD_TURBULENCE) { + VECCOPY(tv, texvec); + + /* find the average speed vectors, for perturbing final density lookup with */ + BLI_bvhtree_range_query(pd->point_tree, texvec, pd->radius, accum_density, &pdr); + + density = 0.0f; + Normalize(vec); + turb = BLI_turbulence(pd->noise_size, texvec[0]+vec[0], texvec[1]+vec[1], texvec[2]+vec[2], pd->noise_depth); + + turb -= 0.5f; /* re-center 0.0-1.0 range around 0 to prevent offsetting result */ + + tv[0] = texvec[0] + pd->noise_fac * turb; + tv[1] = texvec[1] + pd->noise_fac * turb; + tv[2] = texvec[2] + pd->noise_fac * turb; + + /* do density lookup with altered coordinates */ + BLI_bvhtree_range_query(pd->point_tree, tv, pd->radius, accum_density, &pdr); + } + else + BLI_bvhtree_range_query(pd->point_tree, texvec, pd->radius, accum_density, &pdr); + texres->tin = density; - /* - texres->tr = 1.0f; - texres->tg = 1.0f; - texres->tb = 0.0f; + //texres->tr = vec[0]; + //texres->tg = vec[1]; + //texres->tb = vec[2]; + return TEX_INT; + + /* BRICONTRGB; texres->ta = 1.0; @@ -288,7 +339,7 @@ int pointdensitytex(Tex *tex, float *texvec, TexResult *texres) } */ - BRICONT; + //BRICONT; - return rv; + //return rv; } diff --git a/source/blender/src/buttons_shading.c b/source/blender/src/buttons_shading.c index ec5419c1704..b6c3e39bac2 100644 --- a/source/blender/src/buttons_shading.c +++ b/source/blender/src/buttons_shading.c @@ -759,15 +759,42 @@ static void texture_panel_pointdensity(Tex *tex) uiDefBut(block, LABEL, B_NOP, "Falloff:", X2CLM1, yco-=BUTH, BUTW2, BUTH, 0, 0, 0, 0, 0, ""); uiDefButS(block, MENU, B_REDR, "Standard %x0|Smooth %x1|Sharp %x2|Constant %x3|Root %x4", - X2CLM1, yco-=BUTH, BUTW2, BUTH, &pd->falloff_type, 0.0, 0.0, 0, 0, "Falloff type"); + X2CLM1, yco-=BUTH, BUTW2, BUTH, &pd->falloff_type, 0.0, 0.0, 0, 0, "Falloff type"); + yco -= YSPACE; + + + uiBlockBeginAlign(block); + uiDefButBitS(block, TOG, TEX_PD_TURBULENCE, B_REDR, "Turbulence", + X2CLM1, yco-=BUTH, BUTW2, BUTH, &(pd->flag), 0, 0, 0, 0, "Add directed turbulence to the density estimation"); + + if (pd->flag & TEX_PD_TURBULENCE) { + + uiDefButF(block, NUM, B_REDR, "Size: ", + X2CLM1, yco-=BUTH, BUTW2, BUTH, &(pd->noise_size), 0.001, 100.0, 10, 2, "Turbulence size"); + uiDefButS(block, NUM, B_REDR, "Depth: ", + X2CLM1, yco-=BUTH, BUTW2, BUTH, &(pd->noise_depth), 0.0, 100.0, 10, 2, "Turbulence depth"); + uiDefButF(block, NUM, B_REDR, "Strength: ", + X2CLM1, yco-=BUTH, BUTW2, BUTH, &(pd->noise_fac), 0.001, 100.0, 10, 2, ""); + + uiBlockEndAlign(block); + + yco -= YSPACE; + + if (pd->source == TEX_PD_PSYS) { + uiDefButS(block, MENU, B_REDR, "Noise Influence %t|Static %x0|Velocity %x1|Angular Velocity %x2", + X2CLM1, yco-=BUTH, BUTW2, BUTH, &(pd->noise_influence), 0.0, 0.0, 0, 0, "Noise Influence"); + } + } + uiBlockEndAlign(block); + yco = PANEL_YMAX; uiDefBut(block, LABEL, B_NOP, "Point data source:", - X2CLM2, yco-=BUTH, BUTW2, BUTH, 0, 0, 0, 0, 0, ""); + X2CLM2, yco-=BUTH, BUTW2, BUTH, 0, 0, 0, 0, 0, ""); uiDefButS(block, MENU, B_TEXREDR_PRV, "Particle System %x0|Object Vertices %x1", - X2CLM2, yco-=BUTH, BUTW2, BUTH, &pd->source, 0.0, 0.0, 0, 0, "Source"); + X2CLM2, yco-=BUTH, BUTW2, BUTH, &pd->source, 0.0, 0.0, 0, 0, "Source"); yco -= YSPACE; @@ -788,6 +815,7 @@ static void texture_panel_pointdensity(Tex *tex) X2CLM2, yco-=BUTH, BUTW2, BUTH, 0, 0, 0, 0, 0, ""); uiDefButS(block, MENU, B_TEXREDR_PRV, "Emit Object Location %x0|Emit Object Space %x1|Global Space %x2", X2CLM2, yco-=BUTH, BUTW2, BUTH, &pd->psys_cache_space, 0.0, 0.0, 0, 0, "Co-ordinate system to cache particles in"); + } else if (pd->source == TEX_PD_OBJECT) { uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_REDR, "Ob:", |