-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgridframe.js
More file actions
131 lines (109 loc) · 4.56 KB
/
Copy pathgridframe.js
File metadata and controls
131 lines (109 loc) · 4.56 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
(function() {
// Create the container for the grid background
const gridContainer = document.createElement('div');
gridContainer.id = 'gridframe-container';
gridContainer.style.cssText = `
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1; /* Same level as canvas-container to replace it seamlessly */
display: none;
background: #050505;
overflow: hidden;
`;
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initGrid);
} else {
initGrid();
}
let scene, camera, renderer, animationId, clock;
let gridHelper, terrain;
function initGrid() {
document.body.insertBefore(gridContainer, document.getElementById('vignette'));
// Three.js Setup
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x050505, 0.03); // Deep fog for infinite look
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 2, 8);
camera.lookAt(0, 0, 0);
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
gridContainer.appendChild(renderer.domElement);
// --- Synthwave / Cyberpunk Grid ---
const gridColor = 0xff00ff; // Neon magenta/purple
const centerLineColor = 0x00ffff; // Neon cyan
gridHelper = new THREE.GridHelper(200, 100, centerLineColor, gridColor);
gridHelper.position.y = -2;
// Add glowing effect to the grid
gridHelper.material.transparent = true;
gridHelper.material.opacity = 0.5;
scene.add(gridHelper);
// --- Wireframe Terrain overlay ---
const geo = new THREE.PlaneGeometry(200, 200, 40, 40);
// Procedurally displace vertices for a low-poly mountain effect
const pos = geo.attributes.position;
for(let i = 0; i < pos.count; i++) {
const x = pos.getX(i);
const y = pos.getY(i);
// Math magic for some cool procedural hills
const z = (Math.sin(x * 0.1) * Math.cos(y * 0.1) * 3) +
(Math.sin(x * 0.05 + 2) * Math.cos(y * 0.05 + 1) * 5);
pos.setZ(i, z);
}
geo.computeVertexNormals();
const mat = new THREE.MeshBasicMaterial({
color: 0x00ffff,
wireframe: true,
transparent: true,
opacity: 0.15
});
terrain = new THREE.Mesh(geo, mat);
terrain.rotation.x = -Math.PI / 2;
terrain.position.y = -6;
scene.add(terrain);
clock = new THREE.Clock();
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
if (!camera || !renderer) return;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
animationId = requestAnimationFrame(animate);
const t = clock.getElapsedTime();
// Infinite forward movement illusion by resetting position
// The grid has 100 divisions over 200 units = 2 units per square
gridHelper.position.z = (t * 10) % 2;
// Move the terrain slightly for dynamic feel
terrain.position.z = (t * 5) % 20;
// Camera gentle float
camera.position.y = 2 + Math.sin(t * 0.5) * 0.5;
renderer.render(scene, camera);
}
// Expose toggle function globally
window.toggleGridFrame = function(active) {
const originalCanvas = document.getElementById('canvas-container');
if (active) {
gridContainer.style.display = 'block';
if (originalCanvas) {
originalCanvas.style.transition = 'opacity 0.5s ease';
originalCanvas.style.opacity = '0';
setTimeout(() => { if(originalCanvas.style.opacity === '0') originalCanvas.style.display = 'none'; }, 500);
}
if (clock) clock.start();
animate();
} else {
if (originalCanvas) {
originalCanvas.style.display = 'block';
setTimeout(() => originalCanvas.style.opacity = '1', 50);
}
gridContainer.style.display = 'none';
if (animationId) cancelAnimationFrame(animationId);
}
};
})();