When i zoom in enough to have the candlestick bodies render, on high numbers of…
- User
When i zoom in enough to have the candlestick bodies render, on high numbers of candlesticks, this becomes unbearably slow and laggy Lets take this step by step and start with a simple solution and expand upon it.
import * as THREE from './three.module.js';
// Scene setup const scene = new THREE.Scene(); scene.background = new THREE.Color(0x000000);
// Camera setup const aspectRatio = window.innerWidth / window.innerHeight; const cameraSize = 200; const camera = new THREE.OrthographicCamera( -cameraSize * aspectRatio, cameraSize * aspectRatio, cameraSize, -cameraSize, 1, 1000 ); camera.position.set(0, 0, 500);
// Renderer setup const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
// Function to generate random OHLC data function generateOHLCData(count) { const data = new Array(count); let open = 50; // Starting price for (let i = 0; i < count; i++) { const high = open + Math.random() * 10; const low = open - Math.random() * 10; const close = low + Math.random() * (high - low); data[i] = { open, high, low, close }; open = close; // Next open starts where the last close was } return data; }
// Convert RGB to normalized color values (0 to 1) function rgbToNormalized(r, g, b) { return [r / 255, g / 255, b / 255]; }
// Create instanced candlesticks with uniform borders and wicks function createInstancedCandlesticks(data) { const barWidth = 1; const maxCandlesticks = data.length;
// Materials const material = new THREE.MeshBasicMaterial({ vertexColors: true });
// Geometries for the candlestick body and borders const geometryBar = new THREE.BoxGeometry(barWidth, 1, 0);
// Create instanced mesh for candlesticks const instancedMesh = new THREE.InstancedMesh(geometryBar, material, maxCandlesticks);
const dummy = new THREE.Object3D(); const wickVertices = new Float32Array(maxCandlesticks * 6); // 2 points for each wick const wickColors = new Float32Array(maxCandlesticks * 6); // Color for each wick point
// Define colors in normalized RGB const bullishColor = rgbToNormalized(0, 70, 0); // Custom RGB for bullish wicks (green) const bearishColor = rgbToNormalized(138, 0, 0); // Custom RGB for bearish wicks (red) const colorArray = new Float32Array(maxCandlesticks * 3); // RGB for each instance
let currentX = 0;
for (let index = 0; index < maxCandlesticks; index++) { const d = data[index]; const isBullish = d.close >= d.open; const height = Math.abs(d.close - d.open); const y = (d.close + d.open) / 2;
// Set the position and scale for the body dummy.position.set(currentX, y, 0); dummy.scale.set(1, height, 1); // Width is set to 1; barWidth is already accounted for in scaling dummy.updateMatrix(); instancedMesh.setMatrixAt(index, dummy.matrix); // Set color for each instance const color = isBullish ? bullishColor : bearishColor; colorArray.set(color, index * 3); // Set color wickColors.set([...color, ...color], index * 6); // Set wick color (two points) // Wick positions const wickIndex = index * 6; wickVertices[wickIndex] = currentX; wickVertices[wickIndex + 1] = d.low; wickVertices[wickIndex + 2] = -0.5; wickVertices[wickIndex + 3] = currentX; wickVertices[wickIndex + 4] = d.high; wickVertices[wickIndex + 5] = -0.5; // Increment x for the next candlestick currentX += barWidth * 1.5; - Assistant