drparse / GeoNoCarSlim

// ==UserScript==
// @name         GeoNoCarSlim
// @description  New slim design! Redacts the car from geoguessr. Shift-K to toggle compass. See GeoNoCar for the original.
// @namespace    https://www.geoguessr.com/
// @version      1.0
// @author       drparse
// @match        https://www.geoguessr.com/*
// @grant        unsafeWindow
// @run-at       document-start
// @updateURL    https://openuserjs.org/meta/drparse/GeoNoCarSlim.meta.js
// @copyright 2020, drparse
// @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @noframes
// ==/UserScript==

(function() {
    'use strict';

function injected() {
    const OPTIONS = {
        colorR: 0.5,
        colorG: 0.5,
        colorB: 0.5,
    };

    // If the script breaks, search devtools for "BINTULU" and replace these lines with the new one
    const vertexOld = "const float f=3.1415926;varying vec3 a;uniform vec4 b;attribute vec3 c;attribute vec2 d;uniform mat4 e;void main(){vec4 g=vec4(c,1);gl_Position=e*g;a=vec3(d.xy*b.xy+b.zw,1);a*=length(c);}";
    const fragOld = "precision highp float;const float h=3.1415926;varying vec3 a;uniform vec4 b;uniform float f;uniform sampler2D g;void main(){vec4 i=vec4(texture2DProj(g,a).rgb,f);gl_FragColor=i;}";

    const vertexNew = `
const float f=3.1415926;
varying vec3 a;
varying vec3 potato;
uniform vec4 b;
attribute vec3 c;
attribute vec2 d;
uniform mat4 e;
void main(){
    vec4 g=vec4(c,1);
    gl_Position=e*g;
    a = vec3(d.xy * b.xy + b.zw,1);
    a *= length(c);

    potato = vec3(d.xy, 1.0) * length(c);
}`;
    const fragNew = `precision highp float;
const float h=3.1415926;
varying vec3 a;
varying vec3 potato;
uniform vec4 b;
uniform float f;
uniform sampler2D g;

//float se(float theta, float m1, float ainv, float ainv, float n1, float n2, float n3inv) {
  //float cterm = pow(abs(cos(m1*theta*0.25)*ainv), n1);
  //float sterm = pow(abs(sin(m1*theta*0.25)*binv), n2);
  //return pow(cterm+sterm, -n3inv);
  //return 0.5;
//}


void main(){

vec2 aD = potato.xy / a.z;
float thetaD = aD.y;

float thresholdD1 = 0.6;
float thresholdD2 = 0.7;

float x = aD.x;
float y = abs(4.0*x - 2.0);
float phiD = smoothstep(0.0, 1.0, y > 1.0 ? 2.0 - y : y);


float angle1unit = x;
float angle1rad = (2.0*h) * angle1unit;
float angle2unit = (1.0-thetaD) * 2.0;

//float se_theta = angle1rad;
//float se_m = 6.0;
//float se_ainv = 1.0/0.4;
//float se_binv = 1.0/0.4;
//float se_n1 = 5.34;
//float se_n2 = 3.9;
//float se_n3inv = 1.0/3.65;

float se_theta = angle1rad;
//float se_m = 4.0;
float se_angle = se_theta; // se_theta* se_m*0.25; // this is 1
float se_ainv = 1.0/0.75;
float se_binv = 1.0/0.45;
//float se_n = 4.0; // using 4 here is most efficient
//float se_n1 = se_n;
//float se_n2 = se_n;
//float se_n3inv = 1.0/se_n;

float se_c1 = abs(cos(se_angle)*se_ainv);
float se_c2 = se_c1*se_c1;
float se_c4 = se_c2*se_c2;
float se_s1 = abs(sin(se_angle)*se_binv);
float se_s2 = se_s1*se_s1;
float se_s4 = se_s2*se_s2;
float se_r = inversesqrt(sqrt(se_c4+se_s4)); //, -se_n3inv); //pow(se_cterm+se_sterm, -se_n3inv);
//float se_r = se(se_theta, se_m, se_ainv, se_binv, se_n1, se_n2, se_n3inv);

bool ncnc1 = thetaD > mix(thresholdD1, thresholdD2, phiD);
bool ncnc2 = angle2unit < se_r;
vec3 ncnc1c = vec3(1.0,0.0,0.0);
vec3 ncnc2c = vec3(0.0,1.0,0.0);
vec3 ncnc12c = ncnc1c+ncnc2c;
vec3 og = texture2DProj(g,a).rgb;
vec3 color = ncnc1 ? (ncnc2 ? ncnc12c : ncnc1c) : (ncnc2 ? ncnc2c : og);
vec4 i = vec4(
  mix(og, color, 0.5)
  //ncnc1
  //? (texture2DProj(g,a).rgb * 0.5) // vec3(float(${OPTIONS.colorR}), float(${OPTIONS.colorG}), float(${OPTIONS.colorB})) // texture2DProj(g,a).rgb * 0.25
  //: texture2DProj(g,a).rgb
,f);
gl_FragColor=i;
}`;

    function installShaderSource(ctx) {
        const g = ctx.shaderSource;
        function shaderSource() {
            if (typeof arguments[1] === 'string') {
                let glsl = arguments[1];
                console.log('BINTULU shader', glsl);
                if (glsl === vertexOld) glsl = vertexNew;
                else if (glsl === fragOld) glsl = fragNew;
                return g.call(this, arguments[0], glsl);
            }
            return g.apply(this, arguments);
        }
        shaderSource.bestcity = 'bintulu';
        ctx.shaderSource = shaderSource;
    }
    function installGetContext(el) {
        const g = el.getContext;
        el.getContext = function() {
            if (arguments[0] === 'webgl' || arguments[0] === 'webgl2') {
                const ctx = g.apply(this, arguments);
                if (ctx && ctx.shaderSource && ctx.shaderSource.bestcity !== 'bintulu') {
                    installShaderSource(ctx);
                }
                return ctx;
            }
            return g.apply(this, arguments);
        };
    }
    const f = document.createElement;
    document.createElement = function() {
        if (arguments[0] === 'canvas' || arguments[0] === 'CANVAS') {
            const el = f.apply(this, arguments);
            installGetContext(el);
            return el;
        }
        return f.apply(this, arguments);
    };
    function addCompassStyle() {
        let style = document.createElement('style');
        style.id = 'bintulu_nocompass';
        style.innerHTML = '.compass { display: none } .game-layout__compass { display: none }';
        document.head.appendChild(style);
    }
    addCompassStyle();
    document.addEventListener('keydown', (evt) => {
      if (!evt.repeat && evt.code === 'KeyK' && evt.shiftKey && !evt.altKey && !evt.ctrlKey && !evt.metaKey) {
          let style = document.getElementById('bintulu_nocompass');
          if (!style) {
              addCompassStyle();
          } else {
              style.remove();
          }
      }
   });
}

  unsafeWindow.eval(`(${injected.toString()})()`);

})();