Path: blob/master/editor/shader/text_shader_language_plugin.cpp
14709 views
/**************************************************************************/1/* text_shader_language_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "text_shader_language_plugin.h"3132#include "text_shader_editor.h"3334#include "core/string/string_builder.h"35#include "servers/rendering/shader_types.h"3637bool TextShaderLanguagePlugin::handles_shader(const Ref<Shader> &p_shader) const {38// The text shader editor only edits the base Shader class,39// not classes that inherit from it like VisualShader.40return p_shader->get_class_name() == Shader::get_class_static();41}4243bool TextShaderLanguagePlugin::handles_shader_include(const Ref<ShaderInclude> &p_shader_inc) const {44return p_shader_inc->get_class_static() == ShaderInclude::get_class_static();45}4647ShaderEditor *TextShaderLanguagePlugin::edit_shader(const Ref<Shader> &p_shader) {48TextShaderEditor *editor = memnew(TextShaderEditor);49editor->edit_shader(p_shader);50return editor;51}5253ShaderEditor *TextShaderLanguagePlugin::edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) {54TextShaderEditor *editor = memnew(TextShaderEditor);55editor->edit_shader_include(p_shader_inc);56return editor;57}5859Ref<Shader> TextShaderLanguagePlugin::create_new_shader(int p_variation_index, Shader::Mode p_shader_mode, int p_template_index) {60Ref<Shader> shader;61shader.instantiate();6263StringBuilder code;64const String &shader_type = ShaderTypes::get_singleton()->get_types_list().get(p_shader_mode);65code += vformat("shader_type %s;\n", shader_type);6667if (p_template_index == 0) { // Default template.68switch (p_shader_mode) {69case Shader::MODE_SPATIAL: {70code += R"(71void vertex() {72// Called for every vertex the material is visible on.73}7475void fragment() {76// Called for every pixel the material is visible on.77}7879//void light() {80// // Called for every pixel for every light affecting the material.81// // Uncomment to replace the default light processing function with this one.82//}83)";84} break;85case Shader::MODE_CANVAS_ITEM: {86code += R"(87void vertex() {88// Called for every vertex the material is visible on.89}9091void fragment() {92// Called for every pixel the material is visible on.93}9495//void light() {96// // Called for every pixel for every light affecting the CanvasItem.97// // Uncomment to replace the default light processing function with this one.98//}99)";100} break;101case Shader::MODE_PARTICLES: {102code += R"(103void start() {104// Called when a particle is spawned.105}106107void process() {108// Called every frame on existing particles (according to the Fixed FPS property).109}110)";111} break;112case Shader::MODE_SKY: {113code += R"(114void sky() {115// Called for every visible pixel in the sky background, as well as all pixels116// in the radiance cubemap.117}118)";119} break;120case Shader::MODE_FOG: {121code += R"(122void fog() {123// Called once for every froxel that is touched by an axis-aligned bounding box124// of the associated FogVolume. This means that froxels that just barely touch125// a given FogVolume will still be used.126}127)";128} break;129case Shader::MODE_MAX: {130ERR_FAIL_V_MSG(Ref<Shader>(), "Invalid shader mode for text shader editor.");131} break;132}133}134shader->set_code(code.as_string());135return shader;136}137138Ref<ShaderInclude> TextShaderLanguagePlugin::create_new_shader_include() {139Ref<ShaderInclude> shader_inc;140shader_inc.instantiate();141return shader_inc;142}143144PackedStringArray TextShaderLanguagePlugin::get_language_variations() const {145return PackedStringArray{ "Shader", "ShaderInclude" };146}147148String TextShaderLanguagePlugin::get_file_extension(int p_variation_index) const {149if (p_variation_index == 0) {150return "gdshader";151} else if (p_variation_index == 1) {152return "gdshaderinc";153}154return "tres";155}156157158