Please fix this code Some of the candle bodies are not the same color as the wicks.…
- User
Please fix this code Some of the candle bodies are not the same color as the wicks.
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]; }
// Arrays to store the instanced meshes and wicks const instancedMeshes = []; const wickLineSegmentsArray = [];
// Create instanced candlesticks with chunking function createInstancedCandlesticks(data, chunkSize) { const barWidth = 1;
// Materials const material = new THREE.MeshBasicMaterial({ vertexColors: true }); const wickMaterial = new THREE.LineBasicMaterial({ vertexColors: true }); // Enable vertex colors
// Geometries for the candlestick body const geometryBar = new THREE.BoxGeometry(barWidth, 1, 0); // Width, height, depth
// 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 dataLength = data.length; const numChunks = Math.ceil(dataLength / chunkSize);
let currentX = 0;
const dummy = new THREE.Object3D();
for (let chunkIndex = 0; chunkIndex < numChunks; chunkIndex++) { const startIndex = chunkIndex * chunkSize; const endIndex = Math.min(startIndex + chunkSize, dataLength); const chunkData = data.slice(startIndex, endIndex);
const maxCandlesticks = chunkData.length; // Create instanced mesh for candlesticks const instancedMesh = new THREE.InstancedMesh(geometryBar, material, maxCandlesticks); const wickVertices = new Float32Array(maxCandlesticks * 6); // 2 points per wick, x,y,z per point const wickColors = new Float32Array(maxCandlesticks * 6); // Color per vertex const colorArray = new Float32Array(maxCandlesticks * 3); // Color per instance for (let index = 0; index < maxCandlesticks; index++) { const d = chunkData[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); dummy.updateMatrix(); instancedMesh.setMatrixAt(index, dummy.matrix); // Set color for each instance const color = isBullish ? bullishColor : bearishColor; colorArray.set(color, index * 3); // 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; // Wick colors (same color for both points) wickColors.set([...color, ...color], wickIndex); // Increment x for the next candlestick currentX += barWidth * 1.5; } // Add color attribute to instanced mesh instancedMesh.geometry.setAttribute('color', new THREE.InstancedBufferAttribute(colorArray, 3)); // Add the instances to the scene scene.add(instancedMesh); instancedMeshes.push(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 wickLineSegments = new THREE.LineSegments(wickGeometry, wickMaterial); scene.add(wickLineSegments); wickLineSegmentsArray.push(wickLineSegments); - Assistant