scene.org File Archive

File download

<root>­/­parties­/­2025­/­sessions25­/­code_graphics/ufo_1.txt

File size:
11 998 bytes (11.72K)
File date:
2025-12-16 16:39:04
Download count:
all-time: 2

Preview

precision highp float;
uniform vec2 resolution;
uniform vec2 mouse;
uniform float time;
uniform sampler2D backbuffer;
out vec4 outColor;

float TIME;
int SCENE;
float localTime;
float sceneGlobalStartTime;
float sceneTime;

// Arm Transition
#define SCENE1_START 5.0
// Arm down
#define SCENE2_START 12.0
// Arm up
#define SCENE3_START 17.0
// Arm fall
#define SCENE4_START 22.0
// End
#define SCENE5_START 1000.0

#define END 1000.0

float LoopTime(float x, float start,float len) 
{
    return mod(x - start, len) + start;
}

const float PI = acos(-1.0);

ivec2 font_data[94] = ivec2[](
    //0
    ivec2(0x00000000,0x00000000), //space

    //1~10
    ivec2(0x7e91897e,0x00000000), //0
    ivec2(0x01ff4121,0x00000000), //1
    ivec2(0x71898543,0x00000000), //2
    ivec2(0x6e919142,0x00000000), //3
    ivec2(0x08ff4838,0x00000000), //4
    ivec2(0x8e9191f2,0x00000000), //5
    ivec2(0x0e91916e,0x00000000), //6
    ivec2(0xc0b08f80,0x00000000), //7
    ivec2(0x6e91916e,0x00000000), //8
    ivec2(0x6e919162,0x00000000), //9

    //11~36
    ivec2(0x1e11110e,0x00000001), //a 11
    ivec2(0x0e11117f,0x00000000), //b 12 
    ivec2(0x0a11110e,0x00000000), //c 13
    ivec2(0x7f11110e,0x00000000), //d 14
    ivec2(0x0815150e,0x00000000), //e 15
    ivec2(0x48483f08,0x00000000), //f 16
    ivec2(0x3e494930,0x00000000), //g 17
    ivec2(0x0708087f,0x00000000), //h 18
    ivec2(0x012f0900,0x00000000), //i 19
    ivec2(0x5e111102,0x00000000), //j 20
    ivec2(0x000b047f,0x00000000), //k 21
    ivec2(0x017f4100,0x00000000), //l 22
    ivec2(0x0807080f,0x00000007), //m 23
    ivec2(0x0708080f,0x00000000), //n 24
    ivec2(0x06090906,0x00000000), //o 25
    ivec2(0x1824243f,0x00000000), //p 26
    ivec2(0x3f242418,0x00000000), //q 27
    ivec2(0x0010081f,0x00000000), //r 28
    ivec2(0x0012150d,0x00000000), //s 29
    ivec2(0x11113e10,0x00000000), //t 30
    ivec2(0x0f01010e,0x00000000), //u 31
    ivec2(0x000e010e,0x00000000), //v 32
    ivec2(0x010e010e,0x0000000f), //w 33
    ivec2(0x0a040a11,0x00000011), //x 34
    ivec2(0x3e090930,0x00000000), //y 35
    ivec2(0x00191513,0x00000000), //z 36

    //37~63
    ivec2(0x7f88887f,0x00000000), //A 37
    ivec2(0x6e9191ff,0x00000000), //B 38
    ivec2(0x4281817e,0x00000000), //C 39
    ivec2(0x7e8181ff,0x00000000), //D 40
    ivec2(0x919191ff,0x00000000), //E 41
    ivec2(0x909090ff,0x00000000), //F 42
    ivec2(0x4685817e,0x00000000), //G 43 
    ivec2(0xff1010ff,0x00000000), //H 44
    ivec2(0x0081ff81,0x00000000), //I 45
    ivec2(0x80fe8182,0x00000000), //J 46
    ivec2(0x413608ff,0x00000000), //K 47
    ivec2(0x010101ff,0x00000000), //L 48
    ivec2(0x601060ff,0x000000ff), //M 49
    ivec2(0x0c1060ff,0x000000ff), //N 50
    ivec2(0x7e81817e,0x00000000), //O 51
    ivec2(0x609090ff,0x00000000), //P 52
    ivec2(0x7f83817e,0x00000001), //Q 53
    ivec2(0x619698ff,0x00000000), //R 54
    ivec2(0x4e919162,0x00000000), //S 55
    ivec2(0x80ff8080,0x00000080), //T 56
    ivec2(0xfe0101fe,0x00000000), //U 57
    ivec2(0x0e010ef0,0x000000f0), //V 58
    ivec2(0x031c03fc,0x000000fc), //W 59
    ivec2(0x340834c3,0x000000c3), //X 60
    ivec2(0x300f30c0,0x000000c0), //Y 61
    ivec2(0xe1918d83,0x00000081), //Z 62

    //63~
    ivec2(0x00007d00,0x00000000), //! 63
    ivec2(0x60006000,0x00000000), //" 64
    ivec2(0x3f123f12,0x00000012), //# 65
    ivec2(0x52ff5224,0x0000000c), //$ 66
    ivec2(0x33086661,0x00000043), //% 67
    ivec2(0x374d5926,0x00000001), //& 68
    ivec2(0x00006000,0x00000000), //' 69
    ivec2(0x0081423c,0x00000000), //( 70
    ivec2(0x003c4281,0x00000000), //) 71
    ivec2(0x00143814,0x00000000), //* 72
    ivec2(0x00103810,0x00000000), //+ 73
    ivec2(0x00020100,0x00000000), //, 74
    ivec2(0x08080808,0x00000000), //- 75
    ivec2(0x00000100,0x00000000), //. 76
    ivec2(0x30080601,0x00000040), ///
    ivec2(0x00240000,0x00000000), //:
    ivec2(0x00240200,0x00000000), //;
    ivec2(0x41221408,0x00000000), //<
    ivec2(0x00141414,0x00000000), //=
    ivec2(0x08142241,0x00000000), //>
    ivec2(0xa999423c,0x0000007c), //@
    ivec2(0x008181ff,0x00000000), //[
    ivec2(0x06083040,0x00000001), //\
    ivec2(0x00000000,0x00000000), //] 何故か表示されない
    ivec2(0x00ff8181,0x00000000), //]
    ivec2(0x20402010,0x00000010), //^
    ivec2(0x01010101,0x00000000), //_
    ivec2(0x40408080,0x00000000), //`
    ivec2(0x41413608,0x00000000), //{
    ivec2(0x00ff0000,0x00000000), //|
    ivec2(0x08364141,0x00000000), //}
    ivec2(0x08101008,0x00000010) //~

);
#define FontWidth 8
#define FontHeight 8
#define LineMaxLength 40
#define ID 86

vec3 font(vec2 uv,int id){
    vec2 uv1 = uv;
    uv = uv * 8.0;
    ivec2 texel = ivec2(uv);
    int bit_offset = texel.x * FontWidth + texel.y;

    int s,t;
    s = font_data[id].x;
    t = font_data[id].y;

    int tex = 0;
    
    if(bit_offset <= 31){
        s = s >> bit_offset;
        s = s & 0x00000001;
        tex = s;
    }
    else{
        t = t >> (bit_offset - 32);
        t = t & 0x00000001;
        tex = t;
    }

    tex = (abs(uv1.x - 0.5) < 0.5 && abs(uv1.y - 0.5) < 0.5) ? tex : 0;
    return vec3(tex); 
}

float sdBox( vec3 p, vec3 b )
{
  vec3 q = abs(p) - b;
  return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}

vec2 rot(vec2 p, float a){
    return vec2(p.x * cos(a) - p.y * sin(a), p.x * sin(a) + p.y * cos(a));
}

vec3 rot(vec3 r, vec3 axis, float a){
    return r * cos(a) + cross(axis, r) * sin(a) + axis * dot(axis, r) * (1.0 - cos(a));
}

int indexClane;
// factor : 0.0 ~ 1.0 open, close
float sdClene(vec3 p, float factor){
    float d = sdBox(p - vec3(0.0,4.0,0.0), vec3(0.2,3.0,0.2));
    float d1 = sdBox(p - vec3(0.0,1.0,0.0), vec3(0.5,0.3,0.3));

    vec3 armP = p - vec3(0.0,1.0,0.0);
    armP.x = -abs(armP.x);
    armP.x += 0.2;
    float armRot = mix(0.3, 0.7,factor); // OPEN, CLOSE
    armP = rot(armP, vec3(0,0,1),PI * 0.5 - armRot);
    armP -= vec3(0.0,-0.5,0.0);
    float dP = sdBox(armP, vec3(0.1,0.5,0.1));
    dP = min(dP,sdBox(rot(armP,vec3(0,0,1),PI * 0.5) - vec3(0.4,0.3,0.), vec3(0.1,0.4,0.1)));

    indexClane = (d > d1) ? 2 : 3;
    d = min(d1, d);
    indexClane = (d > dP) ? 3 : indexClane;
    d = min(d, dP);

    return d;
}

float easeOutBounce(float x){
const float n1 = 7.5625;
const float d1 = 2.75;

if (x < 1.0 / d1) {
    return n1 * x * x;
} else if (x < 2.0 / d1) {
    return n1 * (x -= 1.5 / d1) * x + 0.75;
} else if (x < 2.5 / d1) {
    return n1 * (x -= 2.25 / d1) * x + 0.9375;
} else {
    return n1 * (x -= 2.625 / d1) * x + 0.984375;
}
}

#define saturate(x) clamp(x,0.,1.)

float powEaseOut(float x, float e){
    return 1.0 - pow(saturate(1.0 - x), e);
}

vec3 localPos;
int index;
float map(vec3 p){
    vec3 pos = p;
    vec3 boxPos = pos;
    boxPos.y -= 0.5;
    if(SCENE==2){
        boxPos.y -= mix(0.0, 1.0,  saturate((localTime-1.0) / 3.0));
    }
    if(SCENE==3)
    {
        boxPos.y -= 1.0;
        boxPos.y -= mix(0.0, -0.06, saturate((localTime- 0.0) / 2.0));
        float t = saturate((localTime - 2.0) / 2.0);
        boxPos.y += mix(0.0, -0.06, saturate((localTime- 2.0) / 2.0));
        boxPos.y -= - easeOutBounce(saturate(t));
    }

    float d = sdBox(boxPos, vec3(0.5));
    localPos = boxPos;

    vec3 clenePos = pos - vec3(0.0,1.5,0.0);
    float factor = 1.0;
    if(SCENE == 0){
        clenePos.x -= mix(0.0, 2.0, saturate((localTime - 2.0) / 2.0)) - 2.0;
    }
    if(SCENE == 1){
        clenePos.y -= mix(0.0, -1.0, saturate((localTime-1.0) / 3.0));
        factor = mix(1.0,0.0, saturate(localTime / 1.0));
        factor = mix(factor, 0.4, saturate((localTime - 5.0) / 1.0));
    }
    if(SCENE == 2){
        clenePos.y -= mix(-1.0, 0.0,  saturate((localTime-1.0) / 3.0));
        factor = 0.4; 
    }
    if(SCENE == 3){
        //clenePos.x -= mix(2.0, -2.0, saturate((localTime - 5.0) / 4.0)) - 2.0;
        factor = mix(0.4,1.0, easeOutBounce(saturate((localTime - 2.55) / 0.4)));
    }
    if(SCENE == 4){
        clenePos.x -= mix(2.0, -2.0, saturate((localTime - 0.0) / 4.0)) - 2.0;
    }
    float dC = sdClene(clenePos, factor * 2.0);

    index = (d > pos.y) ? 1 : 0;
    d = min(d, pos.y);
    index = (d > dC) ? indexClane : index;
    d = min(d, dC);
    return d;
}

vec3 normalmap( vec3 p) {
  vec2 d = vec2( 0, 1E-4 );
  return normalize(vec3(
    map( p + d.yxx) - map( p - d.yxx),
    map( p + d.xyx) - map( p - d.xyx),
    map( p + d.xxy) - map( p - d.xxy)
  ));
}

vec3 Texture(vec2 uv)
{
    return vec3(0.2,0.8,0.2);
}

void main(){
    TIME = mod(time, SCENE4_START + 7.0);
    //TIME = LoopTime(TIME, SCENE4_START, 7.0);

    if(TIME < SCENE1_START){
        SCENE = 0;
        localTime = TIME;
    }
    else if(TIME < SCENE2_START){
        SCENE = 1;
        localTime = TIME - SCENE1_START;
    }
    else if(TIME < SCENE3_START){
        SCENE = 2;
        localTime = TIME - SCENE2_START;
    }
    else if(TIME < SCENE4_START){
        SCENE = 3;
        localTime = TIME - SCENE3_START;
    }
    else if(TIME < SCENE5_START){
        SCENE = 4;
        localTime = TIME - SCENE4_START;
    }
    else{
        SCENE = 5;
        localTime = TIME - SCENE5_START;
    }

    vec2 uv = (2.0 * (gl_FragCoord.xy) - resolution.xy)/resolution.y;    

    vec3 ro = vec3(0.0, 2.0, 2.0);
    vec3 rd = normalize(vec3(uv, -1.0));
    rd = rot(rd, vec3(1,0,0), PI * 2.0 - 0.4);   

    vec3 pos = ro;
    float t = 0.0;
    float d = 0.0;

    for(int i = 0; i < 100; i++){
        pos = ro + rd * t;
        d = map(pos);
        if(d < 0.001) break;
        t += d;
    }

    vec3 color = vec3(0.0);
    if(d < 0.001){
        vec3 basecolor = vec3(1.0);
        vec3 normal = normalmap(pos);
        if(index == 0){
            if(abs(dot(normal,vec3(0,1,0))) > 0.5)basecolor = Texture(localPos.xz).xyz;
            if(abs(dot(normal,vec3(0,0,1))) > 0.5)basecolor = Texture(localPos.xy).xyz;
            if(abs(dot(normal,vec3(1,0,0))) > 0.5)basecolor = Texture(localPos.yz*vec2(0.1,1.0)).xyz;
        }
        if(index == 1){
            basecolor = vec3(1.0);
        }
        if(index == 2){
            basecolor = vec3(0.8,0.0,0.0);
        }
        if(index == 3){
            basecolor = vec3(0.2);
        }
        color = basecolor;
        
        vec3 lightPos  = vec3(0.0, 3.0, 1.0);
        vec3 lightDir = normalize(lightPos - pos);
        float diff = max(0.0, dot(normal, lightDir) + 0.5);
        float spec = pow(max(0.0, dot(reflect(-lightDir, normal), -rd)), 32.0);
        diff = clamp(diff, 0.0, 1.0);
        color = color * diff + color * spec;

        color = exp(-0.1 * t) * color;  
    }


    float fade = 0.0;
    if(SCENE == 0){
        fade = smoothstep(1.0, 0.0, saturate(localTime / 2.0));
    }
    else if(SCENE == 4){
        fade = smoothstep(0.0, 1.0, saturate((localTime - 1.0) / 2.0));
    }

    color = mix(color, vec3(0.0), fade);

    vec2 uvText = uv;
    uvText *= 5.0;
    int font_indices[13] = int[](
        70, 56, 25, 56, 71, 0, 30, 15, 11, 28, 76, 76, 76
    ); 

    float uvOffsetX[13] = float[](
        1.0,-0.5,-0.4,0.,0.,
        -1.0,0.,0.,0.,0.,
        0.,0.,0.
    );
    

    float charSpacing = 0.0;
    for(int i = 0; i < 13; i++){
        charSpacing += uvOffsetX[i];
        vec2 uvPos = uvText - vec2(float(i - 6) * 1.0 + charSpacing, 0.0);
        vec3 charColor = font(uvPos, font_indices[i]);
        if(SCENE == 4){
            charColor = mix(vec3(0.0), charColor,saturate((localTime - 2.5) / 2.0));
            charColor = mix(charColor, vec3(0.0), saturate((localTime - 4.0) / 2.0));
            color += charColor;
        }
    }

    outColor = vec4(color,1.0);
}