実現したいこと
APIを取得するときにカクつかないようにしたいです。
前提
APIを取得する関数を作ってみたのですが、取得の瞬間に一瞬止まってしまいす。
発生している問題・エラーメッセージ
エラーは出ていませんが、API取得の際にラグが生じてしまいます。
該当のソースコード
Processing
1import peasy.*; 2import processing.net.*; 3import processing.data.JSONObject; 4 5String url = "https://api.wheretheiss.at/v1/satellites/25544"; 6JSONObject issData; 7float latitude, longitude, altitude, speed; 8int refreshInterval = 2 * 1000; // 2秒ごとに更新 9int lastUpdateTime = 0; 10 11PImage earthTexture; 12PShape sphere; 13PeasyCam cam; 14 15float rotationX = 0; 16float rotationY = 0; 17float autoRotationSpeed = radians(5); // 毎秒5度の回転速度 18float sphereRadius = 150; 19float orbitRadius = sphereRadius + 15; // 緑の円の半径 20 21float orbitTheta = 0; // 球が周回する角度 22float sunOrbitTheta = 0; // 太陽が周回する角度 23float sunOrbitRadius = 10000; // 太陽の周回半径 24 25int numStars = 2000; // 星の数を設定 26float angleX = 0; // X軸周りの回転角度 27float angleY = 0; // Y軸周りの回転角度 28float lastMouseX, lastMouseY; 29float radius = 10000; // 星を配置する球の半径 30 31PVector[] stars; // 星の位置を格納する配列 32color[] starColors; // 星の色を格納する配列 33 34void setup() { 35 size(1500, 1000, P3D); // 3Dモードでウィンドウを作成 36 background(0); 37 // 日本語対応のフォントを読み込み 38 39 // 視野角とクリップ範囲を調整 40 float fov = PI / 3; // 視野角を広げる 41 float cameraZ = (height / 2.0) / tan(fov / 2.0); 42 perspective(fov, float(width)/float(height), cameraZ / 10.0, cameraZ * 10000.0); 43 44 // PeasyCamを初期化 45 cam = new PeasyCam(this, 0, 0, 0, 500); // カメラの中心を地球に合わせる 46 47 48 // 地球儀の形状を作成し、テクスチャを設定 49 sphere = createShape(SPHERE, sphereRadius); 50 sphere.setTexture(earthTexture); 51 sphere.setStrokeWeight(0); 52 noStroke(); 53 54 // 星空を初期化 55 stars = new PVector[numStars]; // 星の位置を格納する配列を初期化 56 starColors = new color[numStars]; // 星の色を格納する配列を初期化 57 58 // 星を球面上にランダムに配置 59 for (int i = 0; i < numStars; i++) { 60 float theta = random(TWO_PI); // ランダムな経度 61 float phi = random(PI); // ランダムな緯度 62 float x = radius * sin(phi) * cos(theta); 63 float y = radius * sin(phi) * sin(theta); 64 float z = radius * cos(phi); 65 stars[i] = new PVector(x, y, z); 66 67 // ランダムに星の色を設定 68 if (random(1) < 0.5) { 69 starColors[i] = color(173, 216, 230); // 青白い星 70 } else { 71 starColors[i] = color(255, 255, 255); // 白の星 72 } 73 } 74} 75 76void draw() { 77 //println(mouseX, mouseY); 78 background(0); 79 80 81 // 視点の設定(星の描画) 82 pushMatrix(); 83 rotateX(angleX); 84 rotateY(angleY); 85 86 drawStars(); // 星空を描画 87 popMatrix(); 88 89 // 自動回転 90 rotationY += autoRotationSpeed * (5.0 / frameRate); // フレームレートに応じて回転速度を調整 91 orbitTheta += radians(1); // 球の周回速度を調整 92 sunOrbitTheta += radians(0.05); // 太陽の周回速度を調整 93 94 // ライト(太陽光)を設定 95 directionalLight(255, 255, 255, -cos(sunOrbitTheta), 0, sin(sunOrbitTheta)); // 太陽から光を当てる 96 ambientLight(50, 50, 50); // 環境光 97 98 // 地球儀を描画 99 pushMatrix(); 100 // 中央に地球を描画 101 rotateZ(radians(23.4)); 102 103 // 自動回転 104 rotateX(rotationX); 105 rotateY(rotationY); 106 shape(sphere); // 地球儀の形状を描画 107 108 // 回転軸を描画 109 stroke(255, 255, 255); // 白色の軸線 110 strokeWeight(1.5); 111 line(0, -sphereRadius - 50, 0, 0, sphereRadius + 50, 0); // 北極から南極に向かう線 112 113 // 赤道を描画 114 stroke(255, 0, 0); // 赤色の赤道線 115 noFill(); 116 beginShape(); 117 for (float theta = 0; theta < TWO_PI; theta += 0.05) { 118 float x = sphereRadius * cos(theta); 119 float y = sphereRadius * sin(theta); 120 vertex(x, 0, y); 121 } 122 endShape(CLOSE); 123 124 popMatrix(); 125 126 // 明暗線の描画(固定された位置に) 127 pushMatrix(); 128 rotateZ(radians(90));// 地球の軸に合わせて傾ける 129 rotateX(sunOrbitTheta); 130 rotateY(sunOrbitTheta); 131 stroke(255, 255, 0, 150); // 黄色の線 132 noFill(); 133 beginShape(); 134 for (float theta = 0; theta < TWO_PI; theta += 0.05) { 135 float x = sphereRadius * cos(theta); 136 float y = sphereRadius * sin(theta); 137 vertex(x, 0, y); 138 } 139 endShape(CLOSE); 140 141 popMatrix(); 142 143 // 周回軌道 144 pushMatrix(); 145 rotateZ(radians(90 - 15)); // 地球の軸に合わせて傾ける 146 147 stroke(0, 255, 0, 80); // 緑色の線 148 noFill(); 149 beginShape(); 150 for (float theta = 0; theta < TWO_PI; theta += 0.05) { 151 float x = orbitRadius * cos(theta); // 緑の円の半径 152 float y = orbitRadius * sin(theta); 153 vertex(x, 0, y); 154 } 155 endShape(CLOSE); 156 157 // 周回する球を描画 158 float orbitX = orbitRadius * cos(orbitTheta); 159 float orbitY = orbitRadius * sin(orbitTheta); 160 stroke(0, 0, 255); 161 translate(orbitX, 0, orbitY); // 緑の円に沿って球を配置 162 sphere(5); // 半径5の球体を描画 163 164 popMatrix(); 165 166 // 太陽の描画と周回 167 pushMatrix(); 168 float sunX = sunOrbitRadius * -cos(sunOrbitTheta-radians(180)); 169 float sunZ = sunOrbitRadius * sin(sunOrbitTheta-radians(180)); 170 translate(sunX, 0, sunZ); // 太陽を周回位置に配置 171 172 // 4方向からの点光源 173 directionalLight(255, 255, 255, 1, 0, 0); 174 directionalLight(255, 255, 255, -1, 0, 0); 175 directionalLight(255, 255, 255, 0, 1, 0); 176 directionalLight(255, 255, 255, 0, -1, 0); 177 directionalLight(255, 255, 255, 0, 0, 1); 178 directionalLight(255, 255, 255, 0, 0, -1); 179 180 // 外側3層の太陽 181 for (int i = 10; i <= 40; i += 10) { 182 noStroke(); 183 fill(255, 255, 255, 255 - 3 * i); // 太陽のようなグラデーション効果 184 sphere(50+(i * 2)); // 小さな球体を重ねて描画 185 } 186 187 popMatrix(); 188 noLights();//ライトのリセット(他に影響しなくなる) 189 190 hint(DISABLE_DEPTH_TEST); 191 cam.beginHUD(); 192 pushMatrix(); 193 translate(380, -330); 194 drawISSdata(); 195 popMatrix(); 196 cam.endHUD(); 197 hint(ENABLE_DEPTH_TEST); 198} 199 200void drawStars() { 201 for (int i = 0; i < numStars; i++) { 202 PVector star = stars[i]; 203 pushMatrix(); 204 translate(star.x, star.y, star.z); 205 206 // ビルボード効果で星が常に地球の中心方向を向くようにする 207 PVector toCenter = star.copy().normalize().mult(-1); // 地球の中心方向へのベクトルを計算 208 209 // Z軸を中心方向に回転させる 210 float angleZ = atan2(toCenter.y, toCenter.x); 211 rotateZ(angleZ); 212 213 // Y軸を中心方向に回転させる 214 float hyp = sqrt(toCenter.x * toCenter.x + toCenter.y * toCenter.y); 215 float angleY = atan2(hyp, toCenter.z); 216 rotateY(angleY); 217 218 fill(starColors[i]); // 星の色を設定 219 ellipse(0, 0, 30, 30); // 星を描画 220 popMatrix(); 221 } 222} 223 224void drawISSdata() { 225 fill(255); 226 textSize(20); 227 text("International Space Station (ISS)", width / 2, height / 2 - 100); 228 text("Latitude: " + nf(latitude, 1, 4), width / 2, height / 2 - 50); 229 text("Longitude: " + nf(longitude, 1, 4), width / 2, height / 2); 230 text("Altitude: " + nf(altitude, 1, 3) + " km", width / 2, height / 2 + 50); 231 text("Speed: " + nf(speed/3600, 1, 5) + " km/s", width / 2, height / 2 + 100); 232 233 // 指定した間隔でデータを更新 234 if (millis() - lastUpdateTime > refreshInterval) { 235 try { 236 issData = loadJSONObject(url); 237 latitude = issData.getFloat("latitude"); 238 longitude = issData.getFloat("longitude"); 239 altitude = issData.getFloat("altitude"); 240 speed = issData.getFloat("velocity"); 241 } 242 catch (Exception e) { 243 println("Error fetching ISS data: " + e.getMessage()); 244 } 245 lastUpdateTime = millis(); 246 } 247} 248
試したこと
メインのプログラムを動かしながら、ラグの生じるdrawISSdata()を別で動かしてみようとしたのですが、上手くいきませんでした。並行処理や非同期処理ができるか試してみたのですが、上手くできませんでした。私のやり方が良くなかったのかもしれません。ほかの方法でもなんでも構いませんので、ラグを生じさせないようにしていただけると嬉しいです。
補足情報(FW/ツールのバージョンなど)
カレンダーを表示するコードも書いてあったのですが、最大文字数を超えてしまったので、カットしました。そのため不自然に切れているところがあるかもしれません。
読みづらい上「import peasy.;」等記号(*)が消えたりするので、ソースコードはコードブロックを使用してください(回答のようになります)
[ヘルプ|質問する時のヒント](https://teratail.com/help/question-tips#questionTips35)
質問はいつでも何度でも編集できます。

回答1件
あなたの回答
tips
プレビュー