NEUROMANTIC

自分でC/C++/UE4/Graphics/ゲームなどをやったことをメモするブログ

ゆらゆら・ShaderToyのテンプレートコードのメモ

ゆらゆら

www.shadertoy.com

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = fragCoord/min(iResolution.x, iResolution.y);

    float speed = 0.5f;
    vec2 p = uv * 5.0f;
    for (int i = 1; i < 10; ++i)
    {
        float fi = float(i);
        vec2 mouse = iMouse.xy / 1000.0f;
        p.x += 0.2f / fi * sin(fi * 3.0f * p.y + iTime * speed) + mouse.x;
        p.y += 0.2f / fi * sin(fi * 3.0f * p.x + iTime * speed) + mouse.y;
    }
    
    vec3 col1 = vec3(1, 0, 0.25); // off1 color (primary)
    vec3 col2 = vec3(0, 0.3f, 1); // off2 color (secondary)
    vec3 col3 = vec3(1, 1, 1);    // off3 color (sub)
    
    // [0, 1]
    float off1 = cos(p.x + p.y) * 0.5f + 0.5f;
    float off2 = sin(p.x + p.y) * 0.5f + 0.5f;
    float off3 = (2.0f - (off1 + off2)) * 0.5f;
       
    // over operator
    vec3 color = vec3(0);
    color = col1 * off1 + color * (1.0f - off1);
    color = col2 * off2 + color * (1.0f - off2);
    color = col3 * off3 + color * (1.0f - off3);
    
    fragColor = vec4(color, 1.0f);
}

久しぶりに触りますので簡単なものを作ってみました。(参照の記事のコードを見てちょっと改造などなど)

テンプレートコード

忘れてた時に見たら良さそうで貼りますね。

// Shader Inputs, uniforms
uniform vec3      iResolution;           // viewport resolution (in pixels)
uniform float     iTime;                 // shader playback time (in seconds)
uniform float     iTimeDelta;            // render time (in seconds)
uniform int       iFrame;                // shader playback frame
uniform float     iChannelTime[4];       // channel playback time (in seconds)
uniform vec3      iChannelResolution[4]; // channel resolution (in pixels)
uniform vec4      iMouse;                // mouse pixel coords. xy: current (if MLB down), zw: click
uniform samplerXX iChannel0..3;          // input channel. XX = 2D/Cube
uniform vec4      iDate;                 // (year, month, day, time in seconds)

// Main Funtion
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = fragCoord/iResolution.xy;
    // [0, 1] Squared UV.
    vec2 uv_min = fragCoord / min(iResolution.x, iResolution.y);
    // Time varying pixel color
    vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx+vec3(0,2,4));
    // Output to screen
    fragColor = vec4(col,1.0);
}

参照

viclw17.github.io