Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)

When i zoom in enough to have the candlestick bodies render, on high numbers of…

  1. User

    When i zoom in enough to have the candlestick bodies render, on high numbers of candlesticks, this becomes unbearably slow and laggy Please provide a fix

    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;
    

    }

    // Add the instances to the scene scene.add(instancedMesh);

    // Create and add wick line segments to the scene const wickGeometry = new THREE.BufferGeometry(); wickGeometry.setAttribute('position', new THREE.BufferAttribute(wickVertices, 3)); wickGeometry.setAttribute('color', new THREE.BufferAttribute(wickColors, 3)); // Add color to wicks const wickMaterial = new THREE.LineBasicMaterial({ vertexColors: true }); // Enable vertex colors const wickLineSegments = new THREE.LineSegments(wickGeometry, wickMaterial); scene.add(wickLineSegments);

    // Add color attribute to geometry geometryBar.setAttribute('color', new THREE.InstancedBufferAttribute(colorArray, 3));

    return { instancedMesh, wickLineSegments }; }

    // Generate data and create instanced candlesticks const ohlcData = generateOHLCData(1000000); // Adjust the number as needed const { instancedMesh, wickLineSegments } = createInstancedCandlesticks(ohlcData);

    // Zoom logic (based on mouse wheel and key modifiers) const zoomSpeed = 0.8; let visibilityUpdateTimeout; // Timeout variable for visibility updates

    function onWheel(event) { event.preventDefault();

    const rect = renderer.domElement.getBoundingClientRect(); const mouseX = ((event.clientX - rect.left) / rect.width) * 2 - 1; const mouseY = -((event.clientY - rect.top) / rect.height) * 2 + 1;

    const mouseWorld = new THREE.Vector3(mouseX, mouseY, 0).unproject(camera); const zoomFactor = 1 - event.deltaY * 0.001 * zoomSpeed;

    const ctrlPressed = event.ctrlKey; const shiftPressed = event.shiftKey;

    if (ctrlPressed) { camera.left = (camera.left - mouseWorld.x) * zoomFactor + mouseWorld.x; camera.right = (camera.right - mouseWorld.x) * zoomFactor + mouseWorld.x; } else if (shiftPressed) { camera.top = (camera.top - mouseWorld.y) * zoomFactor + mouseWorld.y; camera.bottom = (camera.bottom - mouseWorld.y) * zoomFactor + mouseWorld.y; } else { camera.left = (camera.left - mouseWorld.x) * zoomFactor + mouseWorld.x; camera.right = (camera.right - mouseWorld.x) * zoomFactor + mouseWorld.x; camera.top = (camera.top - mouseWorld.y) * zoomFactor + mouseWorld.y; camera.bottom = (camera.bottom - mouseWorld.y) * zoomFactor + mouseWorld.y; }

    camera.updateProjectionMatrix();

    // Clear previous timeout if it exists clearTimeout(visibilityUpdateTimeout);

    // Set a new timeout to update visibility after a delay visibilityUpdateTimeout = setTimeout(updateCandlestickVisibility, 100); // Adjust the delay as needed }

    // Function to update candlestick visibility based on zoom level function updateCandlestickVisibility() { const visibleWidth = camera.right - camera.left; const candlestickWidth = 1.5; // Width of each candlestick including spacing const visibleCandlesticks = visibleWidth / candlestickWidth;

    // Adjust the threshold for visibility based on zoom level const threshold = 700;

    if (visibleCandlesticks > threshold) { instancedMesh.visible = false; // Hide bodies if too many are visible wickLineSegments.visible = true; // Always show wicks } else { instancedMesh.visible = true; // Show bodies when zoomed in wickLineSegments.visible = true; // Always show wicks } }

    // Prevent default browser behavior for wheel scrolling with Control or Shift key window.addEventListener('wheel', onWheel, { passive: false });

    // Prevent default context menu on right-click window.addEventListener('contextmenu', (event) => { event.preventDefault(); });

    // Render loop function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); }

    // Handle window resize function onWindowResize() { const aspect = window.innerWidth / window.innerHeight; camera.left = -cameraSize * aspect; camera.right = cameraSize * aspect; camera.top = cameraSize; camera.bottom = -cameraSize; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); updateCandlestickVisibility(); } window.addEventListener('resize', onWindowResize);

    // Start animation animate();

    // Panning functionality let isPanning = false; // Track panning state let isDraggingEndpoint = false; // Track endpoint dragging state let selectedEndpointIndex = null; // Index of the selected endpoint (0 or 1) const previousMousePosition = new THREE.Vector2(); // Store previous mouse position

    // Raycaster for detecting line interaction const raycaster = new THREE.Raycaster(); const mouse = new THREE.Vector2();

    // Interactive line code // Define line geometry and material const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffffff }); const lineGeometry = new THREE.BufferGeometry().setFromPoints([ new THREE.Vector3(-50, 0, 0), new THREE.Vector3(50, 0, 0) ]); const line = new THREE.Line(lineGeometry, lineMaterial); line.frustumCulled = false; // Prevent the line from being culled scene.add(line);

    // Set the distance for grabbing the line (in world units) let distanceForGrab = 2; // You can adjust this value to make grabbing easier

    // Set the raycaster's line precision to distanceForGrab raycaster.params.Line.threshold = distanceForGrab; // For Three.js r127 and above

    // Predefine vectors to avoid excessive allocations const mouseWorld = new THREE.Vector3(); const deltaWorld = new THREE.Vector3(); const currentMouse = new THREE.Vector2(); const previousMouseWorld = new THREE.Vector3();

    // Mouse down event function onMouseDown(event) { if (event.button === 2) { // Right mouse button for panning isPanning = true; previousMousePosition.set(event.clientX, event.clientY); } else if (event.button === 0) { // Left mouse button for line interaction // Prepare raycaster const rect = renderer.domElement.getBoundingClientRect(); mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1; mouse.y = - ((event.clientY - rect.top) / rect.height) * 2 + 1;

    raycaster.setFromCamera(mouse, camera);
    
    // Check intersection with the line
    const intersects = raycaster.intersectObject(line, true);
    
    if (intersects.length > 0) {
      // Clicked on the line
      // Determine which endpoint is closer to the mouse
      const positions = lineGeometry.attributes.position.array;
      const lineStart = new THREE.Vector3(positions[0], positions[1], positions[2]);
      const lineEnd = new THREE.Vector3(positions[3], positions[4], positions[5]);
    
      mouseWorld.copy(intersects[0].point);
    
      const distanceToStart = mouseWorld.distanceTo(lineStart);
      const distanceToEnd = mouseWorld.distanceTo(lineEnd);
    
      selectedEndpointIndex = (distanceToStart < distanceToEnd) ? 0 : 1;
    
      isDraggingEndpoint = true;
      // Set previous mouse position in NDC
      previousMousePosition.set(mouse.x, mouse.y);
    }
    

    } }

    // Mouse move event function onMouseMove(event) { if (isPanning) { // Panning code const deltaX = event.clientX - previousMousePosition.x; const deltaY = event.clientY - previousMousePosition.y;

    const zoomFactorX = camera.right - camera.left;
    const zoomFactorY = camera.top - camera.bottom;
    
    const panX = (deltaX / window.innerWidth) * zoomFactorX;
    const panY = (deltaY / window.innerHeight) * zoomFactorY;
    
    camera.left -= panX;
    camera.right -= panX;
    camera.top += panY;
    camera.bottom += panY;
    camera.updateProjectionMatrix();
    
    previousMousePosition.set(event.clientX, event.clientY); // Update previous position
    
    // Update candlestick visibility after panning
    updateCandlestickVisibility();
    

    } else if (isDraggingEndpoint) { // Get current mouse position in normalized device coordinates (NDC) const rect = renderer.domElement.getBoundingClientRect(); currentMouse.set( ((event.clientX - rect.left) / rect.width) * 2 - 1, - ((event.clientY - rect.top) / rect.height) * 2 + 1 );

    // Convert previous and current mouse positions to world coordinates
    previousMouseWorld.set(previousMousePosition.x, previousMousePosition.y, 0).unproject(camera);
    mouseWorld.set(currentMouse.x, currentMouse.y, 0).unproject(camera);
    
    // Compute delta in world coordinates
    deltaWorld.subVectors(mouseWorld, previousMouseWorld);
    
    // Update the selected endpoint
    const positions = lineGeometry.attributes.position.array;
    const index = selectedEndpointIndex * 3; // 0 or 3
    
    positions[index] += deltaWorld.x;
    positions[index + 1] += deltaWorld.y;
    
    lineGeometry.attributes.position.needsUpdate = true;
    lineGeometry.computeBoundingSphere(); // Update bounding sphere for frustum culling and raycasting
    
    previousMousePosition.copy(currentMouse); // Update previous mouse position
    

    } }

    // Mouse up event function onMouseUp(event) { if (event.button === 2) { // Right mouse button isPanning = false; } else if (event.button === 0) { // Left mouse button isDraggingEndpoint = false; } }

    // Add event listeners for mouse actions window.addEventListener('mousedown', onMouseDown); window.addEventListener('mousemove', onMouseMove); window.addEventListener('mouseup', onMouseUp);

  2. Assistant

    Rendering a large number of candlesticks (e.g., 1,000,000) can significantly impact performance, especially when zooming in and rendering detailed geometries. Here are several optimization strategies to improve performance and reduce lag:

    1. Limit the Number of Rendered Candlesticks:

      • Data Downsampling: Reduce the number of candlesticks displayed by aggregating or sampling the data based on the current zoom level.
      • Dynamic Level of Detail (LOD): Adjust the level of detail dynamically as the user zooms in or out.
    2. Implement Frustum Culling:

      • Spatial Partitioning: Divide the candlestick data into smaller chunks (e.g., using a grid or quadtree) and render only the chunks within the camera's view.
    3. Optimize Geometry and Materials:

      • Simplify Geometries: Ensure that the geometries used (like BoxGeometry) are as simple as possible.
      • Use Efficient Materials: Utilize MeshBasicMaterial or other lightweight materials that do not require extensive calculations.
    4. Use Web Workers for Data Processing:

      • Offload data processing tasks to Web Workers to prevent blocking the main thread.
    5. Batch Rendering:

      • Multiple InstancedMeshes: Split the data into multiple InstancedMesh objects to allow for more granular culling and visibility control.
    6. Leverage Shader-Based Rendering:

      • Implement custom shaders to handle rendering on the GPU more efficiently.

    Below, I'll provide code modifications and additions to implement some of these optimizations, focusing on limiting the number of rendered candlesticks through dynamic data sampling based on zoom level and implementing spatial partitioning for frustum culling.


    1. Dynamic Data Sampling Based on Zoom Level

    Objective: Reduce the number of candlesticks rendered by sampling the data based on the current zoom level.

    Implementation Steps:

    • Define a Maximum Number of Candlesticks to Render: Set a cap on how many candlesticks can be displayed at once.
    • Sample Data Accordingly: Depending on the zoom level, sample the OHLC data to fit within the maximum count.

    Code Implementation

    a. Update the createInstancedCandlesticks Function:

    Modify the function to accept a subset of data based on the current zoom level.

    function createInstancedCandlesticks(data) {
      const barWidth = 1;
      const maxCandlesticks = Math.min(data.length, 100000); // Cap at 100,000
      // ... rest of the code remains the same
    }
    

    b. Implement Data Sampling:

    Create a function to sample OHLC data based on the current zoom level.

    function sampleOHLCData(data, maxVisible) {
      const sampledData = [];
      const stride = Math.floor(data.length / maxVisible);
      
      for (let i = 0; i < data.length; i += stride) {
        sampledData.push(data[i]);
        if (sampledData.length >= maxVisible) break;
      }
      
      return sampledData;
    }
    

    c. Integrate Sampling with Zoom Logic:

    Adjust the updateCandlestickVisibility function to resample data when zoom level changes.

    let currentSampledData = [];
    
    function updateCandlestickVisibility() {
      const visibleWidth = camera.right - camera.left;
      const candlestickWidth = 1.5; // Width of each candlestick including spacing
      const visibleCandlesticks = Math.floor(visibleWidth / candlestickWidth);
      
      // Define a maximum number of candlesticks to render
      const maxVisibleCandlesticks = 100000; // Adjust as needed
      
      // Determine if sampling is needed
      if (visibleCandlesticks > 700) {
        instancedMesh.visible = false; // Hide bodies if too many are visible
        wickLineSegments.visible = true; // Always show wicks
        // Sample data to limit visible candlesticks
        currentSampledData = sampleOHLCData(ohlcData, maxVisibleCandlesticks);
        updateInstancedMesh(currentSampledData);
      } else {
        instancedMesh.visible = true; // Show bodies when zoomed in
        wickLineSegments.visible = true; // Always show wicks
        // Use full data or less sampled data
        currentSampledData = sampleOHLCData(ohlcData, Math.min(ohlcData.length, 7000));
        updateInstancedMesh(currentSampledData);
      }
    }
    

    d. Create updateInstancedMesh Function:

    This function updates the InstancedMesh with the new sampled data.

    function updateInstancedMesh(data) {
      const dummy = new THREE.Object3D();
      const colorArray = new Float32Array(data.length * 3); // RGB for each instance
      let currentX = 0;
      
      for (let index = 0; index < data.length; 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); // Adjust as needed
        dummy.updateMatrix();
        
        instancedMesh.setMatrixAt(index, dummy.matrix);
        
        // Set color for each instance
        const color = isBullish ? [0 / 255, 70 / 255, 0 / 255] : [138 / 255, 0 / 255, 0 / 255];
        colorArray.set(color, index * 3);
        
        // Increment x for the next candlestick
        currentX += 1.5; // barWidth * 1.5
      }
      
      instancedMesh.instanceMatrix.needsUpdate = true;
      
      // Update colors
      instancedMesh.geometry.setAttribute('color', new THREE.InstancedBufferAttribute(colorArray, 3));
    }
    

    e. Initial Data Sampling and Mesh Creation:

    Adjust the initial creation to use sampled data.

    const sampledInitialData = sampleOHLCData(ohlcData, 100000); // Start with a manageable number
    const { instancedMesh, wickLineSegments } = createInstancedCandlesticks(sampledInitialData);
    

    2. Spatial Partitioning for Frustum Culling

    Objective: Improve performance by rendering only the candlesticks within the camera's view using spatial partitioning.

    Implementation Steps:

    • Divide the Data into Batches: Split your OHLC data into smaller groups (e.g., grid cells).
    • Create Separate InstancedMesh Objects for Each Batch: This allows individual batches to be culled based on their visibility.
    • Update Visibility Based on Camera Frustum: Check which batches are within the camera's frustum and toggle their visibility accordingly.

    Code Implementation

    a. Create Spatial Batches:

    const batchSize = 10000; // Number of candlesticks per batch
    const batches = [];
    
    for (let i = 0; i < ohlcData.length; i += batchSize) {
      const batchData = ohlcData.slice(i, i + batchSize);
      const { instancedMesh, wickLineSegments } = createInstancedCandlesticks(batchData);
      
      // Optionally, group them for easier management
      const group = new THREE.Group();
      group.add(instancedMesh);
      group.add(wickLineSegments);
      
      // Calculate bounding box for the batch
      const box = new THREE.Box3();
      box.setFromObject(instancedMesh);
      group.userData.boundingBox = box;
      
      scene.add(group);
      batches.push(group);
    }
    

    b. Implement Frustum Culling:

    Update the render loop to toggle visibility based on the camera's frustum.

    const frustum = new THREE.Frustum();
    const cameraViewProjectionMatrix = new THREE.Matrix4();
    
    function animate() {
      requestAnimationFrame(animate);
      
      // Update the camera's view projection matrix
      camera.updateMatrixWorld(); // Ensure the camera matrix is updated
      cameraViewProjectionMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
      frustum.setFromProjectionMatrix(cameraViewProjectionMatrix);
      
      // Iterate through each batch and set visibility
      batches.forEach(batch => {
        if (frustum.intersectsBox(batch.userData.boundingBox)) {
          batch.visible = true;
        } else {
          batch.visible = false;
        }
      });
      
      renderer.render(scene, camera);
    }
    

    c. Adjust createInstancedCandlesticks to Return Batches:

    Ensure that each InstancedMesh handles its subset of data correctly.

    function createInstancedCandlesticks(data) {
      const barWidth = 1;
      const maxCandlesticks = data.length;
    
      // Materials
      const material = new THREE.MeshBasicMaterial({ vertexColors: true, side: THREE.DoubleSide });
    
      // Geometries for the candlestick body and borders
      const geometryBar = new THREE.BoxGeometry(barWidth, 1, 0.1); // Added depth for proper bounding
      
      // Create instanced mesh for candlesticks
      const instancedMesh = new THREE.InstancedMesh(geometryBar, material, maxCandlesticks);
      
      // Enable instance color
      instancedMesh.instanceColor = new THREE.InstancedBufferAttribute(new Float32Array(maxCandlesticks * 3), 3);
      
      const dummy = new THREE.Object3D();
      
      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(index * 1.5, y, 0);
        dummy.scale.set(1, height, 1);
        dummy.updateMatrix();
        
        instancedMesh.setMatrixAt(index, dummy.matrix);
        
        // Set color for each instance
        const color = isBullish ? [0 / 255, 70 / 255, 0 / 255] : [138 / 255, 0 / 255, 0 / 255];
        instancedMesh.instanceColor.setXYZ(index, ...color);
      }
      
      instancedMesh.instanceMatrix.needsUpdate = true;
      instancedMesh.instanceColor.needsUpdate = true;
      
      // Create and add wick line segments (optional, consider batching similarly)
      // ...
    
      return { instancedMesh };
    }
    

    Notes:

    • Bounding Boxes: Accurate bounding boxes are essential for effective frustum culling. Ensure that the BoxGeometry has sufficient depth and that Box3.setFromObject accurately encompasses all instances.
    • Performance Consideration: Managing thousands of InstancedMesh objects can still be performance-intensive. Adjust the batchSize according to your application's needs and test different sizes for optimal performance.

    3. Additional Optimizations

    a. Reduce Geometry Complexity

    Ensure that the BoxGeometry is as simple as possible. For instance, if the depth (z) is not necessary, set it to a minimal value or explore using PlaneGeometry with a shader to simulate depth.

    const geometryBar = new THREE.PlaneGeometry(barWidth, 1); // Simplified geometry
    

    b. Use Efficient Materials

    MeshBasicMaterial is lightweight as it doesn't respond to lighting. If lighting is not essential, continue using it. Otherwise, consider using MeshLambertMaterial with minimal parameters.

    const material = new THREE.MeshBasicMaterial({ vertexColors: true, side: THREE.DoubleSide });
    

    c. Limit Attributes Updates

    Avoid updating buffer attributes every frame unless necessary. Only update them when the data changes (e.g., during zoom or pan events).

    d. Throttle Resize and Zoom Events

    Ensure that expensive operations like data sampling and mesh updates are throttled or debounced to prevent them from triggering too frequently.

    // Example using debounce
    function debounce(func, wait) {
      let timeout;
      return function(...args) {
        const later = () => { clearTimeout(timeout); func.apply(this, args); };
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
      };
    }
    
    window.addEventListener('resize', debounce(onWindowResize, 200));
    window.addEventListener('wheel', debounce(onWheel, 50), { passive: false });
    

    4. Implementing Level of Detail (LOD)

    Using Three.js's LOD object can help manage different levels of detail based on the camera's distance. However, with instanced meshes, integrating LOD requires managing multiple InstancedMesh objects with varying instance counts or details.

    Example Implementation

    // Create LOD object
    const lod = new THREE.LOD();
    
    // Create different levels of detail
    const highDetail = createInstancedCandlesticks(highDetailData).instancedMesh;
    const mediumDetail = createInstancedCandlesticks(mediumDetailData).instancedMesh;
    const lowDetail = createInstancedCandlesticks(lowDetailData).instancedMesh;
    
    // Add to LOD with different levels
    lod.addLevel(highDetail, 0);
    lod.addLevel(mediumDetail, 100); // Distance thresholds
    lod.addLevel(lowDetail, 200);
    
    // Compute LOD
    lod.computeBoundingSphere();
    
    // Add LOD to scene
    scene.add(lod);
    

    Notes:

    • Managing LOD with instanced meshes can become complex, especially with dynamic data. Ensure that each LOD level is optimized for its respective detail level.

    5. Final Optimized Example

    Below is a simplified version of your code integrated with dynamic data sampling and basic frustum culling. This example caps the maximum number of candlesticks and updates the samples based on zoom level.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Three.js Optimized Candlesticks</title>
      <style>
        body { margin: 0; }
        canvas { display: block; }
      </style>
    </head>
    <body>
      <script type="module">
    
    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;
    }
    
    // Data variables
    const ohlcData = generateOHLCData(1000000); // Original data
    let currentSampledData = [];
    
    // Sampling function
    function sampleOHLCData(data, maxVisible) {
      const sampledData = [];
      const stride = Math.floor(data.length / maxVisible);
      
      for (let i = 0; i < data.length; i += stride) {
        sampledData.push(data[i]);
        if (sampledData.length >= maxVisible) break;
      }
      
      return sampledData;
    }
    
    // Create InstancedMesh
    let instancedMesh;
    let wickLineSegments;
    
    function createInstancedCandlesticks(data) {
      const barWidth = 1;
      const maxCandlesticks = data.length;
    
      // Materials
      const material = new THREE.MeshBasicMaterial({ vertexColors: true, side: THREE.DoubleSide });
    
      // Geometries for the candlestick body and borders
      const geometryBar = new THREE.BoxGeometry(barWidth, 1, 0.1); // Simplified depth
    
      // Create instanced mesh for candlesticks
      const mesh = new THREE.InstancedMesh(geometryBar, material, maxCandlesticks);
      mesh.instanceColor = new THREE.InstancedBufferAttribute(new Float32Array(maxCandlesticks * 3), 3);
    
      const dummy = new THREE.Object3D();
      
      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(index * 1.5, y, 0);
        dummy.scale.set(1, height, 1);
        dummy.updateMatrix();
        
        mesh.setMatrixAt(index, dummy.matrix);
        
        // Set color for each instance
        const color = isBullish ? [0 / 255, 70 / 255, 0 / 255] : [138 / 255, 0 / 255, 0 / 255];
        mesh.instanceColor.setXYZ(index, ...color);
      }
      
      mesh.instanceMatrix.needsUpdate = true;
      mesh.instanceColor.needsUpdate = true;
      
      // Create wick line segments (optional: can be optimized similarly)
      const wickVertices = new Float32Array(maxCandlesticks * 6); // 2 points per wick
      const wickColors = new Float32Array(maxCandlesticks * 6); // RGB per point
      
      const bullishColor = [0 / 255, 70 / 255, 0 / 255];
      const bearishColor = [138 / 255, 0 / 255, 0 / 255];
      
      for (let index = 0; index < maxCandlesticks; index++) {
        const d = data[index];
        const isBullish = d.close >= d.open;
        const color = isBullish ? bullishColor : bearishColor;
        
        wickVertices[index * 6] = index * 1.5;
        wickVertices[index * 6 + 1] = d.low;
        wickVertices[index * 6 + 2] = -0.5;
        wickVertices[index * 6 + 3] = index * 1.5;
        wickVertices[index * 6 + 4] = d.high;
        wickVertices[index * 6 + 5] = -0.5;
        
        wickColors.set(color, index * 6);
        wickColors.set(color, index * 6 + 3);
      }
      
      const wickGeometry = new THREE.BufferGeometry();
      wickGeometry.setAttribute('position', new THREE.BufferAttribute(wickVertices, 3));
      wickGeometry.setAttribute('color', new THREE.BufferAttribute(wickColors, 3));
      
      const wickMaterial = new THREE.LineBasicMaterial({ vertexColors: true });
      const wicks = new THREE.LineSegments(wickGeometry, wickMaterial);
      
      scene.add(wicks);
      
      mesh.geometry.setAttribute('color', new THREE.InstancedBufferAttribute(wickColors, 3));
      scene.add(mesh);
      
      return { instancedMesh: mesh, wickLineSegments: wicks };
    }
    
    // Initial Sampling and Mesh Creation
    currentSampledData = sampleOHLCData(ohlcData, 100000); // Initial limit
    ({ instancedMesh, wickLineSegments } = createInstancedCandlesticks(currentSampledData));
    
    // Zoom logic
    const zoomSpeed = 0.8;
    let visibilityUpdateTimeout;
    
    function onWheel(event) {
      event.preventDefault();
    
      const rect = renderer.domElement.getBoundingClientRect();
      const mouseX = ((event.clientX - rect.left) / rect.width) * 2 - 1;
      const mouseY = -((event.clientY - rect.top) / rect.height) * 2 + 1;
    
      const mouseWorld = new THREE.Vector3(mouseX, mouseY, 0).unproject(camera);
      const zoomFactor = 1 - event.deltaY * 0.001 * zoomSpeed;
    
      const ctrlPressed = event.ctrlKey;
      const shiftPressed = event.shiftKey;
    
      if (ctrlPressed) {
        camera.left = (camera.left - mouseWorld.x) * zoomFactor + mouseWorld.x;
        camera.right = (camera.right - mouseWorld.x) * zoomFactor + mouseWorld.x;
      }
      if (shiftPressed) {
        camera.top = (camera.top - mouseWorld.y) * zoomFactor + mouseWorld.y;
        camera.bottom = (camera.bottom - mouseWorld.y) * zoomFactor + mouseWorld.y;
      }
      if (!ctrlPressed && !shiftPressed) {
        camera.left = (camera.left - mouseWorld.x) * zoomFactor + mouseWorld.x;
        camera.right = (camera.right - mouseWorld.x) * zoomFactor + mouseWorld.x;
        camera.top = (camera.top - mouseWorld.y) * zoomFactor + mouseWorld.y;
        camera.bottom = (camera.bottom - mouseWorld.y) * zoomFactor + mouseWorld.y;
      }
    
      camera.updateProjectionMatrix();
    
      // Clear previous timeout if it exists
      clearTimeout(visibilityUpdateTimeout);
    
      // Set a new timeout to update visibility after a delay
      visibilityUpdateTimeout = setTimeout(updateCandlestickVisibility, 100);
    }
    
    // Update Candlestick Visibility and Resample Data
    function updateCandlestickVisibility() {
      const visibleWidth = camera.right - camera.left;
      const candlestickWidth = 1.5;
      const visibleCandlesticks = Math.floor(visibleWidth / candlestickWidth);
      
      const maxVisibleCandlesticks = 100000;
      
      if (visibleCandlesticks > 700) {
        instancedMesh.visible = false;
        wickLineSegments.visible = true;
        currentSampledData = sampleOHLCData(ohlcData, maxVisibleCandlesticks);
        // Update instanced mesh (requires re-creation or updating existing mesh)
        // For simplicity, recreate the mesh
        scene.remove(instancedMesh);
        instancedMesh.dispose();
        wickLineSegments.geometry.dispose();
        wickLineSegments.material.dispose();
        ({ instancedMesh, wickLineSegments } = createInstancedCandlesticks(currentSampledData));
      } else {
        instancedMesh.visible = true;
        wickLineSegments.visible = true;
        currentSampledData = sampleOHLCData(ohlcData, Math.min(ohlcData.length, 7000));
        // Update instanced mesh
        scene.remove(instancedMesh);
        instancedMesh.dispose();
        wickLineSegments.geometry.dispose();
        wickLineSegments.material.dispose();
        ({ instancedMesh, wickLineSegments } = createInstancedCandlesticks(currentSampledData));
      }
    }
    
    // Prevent default browser behavior for wheel scrolling with Control or Shift key
    window.addEventListener('wheel', onWheel, { passive: false });
    
    // Render loop
    function animate() {
      requestAnimationFrame(animate);
      renderer.render(scene, camera);
    }
    
    // Handle window resize
    function onWindowResize() {
      const aspect = window.innerWidth / window.innerHeight;
      camera.left = -cameraSize * aspect;
      camera.right = cameraSize * aspect;
      camera.top = cameraSize;
      camera.bottom = -cameraSize;
      camera.updateProjectionMatrix();
      renderer.setSize(window.innerWidth, window.innerHeight);
      updateCandlestickVisibility();
    }
    window.addEventListener('resize', onWindowResize);
    
    // Start animation
    animate();
    
    // Optional: Add panning and interactive line as per your original code
    // ...
    
      </script>
    </body>
    </html>
    

    Key Points in the Optimized Example:

    • Data Sampling: The sampleOHLCData function reduces the data size based on a defined maximum.
    • Visibility Toggle: Based on the number of visible candlesticks, the updateCandlestickVisibility function toggles between showing candlestick bodies and only wicks.
    • Resource Management: Properly dispose of geometries and materials when recreating meshes to prevent memory leaks.
    • Performance Consideration: Recreating InstancedMesh objects on every zoom can be expensive. For a more performant solution, consider updating existing instances instead of recreating them.

    6. Advanced Optimizations (Optional)

    For further performance improvements, consider the following advanced techniques:

    a. Custom Shaders for Candlesticks

    Implement custom shaders to handle candlestick rendering on the GPU, reducing CPU-GPU communication overhead.

    b. Use Points Instead of InstancedMesh

    If detailed rendering is not essential, representing candlesticks as points with custom shaders can significantly improve performance.

    c. WebGL2 Features

    Leverage advanced WebGL2 features like transform feedback or shader storage buffer objects (SSBOs) for more efficient data handling.


    Conclusion

    Rendering a vast number of candlesticks efficiently in Three.js requires careful optimization of both data handling and rendering techniques. By implementing dynamic data sampling based on zoom levels and leveraging spatial partitioning for frustum culling, you can significantly improve performance and reduce lag. Additionally, exploring advanced rendering techniques can further enhance performance, especially for applications requiring real-time interactivity with large datasets.

    Feel free to adjust the provided code snippets to better fit your specific requirements and data characteristics. Testing and profiling are essential to identify the most effective optimizations for your particular use case.