-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateCube.js
More file actions
188 lines (166 loc) · 5.86 KB
/
createCube.js
File metadata and controls
188 lines (166 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import * as THREE from 'three';
// Configuration - map these to your actual data columns
const DATA_MAPPING = {
YEAR: 'PubYear', // Publication year
JOURNAL: 'Source', // Journal name
PMID: 'PMID', // PubMed ID
TAG: 'Keyword_1', // First keyword
AUTHOR: 'Author_1', // First author
TITLE: 'Title' // Article title
};
export function createCube(data, allData) {
// Fixed size since we don't have citations
const size = 0.8;
const geometry = new THREE.BoxGeometry(size, size, size);
const baseColor = getColorForYear(getField(data, DATA_MAPPING.YEAR));
// Create materials for each face with proper fallbacks
const materials = [
createFaceMaterial( // Year face
baseColor,
getField(data, DATA_MAPPING.YEAR, 'Year?'),
'Year'
),
createFaceMaterial( // Journal face
lightenColor(baseColor, 0.2),
getJournalAbbreviation(getField(data, DATA_MAPPING.JOURNAL)),
'Journal'
),
createFaceMaterial( // PMID face
darkenColor(baseColor, 0.2),
getField(data, DATA_MAPPING.PMID, 'PMID?'),
'ID'
),
createFaceMaterial( // Keyword face
complementColor(baseColor),
getFirstTag(getField(data, DATA_MAPPING.TAG)),
'Keyword'
),
createFaceMaterial( // Author face
lightenColor(baseColor, 0.1),
getFirstAuthorInitial(getField(data, DATA_MAPPING.AUTHOR)),
'Author'
),
createFaceMaterial( // Title face
darkenColor(baseColor, 0.1),
getTitleAbbreviation(getField(data, DATA_MAPPING.TITLE)),
'Title'
)
];
const cube = new THREE.Mesh(geometry, materials);
cube.position.set(...calculatePosition(data, allData));
cube.userData = data;
return cube;
}
// Safe field access with fallback
function getField(data, fieldName, fallback = '') {
if (!data) return fallback;
if (fieldName in data) {
const value = data[fieldName];
return value !== undefined && value !== null ? value : fallback;
}
return fallback;
}
// Helper function to calculate position
function calculatePosition(data, allData) {
const index = allData.indexOf(data);
const gridSize = Math.ceil(Math.sqrt(allData.length));
const x = (index % gridSize) * 2 - gridSize;
const z = Math.floor(index / gridSize) * 2 - gridSize;
return [x, 0, z];
}
function createFaceMaterial(color, text, label) {
const canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 256;
const context = canvas.getContext('2d');
// Fill with color
context.fillStyle = `#${color.getHexString()}`;
context.fillRect(0, 0, canvas.width, canvas.height);
// Add main text (centered, larger)
context.font = 'Bold 60px Arial';
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillStyle = 'white';
// Split long text into two lines if needed
if (String(text).length > 8) {
const mid = Math.floor(String(text).length / 2);
context.fillText(String(text).substring(0, mid), canvas.width/2, canvas.height/2 - 20);
context.fillText(String(text).substring(mid), canvas.width/2, canvas.height/2 + 20);
} else {
context.fillText(String(text), canvas.width/2, canvas.height/2);
}
// Add label (smaller, at bottom)
context.font = '20px Arial';
context.fillText(label, canvas.width/2, canvas.height - 20);
// Create texture from canvas
const texture = new THREE.CanvasTexture(canvas);
return new THREE.MeshPhongMaterial({
map: texture,
transparent: true,
opacity: 0.95,
shininess: 30,
emissive: 0x000000,
emissiveIntensity: 0
});
}
// Color manipulation helpers (unchanged)
function lightenColor(color, amount) {
const hsl = { h: 0, s: 0, l: 0 };
color.getHSL(hsl);
hsl.l += (1 - hsl.l) * amount;
const result = new THREE.Color();
result.setHSL(hsl.h, hsl.s, hsl.l);
return result;
}
function darkenColor(color, amount) {
const hsl = { h: 0, s: 0, l: 0 };
color.getHSL(hsl);
hsl.l -= hsl.l * amount;
const result = new THREE.Color();
result.setHSL(hsl.h, hsl.s, hsl.l);
return result;
}
function complementColor(color) {
const hsl = { h: 0, s: 0, l: 0 };
color.getHSL(hsl);
hsl.h = (hsl.h + 0.5) % 1;
const result = new THREE.Color();
result.setHSL(hsl.h, hsl.s, hsl.l);
return result;
}
// Data formatting helpers
function getJournalAbbreviation(journal) {
if (!journal) return 'N/A';
// Better abbreviation that keeps journal recognizable
return journal.split(' ')
.map(word => word.length > 3 ? word.substring(0,3) : word)
.join(' ')
.substring(0, 12);
}
function getFirstTag(tag) {
if (!tag) return '';
return tag.length > 10 ? tag.substring(0,10) + '...' : tag;
}
function getFirstAuthorInitial(author) {
if (!author) return '?';
const parts = author.split(' ');
return parts.length > 1 ? `${parts[0][0]}${parts[parts.length-1][0]}` : parts[0][0];
}
function getTitleAbbreviation(title) {
if (!title) return 'No Title';
// Get first meaningful word (skip articles)
const firstWord = title.replace(/^(the|a|an)\s+/i, '').split(' ')[0];
return firstWord.length > 8 ? firstWord.substring(0,8) : firstWord;
}
// Year-based coloring (unchanged)
function getColorForYear(year) {
if (!year) return new THREE.Color(0x999999);
const minYear = 1950;
const maxYear = new Date().getFullYear();
const normalized = (year - minYear) / (maxYear - minYear);
return new THREE.Color().lerpColors(
new THREE.Color(0x1a2b6d),
new THREE.Color(0xd62246),
Math.min(1, Math.max(0, normalized))
);
}