|
| 1 | +import * as THREE from "three"; |
| 2 | +import {scene} from "./scene.ts"; |
| 3 | +import {label} from "./label.ts"; |
| 4 | + |
| 5 | +const graphicSize = 1.5; |
| 6 | + |
| 7 | +export type TechUsage = { |
| 8 | + technology: string, |
| 9 | + percentage: number |
| 10 | +} |
| 11 | + |
| 12 | +const triangle = [ |
| 13 | + {x: -graphicSize, y: -graphicSize}, |
| 14 | + {x: 0, y: graphicSize}, |
| 15 | + {x: graphicSize, y: -graphicSize} |
| 16 | +] |
| 17 | + |
| 18 | +const extrudeSettings = { |
| 19 | + steps: 1, |
| 20 | + depth: 0, |
| 21 | + bevelEnabled: false, |
| 22 | +}; |
| 23 | + |
| 24 | + |
| 25 | +//----------------------------------------------------------------------- |
| 26 | + |
| 27 | +export const initRadar = (techs: TechUsage[]) => { |
| 28 | + const radarSize = determineRadarSize(techs.length); |
| 29 | + if (radarSize == undefined) { return; } |
| 30 | + |
| 31 | + const shape = drawShape(radarSize); |
| 32 | + const mainMesh = buildMesh(shape, 0xffffff); |
| 33 | + scene.add(mainMesh); |
| 34 | + |
| 35 | + for (let i = 0; i < 4; i++) { |
| 36 | + createDepthMesh(shape, i); |
| 37 | + } |
| 38 | + |
| 39 | + for (let i = 0; i < techs.length; i++) { |
| 40 | + label(techs[i].technology, radarSize[i]); |
| 41 | + } |
| 42 | + |
| 43 | + const valueShape = determineValueShape(techs.map(tech => tech.percentage)); |
| 44 | + const valueMesh = buildMesh(valueShape, 0x3BFFC5); |
| 45 | + scene.add(valueMesh); |
| 46 | +} |
| 47 | + |
| 48 | +//----------------------------------------------------------------------- |
| 49 | + |
| 50 | +const buildMesh = (shape: THREE.Shape, shapeColor: THREE.ColorRepresentation) => { |
| 51 | + const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings); |
| 52 | + const material = new THREE.MeshBasicMaterial({ |
| 53 | + color: shapeColor, |
| 54 | + wireframe: true |
| 55 | + }); |
| 56 | + |
| 57 | + return new THREE.Mesh(geometry, material); |
| 58 | +} |
| 59 | + |
| 60 | +const createDepthMesh = (depthShape: THREE.Shape, iteration: number) => { |
| 61 | + const mesh = buildMesh(depthShape, 0x333333); |
| 62 | + const size = iteration * 0.25; |
| 63 | + const offset = 2.5; |
| 64 | + |
| 65 | + mesh.scale.set(size, size, size); |
| 66 | + mesh.position.z = iteration * 0.6 - offset; |
| 67 | + scene.add(mesh); |
| 68 | +} |
| 69 | + |
| 70 | +const drawShape = (points: {x: number, y: number}[]) => { |
| 71 | + const shape = new THREE.Shape(); |
| 72 | + |
| 73 | + if (points.length > 0) { |
| 74 | + shape.moveTo(points[0].x, points[0].y); |
| 75 | + for (let i = 1; i < points.length; i++) { |
| 76 | + shape.lineTo(points[i].x, points[i].y); |
| 77 | + } |
| 78 | + shape.lineTo(points[0].x, points[0].y); |
| 79 | + } |
| 80 | + |
| 81 | + return shape; |
| 82 | +} |
| 83 | + |
| 84 | +const determineValueShape = (values: number[]) => { |
| 85 | + let valueShape: {x: number, y: number}[] = []; |
| 86 | + const score = values.map(value => ((100 - value) / 100) * graphicSize); |
| 87 | + |
| 88 | + valueShape = [ |
| 89 | + {x: triangle[0].x + score[0], y: triangle[0].y + score[0]}, |
| 90 | + {x: triangle[1].x, y: triangle[1].y - score[1]}, |
| 91 | + {x: triangle[2].x - score[2], y: triangle[2].y + score[2]} |
| 92 | + ]; |
| 93 | + |
| 94 | + return drawShape(valueShape); |
| 95 | +} |
| 96 | + |
| 97 | +const determineRadarSize = (size: number) => { |
| 98 | + if (size == 3) { |
| 99 | + return triangle; |
| 100 | + } |
| 101 | + else { |
| 102 | + console.log("Invalid number of technology. Create a new radar size or modify the number of technologies."); |
| 103 | + return; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
0 commit comments