Skip to content

Instantly share code, notes, and snippets.

@thc282
Last active February 8, 2025 19:06
Show Gist options
  • Save thc282/affe429706550ddcdc6905199ac911d0 to your computer and use it in GitHub Desktop.
Save thc282/affe429706550ddcdc6905199ac911d0 to your computer and use it in GitHub Desktop.
All threeJS useful script

⚠⚠⚠⚠⚠this must be set when new a renderer object⚠⚠⚠⚠⚠
{preserveDrawingBuffer: true}

//⚠⚠⚠⚠⚠this must be set when new a renderer object⚠⚠⚠⚠⚠
//{preserveDrawingBuffer: true}
const renderer = new THREE.WebGLRenderer({preserveDrawingBuffer: true});
//set a id for getting the canvas
renderer.domElement.id = 'screenshot';

//to get canvas and turn to base64URL
const canvas = document.getElementById("screenshot");
const dataURL = canvas.toDataURL();
//console.log(dataURL);

//create a fake link to download
var a = document.createElement("a");
a.setAttribute("download", "screenshot");
a.setAttribute("href", dataURL);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
function disposeChild(mesh) {
if (mesh instanceof THREE.Mesh) {
if (mesh.geometry?.dispose) {
mesh.geometry.dispose(); //remove geometry
}
if (mesh.material?.dispose) {
mesh.material.dispose(); //remove material
}
if (mesh.material?.texture?.dispose) {
mesh.material.texture.dispose();
}
}
if (mesh instanceof THREE.Group) {
mesh.clear();
}
if (mesh instanceof THREE.Object3D) {
mesh.clear();
}
}
//change uv mapping
var inRadius = planetInfo.radius * 1.5; var outRadius = planetInfo.radius * 2;
const ringGeometry = new THREE.RingBufferGeometry(inRadius, outRadius, 100);
var pos = ringGeometry.attributes.position;
var v3 = new THREE.Vector3();
for (let i = 0; i < pos.count; i++){
v3.fromBufferAttribute(pos, i);
ringGeometry.attributes.uv.setXY(i, v3.length() < inRadius + 1 ? 0 : 1, 1);
}
window.addEventListener( 'resize', onWindowResize );
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment