Problem
Before buying a skin you want to see how it looks with a specific float and pattern, next to a knife and gloves. Typical viewers show an approximation: colours, wear and pattern placement differ from the game. The goal of this project is to show a skin in the browser the way CS2 itself draws it.
Solution
The main principle is to port, not approximate. A skin is built by the same algorithm the game uses: the CS2 skin compositor is ported to WebGL2 instead of being matched by eye. When the picture differs from the game, the cause is looked for in the original shader rather than tweaked by hand.
The whole loadout — weapon, knife, gloves, float, pattern, stickers, keychain — lives in the page address, so it can be shared as a link. Any item can be opened right in the game: the site builds a CS2 inspect link itself, and the item doesn't have to exist in anyone's inventory. There is no server: the site is static, and about 3.4 GB of models and textures are served from cloud storage.
The project is independent and not affiliated with Valve. Models and textures belong to Valve and are kept out of the repository.
Challenges
- Game shaders in the browser. The compositor shaders (
csgo_customweapon.vfxfor weapons,csgo_customglove.vfxfor gloves) are decompiled from the game files and translated automatically from Vulkan GLSL to WebGL2 GLSL by a custom translator. New incompatibilities are fixed in the translator, not by hand in its output. - WebGL limits. WebGL2 offers 16 texture slots and the compositor needs more, so textures are packed into arrays with mipmaps built by hand. UV islands of the models go outside 0..1, so the composite targets use repeat wrapping.
- Patterns that match the game. Pattern placement depends on the pattern seed. To match it, the game's random number generator (
CUniformRandomStream) is ported: every compositor parameter takes its number from it in the same order the game does. The result is checked against known Karambit Fade seeds. - Measuring fidelity. Internal pages
/auditand/comparedraw skins with our renderer, compare them with the official CS2 icons and rank them by colour difference, worst first. This is how porting errors are found. - First-person view. The weapon and hands sit on the game's animation skeleton: inspect, reload, switching hands. Stickers are applied by a patch of the weapon shader, and the keychain hangs from the model's attachment point.
- Offline pipeline. Data is pulled out of the game files by Python scripts: the item catalog, models, skin kits, stickers, map backdrops. Long exports take hours, and re-uploading to storage sends only the changed files.
Architecture
- Game filesmodels, textures, shaders, skin definitions
- Offline pipeline
model export, shader decompilation and translation, item catalog
- Asset storagestatic files, about 3.4 GB, kept apart from the code
- Compositor in the browser
the skin is built on the GPU from the parameters in the address
- Scenefirst-person view, inspect, map backdrop
Code samples
A port of the CUniformRandomStream random number generator. It decides where the pattern lands for a given seed, so it follows the original step by step:
export class ValveRandom {
private idum = 0
private iy = 0
private iv = new Array<number>(32).fill(0)
constructor(seed: number) {
this.idum = seed < 0 ? seed : -seed
this.iy = 0
}
private next(): number {
const IA = 16807, IM = 2147483647, IQ = 127773, IR = 2836, NTAB = 32
const NDIV = 1 + Math.floor((IM - 1) / NTAB)
let k: number
if (this.idum <= 0 || this.iy === 0) {
this.idum = -this.idum < 1 ? 1 : -this.idum
for (let j = NTAB + 7; j >= 0; j--) {
k = Math.floor(this.idum / IQ)
this.idum = IA * (this.idum - k * IQ) - IR * k
if (this.idum < 0) this.idum += IM
if (j < NTAB) this.iv[j] = this.idum
}
this.iy = this.iv[0]
}
k = Math.floor(this.idum / IQ)
this.idum = IA * (this.idum - k * IQ) - IR * k
if (this.idum < 0) this.idum += IM
const j = Math.floor(this.iy / NDIV)
this.iy = this.iv[j]
this.iv[j] = this.idum
return this.iy
}
/** RandomFloat(low, high) */
float(low: number, high: number): number {
const f = Math.min((1 / 2147483647) * this.next(), 1 - 1.2e-7)
return f * (high - low) + low
}
}The tail of a CS2 inspect link. The item description is packed into protobuf, and the game verifies a checksum — without it the link won't open:
/** 00 | block | checksum, upper-case hex. */
export function inspectHex(block: number[]): string {
const buffer = [0, ...block]
const crc = crc32(buffer)
// (crc & 0xffff) ^ (len * crc), kept to 32 bits; Math.imul wraps the product like the game does
const sum = ((crc & 0xffff) ^ Math.imul(block.length, crc)) >>> 0
const bytes = [...buffer, sum >>> 24, (sum >>> 16) & 0xff, (sum >>> 8) & 0xff, sum & 0xff]
return bytes.map((b) => b.toString(16).padStart(2, "0")).join("").toUpperCase()
}How to use it
- Open the website, pick a weapon, knife, gloves and agent, then set the float and pattern. The scene with the models takes 10–20 seconds to load.
- In first-person view, switch between the weapon, knife and gloves with keys 1, 3 and 6. F plays the inspect animation, R reloads, H switches hands.
- Turn on inspect and rotate the model with the mouse. You can add stickers and a keychain to the weapon and place a sticker exactly where you want it in the application mode, just like in the game. P saves a screenshot with the map backdrop.
- "Share combo" copies a link to the whole loadout, and "Open in CS2" opens the chosen item right in the game.