<commit_before>"use strict";

/*
Things to read

 https://www.opengl.org/discussion_boards/showthread.php/167751-day-night-cycle-light
 http://forum.celestialmatters.org/viewtopic.php?f=9&t=116&sid=37befac93f977998de203896f51e4789
 https://www.google.be/search?q=Preetham+sky+model&oq=Preetham+sky+model&aqs=chrome..69i57j0l2.544j0j7&sourceid=chrome&es_sm=91&ie=UTF-8
 https://github.com/bblodfon/skylighting-Web-app
 https://www.youtube.com/watch?v=jMdCC4uRZkg
 http://twiik.net/articles/simplest-possible-day-night-cycle-in-unity-5
 https://wiki.unrealengine.com/Tutorial:_Time_of_Day

 */

var maxFog = 0.00045,
    minFog = 0.00015;

var fogColorRed = [0xB9 / 255, 0xB9 / 255, 0x66 / 255, 0x40 / 255],
    fogColorGreen = [0xB9 / 255, 0xB9 / 255, 0x30 / 255, 0x30 / 255],
    fogColorBlue = [0xA9 / 255, 0xA9 / 255, 0x80 / 255, 0x66 / 255];

var sunColorRed = [0xF5 / 255, 0xF5 / 255],
    sunColorGreen = [0xF5 / 255, 0xC0 / 255],
    sunColorBlue = [0xD0 / 255, 0x80 / 255];

var DayNightCycle = function DayNightCycle (renderer, fog, directionalLight, hemisphereLight, sun, shaderMaterial) {
    this.renderer = renderer;
    this.fog = fog;
    this.directionalLight = directionalLight;
    this.hemisphereLight = hemisphereLight;
    this.sun = sun;
    this.shaderMaterial = shaderMaterial;
    this.time = 0;
};

var lerp = function lerp (min, max, t) {
    return t * max + (1 - t) * min;
};

var wshaper = function wshaper (value, shape) {
    "use strict";

    value *= shape.length - 1;

    var start = Math.floor(value),
        pos = value % 1;

    if (start === shape.length - 1) {
        value = shape[start];
    } else {
        value = shape[start] * (1 - pos) + shape[start + 1] * (pos);
    }

    return value;
};

DayNightCycle.prototype.update = function (player, dt) {
    this.time = this.time + dt / 100;

    var duration = 3000;

    var rampTime = (duration * 0.75 + this.time) % duration / duration, // 1
        ratioFog = Math.abs(Math.pow(Math.sin(rampTime * Math.PI), 1)),
        positionSunX = Math.sin((rampTime + 0) * Math.PI * 2),
        positionSunY = Math.cos((rampTime + 0) * Math.PI * 2),
        ratioSun = Math.pow(1 - Math.max(positionSunY, 0), 3);

    //var gameTime = (rampTime + 0.5) * 24 % 24 * 60 | 0;
    //console.log(gameTime / 60 | 0, ':', gameTime % 60);

    this.sun.material.color.setRGB(
        wshaper(ratioSun, sunColorRed),
        wshaper(ratioSun, sunColorGreen),
        wshaper(ratioSun, sunColorBlue)
    );

    this.fog.density = lerp(minFog, maxFog, ratioFog);
    this.fog.color.setRGB(
        wshaper(ratioFog, fogColorRed),
        wshaper(ratioFog, fogColorGreen),
        wshaper(ratioFog, fogColorBlue)
    );

    this.hemisphereLight.color.set(this.fog.color);
    this.directionalLight.color.set(this.fog.color);
    this.directionalLight.position.set(positionSunX, positionSunY, 1);
    this.directionalLight.position.normalize();

    this.shaderMaterial.uniforms.color.value.set(this.sun.material.color);

    this.sun.position.set(player.position.x + positionSunX * 50000, positionSunY * 50000, player.position.z + 50000);
    this.renderer.setClearColor(this.fog.color, 1);
};

module.exports = DayNightCycle;
<commit_msg>fix the directionalLight still targeting 0,0,0 even when the position of the sun itself is translated<commit_after>"use strict";

/*
Things to read

 https://www.opengl.org/discussion_boards/showthread.php/167751-day-night-cycle-light
 http://forum.celestialmatters.org/viewtopic.php?f=9&t=116&sid=37befac93f977998de203896f51e4789
 https://www.google.be/search?q=Preetham+sky+model&oq=Preetham+sky+model&aqs=chrome..69i57j0l2.544j0j7&sourceid=chrome&es_sm=91&ie=UTF-8
 https://github.com/bblodfon/skylighting-Web-app
 https://www.youtube.com/watch?v=jMdCC4uRZkg
 http://twiik.net/articles/simplest-possible-day-night-cycle-in-unity-5
 https://wiki.unrealengine.com/Tutorial:_Time_of_Day

 */

var maxFog = 0.00045,
    minFog = 0.00015;

var fogColorRed = [0xB9 / 255, 0xB9 / 255, 0x66 / 255, 0x40 / 255],
    fogColorGreen = [0xB9 / 255, 0xB9 / 255, 0x30 / 255, 0x30 / 255],
    fogColorBlue = [0xA9 / 255, 0xA9 / 255, 0x80 / 255, 0x66 / 255];

var sunColorRed = [0xF5 / 255, 0xF5 / 255],
    sunColorGreen = [0xF5 / 255, 0xC0 / 255],
    sunColorBlue = [0xD0 / 255, 0x80 / 255];

var DayNightCycle = function DayNightCycle (renderer, fog, directionalLight, hemisphereLight, sun, shaderMaterial) {
    this.renderer = renderer;
    this.fog = fog;
    this.directionalLight = directionalLight;
    this.hemisphereLight = hemisphereLight;
    this.sun = sun;
    this.shaderMaterial = shaderMaterial;
    this.time = 0;
};

var lerp = function lerp (min, max, t) {
    return t * max + (1 - t) * min;
};

var wshaper = function wshaper (value, shape) {
    "use strict";

    value *= shape.length - 1;

    var start = Math.floor(value),
        pos = value % 1;

    if (start === shape.length - 1) {
        value = shape[start];
    } else {
        value = shape[start] * (1 - pos) + shape[start + 1] * (pos);
    }

    return value;
};

DayNightCycle.prototype.update = function (player, dt) {
    this.time = this.time + dt / 100;

    var duration = 3000;

    var rampTime = (duration * 0.75 + this.time) % duration / duration, // 1
        ratioFog = Math.abs(Math.pow(Math.sin(rampTime * Math.PI), 1)),
        positionSunX = Math.sin((rampTime + 0) * Math.PI * 2),
        positionSunY = Math.cos((rampTime + 0) * Math.PI * 2),
        ratioSun = Math.pow(1 - Math.max(positionSunY, 0), 3);

    //var gameTime = (rampTime + 0.5) * 24 % 24 * 60 | 0;
    //console.log(gameTime / 60 | 0, ':', gameTime % 60);

    this.sun.material.color.setRGB(
        wshaper(ratioSun, sunColorRed),
        wshaper(ratioSun, sunColorGreen),
        wshaper(ratioSun, sunColorBlue)
    );

    this.fog.density = lerp(minFog, maxFog, ratioFog);
    this.fog.color.setRGB(
        wshaper(ratioFog, fogColorRed),
        wshaper(ratioFog, fogColorGreen),
        wshaper(ratioFog, fogColorBlue)
    );

    this.hemisphereLight.color.set(this.fog.color);
    this.directionalLight.color.set(this.fog.color);
    this.directionalLight.position.set(positionSunX, positionSunY, 1);
    this.directionalLight.target.position.set(player.x, 0, player.z);
    this.directionalLight.position.normalize();

    this.shaderMaterial.uniforms.color.value.set(this.sun.material.color);

    this.sun.position.set(player.position.x + positionSunX * 50000, positionSunY * 50000, player.position.z + 50000);
    this.renderer.setClearColor(this.fog.color, 1);
};

module.exports = DayNightCycle;

{'fraction_non_alphanumeric': 0.14508793208004853, 'fraction_numerical': 0.06549423893268648, 'mean_word_length': 3.1779607346421788, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3717742', 'n_tokens_mistral': 2793, 'n_tokens_neox': 2422, 'n_words': 454}