Color Ramp and Curve Map added to visual shader editing.

Added Color Ramp and Curve Map to shader nodes.
Fixed an issue that crashed Godot Editor right when opened.
This commit is contained in:
Juan Linietsky 2015-01-19 02:39:58 -03:00
parent e0c0aef615
commit a0511ed59a
9 changed files with 1240 additions and 10 deletions

1
.gitignore vendored
View file

@ -258,3 +258,4 @@ Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
logo.h
*.autosave

View file

@ -46,14 +46,15 @@ void main() {
color_interp = color_attrib;
uv_interp = uv_attrib;
highp vec4 outvec = vec4(vertex, 1.0);
highp vec4 outvec = vec4(vertex, 1.0);
{
vec2 src_vtx=outvec.xy;
vec2 src_vtx=outvec.xy;
VERTEX_SHADER_CODE
}
outvec = extra_matrix * outvec;
outvec = modelview_matrix * outvec;
outvec = extra_matrix * outvec;
outvec = modelview_matrix * outvec;
#ifdef USE_PIXEL_SNAP
outvec.xy=floor(outvec.xy+0.5);

View file

@ -221,6 +221,13 @@ void ShaderGraph::_bind_methods() {
ObjectTypeDB::bind_method(_MD("comment_node_set_text","shader_type","id","text"),&ShaderGraph::comment_node_set_text);
ObjectTypeDB::bind_method(_MD("comment_node_get_text","shader_type","id"),&ShaderGraph::comment_node_get_text);
ObjectTypeDB::bind_method(_MD("color_ramp_node_set_ramp","shader_type","id","colors","offsets"),&ShaderGraph::color_ramp_node_set_ramp);
ObjectTypeDB::bind_method(_MD("color_ramp_node_get_colors","shader_type","id"),&ShaderGraph::color_ramp_node_get_colors);
ObjectTypeDB::bind_method(_MD("color_ramp_node_get_offsets","shader_type","id"),&ShaderGraph::color_ramp_node_get_offsets);
ObjectTypeDB::bind_method(_MD("curve_map_node_set_points","shader_type","id","points"),&ShaderGraph::curve_map_node_set_points);
ObjectTypeDB::bind_method(_MD("curve_map_node_get_points","shader_type","id"),&ShaderGraph::curve_map_node_get_points);
ObjectTypeDB::bind_method(_MD("connect_node:Error","shader_type","src_id","src_slot","dst_id","dst_slot"),&ShaderGraph::connect_node);
ObjectTypeDB::bind_method(_MD("is_node_connected","shader_type","src_id","src_slot","dst_id","dst_slot"),&ShaderGraph::is_node_connected);
ObjectTypeDB::bind_method(_MD("disconnect_node","shader_type","src_id","src_slot","dst_id","dst_slot"),&ShaderGraph::disconnect_node);
@ -522,8 +529,8 @@ void ShaderGraph::node_add(ShaderType p_type, NodeType p_node_type,int p_id) {
case NODE_XFORM_TO_VEC: {} break; // 3 scalar input: {} break; 1 vec3 output
case NODE_SCALAR_INTERP: {} break; // scalar interpolation (with optional curve)
case NODE_VEC_INTERP: {} break; // vec3 interpolation (with optional curve)
case NODE_COLOR_RAMP: { node.param1=Array();} break; // vec3 interpolation (with optional curve)
case NODE_CURVE_MAP: { node.param1=Array();} break; // vec3 interpolation (with optional curve)
case NODE_COLOR_RAMP: { node.param1=DVector<Color>(); node.param2=DVector<real_t>();} break; // vec3 interpolation (with optional curve)
case NODE_CURVE_MAP: { node.param1=DVector<Vector2>();} break; // vec3 interpolation (with optional curve)
case NODE_SCALAR_INPUT: {node.param1=_find_unique_name("Scalar"); node.param2=0;} break; // scalar uniform (assignable in material)
case NODE_VEC_INPUT: {node.param1=_find_unique_name("Vec3");node.param2=Vector3();} break; // vec3 uniform (assignable in material)
case NODE_RGB_INPUT: {node.param1=_find_unique_name("Color");node.param2=Color();} break; // color uniform (assignable in material)
@ -970,6 +977,59 @@ ShaderGraph::VecFunc ShaderGraph::vec_func_node_get_function(ShaderType p_type,
return VecFunc(func);
}
void ShaderGraph::color_ramp_node_set_ramp(ShaderType p_type,int p_id,const DVector<Color>& p_colors, const DVector<real_t>& p_offsets){
ERR_FAIL_INDEX(p_type,3);
ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
ERR_FAIL_COND(p_colors.size()!=p_offsets.size());
Node& n = shader[p_type].node_map[p_id];
n.param1=p_colors;
n.param2=p_offsets;
_request_update();
}
DVector<Color> ShaderGraph::color_ramp_node_get_colors(ShaderType p_type,int p_id) const{
ERR_FAIL_INDEX_V(p_type,3,DVector<Color>());
ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),DVector<Color>());
const Node& n = shader[p_type].node_map[p_id];
return n.param1;
}
DVector<real_t> ShaderGraph::color_ramp_node_get_offsets(ShaderType p_type,int p_id) const{
ERR_FAIL_INDEX_V(p_type,3,DVector<real_t>());
ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),DVector<real_t>());
const Node& n = shader[p_type].node_map[p_id];
return n.param2;
}
void ShaderGraph::curve_map_node_set_points(ShaderType p_type,int p_id,const DVector<Vector2>& p_points) {
ERR_FAIL_INDEX(p_type,3);
ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
Node& n = shader[p_type].node_map[p_id];
n.param1=p_points;
_request_update();
}
DVector<Vector2> ShaderGraph::curve_map_node_get_points(ShaderType p_type,int p_id) const{
ERR_FAIL_INDEX_V(p_type,3,DVector<Vector2>());
ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),DVector<Vector2>());
const Node& n = shader[p_type].node_map[p_id];
return n.param1;
}
void ShaderGraph::input_node_set_name(ShaderType p_type,int p_id,const String& p_name){
ERR_FAIL_INDEX(p_type,3);
@ -1305,7 +1365,7 @@ const ShaderGraph::NodeSlotInfo ShaderGraph::node_slot_info[]= {
{NODE_SCALAR_TO_VEC,{SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR},{SLOT_TYPE_VEC,SLOT_MAX}}, // 3 scalar input,{SLOT_MAX},{SLOT_MAX}}, 1 vec3 output
{NODE_SCALAR_INTERP,{SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // scalar interpolation (with optional curve)
{NODE_VEC_INTERP,{SLOT_TYPE_VEC,SLOT_TYPE_VEC,SLOT_TYPE_SCALAR},{SLOT_TYPE_VEC,SLOT_MAX}}, // vec3 interpolation (with optional curve)
{NODE_COLOR_RAMP,{SLOT_TYPE_SCALAR,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // vec3 interpolation (with optional curve)
{NODE_COLOR_RAMP,{SLOT_TYPE_SCALAR,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_TYPE_SCALAR,SLOT_MAX}}, // vec3 interpolation (with optional curve)
{NODE_CURVE_MAP,{SLOT_TYPE_SCALAR,SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // vec3 interpolation (with optional curve)
{NODE_SCALAR_INPUT,{SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // scalar uniform (assignable in material)
{NODE_VEC_INPUT,{SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // vec3 uniform (assignable in material)
@ -1696,6 +1756,134 @@ void ShaderGraph::_update_shader() {
emit_signal(SceneStringNames::get_singleton()->updated);
}
void ShaderGraph::_plot_curve(const Vector2& p_a,const Vector2& p_b,const Vector2& p_c,const Vector2& p_d,uint8_t* p_heights,bool *p_useds) {
float geometry[4][4];
float tmp1[4][4];
float tmp2[4][4];
float deltas[4][4];
double x, dx, dx2, dx3;
double y, dy, dy2, dy3;
double d, d2, d3;
int lastx, lasty;
int newx, newy;
int ntimes;
int i,j;
int xmax=255;
int ymax=255;
/* construct the geometry matrix from the segment */
for (i = 0; i < 4; i++) {
geometry[i][2] = 0;
geometry[i][3] = 0;
}
geometry[0][0] = (p_a[0] * xmax);
geometry[1][0] = (p_b[0] * xmax);
geometry[2][0] = (p_c[0] * xmax);
geometry[3][0] = (p_d[0] * xmax);
geometry[0][1] = (p_a[1] * ymax);
geometry[1][1] = (p_b[1] * ymax);
geometry[2][1] = (p_c[1] * ymax);
geometry[3][1] = (p_d[1] * ymax);
/* subdivide the curve ntimes (1000) times */
ntimes = 4 * xmax;
/* ntimes can be adjusted to give a finer or coarser curve */
d = 1.0 / ntimes;
d2 = d * d;
d3 = d * d * d;
/* construct a temporary matrix for determining the forward differencing deltas */
tmp2[0][0] = 0; tmp2[0][1] = 0; tmp2[0][2] = 0; tmp2[0][3] = 1;
tmp2[1][0] = d3; tmp2[1][1] = d2; tmp2[1][2] = d; tmp2[1][3] = 0;
tmp2[2][0] = 6*d3; tmp2[2][1] = 2*d2; tmp2[2][2] = 0; tmp2[2][3] = 0;
tmp2[3][0] = 6*d3; tmp2[3][1] = 0; tmp2[3][2] = 0; tmp2[3][3] = 0;
/* compose the basis and geometry matrices */
static const float CR_basis[4][4] =
{
{ -0.5, 1.5, -1.5, 0.5 },
{ 1.0, -2.5, 2.0, -0.5 },
{ -0.5, 0.0, 0.5, 0.0 },
{ 0.0, 1.0, 0.0, 0.0 },
};
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
tmp1[i][j] = (CR_basis[i][0] * geometry[0][j] +
CR_basis[i][1] * geometry[1][j] +
CR_basis[i][2] * geometry[2][j] +
CR_basis[i][3] * geometry[3][j]);
}
}
/* compose the above results to get the deltas matrix */
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
deltas[i][j] = (tmp2[i][0] * tmp1[0][j] +
tmp2[i][1] * tmp1[1][j] +
tmp2[i][2] * tmp1[2][j] +
tmp2[i][3] * tmp1[3][j]);
}
}
/* extract the x deltas */
x = deltas[0][0];
dx = deltas[1][0];
dx2 = deltas[2][0];
dx3 = deltas[3][0];
/* extract the y deltas */
y = deltas[0][1];
dy = deltas[1][1];
dy2 = deltas[2][1];
dy3 = deltas[3][1];
lastx = CLAMP (x, 0, xmax);
lasty = CLAMP (y, 0, ymax);
p_heights[lastx] = lasty;
p_useds[lastx] = true;
/* loop over the curve */
for (i = 0; i < ntimes; i++)
{
/* increment the x values */
x += dx;
dx += dx2;
dx2 += dx3;
/* increment the y values */
y += dy;
dy += dy2;
dy2 += dy3;
newx = CLAMP ((Math::round (x)), 0, xmax);
newy = CLAMP ((Math::round (y)), 0, ymax);
/* if this point is different than the last one...then draw it */
if ((lastx != newx) || (lasty != newy))
{
p_useds[newx]=true;
p_heights[newx]=newy;
}
lastx = newx;
lasty = newy;
}
}
void ShaderGraph::_add_node_code(ShaderType p_type,Node *p_node,const Vector<String>& p_inputs,String& code) {
@ -1998,8 +2186,125 @@ void ShaderGraph::_add_node_code(ShaderType p_type,Node *p_node,const Vector<Str
}break;
case NODE_COLOR_RAMP: {
static const int color_ramp_len=512;
DVector<uint8_t> cramp;
cramp.resize(color_ramp_len*4);
{
DVector<Color> colors=p_node->param1;
DVector<real_t> offsets=p_node->param2;
int cc =colors.size();
DVector<uint8_t>::Write crw = cramp.write();
DVector<Color>::Read cr = colors.read();
DVector<real_t>::Read ofr = offsets.read();
int at=0;
Color color_at(0,0,0,1);
for(int i=0;i<=cc;i++) {
int pos;
Color to;
if (i==cc) {
if (at==color_ramp_len)
break;
pos=color_ramp_len;
to=Color(1,1,1,1);
} else {
to=cr[i];
pos= MIN(ofr[i]*color_ramp_len,color_ramp_len);
}
for(int j=at;j<pos;j++) {
float t = (j-at)/float(pos-at);
Color c = color_at.linear_interpolate(to,t);
crw[j*4+0]=Math::fast_ftoi( CLAMP(c.r*255.0,0,255) );
crw[j*4+1]=Math::fast_ftoi( CLAMP(c.g*255.0,0,255) );
crw[j*4+2]=Math::fast_ftoi( CLAMP(c.b*255.0,0,255) );
crw[j*4+3]=Math::fast_ftoi( CLAMP(c.a*255.0,0,255) );
}
at=pos;
color_at=to;
}
}
Image gradient(color_ramp_len,1,0,Image::FORMAT_RGBA,cramp);
Ref<ImageTexture> it = memnew( ImageTexture );
it->create_from_image(gradient,Texture::FLAG_FILTER|Texture::FLAG_MIPMAPS);
String crampname= "cramp_"+itos(p_node->id);
set_default_texture_param(crampname,it);
code +="uniform texture "+crampname+";\n";
code +="vec4 "+crampname+"_r=tex("+crampname+",vec2("+p_inputs[0]+",0));\n";
code += OUTNAME(p_node->id,0)+"="+crampname+"_r.rgb;\n";
code += OUTNAME(p_node->id,1)+"="+crampname+"_r.a;\n";
}break;
case NODE_CURVE_MAP: {
static const int curve_map_len=256;
bool mapped[256];
zeromem(mapped,sizeof(mapped));
DVector<uint8_t> cmap;
cmap.resize(curve_map_len);
{
DVector<Point2> points=p_node->param1;
int pc =points.size();
DVector<uint8_t>::Write cmw = cmap.write();
DVector<Point2>::Read pr = points.read();
Vector2 prev=Vector2(0,0);
Vector2 prev2=Vector2(0,0);
for(int i=-1;i<pc;i++) {
Vector2 next;
Vector2 next2;
if (i+1>=pc) {
next=Vector2(1,1);
} else {
next=Vector2(pr[i+1].x,pr[i+1].y);
}
if (i+2>=pc) {
next2=Vector2(1,1);
} else {
next2=Vector2(pr[i+2].x,pr[i+2].y);
}
/*if (i==-1 && prev.offset==next.offset) {
prev=next;
continue;
}*/
_plot_curve(prev2,prev,next,next2,cmw.ptr(),mapped);
prev2=prev;
prev=next;
}
uint8_t pp=0;
for(int i=0;i<curve_map_len;i++) {
if (!mapped[i]) {
cmw[i]=pp;
} else {
pp=cmw[i];
}
}
}
Image gradient(curve_map_len,1,0,Image::FORMAT_GRAYSCALE,cmap);
Ref<ImageTexture> it = memnew( ImageTexture );
it->create_from_image(gradient,Texture::FLAG_FILTER|Texture::FLAG_MIPMAPS);
String cmapname= "cmap_"+itos(p_node->id);
set_default_texture_param(cmapname,it);
code +="uniform texture "+cmapname+";\n";
code += OUTNAME(p_node->id,0)+"=tex("+cmapname+",vec2("+p_inputs[0]+",0)).r;\n";
}break;
case NODE_SCALAR_INPUT: {
@ -2059,7 +2364,19 @@ void ShaderGraph::_add_node_code(ShaderType p_type,Node *p_node,const Vector<Str
}break;
case NODE_DEFAULT_TEXTURE: {
if (get_mode()==MODE_CANVAS_ITEM && p_type==SHADER_TYPE_FRAGMENT) {
String rname="rt_default_tex"+itos(p_node->id);
code +="vec4 "+rname+"=tex(TEXTURE,"+p_inputs[0]+".xy);\n";
code += OUTNAME(p_node->id,0)+"="+rname+".rgb;\n";
code += OUTNAME(p_node->id,1)+"="+rname+".a;\n";
} else {
code += OUTNAME(p_node->id,0)+"=vec3(0,0,0);\n";
code += OUTNAME(p_node->id,1)+"=1.0;\n";
}
} break;
case NODE_OUTPUT: {

View file

@ -175,6 +175,7 @@ private:
void _update_shader();
void _request_update();
void _plot_curve(const Vector2& p_a,const Vector2& p_b,const Vector2& p_c,const Vector2& p_d,uint8_t* p_heights,bool *p_useds);
void _add_node_code(ShaderType p_type,Node *p_node,const Vector<String>& p_inputs,String& code);
Array _get_node_list(ShaderType p_type) const;
@ -316,6 +317,13 @@ public:
void vec_func_node_set_function(ShaderType p_which,int p_id,VecFunc p_func);
VecFunc vec_func_node_get_function(ShaderType p_which,int p_id) const;
void color_ramp_node_set_ramp(ShaderType p_which,int p_id,const DVector<Color>& p_colors, const DVector<real_t>& p_offsets);
DVector<Color> color_ramp_node_get_colors(ShaderType p_which,int p_id) const;
DVector<real_t> color_ramp_node_get_offsets(ShaderType p_which,int p_id) const;
void curve_map_node_set_points(ShaderType p_which, int p_id, const DVector<Vector2>& p_points);
DVector<Vector2> curve_map_node_get_points(ShaderType p_which,int p_id) const;
void input_node_set_name(ShaderType p_which,int p_id,const String& p_name);
String input_node_get_name(ShaderType p_which,int p_id);

Binary file not shown.

After

Width:  |  Height:  |  Size: 290 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 619 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 326 B

View file

@ -32,6 +32,642 @@
#include "scene/gui/menu_button.h"
#include "scene/gui/panel.h"
#include "spatial_editor_plugin.h"
#include "os/keyboard.h"
void GraphColorRampEdit::_input_event(const InputEvent& p_event) {
if (p_event.type==InputEvent::KEY && p_event.key.pressed && p_event.key.scancode==KEY_DELETE && grabbed!=-1) {
points.remove(grabbed);
grabbed=-1;
update();
emit_signal("ramp_changed");
accept_event();
}
if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && p_event.mouse_button.pressed) {
update();
int x = p_event.mouse_button.x;
int total_w = get_size().width-get_size().height-3;
if (x>total_w+3) {
if (grabbed==-1)
return;
Size2 ms = Size2(350, picker->get_combined_minimum_size().height+10);
picker->set_color(points[grabbed].color);
popup->set_pos(get_global_pos()-Size2(0,ms.height));
popup->set_size(ms);
popup->popup();
return;
}
float ofs = CLAMP(x/float(total_w),0,1);
grabbed=-1;
grabbing=true;
int pos=-1;
for(int i=0;i<points.size();i++) {
if (ABS(x-points[i].offset*total_w)<4) {
grabbed=i;
}
if (points[i].offset<ofs)
pos=i;
}
grabbed_at=ofs;
//grab or select
if (grabbed!=-1) {
return;
}
//insert
Point p;
p.offset=ofs;
Point prev;
Point next;
if (pos==-1) {
prev.color=Color(0,0,0);
prev.offset=0;
if (points.size()) {
next=points[0];
} else {
next.color=Color(1,1,1);
next.offset=1.0;
}
} else {
if (pos==points.size()-1) {
next.color=Color(1,1,1);
next.offset=1.0;
} else {
next=points[pos+1];
}
prev=points[pos];
}
p.color=prev.color.linear_interpolate(next.color,(p.offset-prev.offset)/(next.offset-prev.offset));
points.push_back(p);
points.sort();
for(int i=0;i<points.size();i++) {
if (points[i].offset==ofs) {
grabbed=i;
break;
}
}
emit_signal("ramp_changed");
}
if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && !p_event.mouse_button.pressed) {
if (grabbing) {
grabbing=false;
emit_signal("ramp_changed");
}
update();
}
if (p_event.type==InputEvent::MOUSE_MOTION && grabbing) {
int total_w = get_size().width-get_size().height-3;
int x = p_event.mouse_motion.x;
float newofs = CLAMP(x/float(total_w),0,1);
bool valid=true;
for(int i=0;i<points.size();i++) {
if (points[i].offset==newofs && i!=grabbed) {
valid=false;
}
}
if (!valid)
return;
points[grabbed].offset=newofs;
points.sort();
for(int i=0;i<points.size();i++) {
if (points[i].offset==newofs) {
grabbed=i;
break;
}
}
emit_signal("ramp_changed");
update();
}
}
void GraphColorRampEdit::_notification(int p_what){
if (p_what==NOTIFICATION_ENTER_TREE) {
picker->connect("color_changed",this,"_color_changed");
}
if (p_what==NOTIFICATION_DRAW) {
Point prev;
prev.offset=0;
prev.color=Color(0,0,0);
int w = get_size().x;
int h = get_size().y;
int total_w = get_size().width-get_size().height-3;
for(int i=-1;i<points.size();i++) {
Point next;
if (i+1==points.size()) {
next.color=Color(1,1,1);
next.offset=1;
} else {
next=points[i+1];
}
if (prev.offset==next.offset) {
prev=next;
continue;
}
Vector<Vector2> points;
Vector<Color> colors;
points.push_back(Vector2(prev.offset*total_w,h));
points.push_back(Vector2(prev.offset*total_w,0));
points.push_back(Vector2(next.offset*total_w,0));
points.push_back(Vector2(next.offset*total_w,h));
colors.push_back(prev.color);
colors.push_back(prev.color);
colors.push_back(next.color);
colors.push_back(next.color);
draw_primitive(points,colors,Vector<Point2>());
prev=next;
}
for(int i=0;i<points.size();i++) {
Color col=i==grabbed?Color(1,0.0,0.0,0.9):Color(1,1,1,0.8);
draw_line(Vector2(points[i].offset*total_w,0),Vector2(points[i].offset*total_w,h-1),Color(0,0,0,0.7));
draw_line(Vector2(points[i].offset*total_w-1,h/2),Vector2(points[i].offset*total_w-1,h-1),col);
draw_line(Vector2(points[i].offset*total_w+1,h/2),Vector2(points[i].offset*total_w+1,h-1),col);
draw_line(Vector2(points[i].offset*total_w-1,h/2),Vector2(points[i].offset*total_w+1,h/2),col);
draw_line(Vector2(points[i].offset*total_w-1,h-1),Vector2(points[i].offset*total_w+1,h-1),col);
}
if (grabbed!=-1) {
draw_rect(Rect2(total_w+3,0,h,h),points[grabbed].color);
}
if (has_focus()) {
draw_line(Vector2(-1,-1),Vector2(total_w+1,-1),Color(1,1,1,0.6));
draw_line(Vector2(total_w+1,-1),Vector2(total_w+1,h+1),Color(1,1,1,0.6));
draw_line(Vector2(total_w+1,h+1),Vector2(-1,h+1),Color(1,1,1,0.6));
draw_line(Vector2(-1,-1),Vector2(-1,h+1),Color(1,1,1,0.6));
}
}
}
Size2 GraphColorRampEdit::get_minimum_size() const {
return Vector2(0,16);
}
void GraphColorRampEdit::_color_changed(const Color& p_color) {
if (grabbed==-1)
return;
points[grabbed].color=p_color;
update();
emit_signal("ramp_changed");
}
void GraphColorRampEdit::set_ramp(const Vector<float>& p_offsets,const Vector<Color>& p_colors) {
ERR_FAIL_COND(p_offsets.size()!=p_colors.size());
points.clear();
for(int i=0;i<p_offsets.size();i++) {
Point p;
p.offset=p_offsets[i];
p.color=p_colors[i];
points.push_back(p);
}
points.sort();
update();
}
Vector<float> GraphColorRampEdit::get_offsets() const{
Vector<float> ret;
for(int i=0;i<points.size();i++)
ret.push_back(points[i].offset);
return ret;
}
Vector<Color> GraphColorRampEdit::get_colors() const{
Vector<Color> ret;
for(int i=0;i<points.size();i++)
ret.push_back(points[i].color);
return ret;
}
void GraphColorRampEdit::_bind_methods(){
ObjectTypeDB::bind_method(_MD("_input_event"),&GraphColorRampEdit::_input_event);
ObjectTypeDB::bind_method(_MD("_color_changed"),&GraphColorRampEdit::_color_changed);
ADD_SIGNAL(MethodInfo("ramp_changed"));
}
GraphColorRampEdit::GraphColorRampEdit(){
grabbed=-1;
grabbing=false;
set_focus_mode(FOCUS_ALL);
popup = memnew( PopupPanel );
picker = memnew( ColorPicker );
popup->add_child(picker);
popup->set_child_rect(picker);
add_child(popup);
}
////////////
void GraphCurveMapEdit::_input_event(const InputEvent& p_event) {
if (p_event.type==InputEvent::KEY && p_event.key.pressed && p_event.key.scancode==KEY_DELETE && grabbed!=-1) {
points.remove(grabbed);
grabbed=-1;
update();
emit_signal("curve_changed");
accept_event();
}
if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && p_event.mouse_button.pressed) {
update();
Point2 p = Vector2(p_event.mouse_button.x,p_event.mouse_button.y)/get_size();
p.y=1.0-p.y;
grabbed=-1;
grabbing=true;
for(int i=0;i<points.size();i++) {
Vector2 ps = p*get_size();
Vector2 pt = Vector2(points[i].offset,points[i].height)*get_size();
if (ps.distance_to(pt)<4) {
grabbed=i;
}
}
//grab or select
if (grabbed!=-1) {
return;
}
//insert
Point np;
np.offset=p.x;
np.height=p.y;
points.push_back(np);
points.sort();
for(int i=0;i<points.size();i++) {
if (points[i].offset==p.x && points[i].height==p.y) {
grabbed=i;
break;
}
}
emit_signal("curve_changed");
}
if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && !p_event.mouse_button.pressed) {
if (grabbing) {
grabbing=false;
emit_signal("curve_changed");
}
update();
}
if (p_event.type==InputEvent::MOUSE_MOTION && grabbing) {
Point2 p = Vector2(p_event.mouse_button.x,p_event.mouse_button.y)/get_size();
p.y=1.0-p.y;
p.x = CLAMP(p.x,0.0,1.0);
p.y = CLAMP(p.y,0.0,1.0);
bool valid=true;
for(int i=0;i<points.size();i++) {
if (points[i].offset==p.x && points[i].height==p.y && i!=grabbed) {
valid=false;
}
}
if (!valid)
return;
points[grabbed].offset=p.x;
points[grabbed].height=p.y;
points.sort();
for(int i=0;i<points.size();i++) {
if (points[i].offset==p.x && points[i].height==p.y) {
grabbed=i;
break;
}
}
emit_signal("curve_changed");
update();
}
}
void GraphCurveMapEdit::_plot_curve(const Vector2& p_a,const Vector2& p_b,const Vector2& p_c,const Vector2& p_d) {
float geometry[4][4];
float tmp1[4][4];
float tmp2[4][4];
float deltas[4][4];
double x, dx, dx2, dx3;
double y, dy, dy2, dy3;
double d, d2, d3;
int lastx, lasty;
int newx, newy;
int ntimes;
int i,j;
int xmax=get_size().x;
int ymax=get_size().y;
/* construct the geometry matrix from the segment */
for (i = 0; i < 4; i++) {
geometry[i][2] = 0;
geometry[i][3] = 0;
}
geometry[0][0] = (p_a[0] * xmax);
geometry[1][0] = (p_b[0] * xmax);
geometry[2][0] = (p_c[0] * xmax);
geometry[3][0] = (p_d[0] * xmax);
geometry[0][1] = (p_a[1] * ymax);
geometry[1][1] = (p_b[1] * ymax);
geometry[2][1] = (p_c[1] * ymax);
geometry[3][1] = (p_d[1] * ymax);
/* subdivide the curve ntimes (1000) times */
ntimes = 4 * xmax;
/* ntimes can be adjusted to give a finer or coarser curve */
d = 1.0 / ntimes;
d2 = d * d;
d3 = d * d * d;
/* construct a temporary matrix for determining the forward differencing deltas */
tmp2[0][0] = 0; tmp2[0][1] = 0; tmp2[0][2] = 0; tmp2[0][3] = 1;
tmp2[1][0] = d3; tmp2[1][1] = d2; tmp2[1][2] = d; tmp2[1][3] = 0;
tmp2[2][0] = 6*d3; tmp2[2][1] = 2*d2; tmp2[2][2] = 0; tmp2[2][3] = 0;
tmp2[3][0] = 6*d3; tmp2[3][1] = 0; tmp2[3][2] = 0; tmp2[3][3] = 0;
/* compose the basis and geometry matrices */
static const float CR_basis[4][4] =
{
{ -0.5, 1.5, -1.5, 0.5 },
{ 1.0, -2.5, 2.0, -0.5 },
{ -0.5, 0.0, 0.5, 0.0 },
{ 0.0, 1.0, 0.0, 0.0 },
};
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
tmp1[i][j] = (CR_basis[i][0] * geometry[0][j] +
CR_basis[i][1] * geometry[1][j] +
CR_basis[i][2] * geometry[2][j] +
CR_basis[i][3] * geometry[3][j]);
}
}
/* compose the above results to get the deltas matrix */
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
deltas[i][j] = (tmp2[i][0] * tmp1[0][j] +
tmp2[i][1] * tmp1[1][j] +
tmp2[i][2] * tmp1[2][j] +
tmp2[i][3] * tmp1[3][j]);
}
}
/* extract the x deltas */
x = deltas[0][0];
dx = deltas[1][0];
dx2 = deltas[2][0];
dx3 = deltas[3][0];
/* extract the y deltas */
y = deltas[0][1];
dy = deltas[1][1];
dy2 = deltas[2][1];
dy3 = deltas[3][1];
lastx = CLAMP (x, 0, xmax);
lasty = CLAMP (y, 0, ymax);
/* if (fix255)
{
cd->curve[cd->outline][lastx] = lasty;
}
else
{
cd->curve_ptr[cd->outline][lastx] = lasty;
if(gb_debug) printf("bender_plot_curve xmax:%d ymax:%d\n", (int)xmax, (int)ymax);
}
*/
/* loop over the curve */
for (i = 0; i < ntimes; i++)
{
/* increment the x values */
x += dx;
dx += dx2;
dx2 += dx3;
/* increment the y values */
y += dy;
dy += dy2;
dy2 += dy3;
newx = CLAMP ((Math::round (x)), 0, xmax);
newy = CLAMP ((Math::round (y)), 0, ymax);
/* if this point is different than the last one...then draw it */
if ((lastx != newx) || (lasty != newy))
{
#if 0
/*
if(fix255)
{
/* use fixed array size (for the curve graph) */
cd->curve[cd->outline][newx] = newy;
}
else
{
/* use dynamic allocated curve_ptr (for the real curve) */
cd->curve_ptr[cd->outline][newx] = newy;
if(gb_debug) printf("outline: %d cX: %d cY: %d\n", (int)cd->outline, (int)newx, (int)newy);
}
#endif
draw_line(Vector2(lastx,ymax-lasty),Vector2(newx,ymax-newy),Color(0.8,0.8,0.8,0.8),2.0);
}
lastx = newx;
lasty = newy;
}
}
void GraphCurveMapEdit::_notification(int p_what){
if (p_what==NOTIFICATION_DRAW) {
draw_style_box(get_stylebox("bg","Tree"),Rect2(Point2(),get_size()));
int w = get_size().x;
int h = get_size().y;
Vector2 prev=Vector2(0,0);
Vector2 prev2=Vector2(0,0);
for(int i=-1;i<points.size();i++) {
Vector2 next;
Vector2 next2;
if (i+1>=points.size()) {
next=Vector2(1,1);
} else {
next=Vector2(points[i+1].offset,points[i+1].height);
}
if (i+2>=points.size()) {
next2=Vector2(1,1);
} else {
next2=Vector2(points[i+2].offset,points[i+2].height);
}
/*if (i==-1 && prev.offset==next.offset) {
prev=next;
continue;
}*/
_plot_curve(prev2,prev,next,next2);
prev2=prev;
prev=next;
}
for(int i=0;i<points.size();i++) {
Color col=i==grabbed?Color(1,0.0,0.0,0.9):Color(1,1,1,0.8);
draw_rect(Rect2( Vector2(points[i].offset,1.0-points[i].height)*get_size()-Vector2(2,2),Vector2(5,5)),col);
}
/* if (grabbed!=-1) {
draw_rect(Rect2(total_w+3,0,h,h),points[grabbed].color);
}
*/
if (has_focus()) {
draw_line(Vector2(-1,-1),Vector2(w+1,-1),Color(1,1,1,0.6));
draw_line(Vector2(w+1,-1),Vector2(w+1,h+1),Color(1,1,1,0.6));
draw_line(Vector2(w+1,h+1),Vector2(-1,h+1),Color(1,1,1,0.6));
draw_line(Vector2(-1,-1),Vector2(-1,h+1),Color(1,1,1,0.6));
}
}
}
Size2 GraphCurveMapEdit::get_minimum_size() const {
return Vector2(64,64);
}
void GraphCurveMapEdit::set_points(const Vector<Vector2>& p_points) {
points.clear();
for(int i=0;i<p_points.size();i++) {
Point p;
p.offset=p_points[i].x;
p.height=p_points[i].y;
points.push_back(p);
}
points.sort();
update();
}
Vector<Vector2> GraphCurveMapEdit::get_points() const {
Vector<Vector2> ret;
for(int i=0;i<points.size();i++)
ret.push_back(Vector2(points[i].offset,points[i].height));
return ret;
}
void GraphCurveMapEdit::_bind_methods(){
ObjectTypeDB::bind_method(_MD("_input_event"),&GraphCurveMapEdit::_input_event);
ADD_SIGNAL(MethodInfo("curve_changed"));
}
GraphCurveMapEdit::GraphCurveMapEdit(){
grabbed=-1;
grabbing=false;
set_focus_mode(FOCUS_ALL);
}
////cbacks
///
@ -306,6 +942,84 @@ void ShaderGraphView::_comment_edited(int p_id,Node* p_button) {
}
void ShaderGraphView::_color_ramp_changed(int p_id,Node* p_ramp) {
GraphColorRampEdit *cr=p_ramp->cast_to<GraphColorRampEdit>();
UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
Vector<float> offsets=cr->get_offsets();
Vector<Color> colors=cr->get_colors();
DVector<float> new_offsets;
DVector<Color> new_colors;
{
new_offsets.resize(offsets.size());
new_colors.resize(colors.size());
DVector<float>::Write ow=new_offsets.write();
DVector<Color>::Write cw=new_colors.write();
for(int i=0;i<new_offsets.size();i++) {
ow[i]=offsets[i];
cw[i]=colors[i];
}
}
DVector<float> old_offsets=graph->color_ramp_node_get_offsets(type,p_id);
DVector<Color> old_colors=graph->color_ramp_node_get_colors(type,p_id);
if (old_offsets.size()!=new_offsets.size())
ur->create_action("Add/Remove to Color Ramp");
else
ur->create_action("Modify Color Ramp",true);
ur->add_do_method(graph.ptr(),"color_ramp_node_set_ramp",type,p_id,new_colors,new_offsets);
ur->add_undo_method(graph.ptr(),"color_ramp_node_set_ramp",type,p_id,old_colors,old_offsets);
ur->add_do_method(this,"_update_graph");
ur->add_undo_method(this,"_update_graph");
block_update=true;
ur->commit_action();
block_update=false;
}
void ShaderGraphView::_curve_changed(int p_id,Node* p_curve) {
GraphCurveMapEdit *cr=p_curve->cast_to<GraphCurveMapEdit>();
UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
Vector<Point2> points=cr->get_points();
DVector<Vector2> new_points;
{
new_points.resize(points.size());
DVector<Vector2>::Write ow=new_points.write();
for(int i=0;i<new_points.size();i++) {
ow[i]=points[i];
}
}
DVector<Vector2> old_points=graph->curve_map_node_get_points(type,p_id);
if (old_points.size()!=new_points.size())
ur->create_action("Add/Remove to Curve Map");
else
ur->create_action("Modify Curve Map",true);
ur->add_do_method(graph.ptr(),"curve_map_node_set_points",type,p_id,new_points);
ur->add_undo_method(graph.ptr(),"curve_map_node_set_points",type,p_id,old_points);
ur->add_do_method(this,"_update_graph");
ur->add_undo_method(this,"_update_graph");
block_update=true;
ur->commit_action();
block_update=false;
}
void ShaderGraphView::_input_name_changed(const String& p_name, int p_id, Node *p_line_edit) {
@ -1026,6 +1740,96 @@ void ShaderGraphView::_create_node(int p_id) {
gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],false,0,Color());
} break; // vec3 interpolation (with optional curve)
case ShaderGraph::NODE_COLOR_RAMP: {
gn->set_title("ColorRamp");
GraphColorRampEdit * ramp = memnew( GraphColorRampEdit );
DVector<real_t> offsets = graph->color_ramp_node_get_offsets(type,p_id);
DVector<Color> colors = graph->color_ramp_node_get_colors(type,p_id);
int oc = offsets.size();
if (oc) {
DVector<real_t>::Read rofs = offsets.read();
DVector<Color>::Read rcol = colors.read();
Vector<float> ofsv;
Vector<Color> colorv;
for(int i=0;i<oc;i++) {
ofsv.push_back(rofs[i]);
colorv.push_back(rcol[i]);
}
ramp->set_ramp(ofsv,colorv);
}
ramp->connect("ramp_changed",this,"_color_ramp_changed",varray(p_id,ramp));
ramp->set_custom_minimum_size(Size2(128,1));
gn->add_child(ramp);
HBoxContainer *hbc = memnew( HBoxContainer );
hbc->add_constant_override("separation",0);
hbc->add_child( memnew(Label("c")));
hbc->add_spacer();
Label *l=memnew(Label("rgb"));
l->set_align(Label::ALIGN_RIGHT);
hbc->add_child( l);
gn->add_child(hbc);
l=memnew(Label("alpha"));
l->set_align(Label::ALIGN_RIGHT);
gn->add_child( l);
gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
gn->set_slot(2,false,ShaderGraph::SLOT_MAX,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
} break; // scalar interpolation (with optional curve)
case ShaderGraph::NODE_CURVE_MAP: {
gn->set_title("CurveMap");
GraphCurveMapEdit * map = memnew( GraphCurveMapEdit );
DVector<Vector2> points = graph->curve_map_node_get_points(type,p_id);
int oc = points.size();
if (oc) {
DVector<Vector2>::Read rofs = points.read();
Vector<Vector2> ofsv;
for(int i=0;i<oc;i++) {
ofsv.push_back(rofs[i]);
}
map->set_points(ofsv);
}
map->connect("curve_changed",this,"_curve_changed",varray(p_id,map));
//map->connect("map_changed",this,"_curve_map_changed",varray(p_id,map));
map->set_custom_minimum_size(Size2(128,64));
gn->add_child(map);
HBoxContainer *hbc = memnew( HBoxContainer );
hbc->add_constant_override("separation",0);
hbc->add_child( memnew(Label("c")));
hbc->add_spacer();
Label *l=memnew(Label("cmap"));
l->set_align(Label::ALIGN_RIGHT);
hbc->add_child( l);
gn->add_child(hbc);
gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
} break; // scalar interpolation (with optional curve)
case ShaderGraph::NODE_SCALAR_INPUT: {
gn->set_title("ScalarUniform");
@ -1173,6 +1977,28 @@ void ShaderGraphView::_create_node(int p_id) {
gn->set_slot(3,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
} break; // cubemap input (assignable in material)
case ShaderGraph::NODE_DEFAULT_TEXTURE: {
gn->set_title("CanvasItemTex");
HBoxContainer *hbc = memnew( HBoxContainer );
hbc->add_constant_override("separation",0);
hbc->add_child( memnew(Label("UV")));
hbc->add_spacer();
Label *l=memnew(Label("RGB"));
l->set_align(Label::ALIGN_RIGHT);
hbc->add_child(l);
gn->add_child(hbc);
l = memnew( Label );
l->set_text("Alpha");
l->set_align(Label::ALIGN_RIGHT);
gn->add_child(l);
gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
gn->set_slot(1,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
} break; // screen texture sampler (takes UV) (only usable in fragment case Shader)
case ShaderGraph::NODE_OUTPUT: {
gn->set_title("Output");
@ -1360,6 +2186,8 @@ void ShaderGraphView::_bind_methods() {
ObjectTypeDB::bind_method("_variant_edited",&ShaderGraphView::_variant_edited);
ObjectTypeDB::bind_method("_cube_edited",&ShaderGraphView::_cube_edited);
ObjectTypeDB::bind_method("_comment_edited",&ShaderGraphView::_comment_edited);
ObjectTypeDB::bind_method("_color_ramp_changed",&ShaderGraphView::_color_ramp_changed);
ObjectTypeDB::bind_method("_curve_changed",&ShaderGraphView::_curve_changed);
ObjectTypeDB::bind_method("_sg_updated",&ShaderGraphView::_sg_updated);
}
@ -1455,13 +2283,16 @@ const char* ShaderGraphEditor::node_names[ShaderGraph::NODE_TYPE_MAX]={
"GraphVecsToXform:Vectors -> XForm:", // 3 vec input", 1 xform output
"GraphScalarInterp:Scalar Interpolate", // scalar interpolation (with optional curve)
"GraphVecInterp:Vector Interpolate:", // vec3 interpolation (with optional curve)
"GraphScalarUniform:Scalar Uniform", // scalar uniform (assignable in material)
"GraphColorRamp:Color Ramp", // vec3 interpolation (with optional curve)
"GraphCurveMap:Curve Remap:", // vec3 interpolation (with optional curve)
"GraphScalarUniform:Scalar Uniform", // scalar uniform (assignable in material)
"GraphVectorUniform:Vector Uniform", // vec3 uniform (assignable in material)
"GraphRgbUniform:RGB Uniform", // color uniform (assignable in material)
"GraphXformUniform:XForm Uniform", // mat4 uniform (assignable in material)
"GraphTextureUniform:Texture Uniform", // texture input (assignable in material)
"GraphCubeUniform:CubeMap Uniform:", // cubemap input (assignable in material)
"Output", // output (shader type dependent)
"GraphDefaultTexture:CanvasItem Texture:", // cubemap input (assignable in material)
"Output", // output (shader type dependent)
"GraphComment:Comment", // comment

View file

@ -45,6 +45,77 @@
*/
class GraphColorRampEdit : public Control {
OBJ_TYPE(GraphColorRampEdit,Control);
struct Point {
float offset;
Color color;
bool operator<(const Point& p_ponit) const {
return offset<p_ponit.offset;
}
};
PopupPanel *popup;
ColorPicker *picker;
bool grabbing;
int grabbed;
float grabbed_at;
Vector<Point> points;
void _color_changed(const Color& p_color);
protected:
void _input_event(const InputEvent& p_event);
void _notification(int p_what);
static void _bind_methods();
public:
void set_ramp(const Vector<float>& p_offsets,const Vector<Color>& p_colors);
Vector<float> get_offsets() const;
Vector<Color> get_colors() const;
virtual Size2 get_minimum_size() const;
GraphColorRampEdit();
};
class GraphCurveMapEdit : public Control {
OBJ_TYPE(GraphCurveMapEdit,Control);
struct Point {
float offset;
float height;
bool operator<(const Point& p_ponit) const {
return offset<p_ponit.offset;
}
};
bool grabbing;
int grabbed;
Vector<Point> points;
void _plot_curve(const Vector2& p_a,const Vector2& p_b,const Vector2& p_c,const Vector2& p_d);
protected:
void _input_event(const InputEvent& p_event);
void _notification(int p_what);
static void _bind_methods();
public:
void set_points(const Vector<Vector2>& p_points);
Vector<Vector2> get_points() const;
virtual Size2 get_minimum_size() const;
GraphCurveMapEdit();
};
class ShaderGraphView : public Node {
OBJ_TYPE(ShaderGraphView,Node);
@ -95,7 +166,8 @@ class ShaderGraphView : public Node {
void _cube_edited(int p_id,Node* p_button);
void _variant_edited();
void _comment_edited(int p_id,Node* p_button);
void _color_ramp_changed(int p_id,Node* p_ramp);
void _curve_changed(int p_id,Node* p_curve);
void _sg_updated();
Map<int,GraphNode*> node_map;
protected: