JSFiddle - React, Tailwind, and code Playground
HTML
<div id="ui-layer">
<h1>Equi to Cube Converter</h1>
<div class="control-group">
<label for="fileInput" class="file-upload-label">
<span class="loader" id="loader"></span>Upload Equirectangular Image
</label>
<input type="file" id="fileInput" accept="image/*">
</div>
<div id="status">Waiting for image...</div>
<div style="margin-top:10px; font-size: 0.75rem; color: #666;">
Drag to rotate view • Scroll to zoom • WebGL2 Optimized
</div>
</div>
<canvas id="glcanvas"></canvas>
CSS
body {
margin: 0;
background: #121212;
color: #ffffff;
font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
user-select: none;
}
#ui-layer {
position: absolute;
top: 20px;
left: 20px;
z-index: 10;
background: rgba(0, 0, 0, 0.6);
backdrop-filter: blur(8px);
padding: 20px;
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
pointer-events: auto;
max-width: 300px;
box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}
h1 { font-size: 1.1rem; margin: 0 0 15px 0; font-weight: 600; letter-spacing: 0.5px; }
.control-group { margin-bottom: 15px; }
button, .file-upload-label {
display: inline-block;
background: #3b82f6;
color: white;
padding: 10px 16px;
border-radius: 6px;
cursor: pointer;
font-size: 0.9rem;
font-weight: 500;
border: none;
transition: background 0.2s;
text-align: center;
width: 100%;
box-sizing: border-box;
}
button:hover, .file-upload-label:hover { background: #2563eb; }
button:active { background: #1d4ed8; }
input[type="file"] { display: none; }
#status {
margin-top: 10px;
font-size: 0.85rem;
color: #aaa;
line-height: 1.4;
}
.status-ok { color: #4ade80 !important; }
.status-err { color: #f87171 !important; }
canvas {
display: block;
width: 100%;
...
JavaScript
/**
* GPU Equirectangular to Cubemap Converter
* Optimized with pre-baked vertex directions, removing fragment shader branching/array indexing.
*/
const canvas = document.getElementById('glcanvas');
const statusDiv = document.getElementById('status');
const fileInput = document.getElementById('fileInput');
const loader = document.getElementById('loader');
// --------------------------------------------------------
// Context Setup & Feature Detection
// --------------------------------------------------------
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Prioritize WebGL2
const gl = canvas.getContext('webgl2', { antialias: true, alpha: false })
|| canvas.getContext('webgl', { antialias: true, alpha: false });
const isWebGL2 = !!gl && gl instanceof WebGL2RenderingContext;
if (!gl) {
statusDiv.innerHTML = "<span class='status-err'>WebGL not supported in this browser.</span>";
throw new Error("WebGL not supported");
}
console.log(`Context: ${isWebGL2 ? 'WebGL 2.0' : 'WebGL 1.0'}`);
// --------------------------------------------------------
// Shader Sources
// --------------------------------------------------------
// 1. Conversion Shaders (Equirect -> 3x2 Grid)
const vsConvSource = isWebGL2 ? `#version 300 es
in vec2 a_position;
in vec3 a_direction;
out vec3 v_dir;
void main() {
v_dir = a_direction;
gl_Position = vec4(a_position, 0.0, 1.0);
}` : `#version 100
attribute vec2 a_position;
attribute vec3 a_direction;
varying vec3 v_dir;
void main() {
v_dir = a_direction;
gl_Position = vec4(a_position, 0.0, 1.0);
}`;
const fsConvSource = isWebGL2 ? `#version 300 es
precision highp float;
in vec3 v_dir;
uniform sampler2D u_equirect;
out vec4 outColor;
#define PI 3.14159265359
void main() {
vec3 dir = normalize(v_dir);
float phi = atan(dir.z, dir.x);
float theta = asin(dir.y);
vec2 equirectUV = vec2(0.5 + phi / (2.0...