diff options
author | Juan Linietsky <reduzio@gmail.com> | 2017-01-02 19:12:25 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2017-01-02 19:12:25 -0300 |
commit | ce26eb74bca48f16e9a34b4eb1c34e50dfc5daae (patch) | |
tree | 5f9c387037d0142d40f7275575436483dc0a7237 /scene/resources/shader.cpp | |
parent | ab4126f51061277e87b41c48b40e7b54942d4eca (diff) | |
parent | 45c5c89de961357a7042d9e1f063e288d7a510cf (diff) |
Merge branch 'master' of https://github.com/godotengine/godot
Diffstat (limited to 'scene/resources/shader.cpp')
-rw-r--r-- | scene/resources/shader.cpp | 340 |
1 files changed, 13 insertions, 327 deletions
diff --git a/scene/resources/shader.cpp b/scene/resources/shader.cpp index ee9f23dd2a..3ce1a00af4 100644 --- a/scene/resources/shader.cpp +++ b/scene/resources/shader.cpp @@ -5,7 +5,7 @@ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -39,37 +39,17 @@ Shader::Mode Shader::get_mode() const { return mode; } -void Shader::set_code( const String& p_vertex, const String& p_fragment, const String& p_light,int p_fragment_ofs,int p_light_ofs) { +void Shader::set_code(const String& p_code) { - VisualServer::get_singleton()->shader_set_code(shader,p_vertex,p_fragment,p_light,0,p_fragment_ofs,p_light_ofs); + VisualServer::get_singleton()->shader_set_code(shader,p_code); params_cache_dirty=true; emit_signal(SceneStringNames::get_singleton()->changed); } -String Shader::get_vertex_code() const { +String Shader::get_code() const { - return VisualServer::get_singleton()->shader_get_vertex_code(shader); -} - -String Shader::get_fragment_code() const { - - return VisualServer::get_singleton()->shader_get_fragment_code(shader); - -} - -String Shader::get_light_code() const { - - return VisualServer::get_singleton()->shader_get_light_code(shader); - -} - -bool Shader::has_param(const StringName& p_param) const { - - if (params_cache_dirty) - get_param_list(NULL); - - return (params_cache.has(p_param)); + return VisualServer::get_singleton()->shader_get_code(shader); } @@ -101,48 +81,6 @@ RID Shader::get_rid() const { return shader; } -Dictionary Shader::_get_code() { - - String fs = VisualServer::get_singleton()->shader_get_fragment_code(shader); - String vs = VisualServer::get_singleton()->shader_get_vertex_code(shader); - String ls = VisualServer::get_singleton()->shader_get_light_code(shader); - - Dictionary c; - c["fragment"]=fs; - c["fragment_ofs"]=0; - c["vertex"]=vs; - c["vertex_ofs"]=0; - c["light"]=ls; - c["light_ofs"]=0; - Array arr; - for(const Map<StringName,Ref<Texture> >::Element *E=default_textures.front();E;E=E->next()) { - arr.push_back(E->key()); - arr.push_back(E->get()); - } - if (arr.size()) - c["default_tex"]=arr; - return c; -} - -void Shader::_set_code(const Dictionary& p_string) { - - ERR_FAIL_COND(!p_string.has("fragment")); - ERR_FAIL_COND(!p_string.has("vertex")); - String light; - if (p_string.has("light")) - light=p_string["light"]; - - set_code(p_string["vertex"],p_string["fragment"],light); - if (p_string.has("default_tex")) { - Array arr=p_string["default_tex"]; - if ((arr.size()&1)==0) { - for(int i=0;i<arr.size();i+=2) { - - set_default_texture_param(arr[i],arr[i+1]); - } - } - } -} void Shader::set_default_texture_param(const StringName& p_param,const Ref<Texture>& p_texture) { @@ -171,33 +109,31 @@ void Shader::get_default_texture_param_list(List<StringName>* r_textures) const{ } } +bool Shader::has_param(const StringName& p_param) const { + return params_cache.has(p_param); +} void Shader::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_mode"),&Shader::get_mode); - ObjectTypeDB::bind_method(_MD("set_code","vcode","fcode","lcode","fofs","lofs"),&Shader::set_code,DEFVAL(0),DEFVAL(0)); - ObjectTypeDB::bind_method(_MD("get_vertex_code"),&Shader::get_vertex_code); - ObjectTypeDB::bind_method(_MD("get_fragment_code"),&Shader::get_fragment_code); - ObjectTypeDB::bind_method(_MD("get_light_code"),&Shader::get_light_code); + ObjectTypeDB::bind_method(_MD("set_code","code"),&Shader::set_code); + ObjectTypeDB::bind_method(_MD("get_code"),&Shader::get_code); ObjectTypeDB::bind_method(_MD("set_default_texture_param","param","texture:Texture"),&Shader::set_default_texture_param); ObjectTypeDB::bind_method(_MD("get_default_texture_param:Texture","param"),&Shader::get_default_texture_param); ObjectTypeDB::bind_method(_MD("has_param","name"),&Shader::has_param); - ObjectTypeDB::bind_method(_MD("_set_code","code"),&Shader::_set_code); - ObjectTypeDB::bind_method(_MD("_get_code"),&Shader::_get_code); - //ObjectTypeDB::bind_method(_MD("get_param_list"),&Shader::get_fragment_code); - ADD_PROPERTY( PropertyInfo(Variant::STRING, "_code",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_code"), _SCS("_get_code") ); + ADD_PROPERTY( PropertyInfo(Variant::STRING, "code",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("set_code"), _SCS("get_code") ); - BIND_CONSTANT( MODE_MATERIAL ); + BIND_CONSTANT( MODE_SPATIAL); BIND_CONSTANT( MODE_CANVAS_ITEM ); - BIND_CONSTANT( MODE_POST_PROCESS ); + BIND_CONSTANT( MODE_PARTICLES ); } @@ -214,253 +150,3 @@ Shader::~Shader() { } - -/************ Loader from text ***************/ - - - -RES ResourceFormatLoaderShader::load(const String &p_path, const String& p_original_path, Error *r_error) { - - if (r_error) - *r_error=ERR_FILE_CANT_OPEN; - - String fragment_code; - String vertex_code; - String light_code; - - int mode=-1; - - Error err; - FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err); - - - ERR_EXPLAIN("Unable to open shader file: "+p_path); - ERR_FAIL_COND_V(err,RES()); - String base_path = p_path.get_base_dir(); - - if (r_error) - *r_error=ERR_FILE_CORRUPT; - - Ref<Shader> shader;//( memnew( Shader ) ); - - int line=0; - - while(!f->eof_reached()) { - - String l = f->get_line(); - line++; - - if (mode<=0) { - l = l.strip_edges(); - int comment = l.find(";"); - if (comment!=-1) - l=l.substr(0,comment); - } - - if (mode<1) - vertex_code+="\n"; - if (mode<2) - fragment_code+="\n"; - - if (mode < 1 && l=="") - continue; - - if (l.begins_with("[")) { - l=l.strip_edges(); - if (l=="[params]") { - if (mode>=0) { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Misplaced [params] section."); - ERR_FAIL_V(RES()); - } - mode=0; - } else if (l=="[vertex]") { - if (mode>=1) { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Misplaced [vertex] section."); - ERR_FAIL_V(RES()); - } - mode=1; - } else if (l=="[fragment]") { - if (mode>=2) { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Misplaced [fragment] section."); - ERR_FAIL_V(RES()); - } - mode=1; - } else { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Unknown section type: '"+l+"'."); - ERR_FAIL_V(RES()); - } - continue; - } - - if (mode==0) { - - int eqpos = l.find("="); - if (eqpos==-1) { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Expected '='."); - ERR_FAIL_V(RES()); - } - - - String right=l.substr(eqpos+1,l.length()).strip_edges(); - if (right=="") { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Expected value after '='."); - ERR_FAIL_V(RES()); - } - - Variant value; - - if (right=="true") { - value = true; - } else if (right=="false") { - value = false; - } else if (right.is_valid_float()) { - //is number - value = right.to_double(); - } else if (right.is_valid_html_color()) { - //is html color - value = Color::html(right); - } else { - //attempt to parse a constructor - int popenpos = right.find("("); - - if (popenpos==-1) { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid constructor syntax: "+right); - ERR_FAIL_V(RES()); - } - - int pclosepos = right.find_last(")"); - - if (pclosepos==-1) { - ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid constructor parameter syntax: "+right); - ERR_FAIL_V(RES()); - - } - - String type = right.substr(0,popenpos); - String param = right.substr(popenpos+1,pclosepos-popenpos-1).strip_edges(); - - - if (type=="tex") { - - if (param=="") { - - value=RID(); - } else { - - String path; - - if (param.is_abs_path()) - path=param; - else - path=base_path+"/"+param; - - Ref<Texture> texture = ResourceLoader::load(path); - if (!texture.is_valid()) { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Couldn't find icon at path: "+path); - ERR_FAIL_V(RES()); - } - - value=texture; - } - - } else if (type=="vec3") { - - if (param=="") { - value=Vector3(); - } else { - Vector<String> params = param.split(","); - if (params.size()!=3) { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid param count for vec3(): '"+right+"'."); - ERR_FAIL_V(RES()); - - } - - Vector3 v; - for(int i=0;i<3;i++) - v[i]=params[i].to_double(); - value=v; - } - - - } else if (type=="xform") { - - if (param=="") { - value=Transform(); - } else { - - Vector<String> params = param.split(","); - if (params.size()!=12) { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid param count for xform(): '"+right+"'."); - ERR_FAIL_V(RES()); - - } - - Transform t; - for(int i=0;i<9;i++) - t.basis[i%3][i/3]=params[i].to_double(); - for(int i=0;i<3;i++) - t.origin[i]=params[i-9].to_double(); - - value=t; - } - - } else { - memdelete(f); - ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid constructor type: '"+type+"'."); - ERR_FAIL_V(RES()); - - } - - } - - String left= l.substr(0,eqpos); - -// shader->set_param(left,value); - } else if (mode==1) { - - vertex_code+=l; - - } else if (mode==2) { - - fragment_code+=l; - } - } - - shader->set_code(vertex_code,fragment_code,light_code); - - f->close(); - memdelete(f); - if (r_error) - *r_error=OK; - - return shader; -} - -void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const { - - ObjectTypeDB::get_extensions_for_type("Shader", p_extensions); -} - -bool ResourceFormatLoaderShader::handles_type(const String& p_type) const { - - return ObjectTypeDB::is_type(p_type, "Shader"); -} - - -String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const { - - if (p_path.extension().to_lower()=="shd") - return "Shader"; - return ""; -} - |