Make particle size follow world space instead of screen space
authorLuca Rood <dev@lucarood.com>
Tue, 6 Jun 2017 07:56:27 +0000 (09:56 +0200)
committerLuca Rood <dev@lucarood.com>
Tue, 6 Jun 2017 07:57:18 +0000 (09:57 +0200)
source/blender/blenkernel/intern/particle.c
source/blender/draw/modes/object_mode.c
source/blender/draw/modes/shaders/object_particle_dot_vert.glsl
source/blender/draw/modes/shaders/object_particle_prim_vert.glsl
source/blender/makesdna/DNA_particle_types.h
source/blender/makesrna/intern/rna_particle.c

index 7901dff129e0842ff81fe0f6a62d5ec473085b2f..2084e7ec4347e4d7b40a73f52d1c7540446f68b7 100644 (file)
@@ -3289,7 +3289,7 @@ static void default_particle_settings(ParticleSettings *part)
 
        part->omat = 1;
        part->use_modifier_stack = false;
-       part->draw_size = 4;
+       part->draw_size = 0.1f;
 }
 
 
index 45e035acc73d21ef5f75afbdc27552590be7bb81..7ca8006f36ced21079bef260aae1bcfacbf01676 100644 (file)
@@ -1461,7 +1461,8 @@ static void OBJECT_cache_populate_particles(Object *ob,
                                                shgrp = DRW_shgroup_create(e_data.part_dot_sh, psl->particle);
                                                DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
                                                DRW_shgroup_uniform_vec3(shgrp, "outlineColor", ma ? &ma->specr : def_sec_col, 1);
-                                               DRW_shgroup_uniform_short_to_int(shgrp, "size", &part->draw_size, 1);
+                                               DRW_shgroup_uniform_float(shgrp, "pixel_size", DRW_viewport_pixelsize_get(), 1);
+                                               DRW_shgroup_uniform_float(shgrp, "size", &part->draw_size, 1);
                                                DRW_shgroup_uniform_texture(shgrp, "ramp", globals_ramp);
                                                DRW_shgroup_call_add(shgrp, geom, mat);
                                                break;
@@ -1487,8 +1488,7 @@ static void OBJECT_cache_populate_particles(Object *ob,
 
                                if (shgrp) {
                                        if (draw_as != PART_DRAW_DOT) {
-                                               DRW_shgroup_uniform_short_to_int(shgrp, "draw_size", &part->draw_size, 1);
-                                               DRW_shgroup_uniform_float(shgrp, "pixel_size", DRW_viewport_pixelsize_get(), 1);
+                                               DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
                                                DRW_shgroup_instance_batch(shgrp, geom);
                                        }
                                }
index 7b163dbdc31da137f0c698e0f2e07e2d97a821d6..6dfc212a77699d819ffb5b752929420a71d0e8ef 100644 (file)
@@ -1,6 +1,8 @@
 
-uniform mat4 ModelViewProjectionMatrix;
-uniform int size;
+uniform mat4 ModelViewMatrix;
+uniform mat4 ProjectionMatrix;
+uniform float pixel_size;
+uniform float size;
 
 in vec3 pos;
 in float val;
@@ -9,11 +11,14 @@ out vec4 radii;
 flat out float finalVal;
 
 void main() {
-       gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
-       gl_PointSize = size;
+       gl_Position = ModelViewMatrix * vec4(pos, 1.0);
+
+       float psize = (ProjectionMatrix[3][3] == 0.0) ? (size / (-gl_Position.z * pixel_size)) : (size / pixel_size);
+
+       gl_PointSize = psize;
 
        // calculate concentric radii in pixels
-       float radius = 0.5 * size;
+       float radius = 0.5 * psize;
 
        // start at the outside and progress toward the center
        radii[0] = radius;
@@ -22,7 +27,9 @@ void main() {
        radii[3] = radius - 2.0;
 
        // convert to PointCoord units
-       radii /= size;
+       radii /= psize;
+
+       gl_Position = ProjectionMatrix * gl_Position;
 
        finalVal = val;
 }
index 55b183b6933cb571be9d87a46e934adf0dbbbf2c..a950a9b86ba9f663145e2d5baee9ab2da172f8d9 100644 (file)
@@ -4,8 +4,7 @@ uniform mat4 ViewProjectionMatrix;
 uniform mat4 ModelViewMatrix;
 uniform mat4 ProjectionMatrix;
 uniform int screen_space;
-uniform float pixel_size;
-uniform int draw_size;
+uniform float draw_size;
 uniform vec3 color;
 uniform sampler1D ramp;
 
@@ -23,29 +22,20 @@ vec3 rotate(vec3 vec, vec4 quat)
        return vec + 2.0 * cross(quat.yzw, cross(quat.yzw, vec) + quat.x * vec);
 }
 
-float mul_project_m4_v3_zfac(in vec3 co)
-{
-       return (ViewProjectionMatrix[0][3] * co.x) +
-              (ViewProjectionMatrix[1][3] * co.y) +
-              (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3];
-}
-
 void main()
 {
-       float pix_size = mul_project_m4_v3_zfac(pos) * pixel_size;
-
        if (screen_space == 1) {
-               gl_Position = ModelViewMatrix * vec4(pos, 1.0) + vec4(inst_pos * pix_size * draw_size, 0.0);
+               gl_Position = ModelViewMatrix * vec4(pos, 1.0) + vec4(inst_pos * draw_size, 0.0);
                gl_Position = ProjectionMatrix * gl_Position;
        }
        else {
-               int size = draw_size;
+               float size = draw_size;
 
                if (axis > -1) {
                        size *= 2;
                }
 
-               gl_Position = ModelViewProjectionMatrix * vec4(pos + rotate(inst_pos * pix_size * size, rot), 1.0);
+               gl_Position = ModelViewProjectionMatrix * vec4(pos + rotate(inst_pos * size, rot), 1.0);
        }
 
 #ifdef USE_AXIS
index 5d9d7bd7be0ead90a2e8ec56cd93e86c909ea7e4..cde8eef6ef39dc4f7997bf759facf0c9b58d1b73 100644 (file)
@@ -166,8 +166,9 @@ typedef struct ParticleSettings {
        short type, from, distr, texact;
        /* physics modes */
        short phystype, rotmode, avemode, reactevent;
-       int draw, pad1;
-       short draw_as, draw_size, childtype, pad2;
+       int draw;
+       float draw_size;
+       short draw_as, pad1, childtype, pad2;
        short ren_as, subframes, draw_col;
        /* number of path segments, power of 2 except */
        short draw_step, ren_step;
index 067ce28fc59c35a0c44be5378b514fcde4b6cb85..4a4d8582894bfee70baaa5f521b16fda1740b427 100644 (file)
@@ -2338,10 +2338,10 @@ static void rna_def_particle_settings(BlenderRNA *brna)
        RNA_def_property_ui_text(prop, "Draw Color", "Draw additional particle data as a color");
        RNA_def_property_update(prop, 0, "rna_Particle_redo");
 
-       prop = RNA_def_property(srna, "draw_size", PROP_INT, PROP_PIXEL);
-       RNA_def_property_range(prop, 1, 1000);
-       RNA_def_property_ui_range(prop, 1, 100, 1, -1);
-       RNA_def_property_ui_text(prop, "Draw Size", "Size of particles on viewport in pixels (0=default)");
+       prop = RNA_def_property(srna, "draw_size", PROP_FLOAT, PROP_NONE);
+       RNA_def_property_range(prop, 0, 1000);
+       RNA_def_property_ui_range(prop, 0, 100, 1, -1);
+       RNA_def_property_ui_text(prop, "Draw Size", "Size of particles on viewport in BU (0.1 = default)");
        RNA_def_property_update(prop, 0, "rna_Particle_redo");
 
        prop = RNA_def_property(srna, "child_type", PROP_ENUM, PROP_NONE);