Author Topic: Animated Backgrounds?  (Read 52532 times)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: Animated Backgrounds?
« Reply #120 on: December 17, 2019, 07:56:42 pm »
This HUD is not a real project, it is just a template/helper to adjust the screen size.

Save pose to mtl file will do exactly what is needed to setup the startup location, thank you!
« Last Edit: December 17, 2019, 08:02:14 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #121 on: December 17, 2019, 08:38:49 pm »
Okay Patrice,

Here come the mods for model initial posing anywhere in the 3D viewport.

globals.h:

struct PROPM {
    BOOL  bHasPositions;
    BOOL  bHasTextureCoords;
    BOOL  bHasNormals;
    BOOL  bHasTangents;
    long  numberOfVertexCoords;
    long  numberOfTextureCoords;
    long  numberOfNormals;
    long  numberOfTriangles;
    long  numberOfMaterials;
    long  numberOfMeshes;
    long  numberOfTextures;
    float center[3];
    float pose[6];      // MLL 12-17-2019: model initial translational/rotational "pose" other than at [0,0,0]
    float rWidth;
    float rHeight;
    float rDepth;
    float rExtent;
};


constants.h near line 231:

........
// MLL 12-08-2018: FOV support
#define MENU_FOV_30             40068
#define MENU_FOV_45             40069
#define MENU_FOV_90             40070

// MLL 12-17-2019: pose support
#define MENU_SAVE_POSE          40071
// Note: put the number that actually follows MENU_FOV_90 in your code!!! (your constants may differ)
........


Main.cpp near line 985:

void zeroCamera() { // MLL 12-20-2018: immediate non-animated reset; 12-17-2019: #pose support
    gP.rTargetPos[0] = gM.center[0] + gM.pose[0];
    gP.rTargetPos[1] = gM.center[1] + gM.pose[1];
    gP.rTargetPos[2] = gM.center[2];

    gP.rCameraPos[0] = gP.rTargetPos[0] + gM.pose[0];
    gP.rCameraPos[1] = gP.rTargetPos[1] + gM.pose[1];
    gP.rCameraPos[2] = CAMERA_OFFZ + gM.pose[2];

    //gP.rPitch = gP.rYaw = gP.rRoll = 0.0f; // MLL 10-28-2018:
    gP.rPitch = gM.pose[3];
    gP.rYaw = gM.pose[4];
    gP.rRoll = gM.pose[5];
}


Main.cpp near line 2765:

........
    case MENU_SAVE_POSE:  // MLL 12-17-2019:
        if (gP.bObjectLoaded)
            Mobj_saveLight(TRUE); // this updates pose values only
        break;

    case MENU_SAVE_LIGHT: // PAT: 01-27-2018
........


Main.cpp in menu creation: (I have it as a separate sub)

........
    // PAT: 12-31-2018 avoid menu duplication (see View)
    //AppendMenu(hSaveMenu, MF_ENABLED, MENU_WALLPAPER, L"Take screenshot...");
    //AppendMenu(hSaveMenu, MF_ENABLED, MENU_PRINT, L"Send screenshot to printer...");
    //AppendMenu(hFileMenu, MF_POPUP, (UINT_PTR)hSaveMenu, L"Save scene as"); // e.g. after modification

    AppendMenu(hFileMenu, MF_SEPARATOR, 0, 0);
    AppendMenu(hFileMenu, MF_ENABLED, MENU_SAVE_POSE, L"Save pose to mtl file");
    AppendMenu(hFileMenu, MF_ENABLED, MENU_SAVE_LIGHT, L"Save lighting to mtl file");

    AppendMenu(hFileMenu, MF_SEPARATOR, 0, 0);
    AppendMenu(hFileMenu, MF_ENABLED, MENU_FILE_CLOSE, L"Close all and clear scene");
........
(much lower)
........
    AppendMenu(hLightsMenu, MF_ENABLED, MENU_LIGHTSETTING, L"Control panel\t[F2]");// L"Simplified light settings");
    //AppendMenu(hLightsMenu, MF_SEPARATOR, 0, 0);
    //AppendMenu(hLightsMenu, MF_ENABLED, MENU_SAVE_LIGHT, L"Save lighting to mtl file");

    AppendMenu(hLightsMenu, MF_SEPARATOR, 0, 0);
........


Mobj.h near line 905:

void Mobj_saveLight(IN BOOL poseOnly = FALSE) { // MLL 12-17-2019: #pose support
    if (gP.hMaterial) {
        long nCount = (long) SendMessageA(gP.hMaterial, LB_GETCOUNT, 0, 0);
        if (nCount > 1) {
            long nLen, nItem = 0;
            char szLine[MAX_PATH]; ClearMemory(szLine, sizeof(szLine));
            SendMessageA(gP.hMaterial, LB_GETTEXT, nItem, (LPARAM) szLine);

            // Create .bak file
            char szBak[MAX_PATH]; strcpy_s(szBak, szLine); nLen = lstrlenA(szBak) - 1;
            szBak[nLen] = 'k'; nLen--;
            szBak[nLen] = 'a'; nLen--;
            szBak[nLen] = 'b';
            CopyFileA(szLine, szBak, FALSE);

            FILE* pFile = NULL;
            if (poseOnly) { // MLL 12-17-2019: update #pose entry only
                BOOL hasPose = FALSE, hasLights = FALSE;
                FILE* pBak = fopen(szBak, "r");
                pFile = fopen(szLine, "w");
                if (pBak && pFile) {
                    memcpy(gM.pose, gP.rCameraPos, sizeof(float) * 3);
                    gM.pose[3] = gP.rPitch; gM.pose[4] = gP.rYaw; gM.pose[5] = gP.rRoll;
                    ClearMemory(szLine, sizeof(szLine));
                    while (fgets(szLine, MAX_PATH, pBak) != NULL) {
                        if (szLine[0] == '#') {
                            CharLowerA(szLine);
                            if (strstr(szLine, "#pose")) { // #pose found; insert new values
                                sprintf(szLine, "#pose %.2f %.2f %.2f %.2f %.2f %.2f\n", // better precision is imperceptible
                                    gM.pose[0], gM.pose[1], gM.pose[2], gM.pose[3], gM.pose[4], gM.pose[5]);
                                hasPose = TRUE;
                            }
                            else if (strstr(szLine, "#light"))
                                hasLights = TRUE;
                        }
                        fputs(szLine, pFile);
                    }
                    // MLL 12-17-2019: Not very elegant but working
                    if (!hasPose) { // #pose not found; insert new #pose meta
                        fseek(pBak, SEEK_SET, 0);
                        fseek(pFile, SEEK_SET, 0);
                        if (hasLights) {
                            int nLights = 0;
                            while ((fgets(szLine, MAX_PATH, pBak) != NULL)) { // skip #light metas
                                if (szLine[0] == '#') {
                                    CharLowerA(szLine);
                                    if (strstr(szLine, "#light")) {
                                        fputs(szLine, pFile);
                                        nLights++;
                                    }
                                    if (nLights == 12)
                                        break;
                                }
                            }
                        }
                        sprintf(szLine, "\n#pose %.2f %.2f %.2f %.2f %.2f %.2f\n", // add new #pose meta
                            gM.pose[0], gM.pose[1], gM.pose[2], gM.pose[3], gM.pose[4], gM.pose[5]);
                        fputs(szLine, pFile);
                        while ((fgets(szLine, MAX_PATH, pBak) != NULL)) // add all other remaining lines
                            fputs(szLine, pFile);
                    }
                    fclose(pBak);
                    fclose(pFile);
                }
            } else {
                pFile = fopen(szLine, "w");
                if (pFile) {
                    SaveLighting(pFile);
                    for (nItem = 1; nItem < nCount; nItem++) {
                        ClearMemory(szLine, sizeof(szLine));
                        SendMessageA(gP.hMaterial, LB_GETTEXT, nItem, (LPARAM)szLine);
                        if ((nItem == 1) && (szLine[0] == '\n')) { continue; }
                        fputs(szLine, pFile);
                    }
                    fclose(pFile);
                }
            }
            if (pFile)
                skDialogInfo($NULL, L"\nMaterial file updated successfully.", $NULL);
        }
    }
}


Mobj.h in Mobj_importMaterials():

(at the top)
........
    char fullPath[MAX_PATH] = { 0 };
    char s1[MAX_PATH] = { 0 }; char s2[32] = { 0 }; char s3[32] = { 0 }; // 10-09-2019 Bass.dll
    char s4[32], s5[32], s6[32]; // MLL 12-17-2019:
    HWND hCtrl = 0; // PAT: 11-05-2019
........
(lower in global #metas)
........
                } else if (strcmp(szBuffer, "#demo") == 0) {                // PAT: 11-02-2019 MODEL ROTATION
                    if (gP.bGiration == 0)
                        PutFocusOn(GetDlgItem(gP.hMain, IDC_DEMO_MODE));
                } else if (strcmp(szBuffer, "#pose") == 0) {                // MLL 12-17-2019: MODEL INITIAL POSE other than at [0,0,0]
                    fgets(szBuffer, sizeof(szBuffer), pFile);
                    s1[0] = s2[0] = s3[0] = s4[0] = s5[0] = s6[0] = 0;
                    sscanf(szBuffer, "%s %s %s %s %s %s", s1, s2, s3, s4, s5, s6);
                    gM.pose[0] = (float)atof(s1); gM.pose[1] = (float)atof(s2); gM.pose[2] = (float)atof(s3);
                    gM.pose[3] = (float)atof(s4); gM.pose[4] = (float)atof(s5); gM.pose[5] = (float)atof(s6);
                    printf("\nModel posed at translation %.1f %.1f %.1f, rotation %.1f %.1f %.1f\n\n",
                        gM.pose[0], gM.pose[1], gM.pose[2], gM.pose[3], gM.pose[4], gM.pose[5]);
                } else {
........

Renderers.h:

// MLL 12-20-2018: animate model position reset; MLL 02-07-2019: FPS-independent; 12-17-2019: #pose support
void lerpReset(IN BOOL start = FALSE) {
    static int frames, maxFrames; // render frames counter
    static float dtpX, dtpY; // target position deltas
    static float dcpX, dcpY, dcpZ; // camera position deltas
    static float drP, drY, drR; // camera rotation deltas
    static float frac;

    // Forward declarations
    void gl_AdjustNearPlane();
    void gl_Resize();
    void gl_DrawScene();
    void zeroCamera();

    if (start) {
        if (gP.rTargetPos[0] == gM.center[0] + gM.pose[0]     &&
            gP.rTargetPos[1] == gM.center[1] + gM.pose[1]     &&
            gP.rTargetPos[2] == gM.center[2]                  &&
            gP.rCameraPos[0] == gP.rTargetPos[0] + gM.pose[0] &&
            gP.rCameraPos[1] == gP.rTargetPos[1] + gM.pose[1] &&
            gP.rCameraPos[2] == CAMERA_OFFZ + gM.pose[2]      &&
            gP.rPitch == gM.pose[3]                           &&
            gP.rYaw == gM.pose[4]                             &&
            gP.rRoll == gM.pose[5]) {
            gP.bIsLerping = FALSE; // no need to lerp; already reset
            return;
        }

        frames = 0;
        maxFrames = (int)(gP.nFPS * 0.5f); // ca. half a second animation regardless of FPS
        frac = 1.0f / maxFrames;
        dtpX = (gP.rTargetPos[0] - gM.pose[0]) * frac;
        dcpX = (gP.rCameraPos[0] - gM.pose[0]) * frac;
        dtpY = (gP.rTargetPos[1] - gM.pose[1]) * frac;
        dcpY = (gP.rCameraPos[1] - gM.pose[1]) * frac;
        dcpZ = (gP.rCameraPos[2] - CAMERA_OFFZ - gM.pose[2]) * frac;
        drP = (gP.rPitch - gM.pose[3]) * frac; if (drP < 0.0f) drP = -drP;
        drY = (gP.rYaw - gM.pose[4]) * frac; if (drY < 0.0f) drY = -drY;
        drR = (gP.rRoll - gM.pose[5]) * frac; if (drR < 0.0f) drR = -drR;
        gP.bIsLerping = TRUE;
        return;
    }

    frames += 1;

    if (gP.bIsLerping) {
        if (frames == maxFrames) {
            zeroCamera();
            dtpX = dtpY = 0.0f;
            dcpX = dcpY = dcpZ = 0.0f;
            drP = drY = drR = 0.0f;
            frames = 0; frac = 0.0f;
            gP.bIsLerping = FALSE;
            gl_AdjustNearPlane();
            gl_Resize();
            return;
        }
    }

    if (dcpX != 0.0f + gM.pose[0]) {
        gP.rTargetPos[0] -= dtpX;
        gP.rCameraPos[0] -= dcpX;
    }
    if (dcpY != 0.0f + gM.pose[1]) {
        gP.rTargetPos[1] -= dtpY;
        gP.rCameraPos[1] -= dcpY;
    }
    if (dcpZ != (CAMERA_OFFZ + gM.pose[2]) * frac)
        gP.rCameraPos[2] -= dcpZ;
    if (drP != 0.0f + gM.pose[3])
        gP.rPitch -= drP;
    if (drY != 0.0f + gM.pose[4])
        gP.rYaw -= drY;
    if (drR != 0.0f + gM.pose[5])
        gP.rRoll -= drR;

    gl_AdjustNearPlane();
    gl_Resize();
}


My sources have different line numbering in many cases, so I can't be more precise. Those code pieces above that represent entire functions should replace the existing ones completely.

Please let me know if my mods are complete and work OK for you.

#pose'ing shouldn't be written manually. Prefer to use the menu after positioning/rotating the model in the viewport.
« Last Edit: December 17, 2019, 08:45:30 pm by Michael Lobko-Lobanovsky »
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: Animated Backgrounds?
« Reply #122 on: December 17, 2019, 09:38:31 pm »
Thanks for the changes you have done already

- Location/translation seems to work well
- But the Zoom factor is not saved/restored.

Indeed i want to retrieve the scene exactly in the same state it had when saved.
« Last Edit: December 17, 2019, 09:43:32 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #123 on: December 17, 2019, 09:55:54 pm »
What do you mean, "is not saved/restored"?! :o

The camera/model Z position is saved in gM.pose[2]. (3rd number in the #pose text)

Below is a snapshot of newly saved and reloaded arbitrary model...
« Last Edit: December 17, 2019, 10:02:39 pm by Michael Lobko-Lobanovsky »
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: Animated Backgrounds?
« Reply #124 on: December 17, 2019, 10:00:36 pm »
Try to save the screen shot of my Tokyo example.

See the screen shot attached to this post
http://www.objreader.com/index.php?topic=197.msg5790#msg5790

Change the size of the model with the mouse wheel before saving.
« Last Edit: December 17, 2019, 10:14:18 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #125 on: December 17, 2019, 10:28:52 pm »
Change the size of the model with the mouse wheel before saving.

How else do you think I put the dragon in the position you're seeing in the above picture?! ::) ;D

Zooming out (minification with respect to [0,0,0]) seems to work while zooming in (magnification) doesn't.

I'll look into this tomorrow morning. I'm already quite tired for today.

See you in the morning!
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: Animated Backgrounds?
« Reply #126 on: December 17, 2019, 10:36:42 pm »
Have a good night my friend!

See you tomorrow.
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #127 on: December 18, 2019, 08:20:55 am »
Patrice,

I'm going to have my landline serviced today. So it's highly probable that I'll be offline till late in the evening.

But I'll be fixing #pose zooming insomuch as my other work permits and I'll come back to you later in the evening with my progress. I'm sure I'll be able to cope with that problem.

See you, my friend, and take care!
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #128 on: December 22, 2019, 07:51:41 pm »
Hi Patrice,

Try this:

globals.h:

struct PROPM {
    BOOL  bHasPositions;
    BOOL  bHasTextureCoords;
    BOOL  bHasNormals;
    BOOL  bHasTangents;
    long  numberOfVertexCoords;
    long  numberOfTextureCoords;
    long  numberOfNormals;
    long  numberOfTriangles;
    long  numberOfMaterials;
    long  numberOfMeshes;
    long  numberOfTextures;
    float center[3];
    float pose[9];      // MLL 12-17-2019: model initial target & camera translational/rotational "pose" other than at [0,0,0]
    float rWidth;
    float rHeight;
    float rDepth;
    float rExtent;
};


Main.cpp near line 985:

void zeroCamera() { // MLL 12-20-2018: immediate non-animated reset, 12-17-2019: #pose support
    if (gM.pose[3] == 0.0f && gM.pose[4] == 0.0f && gM.pose[5] == 0.0f) { // lazy check for non-posed model
        gP.rTargetPos[0] = gM.center[0];
        gP.rTargetPos[1] = gM.center[1];
        gP.rTargetPos[2] = gM.center[2];

        gP.rCameraPos[0] = gP.rTargetPos[0];
        gP.rCameraPos[1] = gP.rTargetPos[1];
        gP.rCameraPos[2] = CAMERA_OFFZ;

        gP.rPitch = gP.rYaw = gP.rRoll = 0.0f; // MLL 10-28-2018:
    }
    else {
        gP.rTargetPos[0] = gM.pose[0];
        gP.rTargetPos[1] = gM.pose[1];
        gP.rTargetPos[2] = gM.pose[2];

        gP.rCameraPos[0] = gM.pose[3];
        gP.rCameraPos[1] = gM.pose[4];
        gP.rCameraPos[2] = gM.pose[5] + CAMERA_OFFZ;

        gP.rPitch = gM.pose[6];
        gP.rYaw = gM.pose[7];
        gP.rRoll = gM.pose[8];
    }
}


Main.cpp in gl_LoadModel near line 1300:

........
    Mobj_setLightColor(); // PAT: 01-28-2018

    if (gP.bLerpReset) { // MLL 12-20-2018: reset animation support
        gP.rCameraPos[0] = gP.rCameraPos[1] = 0.0f; // MLL 12-17-2019: first appear from viewport exact center
        gP.rCameraPos[2] = 99.0f;
    }
    ResetCamera();
    gP.bRedraw = TRUE;
    gl_DrawScene();
........


Mobj.h near line 160:

void Mobj_initObjModel() {
    // Boolean flags
    gM.bHasPositions = FALSE;
    gM.bHasNormals = FALSE;
    gM.bHasTextureCoords = FALSE;
    gM.bHasTangents = FALSE;

    gM.numberOfVertexCoords = 0;
    gM.numberOfTextureCoords = 0;
    gM.numberOfNormals = 0;
    gM.numberOfTriangles = 0;
    gM.numberOfMaterials = 0;
    gM.numberOfMeshes = 0;
    gM.numberOfTextures = 0;

    gM.rWidth = 0.0f;
    gM.rHeight = 0.0f;
    gM.rDepth = 0.0f;
    gM.rExtent = 0.0f;

    gM.center[0] = 0.0f;
    gM.center[1] = 0.0f;
    gM.center[2] = 0.0f;

    // MLL 12-17-2019: #pose support
    gM.pose[0] = 0.0f;
    gM.pose[1] = 0.0f;
    gM.pose[2] = 0.0f;
    gM.pose[3] = 0.0f;
    gM.pose[4] = 0.0f;
    gM.pose[5] = 0.0f;
    gM.pose[6] = 0.0f;
    gM.pose[7] = 0.0f;
    gM.pose[8] = 0.0f;
}


Mobj.h near line 905:

void Mobj_saveLight(IN BOOL poseOnly = FALSE) { // MLL 12-17-2019: #pose support
    if (gP.hMaterial) {
        long nCount = (long) SendMessageA(gP.hMaterial, LB_GETCOUNT, 0, 0);
        if (nCount > 1) {
            long nLen, nItem = 0;
            char szLine[MAX_PATH]; ClearMemory(szLine, sizeof(szLine));
            SendMessageA(gP.hMaterial, LB_GETTEXT, nItem, (LPARAM) szLine);

            // Create .bak file
            char szBak[MAX_PATH]; strcpy_s(szBak, szLine); nLen = lstrlenA(szBak) - 1;
            szBak[nLen] = 'k'; nLen--;
            szBak[nLen] = 'a'; nLen--;
            szBak[nLen] = 'b';
            CopyFileA(szLine, szBak, FALSE);

            FILE* pFile = NULL;
            if (poseOnly) { // MLL 12-17-2019: update #pose entry only
                BOOL hasPose = FALSE, hasLights = FALSE;
                FILE* pBak = fopen(szBak, "r");
                pFile = fopen(szLine, "w");
                if (pBak && pFile) {
                    gM.pose[0] = gP.rTargetPos[0]; gM.pose[1] = gP.rTargetPos[1]; gM.pose[2] = gP.rTargetPos[2];
                    gM.pose[3] = gP.rCameraPos[0]; gM.pose[4] = gP.rCameraPos[1]; gM.pose[5] = gP.rCameraPos[2] - CAMERA_OFFZ;
                    gM.pose[6] = gP.rPitch; gM.pose[7] = gP.rYaw; gM.pose[8] = gP.rRoll;
                    ClearMemory(szLine, sizeof(szLine));
                    while (fgets(szLine, MAX_PATH, pBak) != NULL) {
                        if (szLine[0] == '#') {
                            CharLowerA(szLine);
                            if (strstr(szLine, "#pose")) { // #pose found; insert new values
                                sprintf(szLine, "#pose %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n", // better precision is imperceptible
                                    gM.pose[0], gM.pose[1], gM.pose[2],
                                    gM.pose[3], gM.pose[4], gM.pose[5],
                                    gM.pose[6], gM.pose[7], gM.pose[8]);
                                hasPose = TRUE;
                            }
                            else if (strstr(szLine, "#light"))
                                hasLights = TRUE;
                        }
                        fputs(szLine, pFile);
                    }
                    // MLL 12-17-2019: Not very elegant but working
                    if (!hasPose) { // #pose not found; insert new #pose meta
                        fseek(pBak, SEEK_SET, 0);
                        fseek(pFile, SEEK_SET, 0);
                        if (hasLights) {
                            int nLights = 0;
                            while ((fgets(szLine, MAX_PATH, pBak) != NULL)) { // skip #light metas
                                if (szLine[0] == '#') {
                                    CharLowerA(szLine);
                                    if (strstr(szLine, "#light")) {
                                        fputs(szLine, pFile);
                                        nLights++;
                                    }
                                    if (nLights == 12)
                                        break;
                                }
                            }
                        }
                        sprintf(szLine, "\n#pose %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n", // add new #pose meta
                            gM.pose[0], gM.pose[1], gM.pose[2],
                            gM.pose[3], gM.pose[4], gM.pose[5],
                            gM.pose[6], gM.pose[7], gM.pose[8]);
                        fputs(szLine, pFile);
                        while ((fgets(szLine, MAX_PATH, pBak) != NULL)) // add all other remaining lines
                            fputs(szLine, pFile);
                    }
                    fclose(pBak);
                    fclose(pFile);
                }
            } else {
                pFile = fopen(szLine, "w");
                if (pFile) {
                    SaveLighting(pFile);
                    for (nItem = 1; nItem < nCount; nItem++) {
                        ClearMemory(szLine, sizeof(szLine));
                        SendMessageA(gP.hMaterial, LB_GETTEXT, nItem, (LPARAM)szLine);
                        if ((nItem == 1) && (szLine[0] == '\n')) { continue; }
                        fputs(szLine, pFile);
                    }
                    fclose(pFile);
                }
            }
            if (pFile)
                skDialogInfo($NULL, L"\nMaterial file updated successfully.", $NULL);
        }
    }
}


Mobj.h in Mobj_importMaterials():

(at the top)
........
    char fullPath[MAX_PATH] = { 0 };
    char s1[MAX_PATH] = { 0 }; char s2[32] = { 0 }; char s3[32] = { 0 }; // 10-09-2019 Bass.dll
    char s4[32], s5[32], s6[32], s7[32], s8[32], s9[32]; // MLL 12-17-2019:
    HWND hCtrl = 0; // PAT: 11-05-2019
........
(lower in global #metas)
........
                } else if (strcmp(szBuffer, "#demo") == 0) {                // PAT: 11-02-2019 MODEL ROTATION
                    if (gP.bGiration == 0)
                        PutFocusOn(GetDlgItem(gP.hMain, IDC_DEMO_MODE));
                } else if (strcmp(szBuffer, "#pose") == 0) {                // MLL 12-17-2019: MODEL INITIAL POSE other than at [0,0,0]
                    fgets(szBuffer, sizeof(szBuffer), pFile);
                    s1[0] = s2[0] = s3[0] = s4[0] = s5[0] = s6[0] = s7[0] = s8[0] = s9[0] = 0;
                    sscanf(szBuffer, "%s %s %s %s %s %s %s %s %s", s1, s2, s3, s4, s5, s6, s7, s8, s9);
                    gM.pose[0] = (float)atof(s1); gM.pose[1] = (float)atof(s2); gM.pose[2] = (float)atof(s3);
                    gM.pose[3] = (float)atof(s4); gM.pose[4] = (float)atof(s5); gM.pose[5] = (float)atof(s6);
                    gM.pose[6] = (float)atof(s7); gM.pose[7] = (float)atof(s8); gM.pose[8] = (float)atof(s9);
                    printf("\nModel posed at target translation %.f %.f %.f,"
                        "camera translation %.f %.f %.f, rotation %.f %.f %.f\n\n",
                        gM.pose[0], gM.pose[1], gM.pose[2], gM.pose[3], gM.pose[4], gM.pose[5], gM.pose[6], gM.pose[7], gM.pose[8]);

                } else {
........


Renderers.h:

// MLL 12-17-2019: #pose support
void lerpReset(IN BOOL start = FALSE) {
    static int frames, maxFrames; // render frames counter
    static float dtpX, dtpY, dtpZ; // target position deltas
    static float dcpX, dcpY, dcpZ; // camera position deltas
    static float drP, drY, drR; // camera rotation deltas
    static float frac;

    // Forward declarations
    void gl_AdjustNearPlane();
    void gl_Resize();
    void gl_DrawScene();
    void zeroCamera();

    if (start) {
        if (gP.rTargetPos[0] == (gM.pose[0] ? gM.pose[0] : gM.center[0])              &&
            gP.rTargetPos[1] == (gM.pose[1] ? gM.pose[1] : gM.center[1])              &&
            gP.rTargetPos[2] == (gM.pose[2] ? gM.pose[2] : gM.center[2])              &&
            gP.rCameraPos[0] == (gM.pose[3] ? gM.pose[3] : gP.rTargetPos[0])          &&
            gP.rCameraPos[1] == (gM.pose[4] ? gM.pose[4] : gP.rTargetPos[1])          &&
            gP.rCameraPos[2] == (gM.pose[5] ? gM.pose[5] + CAMERA_OFFZ : CAMERA_OFFZ) &&
            gP.rPitch == gM.pose[6] &&
            gP.rYaw == gM.pose[7] &&
            gP.rRoll == gM.pose[8]) {
            gP.bIsLerping = FALSE; // no need to lerp; already reset
            return;
        }

        frames = 0;
        maxFrames = (int)(gP.nFPS * 0.5f); // ca. half a second animation regardless of FPS
        frac = 1.0f / maxFrames;
        dtpX = (gP.rTargetPos[0] - gM.pose[0]) * frac;
        dtpY = (gP.rTargetPos[1] - gM.pose[1]) * frac;
        dtpZ = (gP.rTargetPos[2] - gM.pose[2]) * frac;
        dcpX = (gP.rCameraPos[0] - gM.pose[3]) * frac;
        dcpY = (gP.rCameraPos[1] - gM.pose[4]) * frac;
        dcpZ = (gP.rCameraPos[2] - gM.pose[5] - CAMERA_OFFZ) * frac;
        drP = (gP.rPitch - gM.pose[6]) * frac; if (drP < 0.0f) drP = -drP;
        drY = (gP.rYaw - gM.pose[7]) * frac; if (drY < 0.0f) drY = -drY;
        drR = (gP.rRoll - gM.pose[8]) * frac; if (drR < 0.0f) drR = -drR;
        gP.bIsLerping = TRUE;
        return;
    }

    frames += 1;

    if (gP.bIsLerping) {
        if (frames >= maxFrames) {
            zeroCamera();
            dtpX = dtpY = dtpZ = 0.0f;
            dcpX = dcpY = dcpZ = 0.0f;
            drP = drY = drR = 0.0f;
            frames = 0; frac = 0.0f;
            gP.bIsLerping = FALSE;
            gl_AdjustNearPlane();
            gl_Resize();
            return;
        }
    }

    if (dcpX != 0.0) {
        gP.rTargetPos[0] -= dtpX;
        gP.rCameraPos[0] -= dcpX;
    }
    if (dcpY != 0.0) {
        gP.rTargetPos[1] -= dtpY;
        gP.rCameraPos[1] -= dcpY;
    }
    if (dcpZ != (CAMERA_OFFZ * frac)) {
        gP.rTargetPos[2] -= dtpZ;
        gP.rCameraPos[2] -= dcpZ;
    }
    if (drP != 0.0f)
        gP.rPitch -= drP;
    if (drY != 0.0f)
        gP.rYaw -= drY;
    if (drR != 0.0f)
        gP.rRoll -= drR;

    gl_AdjustNearPlane();
    gl_Resize();
}



Hopefully, this time it's gonna work. I know lerpReset() still isn't perfect but it's passable on fast animations and a low number of animated frames (up to 0.5 secs in duration all in all).

Please let me know your impression.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: Animated Backgrounds?
« Reply #129 on: December 22, 2019, 10:04:53 pm »
EXCELLENT, that is exactly what i was especting.

That would help me to produce more realistic scene.

Thank you very much !
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #130 on: December 23, 2019, 11:51:44 am »
Hey Patrice,

WebGL accepts negative arguments to smoothstep() while OpenGL doesn't.

Now catch this Postcard.fs:

Code: [Select]
// Postcard by nimitz (twitter: @stormoid)
// https://www.shadertoy.com/view/XdBSWd
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// Contact the author for other licensing options

/*
Implementation of: http://iquilezles.org/www/articles/dynclouds/dynclouds.htm

Added some raymarched mountains and normal mapped water to complete the scene.

One thing I did differently is modyfying the scale of the fbm based on the distance
from the shaded clouds allowing for a much less "planar" look to the cloud layer.
*/

uniform vec4 iDate; // (year, month, day, time in seconds) MLL: year/month/day not used
uniform vec3 iMouse; // mouse pixel coords. xy: current (if MLB down); if vec3 then z: click
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)
uniform sampler2D colornoise_png;

//Compare with simple clouds
//#define BASIC_CLOUDS

#define time iTime * 2.0
#define FAR 420.0

//------------------------------------------------------------------
//----------------------Utility functions---------------------------
//------------------------------------------------------------------
vec3 rotx(vec3 p, float a) {
    float s = sin(a), c = cos(a);
    return vec3(p.x, c * p.y - s * p.z, s * p.y + c * p.z);
}

vec3 roty(vec3 p, float a) {
    float s = sin(a), c = cos(a);
    return vec3(c * p.x + s * p.z, p.y, -s * p.x + c * p.z);
}

float nmzHash(vec2 q) {
    uvec2 p = uvec2(ivec2(q));
    p = p * uvec2(374761393U, 22695477U) + p.yx;
    p.x = p.x * (p.y ^ (p.x >> 15U));
    return float(p.x ^ (p.x >> 16U)) * (1.0 / float(0xffffffffU));
}

float noise(in vec2 p) {
    vec2 ip = floor(p);
    vec2 fp = fract(p);
vec2 u = fp * fp * (3.0 - 2.0 * fp);
    return -1.0 + 2.0 * mix(mix(nmzHash(ip + vec2(0.0, 0.0)), nmzHash(ip + vec2(1.0, 0.0)), u.x),
                mix(nmzHash(ip + vec2(0.0, 1.0)), nmzHash(ip + vec2(1.0, 1.0)), u.x), u.y);
}
//------------------------------------------------------------------
//---------------------------Terrain--------------------------------
//------------------------------------------------------------------
float terrain(in vec2 p) {
    p *= 0.035;
    float rz = 0.;
    float m = 1.;
    float z = 1.;
    for (int i = 0; i <= 2; i++) {
        rz += (sin(noise(p / m) * 1.7) * 0.5 + 0.5) * z;
        m *= -0.25;
        z *= .2;
    }
    rz = exp2(rz - 1.5);
    rz -= sin(p.y * .2 + sin(p.x * .45));
    return rz * 20. - 14.;
}

float tmap(in vec3 p) {return p.y - terrain(p.zx);}
//Using "cheap AA" from eiffie (https://www.shadertoy.com/view/XsSXDt)
vec3 tmarch(in vec3 ro, in vec3 rd, in float d) {
float precis = 0.01;
    float h = precis * 2.0;
    float hm = 100., dhm = 0.;
    for (int i = 0; i < 15; i++) {
        d += h = tmap(ro + rd * d) * 1.5;
        if (h < hm) {
            hm = h;
            dhm = d;
        }
        if(abs(h) < precis || d > FAR) break;
    }
return vec3(d, hm, dhm);
}

vec3 normal(in vec3 pos, float t) {
float e = 0.001 * t;
    vec2  eps = vec2(e, 0.0);
    float h = terrain(pos.xz);
    return normalize(vec3(terrain(pos.xz - eps.xy) - h, e, terrain(pos.xz - eps.yx) - h));
}

float plane(in vec3 ro, in vec3 rd, vec3 c, vec3 u, vec3 v) {
vec3 q = ro - c;
vec3 n = cross(u, v);
    return -dot(n, q) / dot(rd, n);
}
//------------------------------------------------------------------
//-------------------------2d Clouds--------------------------------
//------------------------------------------------------------------
vec3 lgt = normalize(vec3(-1.0, 0.1, .0));
vec3 hor = vec3(0.);

float nz(in vec2 p) {return texture(colornoise_png, p * .01).x;}
mat2 m2 = mat2(0.80,  0.60, -0.60,  0.80);
float fbm(in vec2 p, in float d) {
d = smoothstep(0., 100., d);
    p *= .3 / (d + 0.2);
    float z = 2.;
float rz = 0.;
    p  -= time * 0.02;
for (float i = 1.; i <= 5.; i++) {
rz += (sin(nz(p) * 6.5) * 0.5 + 0.5) * 1.25 / z;
z *= 2.1;
p *= 2.15;
        p += time * 0.027;
        p *= m2;
}
    return pow(abs(rz), 2. -d);
}

vec4 clouds(in vec3 ro, in vec3 rd, in bool wtr) {
    //Base sky coloring is from iq's "Canyon" (https://www.shadertoy.com/view/MdBGzG)
    float sun = clamp(dot(lgt, rd), 0.0, 1.0);
    hor = mix(1. * vec3(0.70, 1.0, 1.0), vec3(1.3, 0.55, 0.15), 0.25 + 0.75 * sun);
    vec3 col = mix(vec3(0.5, 0.75, 1.), hor, exp(-(4. + 2. * (1. - sun)) * max(0.0, rd.y - 0.05)));
    col *= 0.4;

    if (!wtr) {
        col += 0.8 * vec3(1.0, 0.8, 0.7) * pow(sun, 512.0);
        col += 0.2 * vec3(1.0, 0.4, 0.2) * pow(sun, 32.0);
    }
    else {
        col += 1.5 * vec3(1.0, 0.8, 0.7) * pow(sun, 512.0);
        col += 0.3 * vec3(1.0, 0.4, 0.2) * pow(sun, 32.0);
    }
    col += 0.1 * vec3(1.0, 0.4, 0.2) * pow(sun, 4.0);

float pt = (90.0 - ro.y) / rd.y;
    vec3 bpos = ro + pt * rd;
    float dist = sqrt(distance(ro, bpos));
    float s2p = distance(bpos, lgt * 100.);

    const float cls = 0.002;
    float bz = fbm(bpos.xz * cls, dist);
    float tot = bz;
    const float stm = .0;
    const float stx = 1.15;
    tot = smoothstep(stm, stx, tot);
    float ds = 2.;
    for (float i = 0.; i <= 3.; i++) {
        vec3 pp = bpos + ds * lgt;
        float v = fbm(pp.xz * cls, dist);
        v = smoothstep(stm, stx, v);
        tot += v;
        #ifndef BASIC_CLOUDS
        ds *= .14 * dist;
        #endif
    }

    col = mix(col, vec3(.5, 0.5, 0.55) * 0.2, pow(bz, 1.5));
    tot = smoothstep(-7.5, 0., 1. - tot);
    vec3 sccol = mix(vec3(0.11, 0.1, 0.2), vec3(.2, 0., 0.1), smoothstep(0., 900., s2p));
    col = mix(col, sccol, 1. - tot) * 1.6;
    vec3 sncol = mix(vec3(1.4, 0.3, 0.), vec3(1.5, .65, 0.),smoothstep(0., 1200., s2p));
    float sd = pow(sun, 10.) + .7;
    col += sncol * bz * bz * bz * tot * tot * tot * sd;

    if (wtr) col = mix(col, vec3(0.5, 0.7, 1.) * 0.3, 0.4); //make the water blue-er
    return vec4(col, tot);
}
//------------------------------------------------------------------
//-------------------------------Extras-----------------------------
//------------------------------------------------------------------
float bnoise(in vec2 p) {
    float d = sin(p.x * 1.5 + sin(p.y * .2)) * 0.1;
    return d += texture(colornoise_png, p.xy * 0.01 + time * 0.001).x * 0.04;
}

vec3 bump(in vec2 p, in vec3 n, in float t) {
    vec2 e = vec2(40., 0.) / (t * t);
    float n0 = bnoise(p);
    vec3 d = vec3(bnoise(p + e.xy) - n0, 2., bnoise(p + e.yx) - n0) / e.x;
    n = normalize(n - d);
    return n;
}
//------------------------------------------------------------------
//------------------------------------------------------------------
void main() {
    vec2 bp = gl_FragCoord.xy / iResolution.xy * 2. - 1.;
    vec2 p  = bp;
p.x *= iResolution.x / iResolution.y;
vec2 mo = iMouse.xy / iResolution.xy - .5;
    mo = (mo == vec2(-.5)) ? mo = vec2(-0.4, -0.15) : mo;
mo.x *= iResolution.x / iResolution.y;
vec3 ro = vec3(140., 0., 100.);
    vec3 rd = normalize(vec3(p, -2.7));
    rd = rotx(rd, 0.15 + mo.y * 0.4); rd = roty(rd, 1.5 + mo.x * 0.5);
    vec3 brd = rd;
    vec3 col = vec3(0);

float pln = plane(ro, rd, vec3(0., -4., 0.), vec3(1., 0., 0.), vec3(0.0, .0, 1.0));
    vec3 ppos = ro + rd * pln;
    bool wtr = false;
    vec3 bm = vec3(0);
    if (pln < 500. && pln > 0.) {
        vec3 n = vec3(0., 1., 0.);
        float d = distance(ro, ppos);
        n = bump(ppos.xz, n, d);
        bm = n;
        rd = reflect(rd, n);
        wtr = true;
    }
    vec4 clo = clouds(ro, rd, wtr);
    col = clo.rgb;

    vec3 rz = tmarch(ro, brd, 350.);
    float px = 3.5 / iResolution.y;
    if (rz.x < FAR && (rz.x < pln || pln < 0.)) {
        vec3 pos = ro + brd * rz.x;
        float dst = distance(pos, ro);
        vec3 nor = normal(pos, dst);
        float nl = clamp(dot(nor, lgt), 0., 1.);
        vec3 mcol = vec3(0.04) + vec3(nl) * 0.4 * vec3(.5, 0.35, 0.1);
        mcol = mix(mcol, hor, smoothstep(210., 400., rz.x - (pos.y + 18.) * 5.));//fogtains
        col = mix(mcol, col, clamp(rz.y / (px * rz.z), 0., 1.));
    }

    //smooth water edge
    if (wtr && rz.x > pln) col = mix(col, hor * vec3(0.3, 0.4, .6) * 0.4, smoothstep(10., 200., pln));

    //post
    col = pow(clamp(col, 0.0, 1.0), vec3(.9));
    col.g *= 0.93;
    //fancy vignetting
    float vgn1 = pow(smoothstep(0.0, .3, (bp.x + 1.) * (bp.y + 1.) * (bp.x - 1.) * (bp.y - 1.)), .5);
    float vgn2 = 1. - pow(dot(vec2(bp.x * .3, bp.y), bp), 3.);
    col *= mix(vgn1, vgn2, .4) * .5 + 0.5;
gl_FragColor = vec4(col, 1.0);
}


Enjoy! :)


P.S. You can also optionally make all occurrencies of colornoise_png unmipmapped by adding nomip_colornoise_png. It will make the clouds a little less dense.
« Last Edit: December 23, 2019, 11:58:39 am by Michael Lobko-Lobanovsky »
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: Animated Backgrounds?
« Reply #131 on: December 23, 2019, 04:04:31 pm »
Thank you for this new shader !

I shall release soon, version #2.85, to be able to use the new #pose meta command within material files.
« Last Edit: December 23, 2019, 04:09:00 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #132 on: December 24, 2019, 02:16:19 am »
Here are some more practical animated backgrounds:

ThinWater.fs:

Code: [Select]
uniform vec4 iDate; // (year, month, day, time in seconds) MLL: year/month/day not used
uniform vec3 iMouse; // mouse pixel coords. xy: current (if MLB down); if vec3 then z: click
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)
uniform sampler2D stone_jpg;
uniform sampler2D moss_jpg;

float lum(vec3 rgb) {
return dot(rgb,vec3(0.299, 0.587, 0.114));
}

float depthf(vec2 uv) {
    vec2 uv_= uv;
    uv_.y += iTime * .25;
    float back = lum(texture(stone_jpg, uv).rgb);//luminance approximates depth
    uv_.y += back * .2;//create flow
    float water = lum(texture(moss_jpg, uv_).rgb);
return dot(vec2(water, back), vec2(.5, .5));
}

void main() {
    vec2 uv = gl_FragCoord / iResolution.xy;

    float d = 0.03;
    float d0 = depthf(uv);
    vec2 grad = (vec2(
    depthf(uv + vec2(d, 0)),
        depthf(uv + vec2(0, d))
    ) - d0) / d;
    vec3 norm = normalize(1. - vec3(grad.x, grad.y, 0.));

    float shine = max(0., reflect(normalize(vec3(1.)), norm).z) * .2;

    uv += refract(vec3(0., 0., 1.), norm, .5).xy * .008;

    vec3 col = texture(stone_jpg, uv).rgb;
    col = pow(col, vec3(1. / 2.2)); //gamma

    col += shine * shine;

    col = pow(col, vec3(2.2));
    gl_FragColor = vec4(col, 1.0);
}

GlowFrame.fs:

Code: [Select]
// Inner outline shader for Match2 game cell.
// Based on shader created by @remonvv
// https://www.shadertoy.com/view/MdjfRK
//
// Thanks to t.me/ru_cocos2dx@satan_black for help

uniform vec4 iDate; // (year, month, day, time in seconds) MLL: year/month/day not used
uniform vec3 iMouse; // mouse pixel coords. xy: current (if MLB down); if vec3 then z: click
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)

float rand(vec2 n) {
    return fract(sin(dot(n, vec2(12.9898,12.1414))) * 83758.5453);
}

float noise(vec2 n) {
    const vec2 d = vec2(0.0, 1.0);
    vec2 b = floor(n);
    vec2 f = mix(vec2(0.0), vec2(1.0), fract(n));
    return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
}

vec3 ramp(float t) {
return t <= .5 ? vec3( 1. - t * 1.4, .2, 1.05 ) / t : vec3( .3 * (1. - t) * 2., .2, 1.05 ) / t;
}

float fire(vec2 n) {
    return noise(n) + noise(n * 2.1) * .6 + noise(n * 5.4) * .42;
}

/*
vec3 getLine(vec2 fc, mat2 mtx, float shift){
    float t = iTime;
    vec2 uv = (fc / iResolution.xy) * mtx;

    uv.x += uv.y < .5 ? 23.0 + t * .35 : -11.0 + t * .3;
    uv.y = abs(uv.y - shift);
    uv *= 10.0;

    float q = fire(uv - t * .013) / 2.0;
    vec2 r = vec2(fire(uv + q / 2.0 + t - uv.x - uv.y), fire(uv + q - t));
    vec3 color = vec3(1.0 / (pow(vec3(0.5, 0.0, .1) + 1.61, vec3(4.0))));

    float grad = pow((r.y + r.y) * max(.0, uv.y) + .1, 4.0);
    color = ramp(grad);
    color /= (1.50 + max(vec3(0), color));
    return color;
}

void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
vec2 uv = fragCoord / iResolution.xy;
    fragColor = vec4(getLine(fragCoord, mat2(1., 1., 0., 1.), 1.), 1.);
    fragColor += vec4(getLine(fragCoord, mat2(1., 1., 1., 0.), 1.), 1.);
    fragColor += vec4(getLine(fragCoord, mat2(1., 1., 0., 1.), 0.), 1.);
    fragColor += vec4(getLine(fragCoord, mat2(1., 1., 1., 0.), 0.), 1.);
    if(fragColor.r <= .05 && fragColor.g <= .05 && fragColor.b <= .05 ){
    fragColor = texture(iChannel0, uv);
    } else {
        fragColor += texture(iChannel0, uv);
    }
}
*/

vec3 getLine(vec3 col, vec2 fc, mat2 mtx, float shift) {
    float t = iTime;
    vec2 uv = (fc / iResolution.xy) * mtx;

    uv.x += uv.y < .5 ? 23.0 + t * .35 : -11.0 + t * .3;
    uv.y = abs(uv.y - shift);
    uv *= 5.0;

    float q = fire(uv - t * .013) / 2.0;
    vec2 r = vec2(fire(uv + q / 2.0 + t - uv.x - uv.y), fire(uv + q - t));
    vec3 color = vec3(1.0 / (pow(vec3(0.5, 0.0, .1) + 1.61, vec3(4.0))));

    float grad = pow((r.y + r.y) * max(.0, uv.y) + .1, 4.0);
    color = ramp(grad);
    color /= (1.50 + max(vec3(0), color));

    if(color.b < .00000005)
        color = vec3(.0);

    return mix(col, color, color.b);
}

void main() {
    vec2 uv = gl_FragCoord / iResolution.xy;
    vec3 color = vec3(0.);
    color = getLine(color, gl_FragCoord, mat2(1., 1., 0., 1.), 1.02);
    color = getLine(color, gl_FragCoord, mat2(1., 1., 1., 0.), 1.02);
    color = getLine(color, gl_FragCoord, mat2(1., 1., 0., 1.), -0.02);
    color = getLine(color, gl_FragCoord, mat2(1., 1., 1., 0.), -0.02);

    gl_FragColor = vec4(color, 1.0);
}

IntoTheLight.fs:

Code: [Select]
uniform vec4 iDate; // (year, month, day, time in seconds) MLL: year/month/day not used
uniform vec3 iMouse; // mouse pixel coords. xy: current (if MLB down); if vec3 then z: click
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)

float star(in vec2 uv, in float t) {
    float phi = atan(uv.y, uv.x);
    float d = length(uv)
        * (1.
           + 0.15 * sin( phi * 4. + 0.2 * t)
           + 0.1 * sin( 1.2 * phi * 10. + 0.6 * t)
           + 0.2 * sin( phi * phi * 2. + 0.4 * t)
          ) * 7.7 * ( 1. + 0.1 * sin(t));
    d = log(d * d);
    return 0.7 * d;
}

void main() {
vec2 uv = (gl_FragCoord - .5 * iResolution.xy) / iResolution.x;

    float t = 1.4 * iTime;
    float d = 0.5 * min(star(uv, t), star(uv, t - 2.1));
    d = (1. + 0.1 * sin(t)) * min(d, star(uv, t - 3.1));

    gl_FragColor = vec4(sqrt(smoothstep(1., 0., d)));
}

Kepler.fs:

Code: [Select]
/*--------------------------------------------------------------------------------------
License CC0 - http://creativecommons.org/publicdomain/zero/1.0/
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
----------------------------------------------------------------------------------------
^ This means do ANYTHING YOU WANT with this code. Because we are programmers, not lawyers.
-Otavio Good
*/

uniform vec4 iDate; // (year, month, day, time in seconds) MLL: year/month/day not used
uniform vec3 iMouse; // mouse pixel coords. xy: current (if MLB down); if vec3 then z: click
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)

uniform sampler2D moss_jpg;
uniform sampler2D pebblesud_jpg;
uniform sampler2D bwnoise_png;
uniform sampler2D stars_jpg;

float PI=3.14159265;
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 15.0;
vec3 environmentSphereColor = vec3(0.3001, 0.501, 0.901) * 0.0;

float distFromSphere;
vec3 normal;
vec3 texBlurry;

vec3 saturate(vec3 a) {
return clamp(a, 0.0, 1.0);
}

vec2 saturate(vec2 a) {
return clamp(a, 0.0, 1.0);
}

float saturate(float a) {
return clamp(a, 0.0, 1.0);
}

vec3 GetSunColor(vec3 rayDir, vec3 sunDir) {
float dense = 16.0;
vec3 localRay = normalize(rayDir);
float sunIntensity = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
//sunIntensity = (float)Math.Pow(sunIntensity, 14.0);
sunIntensity = 0.2 / sunIntensity;
sunIntensity = min(sunIntensity, 40000.0);
sunIntensity = max(0.0, sunIntensity - 3.0);

localRay.x = localRay.x + 1.0 - iTime * 0.1;
//vec3 right = normalize(cross(sunDir, vec3(0.0,1.0,0.0)));
//vec3 up = normalize(cross(sunDir, right));
vec2 wrap = fract((localRay.xy)*dense);
vec4 rand = texture(bwnoise_png, floor(localRay.xy*dense)/dense).xyzw;
vec3 starColor = rand.xyz;
starColor = starColor * 0.75 + 0.25;
rand.xy = rand.xy * 2.0 - 1.0;
vec2 center = vec2(0.5, 0.5) + rand.xy * 0.9;// floor(fract((localRay.xy)*8.0)) + 0.5;
float star = length(wrap - center);
float test = star;
star = saturate((1.0 - star));
float blink = texture(bwnoise_png, localRay.xy + iTime * 0.03).x;
float cluster = 0.3;// /*(localRay.x+0.5) */ (localRay.y+0.5) * 2.8 + 0.8;
star = pow(star, 60.0 + saturate(rand.z - 0.0) * 250.0 * cluster);
star *= blink;

float milkyMask = saturate(0.25 - abs(localRay.x - 0.65));
vec3 milkyway = texture(stars_jpg, (localRay.yx*1.5 )+vec2(0.65, 0.3)).yxz;
vec3 milkyLOD = texture(stars_jpg, (localRay.yx*1.5 )+vec2(0.65, 0.3), 3.0).yxz;
vec3 milkyDetail = texture(stars_jpg, (-localRay.yx*8.0 )+vec2(0.65, 0.3)).yxz;
milkyway *= milkyDetail.xxx;
milkyway *= vec3(1.0, 0.8, 0.91)*1.5;
milkyway = pow(milkyway, vec3(2.0, 2.0, 2.0)*3.0);
milkyway += vec3(0.2, 0.0015, 1.001) * milkyLOD * 0.006;

vec3 finalColor = milkyway /* milkyMask*/ * 10850.0;
finalColor += environmentSphereColor + sunCol * sunIntensity + starColor * star * 12000.0 * cluster;
return finalColor;
//return environmentSphereColor + sunCol * sunIntensity + starColor * star * 12000.0 * cluster;
//return vec3(1.0,1.0,1.0)*cluster*1000.0;
}

vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir) {
vec3 localRay = normalize(rayDir);
float sunIntensity = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
//sunIntensity = (float)Math.Pow(sunIntensity, 14.0);
sunIntensity = 0.2 / sunIntensity;
sunIntensity = min(sunIntensity, 40000.0);
return environmentSphereColor + sunCol * sunIntensity;
}

vec3 LensFlare(vec2 uv, vec2 lfPos) {
vec2 delta = uv - lfPos;
float dist = length(delta);
float angle = atan(delta.x, delta.y);
vec3 tex = texture(moss_jpg, vec2(angle*5.0, dist*0.125) /*- iTime*0.1*/).xyz;
float bump = sin(angle * 6.0) * 0.5 + 0.54;
bump -= pow(dist, 0.0125);
bump = saturate(bump);

return sunCol * tex.x * 0.1 * bump / (dist);
}

float IntersectSphereAndRay(vec3 pos, float radius, vec3 posA, vec3 posB, out vec3 intersectA2, out vec3 intersectB2) {
// Use dot product along line to find closest point on line
vec3 eyeVec2 = normalize(posB-posA);
float dp = dot(eyeVec2, pos - posA);
vec3 pointOnLine = eyeVec2 * dp + posA;
// Clamp that point to line end points if outside
//if ((dp - radius) < 0) pointOnLine = posA;
//if ((dp + radius) > (posB-posA).Length()) pointOnLine = posB;
// Distance formula from that point to sphere center, compare with radius.
float distance = length(pointOnLine - pos);
float ac = radius*radius - distance*distance;
float rightLen = 0.0;
if (ac >= 0.0) rightLen = sqrt(ac);
intersectA2 = pointOnLine - eyeVec2 * rightLen;
intersectB2 = pointOnLine + eyeVec2 * rightLen;
distFromSphere = distance - radius;
if (distance <= radius) return 1.0;
return 0.0;
}

vec2 Spiral(vec2 uv) {
float reps = 2.0;
vec2 uv2 = fract(uv*reps);
vec2 center = floor(fract(uv*reps)) + 0.5;
vec2 delta = uv2 - center;
float dist = length(delta);
float angle = atan(delta.y, delta.x);
//if (distance(center, uv2) < 0.02) return vec2(10,10);
float nudge = dist * 4.0;
vec2 offset = vec2(delta.y, -delta.x);// * 0.2 / dist ;// vec2(sin(angle+nudge), cos(angle+nudge));
float blend = max(abs(delta.x), abs(delta.y))* 2.0;
blend = clamp((0.5 - dist) * 2.0, 0.0, 1.0);
blend = pow(blend, 1.5);
//offset *= clamp(1.0 - blend, 0.0, 1.0);
offset *= clamp(blend, 0.0, 1.0);
//if (dist > 0.5) offset = vec2(0,0);
//offset *= dist;
return uv + offset*vec2(1.0,1.0)*1.1*texBlurry.x ;//+ vec2(iTime*0.03, 0.0);
}

void main() {
//vec2 uv = fragCoord.xy / iResolution.yy + vec2(-0.4,0.0);
//vec3 worldPix = vec3(uv*2.0 - 1.0, 1.65);// + (iMouse.x - iResolution.x * 0.2)* 0.01);
//vec3 camPos = vec3(0.0,0.1,0.0);

vec2 uv = gl_FragCoord.xy / iResolution.xy - 0.5;

// Camera up vector.
vec3 camUp=vec3(0,1,0); // vuv

// Camera lookat.
vec3 camLookat=vec3(0,0.0,0); // vrp

float mx=-PI/2.0;//iMouse.x/iResolution.x*PI*2.0;
float my=0.0;//-iMouse.y/iResolution.y*10.0;//*PI/2.01;
vec3 camPos=vec3(cos(my)*cos(mx),sin(my),cos(my)*sin(mx))*(2.5); // prp

// Camera setup.
vec3 camVec=normalize(camLookat - camPos);//vpn
vec3 sideNorm=normalize(cross(camUp, camVec)); // u
vec3 upNorm=cross(camVec, sideNorm);//v
vec3 worldFacing=(camPos + camVec);//vcv
vec3 worldPix = worldFacing + uv.x * sideNorm * (iResolution.x/iResolution.y) + uv.y * upNorm;//scrCoord
vec3 relVec = normalize(worldPix - camPos);//scp


vec3 planetPos = vec3(0.0,0.0,0.0);
vec3 iA, iB, iA2, iB2;
float t = iTime * 0.05 + 0.35 - iMouse.x*0.01; // MLL: was *0.1+0.7
float cloudT = iTime * 0.1;
float distFromSphere2;
vec3 normal2;
float hit2 = IntersectSphereAndRay(planetPos, 1.05, camPos, worldPix, iA2, iB2);
normal2 = normal;
distFromSphere2 = distFromSphere;
float hit = IntersectSphereAndRay(planetPos, 1.0, camPos, worldPix, iA, iB);
//float hit = IntersectSphereAndRay(planetPos, 1.0, camPos, pixPos, iA, iB);
normal = normalize(iA - planetPos);
//if (abs(normal.x) <= 0.001) normal.x += 0.001;
vec2 polar = vec2(atan(normal.x, normal.z)/*0.955*/, acos(normal.y));
polar.x = (polar.x + PI) / (PI * 2.0);
polar.y = polar.y / PI;// + 0.5;
if (abs(normal.x) <= 0.02) {
//polar.x = 0.0;
}
polar.x = (polar.x+2.03);
polar.xy = iA.xy;
//polar.y = floor(polar.y * 32.0) / 32.0;
/* if (abs(normal.x) < abs(normal.z)) {
polar = vec2((atan(normal.z, normal.x))*0.955, acos(iA.y));
//polar.x = 0.0;
}*/
//+ vec2(0.0,iTime * 0.01)
vec4 texNoise = texture(bwnoise_png, (polar.xy+vec2(t,0)) * 2.0);
texNoise.y = texture(bwnoise_png, (polar.xy+vec2(t,0)) * 1.0).y;
texNoise.z = texture(bwnoise_png, (polar.xy+vec2(t,0)) * 4.0).z;
texBlurry = texture(moss_jpg, (polar.xy+vec2(t,0))*0.03125*0.25 ).rgb;

vec3 tex = texture(moss_jpg, (polar.xy+vec2(t,0))*1.0).rgb;
//vec3 tex = texture(moss_jpg, polar.xy, 0.0).rgb;
tex *= tex;
vec3 texFlip = texture(moss_jpg, (1.0 - (polar.xy+vec2(t,0))*0.5)).rgb;
texFlip *= texFlip;

vec3 texS = texture(moss_jpg, (Spiral(polar.xy+vec2(t,0))+vec2(cloudT*0.25,0))*1.0).rgb;
texS *= texS;
vec3 texFlipS = texture(moss_jpg, (1.0 - (Spiral(polar.xy+vec2(t,0))+vec2(cloudT*0.25,0))*0.5)).rgb;
texFlipS *= texFlipS;

float atmosphereDensity = (1.45 + normal.z);
vec3 atmosphereColor = vec3(0.075, 0.35, 0.99) * 0.45;
float cloudDensity = max(0.0, (pow(texFlipS.x * texS.x, 0.7) * 3.0));
vec3 finalAtmosphere = atmosphereColor * atmosphereDensity + cloudDensity;
vec3 finalColor = finalAtmosphere;

vec3 detailMap = min(texture(stars_jpg, (polar.xy+vec2(t,0)) * 2.0).xyz, 0.25) * 4.0;
float land = pow(max(0.0, texture(pebblesud_jpg, (polar.xy+vec2(t,0))* 0.25).z - 0.25), 0.4)*0.75;
float land2 = land * texBlurry.x * 6.0;
land *= detailMap.x;
//land2 *= detailMap.x;
land2 = max(0.0, land2);
land -= tex.x*0.65;
land = max(0.0, land);
float iceFactor = abs(pow(normal.y,2.0));
vec3 landColor = max(vec3(0.0,0.0,0.0), vec3(0.13,0.65,0.01) * land);// * (1.0 + iceFactor*2.0);
vec3 landColor2 = max(vec3(0.0,0.0,0.0), vec3(0.8,0.4,0.01) * land2);
vec3 mixedLand = (landColor + landColor2)* 0.5;
mixedLand *= (detailMap.zyx + 2.0) * 0.333;
//float hardBlur = saturate((texBlurry.x - 0.2)* 104.0 + 0.2)* 0.2 + 0.4;
//vec3 finalLand = mix(landColor, landColor2, hardBlur);
vec3 finalLand = mix(mixedLand, vec3(7.0, 7.0, 7.0) * land, iceFactor);
finalLand = mix(atmosphereColor * 0.05, finalLand, pow(min(1.0,max(0.0,-distFromSphere*1.0)), 0.2));
finalColor += finalLand;
finalColor *= hit;

float refNoise = (texNoise.x + texNoise.y + texNoise.z)* 0.3333;
vec3 noiseNormal = normal;
noiseNormal.x += refNoise*0.05*hit;
noiseNormal.y += tex.x*hit*0.1;
noiseNormal.z += texFlip.x*hit*0.1;
noiseNormal = normalize(noiseNormal);
vec3 ref = reflect(normalize(worldPix - camPos), noiseNormal);

refNoise = refNoise*0.25 + 0.75;
float orbitSpeed = 0.125;
//vec3 sunDir = normalize(vec3(-0.9 + sin(iTime*0.2)*0.5, -0.1, -0.9150));
vec3 sunDir = normalize(vec3(-0.009 + sin(iTime*orbitSpeed), -0.13, -cos(iTime*orbitSpeed)));
vec3 r = normalize(cross(sunDir, vec3(0.0,1.0,0.0)));
vec3 up = normalize(cross(sunDir, r));
float binarySpeed = 0.5;
float binaryDist = 0.3;
sunDir += r * sin(iTime*binarySpeed) * binaryDist + up * cos(iTime*binarySpeed) * binaryDist;
sunDir = normalize(sunDir);

vec3 sunDir2 = normalize(vec3(-0.009 + sin((iTime+0.2)*orbitSpeed), 0.13, -cos((iTime+0.2)*orbitSpeed)));
r = normalize(cross(sunDir2, vec3(0.0,1.0,0.0)));
up = normalize(cross(sunDir2, r));
sunDir2 -= r * sin(iTime*binarySpeed) * binaryDist + up * cos(iTime*binarySpeed) * binaryDist;
sunDir2 = normalize(sunDir2);

vec3 refNorm = normalize(ref);
float glance = saturate(dot(refNorm, sunDir) * saturate(sunDir.z - 0.65));
float glance2 = saturate(dot(refNorm, sunDir2) * saturate(sunDir2.z - 0.65));
float landMask = finalLand.x + finalLand.y * 1.5;
vec3 sunRef = GetSunColorReflection(refNorm, sunDir)*0.005*hit * (1.0 - saturate(landMask*3.5)) * (1.0-texS.x) * refNoise;
vec3 sunRef2 = GetSunColorReflection(refNorm, sunDir2)*0.005*hit * (1.0 - saturate(landMask*3.5)) * (1.0-texS.x) * refNoise;

//sunRef = mix(sunRef, atmosphereColor * vec3(1.0, 0.2, 0.1)*3.0, saturate(atmosphereDensity - 0.35)) * hit;
//sunRef *= atmosphereColor;
// fade to sunset color at outer atmosphere
sunRef = mix(sunRef, vec3(3.75, 0.8, 0.02)* hit, glance);
sunRef2 = mix(sunRef2, vec3(3.75, 0.8, 0.02)* hit, glance2);
finalColor += sunRef;
finalColor += sunRef2;

vec3 sunsColor = GetSunColor(normalize(ref), sunDir) *0.000096*(1.0-hit) +
GetSunColor(normalize(ref), sunDir2)*0.000096*(1.0-hit);

float outerGlow = 1.0 - clamp(distFromSphere * 20.0, 0.0, 1.0);
outerGlow = pow(outerGlow, 5.2);
finalColor += (atmosphereColor + vec3(0.2, 0.2, 0.2)) * outerGlow * (1.0 - hit);

float light = saturate(dot(sunDir, noiseNormal));
light += saturate(dot(sunDir2, noiseNormal));
finalColor *= light * 0.75 + 0.001; // ambient light (from stars, of course)
finalColor += sunsColor;

float scattering, scattering2;
if (hit2 == 1.0) scattering = distance(iA2, iB2);
scattering2 = scattering;
scattering *= pow(saturate(dot(relVec, sunDir) - 0.96), 2.0);
scattering2 *= pow(saturate(dot(relVec, sunDir2) - 0.96), 2.0);
scattering *= hit2 * (1.0 - hit);
scattering2 *= hit2 * (1.0 - hit);
scattering *= outerGlow;
scattering2 *= outerGlow;
finalColor += vec3(1.0, 0.25, 0.05) * scattering * 3060.0;
finalColor += vec3(1.0, 0.25, 0.05) * scattering2 * 3060.0;

//vec3 sunUV = worldFacing + sunDir2.x * sideNorm * (iResolution.x/iResolution.y) + sunDir2.y * upNorm;
//float lame = distance(sunUV, worldPix);
//vec3 sunUV = sunDir2 / vec3(sideNorm  * (iResolution.x/iResolution.y) * 0.4 + upNorm * 0.999);
//finalColor += LensFlare(uv * vec2((iResolution.x/iResolution.y), 1.0), vec2(sunUV.x, sunUV.y));

//finalColor = vec3(1.0,1.0,1.0) * (finalLand.x + finalLand.y)*hit;
//finalColor += vec3(1.0,1.0,1.0) * glance;
//finalColor = vec3(0, polar.x, 0.0);
//finalColor = GetSunColorReflection(normalize(ref), sunDir)*0.005*hit * (1.0 - clamp(finalLand.x*12.0, 0.0, 1.0)) * (1.0-texS.x) * refNoise;
//if (abs(normal.x) <= 0.001) finalColor = vec3(1.0,0.0,0.0);

gl_FragColor = vec4(sqrt(finalColor), 1.0);
}
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: Animated Backgrounds?
« Reply #133 on: December 24, 2019, 10:12:30 am »
Thank you my friend!

I don't know if you have that one.

Flame.fs
Code: [Select]
uniform vec4 iDate; // (year, month, day, time in seconds) MLL: year/month/day not used
uniform vec3 iMouse; // mouse pixel coords. xy: current (if MLB down); if vec3 then z: click
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)

float noise(vec3 p) //Thx to Las^Mercury
{
vec3 i = floor(p);
vec4 a = dot(i, vec3(1., 57., 21.)) + vec4(0., 57., 21., 78.);
vec3 f = cos((p-i)*acos(-1.))*(-.5)+.5;
a = mix(sin(cos(a)*a),sin(cos(1.+a)*(1.+a)), f.x);
a.xy = mix(a.xz, a.yw, f.y);
return mix(a.x, a.y, f.z);
}

float sphere(vec3 p, vec4 spr)
{
return length(spr.xyz-p) - spr.w;
}

float flame(vec3 p)
{
float d = sphere(p*vec3(1.,.5,1.), vec4(.0,-1.,.0,1.));
return d + (noise(p+vec3(.0,iTime*2.,.0)) + noise(p*3.)*.5)*.25*(p.y) ;
}

float scene(vec3 p)
{
return min(100.-length(p) , abs(flame(p)) );
}

vec4 raymarch(vec3 org, vec3 dir)
{
float d = 0.0, glow = 0.0, eps = 0.02;
vec3  p = org;
bool glowed = false;

for(int i=0; i<64; i++)
{
d = scene(p) + eps;
p += d * dir;
if( d>eps )
{
if(flame(p) < .0)
glowed=true;
if(glowed)
        glow = float(i)/64.;
}
}
return vec4(p,glow);
}

void main()
{
vec2 v = -1.0 + 2.0 * gl_FragCoord.xy / iResolution.xy;
v.x *= iResolution.x/iResolution.y;

vec3 org = vec3(0., -2., 4.);
vec3 dir = normalize(vec3(v.x*1.6, -v.y, -1.5));

vec4 p = raymarch(org, dir);
float glow = p.w;

vec4 col = mix(vec4(1.,.5,.1,1.), vec4(0.1,.5,1.,1.), p.y*.02+.4);

gl_FragColor = mix(vec4(0.), col, pow(glow*2.,4.));
//gl_FragColor = mix(vec4(1.), mix(vec4(1.,.5,.1,1.),vec4(0.1,.5,1.,1.),p.y*.02+.4), pow(glow*2.,4.));

}
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: Animated Backgrounds?
« Reply #134 on: December 24, 2019, 10:58:18 am »
I used to play with it in my Objector but I didn't add it to OR ani shaders.

Thank you!

I'm currently adding a lot of PP shaders but they'll go into OR v3.0.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)