/*
 * dessine des spheres avec des particules brillantes dessus
 * @param {type} dots
 * @param {type} couleurs
 * @returns {undefined}
 */
function draw(dots, couleurs, nb_point, nb_param) {
//    renderer.setClearColor(0x808080);

    var geometry = new THREE.SphereGeometry(20, 20, 20);
    var material = new THREE.MeshBasicMaterial({color: 0x000000});
    var counter = 0;
    for (var i = 0; i < nb_point; i++) {
        var sphere = new THREE.Mesh(geometry, material);
        var x = dots[counter];
        var y = dots[counter + 1];
        if (nb_param === 3) {
            var z = dots[counter + 2];
        }
        else var z = 0;
        
        sphere.position.x = x;
        sphere.position.y = y;
        sphere.position.z = z;

        paillette(100, x, y, z, 20);

        group.add(sphere);
        counter += nb_param;
    }


}

/*
 * dessine des paillettes (three js particle) reparties aléatoirement sur une sphere
 */
function paillette(nb, posx, posy, posz, taille) {

    //ajout de <script> dans le html pour les vertex shader
    var s = document.createElement("script");
    s.type = "x-shader/x-vertex";
    s.id = "vertexshader";
    s.innerHTML = "attribute float size; attribute vec3 customColor; varying vec3 vColor; void main() { vColor = customColor; vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) ); gl_Position = projectionMatrix * mvPosition;}";
    $("head").append(s);

    var s = document.createElement("script");
    s.type = "x-shader/x-fragment";
    s.id = "fragmentshader";
    s.innerHTML = "uniform vec3 color; uniform sampler2D texture; varying vec3 vColor; void main() { gl_FragColor = vec4( color * vColor, 1.0 ); gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );}";
    $("head").append(s);

    var particleSystem, uniforms, geometry;

    var particles = nb;
    var attributes = {
        size: {type: 'f', value: null},
        customColor: {type: 'c', value: null}

    };

    uniforms = {
        color: {type: "c", value: new THREE.Color(0xffffff)},
        texture: {type: "t", value: THREE.ImageUtils.loadTexture("../img/texture/spark1.png")}

    };

    var shaderMaterial = new THREE.ShaderMaterial({
        uniforms: uniforms,
        attributes: attributes,
        vertexShader: document.getElementById('vertexshader').textContent,
        fragmentShader: document.getElementById('fragmentshader').textContent,
        blending: THREE.AdditiveBlending,
        depthTest: true,
        transparent: true

    });


    var radius = 200;

    geometry = new THREE.BufferGeometry();

    var positions = new Float32Array(particles * 3);
    var values_color = new Float32Array(particles * 3);
    var values_size = new Float32Array(particles);

    var color = new THREE.Color();

    var starDistance = taille;

    for (var v = 0; v < particles; v++) {

        var x = -1 + Math.random() * 2;
        var y = -1 + Math.random() * 2;
        var z = -1 + Math.random() * 2;
        var d = 1 / Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
        x *= d;
        y *= d;
        z *= d;

        values_size[ v ] = 20;

//        positions[ v * 3 + 0 ] = (Math.random() * 2 - 1) * radius;
//        positions[ v * 3 + 1 ] = (Math.random() * 2 - 1) * radius;
//        positions[ v * 3 + 2 ] = (Math.random() * 2 - 1) * radius;
        positions[ v * 3 + 0 ] = x * starDistance + posx;
        positions[ v * 3 + 1 ] = y * starDistance + posy;
        positions[ v * 3 + 2 ] = z * starDistance + posz;

        color.setHSL(v / particles, 1.0, 0.5);

        values_color[ v * 3 + 0 ] = color.r;
        values_color[ v * 3 + 1 ] = color.g;
        values_color[ v * 3 + 2 ] = color.b;

    }

    geometry.addAttribute('position', new THREE.BufferAttribute(positions, 3));
    geometry.addAttribute('customColor', new THREE.BufferAttribute(values_color, 3));
    geometry.addAttribute('size', new THREE.BufferAttribute(values_size, 1));

    particleSystem = new THREE.PointCloud(geometry, shaderMaterial);

    group.add(particleSystem);
}