# all files / src/geometry/ Sphere.js

100% Statements 59/59
50% Branches 1/2
100% Functions 2/2
100% Lines 56/56

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140

    1×

    6×

    6×
    6×

    6×
    6×
    6×

    6×
    6×
    6×
    6×

    6×
    6×

    6×
    6×
    6×
    6×
    6×
    6×

    6×
    6×
    6×
    6×
    6×
    118×
    4411×
    4411×

    4411×
    4411×
    4411×

    4411×
    4411×
    4411×
    4411×
    4411×
    4411×
    4411×
    4411×
    4411×

    6×

    6×
    6×
    112×
    4102×
    4102×
    4102×
    4102×

    4102×
    4102×
    4102×

    4102×
    4102×
    4102×

    6×
    6×
    6×

     import Geometry from '../Geometry';
    import BoundingBox from '../math/BoundingBox';

    /**
     * @constructor clay.geometry.Sphere
     * @extends clay.Geometry
     * @param {Object} [opt]
     * @param {number} [widthSegments]
     * @param {number} [heightSegments]
     * @param {number} [phiStart]
     * @param {number} [phiLength]
     * @param {number} [thetaStart]
     * @param {number} [thetaLength]
     * @param {number} [radius]
     */
    var Sphere = Geometry.extend(/** @lends clay.geometry.Sphere# */ {
        dynamic: false,
        /**
         * @type {number}
         */
        widthSegments: 40,
        /**
         * @type {number}
         */
        heightSegments: 20,

        /**
         * @type {number}
         */
        phiStart: 0,
        /**
         * @type {number}
         */
        phiLength: Math.PI * 2,

        /**
         * @type {number}
         */
        thetaStart: 0,
        /**
         * @type {number}
         */
        thetaLength: Math.PI,

        /**
         * @type {number}
         */
        radius: 1

    }, function() {
        this.build();
    },
    /** @lends clay.geometry.Sphere.prototype */
    {
        /**
         * Build sphere geometry
         */
        build: function() {
            var heightSegments = this.heightSegments;
            var widthSegments = this.widthSegments;

            var positionAttr = this.attributes.position;
            var texcoordAttr = this.attributes.texcoord0;
            var normalAttr = this.attributes.normal;

            var vertexCount = (widthSegments + 1) * (heightSegments + 1);
            positionAttr.init(vertexCount);
            texcoordAttr.init(vertexCount);
            normalAttr.init(vertexCount);

            var IndicesCtor = vertexCount > 0xffff ? Uint32Array : Uint16Array;
            var indices = this.indices = new IndicesCtor(widthSegments * heightSegments * 6);

            var x, y, z,
                u, v,
                i, j;

            var radius = this.radius;
            var phiStart = this.phiStart;
            var phiLength = this.phiLength;
            var thetaStart = this.thetaStart;
            var thetaLength = this.thetaLength;
            var radius = this.radius;

            var pos = [];
            var uv = [];
            var offset = 0;
            var divider = 1 / radius;
            for (j = 0; j <= heightSegments; j ++) {
                for (i = 0; i <= widthSegments; i ++) {
                    u = i / widthSegments;
                    v = j / heightSegments;

                    // X axis is inverted so texture can be mapped from left to right
                    x = -radius * Math.cos(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength);
                    y = radius * Math.cos(thetaStart + v * thetaLength);
                    z = radius * Math.sin(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength);

                    pos[0] = x; pos[1] = y; pos[2] = z;
                    uv[0] = u; uv[1] = v;
                    positionAttr.set(offset, pos);
                    texcoordAttr.set(offset, uv);
                    pos[0] *= divider;
                    pos[1] *= divider;
                    pos[2] *= divider;
                    normalAttr.set(offset, pos);
                    offset++;
                }
            }

            var i1, i2, i3, i4;

            var len = widthSegments + 1;

            var n = 0;
            for (j = 0; j < heightSegments; j ++) {
                for (i = 0; i < widthSegments; i ++) {
                    i2 = j * len + i;
                    i1 = (j * len + i + 1);
                    i4 = (j + 1) * len + i + 1;
                    i3 = (j + 1) * len + i;

                    indices[n++] = i1;
                    indices[n++] = i2;
                    indices[n++] = i4;

                    indices[n++] = i2;
                    indices[n++] = i3;
                    indices[n++] = i4;
                }
            }

            this.boundingBox = new BoundingBox();
            this.boundingBox.max.set(radius, radius, radius);
            this.boundingBox.min.set(-radius, -radius, -radius);
        }
    });

    export default Sphere;