Create ParticlesMaterialConversionPlugin and format generated shader code

This commit is contained in:
Hiroshi Ogawa 2017-10-12 20:12:50 +09:00
parent 409e58e67a
commit d777681882
5 changed files with 221 additions and 166 deletions

View file

@ -5525,6 +5525,10 @@ EditorNode::EditorNode() {
Ref<SpatialMaterialConversionPlugin> spatial_mat_convert;
spatial_mat_convert.instance();
resource_conversion_plugins.push_back(spatial_mat_convert);
Ref<ParticlesMaterialConversionPlugin> particles_mat_convert;
particles_mat_convert.instance();
resource_conversion_plugins.push_back(particles_mat_convert);
}
circle_step_msec = OS::get_singleton()->get_ticks_msec();
circle_step_frame = Engine::get_singleton()->get_frames_drawn();

View file

@ -32,6 +32,7 @@
// Waiting for PropertyEditor rewrite (planned for 3.1) to be refactored.
#include "material_editor_plugin.h"
#include "scene/3d/particles.h"
#if 0
@ -456,3 +457,41 @@ Ref<Resource> SpatialMaterialConversionPlugin::convert(const Ref<Resource> &p_re
smat->set_render_priority(mat->get_render_priority());
return smat;
}
String ParticlesMaterialConversionPlugin::converts_to() const {
return "ShaderMaterial";
}
bool ParticlesMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
Ref<ParticlesMaterial> mat = p_resource;
return mat.is_valid();
}
Ref<Resource> ParticlesMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) {
Ref<ParticlesMaterial> mat = p_resource;
ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
Ref<ShaderMaterial> smat;
smat.instance();
Ref<Shader> shader;
shader.instance();
String code = VS::get_singleton()->shader_get_code(mat->get_shader_rid());
shader->set_code(code);
smat->set_shader(shader);
List<PropertyInfo> params;
VS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
Variant value = VS::get_singleton()->material_get_param(mat->get_rid(), E->get().name);
smat->set_shader_param(E->get().name, value);
}
smat->set_render_priority(mat->get_render_priority());
return smat;
}

View file

@ -111,4 +111,12 @@ public:
virtual Ref<Resource> convert(const Ref<Resource> &p_resource);
};
class ParticlesMaterialConversionPlugin : public EditorResourceConversionPlugin {
GDCLASS(ParticlesMaterialConversionPlugin, EditorResourceConversionPlugin)
public:
virtual String converts_to() const;
virtual bool handles(const Ref<Resource> &p_resource) const;
virtual Ref<Resource> convert(const Ref<Resource> &p_resource);
};
#endif // MATERIAL_EDITOR_PLUGIN_H

View file

@ -598,6 +598,8 @@ void ParticlesMaterial::_update_shader() {
code += " seed = uint(s);\n";
code += " return float(seed % uint(65536))/65535.0;\n";
code += "}\n";
code += "\n";
//improve seed quality
code += "uint hash(uint x) {\n";
code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
@ -605,9 +607,9 @@ void ParticlesMaterial::_update_shader() {
code += " x = (x >> uint(16)) ^ x;\n";
code += " return x;\n";
code += "}\n";
code += "void vertex() {\n\n";
code += "\n";
code += "void vertex() {\n";
code += " uint base_number = NUMBER/uint(trail_divisor);\n";
code += " uint alt_seed = hash(base_number+uint(1)+RANDOM_SEED);\n";
code += " float angle_rand = rand_from_seed(alt_seed);\n";
@ -615,9 +617,7 @@ void ParticlesMaterial::_update_shader() {
code += " float hue_rot_rand = rand_from_seed(alt_seed);\n";
code += " float anim_offset_rand = rand_from_seed(alt_seed);\n";
code += "\n";
code += "\n";
code += "\n";
code += "\n";
if (emission_shape >= EMISSION_SHAPE_POINTS) {
code += " int point = min(emission_texture_point_count-1,int(rand_from_seed(alt_seed) * float(emission_texture_point_count)));\n";
code += " ivec2 emission_tex_size = textureSize( emission_texture_points, 0 );\n";
@ -759,8 +759,6 @@ void ParticlesMaterial::_update_shader() {
code += " force += length(VELOCITY) > 0.0 ? normalize(VELOCITY) * (linear_accel+tex_linear_accel)*mix(1.0,rand_from_seed(alt_seed),linear_accel_random) : vec3(0.0);\n";
code += " //apply radial acceleration\n";
code += " vec3 org = vec3(0.0);\n";
code += " // if (!p_system->local_coordinates)\n";
code += " //org=p_transform.origin;\n";
code += " vec3 diff = pos-org;\n";
code += " force += length(diff) > 0.0 ? normalize(diff) * (radial_accel+tex_radial_accel)*mix(1.0,rand_from_seed(alt_seed),radial_accel_random) : vec3(0.0);\n";
code += " //apply tangential acceleration;\n";
@ -817,12 +815,10 @@ void ParticlesMaterial::_update_shader() {
code += " vec4(0.299, 0.587, 0.114, 0.0),\n";
code += " vec4(0.299, 0.587, 0.114, 0.0),\n";
code += " vec4(0.000, 0.000, 0.000, 1.0)) +\n";
code += " \n";
code += " mat4( vec4(0.701, -0.587, -0.114, 0.0),\n";
code += " vec4(-0.299, 0.413, -0.114, 0.0),\n";
code += " vec4(-0.300, -0.588, 0.886, 0.0),\n";
code += " vec4(0.000, 0.000, 0.000, 0.0)) * hue_rot_c +\n";
code += "\n";
code += " mat4( vec4(0.168, 0.330, -0.497, 0.0),\n";
code += " vec4(-0.328, 0.035, 0.292, 0.0),\n";
code += " vec4(1.250, -1.050, -0.203, 0.0),\n";
@ -851,20 +847,20 @@ void ParticlesMaterial::_update_shader() {
if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
code += " if (length(VELOCITY) > 0.0) { TRANSFORM[1].xyz = normalize(VELOCITY); } else { TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz); }\n";
code += " if (TRANSFORM[1].xyz == normalize(TRANSFORM[0].xyz)) {\n";
code += "\tTRANSFORM[0].xyz=normalize(cross(normalize(TRANSFORM[1].xyz),normalize(TRANSFORM[2].xyz)));\n";
code += "\tTRANSFORM[2].xyz=normalize(cross(normalize(TRANSFORM[0].xyz),normalize(TRANSFORM[1].xyz)));\n";
code += " TRANSFORM[0].xyz = normalize(cross(normalize(TRANSFORM[1].xyz),normalize(TRANSFORM[2].xyz)));\n";
code += " TRANSFORM[2].xyz = normalize(cross(normalize(TRANSFORM[0].xyz),normalize(TRANSFORM[1].xyz)));\n";
code += " } else {\n";
code += "\tTRANSFORM[2].xyz=normalize(cross(normalize(TRANSFORM[0].xyz),normalize(TRANSFORM[1].xyz)));\n";
code += "\tTRANSFORM[0].xyz=normalize(cross(normalize(TRANSFORM[1].xyz),normalize(TRANSFORM[2].xyz)));\n";
code += " TRANSFORM[2].xyz = normalize(cross(normalize(TRANSFORM[0].xyz),normalize(TRANSFORM[1].xyz)));\n";
code += " TRANSFORM[0].xyz = normalize(cross(normalize(TRANSFORM[1].xyz),normalize(TRANSFORM[2].xyz)));\n";
code += " }\n";
} else {
code += "\tTRANSFORM[0].xyz=normalize(TRANSFORM[0].xyz);\n";
code += "\tTRANSFORM[1].xyz=normalize(TRANSFORM[1].xyz);\n";
code += "\tTRANSFORM[2].xyz=normalize(TRANSFORM[2].xyz);\n";
code += " TRANSFORM[0].xyz = normalize(TRANSFORM[0].xyz);\n";
code += " TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz);\n";
code += " TRANSFORM[2].xyz = normalize(TRANSFORM[2].xyz);\n";
}
//turn particle by rotation in Y
if (flags[FLAG_ROTATE_Y]) {
code += "\tTRANSFORM = TRANSFORM * mat4( vec4(cos(CUSTOM.x),0.0,-sin(CUSTOM.x),0.0), vec4(0.0,1.0,0.0,0.0),vec4(sin(CUSTOM.x),0.0,cos(CUSTOM.x),0.0),vec4(0.0,0.0,0.0,1.0));\n";
code += " TRANSFORM = TRANSFORM * mat4( vec4(cos(CUSTOM.x),0.0,-sin(CUSTOM.x),0.0), vec4(0.0,1.0,0.0,0.0),vec4(sin(CUSTOM.x),0.0,cos(CUSTOM.x),0.0),vec4(0.0,0.0,0.0,1.0));\n";
}
}
//scale by scale
@ -1329,6 +1325,12 @@ Vector3 ParticlesMaterial::get_gravity() const {
return gravity;
}
RID ParticlesMaterial::get_shader_rid() const {
ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
return shader_map[current_key].shader;
}
void ParticlesMaterial::_validate_property(PropertyInfo &property) const {
if (property.name == "color" && color_ramp.is_valid()) {

View file

@ -388,6 +388,8 @@ public:
static void finish_shaders();
static void flush_changes();
RID get_shader_rid() const;
ParticlesMaterial();
~ParticlesMaterial();
};