Skip to content

Instantly share code, notes, and snippets.

@JacobMJones
Last active November 3, 2021 16:20
Show Gist options
  • Save JacobMJones/f44b0c539f7affd6ee97a894d46699f9 to your computer and use it in GitHub Desktop.
Save JacobMJones/f44b0c539f7affd6ee97a894d46699f9 to your computer and use it in GitHub Desktop.
3js Psychedelic rotating marble
//For three.js, based off of https://github.com/PierfrancescoSoffritti/pierfrancescosoffritti.com/blob/master/src/components/home/header/threejs/SceneSubject.js
import * as THREE from 'three'
import alphaTexture from './stripe6.jpg';
export default scene => {
const group = new THREE.Group();
const subjectGeometry = deformGeometry(new THREE.IcosahedronGeometry(7, 5));
const subjectMaterial = new THREE.MeshStandardMaterial({ color: "red", transparent: true, side: THREE.DoubleSide, alphaTest: 0.1 });
subjectMaterial.alphaMap = new THREE.TextureLoader().load(alphaTexture);
subjectMaterial.alphaMap.magFilter = THREE.NearestFilter;
subjectMaterial.alphaMap.wrapT = THREE.RepeatWrapping;
subjectMaterial.alphaMap.repeat.y = 5;
const subjectMesh = new THREE.Mesh(subjectGeometry, subjectMaterial);
group.add(subjectMesh);
// group.add(subjectWireframe);
scene.add(group);
group.rotation.z = Math.PI/2;
const speed = 0.3;
const textureOffsetSpeed = .5;
function deformGeometry(geometry) {
for (let i=0; i<geometry.vertices.length; i+=8) {
const scalar = 1 - Math.random() * .2;
geometry.vertices[i].multiplyScalar(scalar)
}
return geometry;
}
function update(time) {
const angle = time*speed;
group.rotation.y = angle;
subjectMaterial.alphaMap.offset.y = 0.55 + time * textureOffsetSpeed;
// subjectWireframe.material.color.setHSL( Math.sin(angle*2), 0.5, 0.5 );
const scale = (Math.sin(angle*8)+6.4)/5;
// subjectWireframe.scale.set(scale, scale, scale)
}
return {
update
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment