I have some code. Please remove the LOD optimization and simply use only the line chart…
- User
I have some code. Please remove the LOD optimization and simply use only the line chart optimization. These optimizations are conditional on different zoom levels. When zoomed in, the candle bodies should be shown, when zoomed out a medium amount just the candle wicks should be shown, when zoomed out a high amount just the line chart should be shown. These optimizations are to deal with very high numbers of candlesticks
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]; }
// Define colors in normalized RGB const bullishColor = rgbToNormalized(0, 0.7 * 255, 0); // Green const bearishColor = rgbToNormalized(0.7 * 255, 0, 0); // Red
// Generate data (Adjust the count to 5 million) const ohlcData = generateOHLCData(5000000);
// Variables for candlestick rendering let instancedMesh, wickLineSegments, aggregatedLine; let instanceMatrices, instanceColors, wickVertices, wickColors; let geometryBar, wickGeometry, aggregatedGeometry; const maxVisibleCandlesticks = 2000; // Maximum number of visible candlesticks to render const barWidth = 1; const candlestickSpacing = barWidth * 1.5; const totalCandlesticks = ohlcData.length; const halfCandlesticks = totalCandlesticks / 2;
// Create instanced candlesticks with dynamic visibility function createInstancedCandlesticks() { // Materials const material = new THREE.MeshBasicMaterial({ vertexColors: true });
// Geometries for the candlestick body geometryBar = new THREE.BoxGeometry(barWidth, 1, 0);
// Create instanced mesh with capacity for maximum visible candlesticks instancedMesh = new THREE.InstancedMesh(geometryBar, material, maxVisibleCandlesticks); instancedMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
// Prepare instance matrices and colors instanceMatrices = new Float32Array(maxVisibleCandlesticks * 16); // 4x4 matrices instanceColors = new Float32Array(maxVisibleCandlesticks * 3); // RGB per instance
- Assistant