2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* shader.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "shader.h"
|
|
|
|
#include "os/file_access.h"
|
|
|
|
#include "scene/scene_string_names.h"
|
2017-03-19 00:36:26 +01:00
|
|
|
#include "servers/visual_server.h"
|
|
|
|
#include "texture.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Shader::Mode Shader::get_mode() const {
|
|
|
|
|
2015-06-07 03:06:58 +02:00
|
|
|
return mode;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Shader::set_code(const String &p_vertex, const String &p_fragment, const String &p_light, int p_fragment_ofs, int p_light_ofs) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
VisualServer::get_singleton()->shader_set_code(shader, p_vertex, p_fragment, p_light, 0, p_fragment_ofs, p_light_ofs);
|
|
|
|
params_cache_dirty = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->changed);
|
|
|
|
}
|
|
|
|
|
|
|
|
String Shader::get_vertex_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);
|
|
|
|
}
|
|
|
|
|
2014-06-28 04:21:45 +02:00
|
|
|
String Shader::get_light_code() const {
|
|
|
|
|
|
|
|
return VisualServer::get_singleton()->shader_get_light_code(shader);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool Shader::has_param(const StringName &p_param) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (params_cache_dirty)
|
|
|
|
get_param_list(NULL);
|
|
|
|
|
|
|
|
return (params_cache.has(p_param));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::get_param_list(List<PropertyInfo> *p_params) const {
|
|
|
|
|
|
|
|
List<PropertyInfo> local;
|
2017-03-19 00:36:26 +01:00
|
|
|
VisualServer::get_singleton()->shader_get_param_list(shader, &local);
|
2014-02-10 02:10:30 +01:00
|
|
|
params_cache.clear();
|
2017-03-19 00:36:26 +01:00
|
|
|
params_cache_dirty = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<PropertyInfo>::Element *E = local.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
PropertyInfo pi = E->get();
|
|
|
|
pi.name = "shader_param/" + pi.name;
|
|
|
|
params_cache[pi.name] = E->get().name;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_params) {
|
|
|
|
|
|
|
|
//small little hack
|
2017-03-19 00:36:26 +01:00
|
|
|
if (pi.type == Variant::_RID)
|
|
|
|
pi.type = Variant::OBJECT;
|
2014-02-10 02:10:30 +01:00
|
|
|
p_params->push_back(pi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-06-28 04:21:45 +02:00
|
|
|
String ls = VisualServer::get_singleton()->shader_get_light_code(shader);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Dictionary c;
|
2017-03-19 00:36:26 +01:00
|
|
|
c["fragment"] = fs;
|
|
|
|
c["fragment_ofs"] = 0;
|
|
|
|
c["vertex"] = vs;
|
|
|
|
c["vertex_ofs"] = 0;
|
|
|
|
c["light"] = ls;
|
|
|
|
c["light_ofs"] = 0;
|
2014-12-21 15:42:44 +01:00
|
|
|
Array arr;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (const Map<StringName, Ref<Texture> >::Element *E = default_textures.front(); E; E = E->next()) {
|
2014-12-21 15:42:44 +01:00
|
|
|
arr.push_back(E->key());
|
|
|
|
arr.push_back(E->get());
|
|
|
|
}
|
|
|
|
if (arr.size())
|
2017-03-19 00:36:26 +01:00
|
|
|
c["default_tex"] = arr;
|
2014-02-10 02:10:30 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Shader::_set_code(const Dictionary &p_string) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!p_string.has("fragment"));
|
|
|
|
ERR_FAIL_COND(!p_string.has("vertex"));
|
2014-06-28 04:21:45 +02:00
|
|
|
String light;
|
|
|
|
if (p_string.has("light"))
|
2017-03-19 00:36:26 +01:00
|
|
|
light = p_string["light"];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
set_code(p_string["vertex"], p_string["fragment"], light);
|
2014-12-21 15:42:44 +01:00
|
|
|
if (p_string.has("default_tex")) {
|
2017-03-19 00:36:26 +01:00
|
|
|
Array arr = p_string["default_tex"];
|
|
|
|
if ((arr.size() & 1) == 0) {
|
|
|
|
for (int i = 0; i < arr.size(); i += 2) {
|
2015-04-09 05:29:21 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
set_default_texture_param(arr[i], arr[i + 1]);
|
2015-04-09 05:29:21 +02:00
|
|
|
}
|
2014-12-21 15:42:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Shader::set_default_texture_param(const StringName &p_param, const Ref<Texture> &p_texture) {
|
2014-12-21 15:42:44 +01:00
|
|
|
|
2015-01-07 05:45:46 +01:00
|
|
|
if (p_texture.is_valid()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
default_textures[p_param] = p_texture;
|
|
|
|
VS::get_singleton()->shader_set_default_texture_param(shader, p_param, p_texture->get_rid());
|
2015-01-07 05:45:46 +01:00
|
|
|
} else {
|
2014-12-21 15:42:44 +01:00
|
|
|
default_textures.erase(p_param);
|
2017-03-19 00:36:26 +01:00
|
|
|
VS::get_singleton()->shader_set_default_texture_param(shader, p_param, RID());
|
2015-01-07 05:45:46 +01:00
|
|
|
}
|
2014-12-21 15:42:44 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Ref<Texture> Shader::get_default_texture_param(const StringName &p_param) const {
|
2014-12-21 15:42:44 +01:00
|
|
|
|
|
|
|
if (default_textures.has(p_param))
|
|
|
|
return default_textures[p_param];
|
|
|
|
else
|
|
|
|
return Ref<Texture>();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Shader::get_default_texture_param_list(List<StringName> *r_textures) const {
|
2014-12-21 15:42:44 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (const Map<StringName, Ref<Texture> >::Element *E = default_textures.front(); E; E = E->next()) {
|
2014-12-21 15:42:44 +01:00
|
|
|
|
|
|
|
r_textures->push_back(E->key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Shader::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_mode"), &Shader::get_mode);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
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);
|
2014-12-21 15:42:44 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("has_param", "name"), &Shader::has_param);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_code", "code"), &Shader::_set_code);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_get_code"), &Shader::_get_code);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//ObjectTypeDB::bind_method(_MD("get_param_list"),&Shader::get_fragment_code);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "_code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_code"), _SCS("_get_code"));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
BIND_CONSTANT(MODE_MATERIAL);
|
|
|
|
BIND_CONSTANT(MODE_CANVAS_ITEM);
|
|
|
|
BIND_CONSTANT(MODE_POST_PROCESS);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 20:52:37 +01:00
|
|
|
Shader::Shader(Mode p_mode) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
mode = p_mode;
|
2015-01-03 20:52:37 +01:00
|
|
|
shader = VisualServer::get_singleton()->shader_create(VS::ShaderMode(p_mode));
|
2017-03-19 00:36:26 +01:00
|
|
|
params_cache_dirty = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Shader::~Shader() {
|
|
|
|
|
|
|
|
VisualServer::get_singleton()->free(shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
/************ Loader from text ***************/
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
RES ResourceFormatLoaderShader::load(const String &p_path, const String &p_original_path, Error *r_error) {
|
2015-08-24 01:15:56 +02:00
|
|
|
|
|
|
|
if (r_error)
|
2017-03-19 00:36:26 +01:00
|
|
|
*r_error = ERR_FILE_CANT_OPEN;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String fragment_code;
|
|
|
|
String vertex_code;
|
2014-06-28 04:21:45 +02:00
|
|
|
String light_code;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int mode = -1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Error err;
|
2017-03-19 00:36:26 +01:00
|
|
|
FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN("Unable to open shader file: " + p_path);
|
|
|
|
ERR_FAIL_COND_V(err, RES());
|
2014-02-10 02:10:30 +01:00
|
|
|
String base_path = p_path.get_base_dir();
|
|
|
|
|
2015-08-24 01:15:56 +02:00
|
|
|
if (r_error)
|
2017-03-19 00:36:26 +01:00
|
|
|
*r_error = ERR_FILE_CORRUPT;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Ref<Shader> shader; //( memnew( Shader ) );
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int line = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
while (!f->eof_reached()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String l = f->get_line();
|
|
|
|
line++;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (mode <= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
l = l.strip_edges();
|
|
|
|
int comment = l.find(";");
|
2017-03-19 00:36:26 +01:00
|
|
|
if (comment != -1)
|
|
|
|
l = l.substr(0, comment);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (mode < 1)
|
|
|
|
vertex_code += "\n";
|
|
|
|
if (mode < 2)
|
|
|
|
fragment_code += "\n";
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (mode < 1 && l == "")
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (l.begins_with("[")) {
|
2017-03-19 00:36:26 +01:00
|
|
|
l = l.strip_edges();
|
|
|
|
if (l == "[params]") {
|
|
|
|
if (mode >= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Misplaced [params] section.");
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
mode = 0;
|
|
|
|
} else if (l == "[vertex]") {
|
|
|
|
if (mode >= 1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Misplaced [vertex] section.");
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
mode = 1;
|
|
|
|
} else if (l == "[fragment]") {
|
|
|
|
if (mode >= 2) {
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Misplaced [fragment] section.");
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
mode = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Unknown section type: '" + l + "'.");
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (mode == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int eqpos = l.find("=");
|
2017-03-19 00:36:26 +01:00
|
|
|
if (eqpos == -1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Expected '='.");
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String right = l.substr(eqpos + 1, l.length()).strip_edges();
|
|
|
|
if (right == "") {
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Expected value after '='.");
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant value;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (right == "true") {
|
2014-02-10 02:10:30 +01:00
|
|
|
value = true;
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (right == "false") {
|
2014-02-10 02:10:30 +01:00
|
|
|
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("(");
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (popenpos == -1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid constructor syntax: " + right);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
|
|
|
int pclosepos = right.find_last(")");
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (pclosepos == -1) {
|
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid constructor parameter syntax: " + right);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String type = right.substr(0, popenpos);
|
|
|
|
String param = right.substr(popenpos + 1, pclosepos - popenpos - 1).strip_edges();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (type == "tex") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (param == "") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
value = RID();
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
String path;
|
|
|
|
|
|
|
|
if (param.is_abs_path())
|
2017-03-19 00:36:26 +01:00
|
|
|
path = param;
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-19 00:36:26 +01:00
|
|
|
path = base_path + "/" + param;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Ref<Texture> texture = ResourceLoader::load(path);
|
|
|
|
if (!texture.is_valid()) {
|
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Couldn't find icon at path: " + path);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
value = texture;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (type == "vec3") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (param == "") {
|
|
|
|
value = Vector3();
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
Vector<String> params = param.split(",");
|
2017-03-19 00:36:26 +01:00
|
|
|
if (params.size() != 3) {
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid param count for vec3(): '" + right + "'.");
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 v;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
v[i] = params[i].to_double();
|
|
|
|
value = v;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (type == "xform") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (param == "") {
|
|
|
|
value = Transform();
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
Vector<String> params = param.split(",");
|
2017-03-19 00:36:26 +01:00
|
|
|
if (params.size() != 12) {
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid param count for xform(): '" + right + "'.");
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform t;
|
2017-03-19 00:36:26 +01:00
|
|
|
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();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
value = t;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
memdelete(f);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid constructor type: '" + type + "'.");
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String left = l.substr(0, eqpos);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
// shader->set_param(left,value);
|
|
|
|
} else if (mode == 1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
vertex_code += l;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (mode == 2) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
fragment_code += l;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
shader->set_code(vertex_code, fragment_code, light_code);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
2015-08-24 01:15:56 +02:00
|
|
|
if (r_error)
|
2017-03-19 00:36:26 +01:00
|
|
|
*r_error = OK;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
|
2015-11-28 01:11:20 +01:00
|
|
|
ObjectTypeDB::get_extensions_for_type("Shader", p_extensions);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2015-11-28 01:11:20 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool ResourceFormatLoaderShader::handles_type(const String &p_type) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-28 01:11:20 +01:00
|
|
|
return ObjectTypeDB::is_type(p_type, "Shader");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_path.extension().to_lower() == "shd")
|
2014-02-10 02:10:30 +01:00
|
|
|
return "Shader";
|
|
|
|
return "";
|
|
|
|
}
|