Created
December 16, 2020 19:15
-
-
Save llirikkcoder/07210c581a38950a716e8d50818fe2da to your computer and use it in GitHub Desktop.
WebGL Point Sphere - GSAP 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var scene = new THREE.Scene(); | |
document.addEventListener( 'mousemove', onMouseMove, false ); | |
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); | |
var mouseX; | |
var mouseY; | |
var renderer = new THREE.WebGLRenderer(); | |
renderer.setSize( window.innerWidth, window.innerHeight ); | |
document.body.appendChild( renderer.domElement ); | |
window.addEventListener("resize", function() { | |
camera.aspect = window.innerWidth / window.innerHeight; | |
camera.updateProjectionMatrix(); | |
renderer.setSize( window.innerWidth, window.innerHeight ); | |
}); | |
var distance = Math.min(200, window.innerWidth / 4); | |
var geometry = new THREE.Geometry(); | |
for (var i = 0; i < 1600; i++) { | |
var vertex = new THREE.Vector3(); | |
var theta = THREE.Math.randFloatSpread(360); | |
var phi = THREE.Math.randFloatSpread(360); | |
vertex.x = distance * Math.sin(theta) * Math.cos(phi); | |
vertex.y = distance * Math.sin(theta) * Math.sin(phi); | |
vertex.z = distance * Math.cos(theta); | |
geometry.vertices.push(vertex); | |
} | |
var particles = new THREE.Points(geometry, new THREE.PointsMaterial({color: 0xff44ff, size: 2})); | |
particles.boundingSphere = 50; | |
var renderingParent = new THREE.Group(); | |
renderingParent.add(particles); | |
var resizeContainer = new THREE.Group(); | |
resizeContainer.add(renderingParent); | |
scene.add(resizeContainer); | |
camera.position.z = 400; | |
var animate = function () { | |
requestAnimationFrame( animate ); | |
renderer.render( scene, camera ); | |
}; | |
var myTween; | |
function onMouseMove(event) { | |
if(myTween) | |
myTween.kill(); | |
mouseX = ( event.clientX / window.innerWidth ) * 2 - 1; | |
mouseY = - ( event.clientY / window.innerHeight ) * 2 + 1; | |
myTween = gsap.to(particles.rotation, {duration: 0.1, x: mouseY*-1, y: mouseX}); | |
//particles.rotation.x = mouseY*-1; | |
//particles.rotation.y = mouseX; | |
} | |
animate(); | |
// Scaling animation | |
var animProps = {scale: 1, xRot: 0, yRot: 0}; | |
gsap.to(animProps, {duration: 10, scale: 1.3, repeat: -1, yoyo: true, ease: "sine", onUpdate: function() { | |
renderingParent.scale.set(animProps.scale,animProps.scale,animProps.scale); | |
}}); | |
gsap.to(animProps, {duration: 120, xRot: Math.PI * 2, yRot: Math.PI * 4, repeat: -1, yoyo: true, ease: "none", onUpdate: function() { | |
renderingParent.rotation.set(animProps.xRot,animProps.yRot,0); | |
}}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/108/three.min.js"></script> | |
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/gsap-latest-beta.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { margin: 0; } | |
canvas { width: 100%; height: 100% } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment