soylog

技術的なことの置き場
    2016年2月8日

    soy 未分類 0 Comments

    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
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    Ball3D[] balls;
    static final int numBalls = 50;
    float fl = 250;
    float vpX = 0;
    float vpY = 0;
    void setup() {
      size(960, 540);
      frameRate(30);
      initialize();
    }
    void initialize() {
      smooth();
      vpX = width/2;
      vpY = height/2;
      balls = new Ball3D[numBalls];
      for(int i=0; i<numBalls; i++) {
        balls[i] = new Ball3D(5.0);
        balls[i].xpos = random(200) - 100;
        balls[i].ypos = random(200) - 100;
        balls[i].zpos = random(200) - 100;
        balls[i].setFillColor(color(0));  
      }
    }
    void draw() {
      background(255);
      float angleX = (mouseY - vpY) * 0.001;
      float angleY = (mouseX - vpX) * 0.001;
      for(int i=0; i<numBalls; i++) {
        Ball3D ball = balls[i];
        rotateX(ball, angleX);
        rotateY(ball, angleY);
        doPerspective(ball);
      }
      for(int i = 1; i < numBalls; i++) {
        line(balls[i-1].x, balls[i-1].y, balls[i].x, balls[i].y);
      }
      
      for(int i=0; i<numBalls; i++) {
        balls[i].update();
      }
    }
    void rotateX(Ball3D ball, float angleX) {
      float cosX = cos(angleX);
      float sinX = sin(angleX);
      float y1 = ball.ypos * cosX - ball.zpos * sinX;
      float z1 = ball.zpos * cosX + ball.ypos * sinX;
      ball.ypos = y1;
      ball.zpos = z1;
    }
    void rotateY(Ball3D ball, float angleY) {
      float cosY = cos(angleY);
      float sinY = sin(angleY);
      float x1 = ball.xpos * cosY - ball.zpos * sinY;
      float z1 = ball.zpos * cosY + ball.xpos * sinY;
      ball.xpos = x1;
      ball.zpos = z1;  
    }
    void doPerspective(Ball3D ball) {
      if(ball.zpos > -fl) {
        float scale = fl / (fl + ball.zpos);
        ball.scaleX = ball.scaleY = scale;
        ball.x = vpX + ball.xpos * scale;
        ball.y = vpY + ball.ypos * scale;
        ball.visible = true;
      }else{
        ball.visible = false;
      }
    }
    void sortZ() {
      for(int i=0; i<numBalls - 1; i++) {
        for(int j=numBalls-1; j>i; j--) {
          if(balls[j].zpos > balls[j-1].zpos) {
            Ball3D t = balls[j];
            balls[j] = balls[j-1];
            balls[j-1] = t;
          }
        }
      }
      for(int i=0; i<numBalls; i++) {
         Ball3D ball = balls[i];
         ball.update();
      }
    }
    ////////////////////////////////
    class Ball3D extends Sprite {
      float radius;
      float xpos = 0;
      float ypos = 0;
      float zpos = 0;
      float vx = 0;
      float vy = 0;
      float vz = 0;
      float mass = 1;
      
      Ball3D(float radius){
        super();
        this.radius = radius;
      }
      
      void draw(){
        if(dist(mouseX, mouseY, x, y) < radius) {
          isRollOver = true;
        }else{
          isRollOver = false;
        }
        ellipse(0, 0, radius*2, radius*2);
      }
    }
    ////////////////////////////////
    class Sprite extends Object {
      float x, y;
      float scaleX, scaleY;
      float width, height;
      float rotation;
      boolean visible;
      color strokeColor;
      color fillColor;
      Boolean isStroke;
      Boolean isFill;
      Boolean isRollOver;
      Boolean locked;
      float xoff, yoff;
      
      Sprite() {
        super();
        scaleX = 1.0;
        scaleY = 1.0;
        strokeColor = color(0);
        fillColor = color(0);
        isStroke = false;
        isFill = false;
        isRollOver = false;
        locked = false;
        visible = true;
      }
      
      void update() {
        if(! visible) return;
        pushMatrix();
        if(locked){
          x = mouseX - xoff;
          y = mouseY - yoff;
        }  
        translate(x, y);
        scale(scaleX, scaleY);
        rotate(rotation * PI / 180.0);
        if(isStroke) stroke(strokeColor);
        if(isFill) fill(fillColor);
        draw();
        popMatrix();
      }
      
      void setStrokeColor(color strokeColor) {
        this.strokeColor = strokeColor;
        isStroke = true;
      }
      
      void setFillColor(color fillColor) {
        this.fillColor = fillColor;
        isFill = true;
      }
      
      void startDrag() {
        if(isRollOver){
          locked = true;
          xoff = mouseX - x;
          yoff = mouseY - y;
        }
      }
      
      void stopDrag() {
        locked = false;
      }
      
      void draw() {}
    }
    ////////////////////////////////
    class Point extends Object {
      float x, y;
      
      Point(float x, float y) {
        super();
        this.x = x;
        this.y = y;
      }
    }
    ////////////////////////////////
    class Object {
      Object() {}
    }

    2016年2月8日

    soy 未分類 0 Comments

    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
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    Point3D points[];
    static final int numPoints = 4;
    float fl = 250;
    float vpX = 0;
    float vpY = 0;
    void setup() {
      size(960, 700);
      frameRate(30);
      initialize();
    }
    void initialize() {
      smooth();
      noStroke();
      vpX = width/2;
      vpY = height/2;
      
      points = new Point3D[numPoints];
      points[0] = new Point3D(-100, -100,  100);
      points[1] = new Point3D( 100, -100,  100);
      points[2] = new Point3D( 100,  100,  100);
      points[3] = new Point3D(-100,  100,  100);
      for(int i=0; i<numPoints; i++) {
        points[i].setVanishingPoint(vpX, vpY);
      }
    }
    void draw() {
      background(255);
      
      float angleX = (mouseY - vpY) * 0.001;
      float angleY = (mouseX - vpX) * 0.001;
      for(int i=0; i<points.length; i++) {
        Point3D point = points[i];
        point.rotateX(angleX);
        point.rotateY(angleY);
      }
      stroke(0);
      for(int i = 1; i < numPoints; i++) {
        line(points[i-1].screenX(), points[i-1].screenY(), points[i].screenX(), points[i].screenY());
      }
      line(points[numPoints - 1].screenX(), points[numPoints - 1].screenY(), points[0].screenX(), points[0].screenY());
    }
    ////////////////////////////////
    class Triangle extends Sprite {
      Point3D pointA;
      Point3D pointB;
      Point3D pointC;
      color cl;
      
      Triangle(Point3D a, Point3D b, Point3D c, color cl){
        super();
        pointA = a;
        pointB = b;
        pointC = c;
        this.cl = cl;
      }
      
      void draw(){
        fill(cl);
        beginShape(TRIANGLE_STRIP);
        vertex(pointA.screenX(), pointA.screenY());
        vertex(pointB.screenX(), pointB.screenY());
        vertex(pointC.screenX(), pointC.screenY());
        vertex(pointA.screenX(), pointA.screenY());
        endShape();
      }
    }
    ////////////////////////////////
    class Sprite extends Object {
      float x, y;
      float scaleX, scaleY;
      float width, height;
      float rotation;
      boolean visible;
      color strokeColor;
      color fillColor;
      Boolean isStroke;
      Boolean isFill;
      Boolean isRollOver;
      Boolean locked;
      float xoff, yoff;
      
      Sprite() {
        super();
        scaleX = 1.0;
        scaleY = 1.0;
        strokeColor = color(0);
        fillColor = color(0);
        isStroke = false;
        isFill = false;
        isRollOver = false;
        locked = false;
        visible = true;
      }
      
      void update() {
        if(! visible) return;
        pushMatrix();
        if(locked){
          x = mouseX - xoff;
          y = mouseY - yoff;
        }  
        translate(x, y);
        scale(scaleX, scaleY);
        rotate(rotation * PI / 180.0);
        if(isStroke) stroke(strokeColor);
        if(isFill) fill(fillColor);
        draw();
        popMatrix();
      }
      
      void setStrokeColor(color strokeColor) {
        this.strokeColor = strokeColor;
        isStroke = true;
      }
      
      void setFillColor(color fillColor) {
        this.fillColor = fillColor;
        isFill = true;
      }
      
      void startDrag() {
        if(isRollOver){
          locked = true;
          xoff = mouseX - x;
          yoff = mouseY - y;
        }
      }
      
      void stopDrag() {
        locked = false;
      }
      
      void draw() {}
    }
    ////////////////////////////////
    class Point3D extends Object {
      float fl = 250;
      float vpX = 0;
      float vpY = 0;
      float cX = 0;
      float cY = 0;
      float cZ = 0;
      float x = 0;
      float y = 0;
      float z = 0;
      
      Point3D(float x, float y, float z) {
        super();
        this.x = x;
        this.y = y;
        this.z = z;
      }
      
      void setVanishingPoint(float vpX, float vpY) {
        this.vpX = vpX;
        this.vpY = vpY;
      }
      
      void setCenter(float cX, float cY, float cZ) {
        this.cX = cX;
        this.cY = cY;
        this.cZ = cZ;    
      }
      
      float screenX() {
        float scale = fl / (fl + z + cZ);
        return vpX + (cX + x) * scale;  
      }
      
      float screenY() {
        float scale = fl / (fl + z + cZ);
        return vpY + (cY + y) * scale;
      }
      
      void rotateX(float angleX) {
        float cosX = cos(angleX);
        float sinX = sin(angleX);
        float y1 = y * cosX - z * sinX;
        float z1 = z * cosX + y * sinX;
        y = y1;
        z = z1;
      }
      
      void rotateY(float angleY) {
        float cosY = cos(angleY);
        float sinY = sin(angleY);
        float x1 = x * cosY - z * sinY;
        float z1 = z * cosY + x * sinY;
        x = x1;
        z = z1;
      }
      
      void rotateZ(float angleZ) {
        float cosZ = cos(angleZ);
        float sinZ = sin(angleZ);
        float x1 = x * cosZ - y * sinZ;
        float y1 = y * cosZ + x * sinZ;
        x = x1;
        y = y1;
      }
    }
    ////////////////////////////////
    class Point extends Object {
      float x, y;
      
      Point(float x, float y) {
        super();
        this.x = x;
        this.y = y;
      }
    }
    ////////////////////////////////
    class Object {
      Object() {}
    }

    2016年2月8日

    soy 未分類 0 Comments

    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
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    var group;
    var container, controls, stats;
    var particlesData = [];
    var camera, scene, renderer;
    var positions, colors;
    var particles;
    var pointCloud;
    var particlePositions;
    var linesMesh;
     
    var maxParticleCount = 1000;
    var particleCount = 500;
    var r = 800;
    var rHalf = r / 2;
     
    var effectController = {
    showDots: true,
    showLines: true,
    minDistance: 150,
    limitConnections: false,
    maxConnections: 20,
    particleCount: 500
    };
     
    init();
    animate();
     
    function initGUI() {
     
    var gui = new dat.GUI();
     
    gui.add( effectController, "showDots" ).onChange( function( value ) { pointCloud.visible = value; } );
    gui.add( effectController, "showLines" ).onChange( function( value ) { linesMesh.visible = value; } );
    gui.add( effectController, "minDistance", 10, 300 );
    gui.add( effectController, "limitConnections" );
    gui.add( effectController, "maxConnections", 0, 30, 1 );
    gui.add( effectController, "particleCount", 0, maxParticleCount, 1 ).onChange( function( value ) {
     
    particleCount = parseInt( value );
    particles.setDrawRange( 0, particleCount );
     
    });
     
    }
     
    function init() {
     
    initGUI();
     
    container = document.getElementById( 'container' );
     
    //
     
    camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 );
    camera.position.z = 1750;
     
    controls = new THREE.OrbitControls( camera, container );
     
    scene = new THREE.Scene();
     
     
    group = new THREE.Group();
    scene.add( group );
     
    var helper = new THREE.BoxHelper( new THREE.Mesh( new THREE.BoxGeometry( r, r, r ) ) );
    helper.material.color.setHex( 0x080808 );
    helper.material.blending = THREE.AdditiveBlending;
    helper.material.transparent = true;
    group.add( helper );
     
    var segments = maxParticleCount * maxParticleCount;
     
    positions = new Float32Array( segments * 3 );
    colors = new Float32Array( segments * 3 );
     
    var pMaterial = new THREE.PointsMaterial( {
    color: 0xFFFFFF,
    size: 3,
    blending: THREE.AdditiveBlending,
    transparent: true,
    sizeAttenuation: false
    } );
     
    particles = new THREE.BufferGeometry();
    particlePositions = new Float32Array( maxParticleCount * 3 );
     
    for ( var i = 0; i < maxParticleCount; i++ ) {
     
    var x = Math.random() * r - r / 2;
    var y = Math.random() * r - r / 2;
    var z = Math.random() * r - r / 2;
     
    particlePositions[ i * 3     ] = x;
    particlePositions[ i * 3 + 1 ] = y;
    particlePositions[ i * 3 + 2 ] = z;
     
    // add it to the geometry
    particlesData.push( {
    velocity: new THREE.Vector3( -1 + Math.random() * 2, -1 + Math.random() * 2,  -1 + Math.random() * 2 ),
    numConnections: 0
    } );
     
    }
     
    particles.setDrawRange( 0, particleCount );
    particles.addAttribute( 'position', new THREE.BufferAttribute( particlePositions, 3 ).setDynamic( true ) );
     
    // create the particle system
    pointCloud = new THREE.Points( particles, pMaterial );
    group.add( pointCloud );
     
    var geometry = new THREE.BufferGeometry();
     
    geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).setDynamic( true ) );
    geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).setDynamic( true ) );
     
    geometry.computeBoundingSphere();
     
    geometry.setDrawRange( 0, 0 );
     
    var material = new THREE.LineBasicMaterial( {
    vertexColors: THREE.VertexColors,
    blending: THREE.AdditiveBlending,
    transparent: true
    } );
     
    linesMesh = new THREE.LineSegments( geometry, material );
    group.add( linesMesh );
     
    //
     
    renderer = new THREE.WebGLRenderer( { antialias: true } );
    renderer.setPixelRatio( window.devicePixelRatio );
    renderer.setSize( window.innerWidth, window.innerHeight );
     
    renderer.gammaInput = true;
    renderer.gammaOutput = true;
     
    container.appendChild( renderer.domElement );
     
    //
     
    stats = new Stats();
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.top = '0px';
    container.appendChild( stats.domElement );
     
    window.addEventListener( 'resize', onWindowResize, false );
     
    }
     
    function onWindowResize() {
     
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
     
    renderer.setSize( window.innerWidth, window.innerHeight );
     
    }
     
    function animate() {
     
    var vertexpos = 0;
    var colorpos = 0;
    var numConnected = 0;
     
    for ( var i = 0; i < particleCount; i++ )
    particlesData[ i ].numConnections = 0;
     
    for ( var i = 0; i < particleCount; i++ ) {
     
    // get the particle
    var particleData = particlesData[i];
     
    particlePositions[ i * 3     ] += particleData.velocity.x;
    particlePositions[ i * 3 + 1 ] += particleData.velocity.y;
    particlePositions[ i * 3 + 2 ] += particleData.velocity.z;
     
    if ( particlePositions[ i * 3 + 1 ] < -rHalf || particlePositions[ i * 3 + 1 ] > rHalf )
    particleData.velocity.y = -particleData.velocity.y;
     
    if ( particlePositions[ i * 3 ] < -rHalf || particlePositions[ i * 3 ] > rHalf )
    particleData.velocity.x = -particleData.velocity.x;
     
    if ( particlePositions[ i * 3 + 2 ] < -rHalf || particlePositions[ i * 3 + 2 ] > rHalf )
    particleData.velocity.z = -particleData.velocity.z;
     
    if ( effectController.limitConnections && particleData.numConnections >= effectController.maxConnections )
    continue;
     
    // Check collision
    for ( var j = i + 1; j < particleCount; j++ ) {
     
    var particleDataB = particlesData[ j ];
    if ( effectController.limitConnections && particleDataB.numConnections >= effectController.maxConnections )
    continue;
     
    var dx = particlePositions[ i * 3     ] - particlePositions[ j * 3     ];
    var dy = particlePositions[ i * 3 + 1 ] - particlePositions[ j * 3 + 1 ];
    var dz = particlePositions[ i * 3 + 2 ] - particlePositions[ j * 3 + 2 ];
    var dist = Math.sqrt( dx * dx + dy * dy + dz * dz );
     
    if ( dist < effectController.minDistance ) {
     
    particleData.numConnections++;
    particleDataB.numConnections++;
     
    var alpha = 1.0 - dist / effectController.minDistance;
     
    positions[ vertexpos++ ] = particlePositions[ i * 3     ];
    positions[ vertexpos++ ] = particlePositions[ i * 3 + 1 ];
    positions[ vertexpos++ ] = particlePositions[ i * 3 + 2 ];
     
    positions[ vertexpos++ ] = particlePositions[ j * 3     ];
    positions[ vertexpos++ ] = particlePositions[ j * 3 + 1 ];
    positions[ vertexpos++ ] = particlePositions[ j * 3 + 2 ];
     
    colors[ colorpos++ ] = alpha;
    colors[ colorpos++ ] = alpha;
    colors[ colorpos++ ] = alpha;
     
    colors[ colorpos++ ] = alpha;
    colors[ colorpos++ ] = alpha;
    colors[ colorpos++ ] = alpha;
     
    numConnected++;
    }
    }
    }
     
     
    linesMesh.geometry.setDrawRange( 0, numConnected * 2 );
    linesMesh.geometry.attributes.position.needsUpdate = true;
    linesMesh.geometry.attributes.color.needsUpdate = true;
     
    pointCloud.geometry.attributes.position.needsUpdate = true;
     
    requestAnimationFrame( animate );
     
    stats.update();
    render();
     
    }
     
    function render() {
     
    var time = Date.now() * 0.001;
     
    group.rotation.y = time * 0.1;
    renderer.render( scene, camera );
     
    }

    最近の投稿

    • 227
    • 225
    • 223
    • ページ作成基本
    • バグが起きないように、js書くのに結構時間がかかる(各ブラウザ・デバイス間)

    アーカイブ

    • 2016年2月
    • 2015年5月
    • 2015年4月
    • 2015年3月

    カテゴリー

    • frontend
    • 未分類

    © soylog 2025
    Powered by WordPress • Themify WordPress Themes