<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Shaders on Godot 4 レシピ</title><link>https://kamera25.github.io/godot_recipes/4.x/shaders/index.html</link><description>Recent content in Shaders on Godot 4 レシピ</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="https://kamera25.github.io/godot_recipes/4.x/shaders/index.xml" rel="self" type="application/rss+xml"/><item><title>Shaders: intro</title><link>https://kamera25.github.io/godot_recipes/4.x/shaders/intro/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/shaders/intro/index.html</guid><description>&lt;div class="box notices cstyle tip"&gt;
&lt;div class="box-label"&gt;&lt;i class="fa-fw fas fa-lightbulb"&gt;&lt;/i&gt; Tip&lt;/div&gt;
&lt;div class="box-content"&gt;
&lt;p&gt;This article is being updated from Godot 3 to Godot 4.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You want to get started coding shaders.&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;p&gt;A shader is a special program that runs on the computer&amp;rsquo;s GPU (graphics card). The GPU is optimized to perform certain types of math very efficiently. Shader code can be attached to objects to affect how they&amp;rsquo;re rendered on the screen.&lt;/p&gt;
&lt;p&gt;The output of a shader program is the color of the set of pixels of the object. Shaders can be used in 2d (&lt;code&gt;canvas_item&lt;/code&gt; shaders) and 3D (&lt;code&gt;spatial&lt;/code&gt; shaders).&lt;/p&gt;</description></item><item><title>Interacting with Shaders</title><link>https://kamera25.github.io/godot_recipes/4.x/shaders/interacting/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/shaders/interacting/index.html</guid><description>&lt;div class="box notices cstyle tip"&gt;
&lt;div class="box-label"&gt;&lt;i class="fa-fw fas fa-lightbulb"&gt;&lt;/i&gt; Tip&lt;/div&gt;
&lt;div class="box-content"&gt;
&lt;p&gt;This article is being updated from Godot 3 to Godot 4.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You want to interact with a Godot shader from GDScript.&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;p&gt;To access the uniform&amp;rsquo;s value from GDScript, you can use &lt;code&gt;set_shader_param()&lt;/code&gt; on the object&amp;rsquo;s &lt;code&gt;material&lt;/code&gt; property. If the attached material is a ShaderMaterial, then you can access it like so:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-gdscript" data-lang="gdscript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;node&lt;span style="color:#81a1c1"&gt;.&lt;/span&gt;material&lt;span style="color:#81a1c1"&gt;.&lt;/span&gt;&lt;span style="color:#88c0d0"&gt;set_shader_param&lt;/span&gt;&lt;span style="color:#eceff4"&gt;(&lt;/span&gt;&lt;span style="color:#a3be8c"&gt;&amp;#34;param_name&amp;#34;&lt;/span&gt;&lt;span style="color:#eceff4"&gt;,&lt;/span&gt; value&lt;span style="color:#eceff4"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can also get the value with &lt;code&gt;get_shader_param()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For an example of this, see the &lt;a href="https://kamera25.github.io/godot_recipes/4.x/godot_recipes/3.x/shaders/blur/"&gt;Blur Shader&lt;/a&gt; recipe.&lt;/p&gt;</description></item><item><title>Greyscale (monochrome) shader</title><link>https://kamera25.github.io/godot_recipes/4.x/shaders/greyscale/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/shaders/greyscale/index.html</guid><description>&lt;div class="box notices cstyle tip"&gt;
&lt;div class="box-label"&gt;&lt;i class="fa-fw fas fa-lightbulb"&gt;&lt;/i&gt; Tip&lt;/div&gt;
&lt;div class="box-content"&gt;
&lt;p&gt;This article is being updated from Godot 3 to Godot 4.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You want a shader to convert an image to greyscale.&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s start with a &lt;code&gt;canvas_item&lt;/code&gt; (2D) shader. To convert to greyscale but also preserve pixel contrast, we need to &lt;em&gt;average&lt;/em&gt; the pixel&amp;rsquo;s color value. Add the color channels together and divide by 3:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-glsl" data-lang="glsl"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;shader_type canvas_item&lt;span style="color:#eceff4"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#81a1c1;font-weight:bold"&gt;void&lt;/span&gt; fragment&lt;span style="color:#eceff4"&gt;()&lt;/span&gt; &lt;span style="color:#eceff4"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; COLOR &lt;span style="color:#81a1c1"&gt;=&lt;/span&gt; texture&lt;span style="color:#eceff4"&gt;(&lt;/span&gt;TEXTURE&lt;span style="color:#eceff4"&gt;,&lt;/span&gt; UV&lt;span style="color:#eceff4"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#81a1c1;font-weight:bold"&gt;float&lt;/span&gt; avg &lt;span style="color:#81a1c1"&gt;=&lt;/span&gt; &lt;span style="color:#eceff4"&gt;(&lt;/span&gt;COLOR&lt;span style="color:#eceff4"&gt;.&lt;/span&gt;r &lt;span style="color:#81a1c1"&gt;+&lt;/span&gt; COLOR&lt;span style="color:#eceff4"&gt;.&lt;/span&gt;g &lt;span style="color:#81a1c1"&gt;+&lt;/span&gt; COLOR&lt;span style="color:#eceff4"&gt;.&lt;/span&gt;b&lt;span style="color:#eceff4"&gt;)&lt;/span&gt; &lt;span style="color:#81a1c1"&gt;/&lt;/span&gt; &lt;span style="color:#b48ead"&gt;3.0&lt;/span&gt;&lt;span style="color:#eceff4"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; COLOR&lt;span style="color:#eceff4"&gt;.&lt;/span&gt;rgb &lt;span style="color:#81a1c1"&gt;=&lt;/span&gt; &lt;span style="color:#81a1c1;font-weight:bold"&gt;vec3&lt;/span&gt;&lt;span style="color:#eceff4"&gt;(&lt;/span&gt;avg&lt;span style="color:#eceff4"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#eceff4"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;
&lt;a href="#image-f40765464fbd787c88b32fe4cc440cb9" class="lightbox-link"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/godot_recipes/3.x/img/shader_greyscale01.png" alt="alt" style="height: auto; width: auto;" loading="lazy"&gt;
&lt;/a&gt;
&lt;a href="javascript:history.back();" class="lightbox" id="image-f40765464fbd787c88b32fe4cc440cb9"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/godot_recipes/3.x/img/shader_greyscale01.png" alt="alt" class="lightbox-image" loading="lazy"&gt;
&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Blur shader</title><link>https://kamera25.github.io/godot_recipes/4.x/shaders/blur/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/shaders/blur/index.html</guid><description>&lt;div class="box notices cstyle tip"&gt;
&lt;div class="box-label"&gt;&lt;i class="fa-fw fas fa-lightbulb"&gt;&lt;/i&gt; Tip&lt;/div&gt;
&lt;div class="box-content"&gt;
&lt;p&gt;This article is being updated from Godot 3 to Godot 4.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You want a shader to blur an object or the screen.&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-glsl" data-lang="glsl"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;shader_type canvas_item&lt;span style="color:#eceff4"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#81a1c1;font-weight:bold"&gt;uniform&lt;/span&gt; &lt;span style="color:#81a1c1;font-weight:bold"&gt;float&lt;/span&gt; blur_amount &lt;span style="color:#81a1c1"&gt;:&lt;/span&gt; hint_range&lt;span style="color:#eceff4"&gt;(&lt;/span&gt;&lt;span style="color:#b48ead"&gt;0&lt;/span&gt;&lt;span style="color:#eceff4"&gt;,&lt;/span&gt; &lt;span style="color:#b48ead"&gt;5&lt;/span&gt;&lt;span style="color:#eceff4"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#81a1c1;font-weight:bold"&gt;void&lt;/span&gt; fragment&lt;span style="color:#eceff4"&gt;()&lt;/span&gt; &lt;span style="color:#eceff4"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; COLOR &lt;span style="color:#81a1c1"&gt;=&lt;/span&gt; textureLod&lt;span style="color:#eceff4"&gt;(&lt;/span&gt;SCREEN_TEXTURE&lt;span style="color:#eceff4"&gt;,&lt;/span&gt; SCREEN_UV&lt;span style="color:#eceff4"&gt;,&lt;/span&gt; blur_amount&lt;span style="color:#eceff4"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#eceff4"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For example, to gradually blur the entire screen, such as for a scene transition effect:&lt;/p&gt;
&lt;p&gt;
&lt;a href="#image-cd4ed78339f30b892b87d68121100225" class="lightbox-link"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/godot_recipes/3.x/img/blur_shader1.png" alt="alt" style="height: auto; width: auto;" loading="lazy"&gt;
&lt;/a&gt;
&lt;a href="javascript:history.back();" class="lightbox" id="image-cd4ed78339f30b892b87d68121100225"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/godot_recipes/3.x/img/blur_shader1.png" alt="alt" class="lightbox-image" loading="lazy"&gt;
&lt;/a&gt;
&lt;a href="#image-ca569046d30847316faf4b13a0937a19" class="lightbox-link"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/godot_recipes/3.x/img/blur_shader2.png" alt="alt" style="height: auto; width: auto;" loading="lazy"&gt;
&lt;/a&gt;
&lt;a href="javascript:history.back();" class="lightbox" id="image-ca569046d30847316faf4b13a0937a19"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/godot_recipes/3.x/img/blur_shader2.png" alt="alt" class="lightbox-image" loading="lazy"&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can also animate the blurring:&lt;/p&gt;</description></item></channel></rss>