class Sphere {
constructor() {
this.wireframe = false;
this.defaultAnimationSpeed =1;
this.morphAnimationSpeed = 2;
this.SCREEN_WIDTH = window.innerWidth;
this.SCREEN_HEIGHT = window.innerHeight;
this.mouseX = 0;
this.mouseY = 0;
this.deltaX = 0;
this.deltaY = 0;
this.scrollDepht=0
this.windowHalfX = window.innerWidth / 2;
this.windowHalfY = window.innerHeight / 2;
this.normalSpeed = (this.defaultAnimationSpeed / 100);
this.fullSpeed = (this.morphAnimationSpeed / 100);
this.windowHalfX = window.innerWidth / 2;
this.windowHalfY = window.innerHeight / 2;
this.animationVars = {
speed: this.normalSpeed
};
this.SEPARATION = 200;
this.AMOUNTX = 10;
this.AMOUNTY = 10;
this.camera = {};
this.camera2 = {};
this.scene = new THREE.Scene();
this.scene2 = new THREE.Scene();
this.renderer = {};
this.sceneIndex = 1;
this.cameraIndex = 1;
this.camPositionIdx = 1;
this.numberOfParticles = 2000;
this.particleImage = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/605067/particle-tiny.png';
this.particleColor = '0xFFFFFF';
this.particleSize = 20;
this.OBJ_PARAMS= {
'CartoonRocket': {'scale': 33},
'echo': {'scale': 20},
'hat': {'scale': 80},
'gear': {'scale': 20},
'Lightbulb': {'scale': 50},
'kibel': {'scale': 70}
}
this.renderer = new THREE.WebGLRenderer({alpha: true, transparent: true});
this.renderer.setClearColor(0xFFF000, 0);
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(this.SCREEN_WIDTH, this.SCREEN_HEIGHT);
}
init() {
var container, clock = new THREE.Clock();
this.tick = 0;
this.clock = clock
var that = this;
var hero = DOM.svg(600, 600);
var container = document.createElement("div");
this.GPUParticleSystem = null
container.className = "hero__three-container";
hero.appendChild(container);
this.camera = new THREE.PerspectiveCamera(45, this.SCREEN_WIDTH / this.SCREEN_HEIGHT, 100, 10000);
this.camera.position.z = 1000;
//this.GPUParticleSystem = new THREE.GPUParticleSystem( {
// maxParticles: 250000
// });
//this.scene2.add(GPUParticleSystem)
container.appendChild(this.renderer.domElement);
this.container = container;
this.optGPU = {
position: new THREE.Vector3(),
positionRandomness: .3,
velocity: new THREE.Vector3(),
velocityRandomness: .5,
color: 0xaa88ff,
colorRandomness: .2,
turbulence: .5,
lifetime: 2,
size: 5,
sizeRandomness: 1
};
this.spawnerOptions = {
spawnRate: 15000,
horizontalSpeed: 1.5,
verticalSpeed: 1.33,
timeScale: 1
};
this.lines = this.generateLines();
// lines
//this.scene.add(this.lines)
this.scene2.add(this.lines)
// document.addEventListener('mousemove', onDocumentMouseMove.bind(this), false);
// this.triggers[3].addEventListener("click", wireframe.bind(this), false);
window.addEventListener('resize', onWindowResize.bind(this), false);
function onDocumentMouseMove(event) {
this.mouseX = event.clientX - this.windowHalfX;
this.mouseY = event.clientY - this.windowHalfY;
}
function onWindowResize() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
function wireframe(event) {
if (!this.wireframe) {
let wgeometry = new THREE.PlaneGeometry(1000, 1000, 100, 100);
let wmaterial = new THREE.MeshBasicMaterial({color: 0xFFFFFF, wireframe: true, wireframeLinewidth: 1});
let wireplane = new THREE.Mesh(wgeometry, wmaterial);
wireplane.name = "wireplane"
wireplane.scale.set(1, 1, 1);
wireplane.rotation.x = -Math.PI / 2;
this.scene2.add(wireplane);
var helper = new THREE.CameraHelper( 2 );
this.scene2.add( helper );
} else {
this.wireframe = true
let plane = this.scene1.getObjectByName("wireplane");
let plane2 = this.scene2.getObjectByName("wireplane");
this.scene2.remove(plane2);
this.scene.remove(plane);
}
}
return hero
}
generateLines(){
var lines = new THREE.Group();
lines.name = "lines";
for (var i = 0; i < 200; i++) {
let geometry = new THREE.Geometry();
let vertex = new THREE.Vector3(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1);
vertex.normalize();
vertex.multiplyScalar(450);
geometry.vertices.push(vertex);
let vertex2 = vertex.clone();
vertex2.multiplyScalar(Math.random() * 0.25 + 1);
geometry.vertices.push(vertex2);
let line = new THREE.Line(geometry, new THREE.LineBasicMaterial({color: 0x353535, opacity: Math.random()}));
lines.add(line);
}
return lines
}
initMorph(scene, camera, renderer) {
var that = this;
this.particleCount = this.numberOfParticles;
//$("#skills").on("click", this.toObject.bind(that))
function animateVector3(vectorToAnimate, target, options){
options = options || {};
// get targets from options or set to defaults
var to = target || THREE.Vector3(),
easing = options.easing || TWEEN.Easing.Quadratic.In,
duration = options.duration || 2000;
// create the tween
var tweenVector3 = new TWEEN.Tween(vectorToAnimate)
.to({ x: to.x, y: to.y, z: to.z, }, duration)
.easing(easing)
.onUpdate(function(d) {
if(options.update){
options.update(d);
}
})
.onComplete(function(){
if(options.callback) options.callback();
});
// start the tween
tweenVector3.start();
// return the tween in case we want to manipulate it later on
return tweenVector3;
}
window.addEventListener('resize', this.fullScreen.bind(this), false)
// Camera and position
this.camera2 = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 100, 10000);
this.camera2.position.z = 800;
var light = new THREE.AmbientLight(0xffffff, 1); // soft white light
this.scene2.add(light);
//this.renderer.setClearColor(0xfff#
//document.addEventListener("mousewheel", MouseWheelHandler.bind(this), false);
// function MouseWheelHandler(event) {
// console.log( event.deltaY);}
this.particlesStart = new THREE.Geometry();
this.sphereParticles = new THREE.Geometry();
this.cubeParticles = new THREE.Geometry();
this.objParticles = new THREE.Geometry();
this.objPoints = null;
this.hatParticles = new THREE.Geometry();
this.hatPoints = null;
this.echoParticle = new THREE.Geometry();
this.echoPoints = null;
this.gearParticles = new THREE.Geometry();
this.gearPoints = null;
this.astroParticles = new THREE.Geometry();
this.astroPoints = null;
this.bulbParticles = new THREE.Geometry();
this.bulbPoints = null;
var customMaterial = new THREE.ShaderMaterial(
{
uniforms:
{
"c": { type: "f", value: -.3 },
"p": { type: "f", value: 3.4 },
glowColor: { type: "c", value: new THREE.Color(0xafafaf) },
viewVector: { type: "v3", value: camera.position }
},
side: THREE.BackSide,
alphaTest:0.5,
blending: THREE.AdditiveBlending,
transparent: true,
} );
this.loadObject(this.hatParticles, this.hatPoints, "hat");
//this.loadObject(this.objParticles, this.objPoints, "kibel");
//this.loadObject(this.echoParticle, this.echoPoints, "echo");
//this.loadObject(this.gearParticles, this.gearPoints, "gear");to
//this.loadObject(this.astroParticles, this.astroPoints, "CartoonRocket", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/605067/");
this.loadObject(this.bulbParticles, this.bulbPoints, "Lightbulb");
//Custom objects
var sphereGeometry = new THREE.SphereGeometry(5, 30, 32);
let boxGeometry = new THREE.BoxGeometry(9, 9, 9);
for (var p = 0; p < this.numberOfParticles; p++) {
var vertex = new THREE.Vector3();
vertex.x = Math.random() * 2 - 1;
vertex.y = Math.random() * 2 - 1;
vertex.z = Math.random() * 2 - 1;
vertex.normalize();
// vertex.multiplyScalar(-Math.abs(Math.random() * 1));
vertex.multiplyScalar(40 * Math.random() + 100);
this.particlesStart.vertices.push(vertex);
}
this.spherePoints = THREE.GeometryUtils.randomPointsInGeometry(sphereGeometry, this.particleCount)
// this.spherePoints.map(function(el){
// let factor = 2* (2* Math.random() -1);
//
// el.addScalar(factor)
//
// return el
//
// })
this.cubePoints = THREE.GeometryUtils.randomPointsInGeometry(boxGeometry, this.particleCount);
// Particles
this.burstParticles = this.particlesStart.clone()
this.createVertices(this.sphereParticles, this.spherePoints, null, null);
this.createVertices(this.cubeParticles, this.cubePoints, null, 1);
this.pMaterial = new THREE.PointsMaterial({
// color: 0xFFFFFF,
//vertexColors: THREE.VertexColors,
size: this.particleSize,
alphaTest: 0.5,
//depthTest: false,
opacity: 0.7,
map: generateSprite(),//THREE.ImageUtils.loadTexture('./images/dot.png'),
blending: THREE.AdditiveBlending,
transparent: true
});
//this.triggers[0].addEventListener('click', this.toSphere.bind(this))
//this.triggers[1].addEventListener('click', this.toCube.bind(this))
$("#education").on("click", this.toHat.bind(that))
$("#skills").on("click", this.toBulb.bind(that))
this.particleSystem = new THREE.Points(
this.particlesStart,
this.pMaterial
);
this.particleSystem.sortParticles = true;
// Add the particles to the scene
//console.log(this.particleSystem)
//this.scene.add(this.particleSystem);
this.scene2.add(this.particleSystem);
this.moonGlow = new THREE.Mesh( sphereGeometry.clone(), customMaterial.clone() );
//this.moonGlow.position = moon.position;
this.moonGlow.scale.multiplyScalar(55);
this.scene2.add( this.moonGlow );
// Animate
this.sceneIndex = 2
//this.animate();
//etTimeout(this.toBurst(), 1500);
this.toBurst()
//this.tweenScale(this.particleSystem.scale, 4, {x: 5.5, y: 5.5, z: 5.5});
function generateSprite() {
let canvas = document.createElement('canvas');
canvas.width = 16;
canvas.height = 16;
let context = canvas.getContext('2d');
var gradient = context.createRadialGradient(canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2);
let t =1// Math.round(1.5-Math.random() * 100) / 100;
let clr='rgba(255,255,255,'+t+')'
gradient.addColorStop(0, clr);
gradient.addColorStop(1, 'transparent');
// gradient.addColorStop(0.2, 'rgba(0,255,255,1)');
//gradient.addColorStop(0.4, 'rgba(0,0,64,1)');
// gradient.addColorStop(1, 'rgba(0,0,0,1)');
context.fillStyle = gradient;
context.fillRect(0, 0, canvas.width, canvas.height);
var texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
return texture;
}
}
changeOpacity(objects, opacity = 1, duration = 1) {
var that = this
objects.children.map(function (e) {
TweenMax.to(e.material, duration, {
ease: TWEEN.Power4.easeIn, speed: that.fullSpeed, onComplete: that.slowDown.bind(that),
opacity: opacity
})
})
}
morphTo(newParticles, color = '0x000000') {
let that = this;
TweenMax.to(this.animationVars, .4, {
ease: TWEEN.Power4.easeIn, speed: this.fullSpeed, onComplete: that.slowDown.bind(that)
});
// this.particleSystem.material.color.setHex(color);
for (var i = 0; i < this.particlesStart.vertices.length; i++) {
TweenMax.to(this.particlesStart.vertices[i], 2, {
ease: TweenMax.Elastic.easeOut.config(1, 0.75),
x: newParticles.vertices[i].x,
y: newParticles.vertices[i].y,
z: newParticles.vertices[i].z
})
}
}
tweenScale(duration = 1, toValue = {x: 2, y: 2, z: 2}) {
TweenMax.to(this.particleSystem.scale, duration, toValue,
{
ease: TweenMax.Elastic.easeOut.config(1, 0.45),
});
}
createVertices(emptyArray, points, yOffset = 0, trigger = null) {
// console.log(this.particleCount, points, yOffset)
for (var p = 0; p < this.particleCount; p++) {
var vertex = new THREE.Vector3();
vertex.x = points[p]['x'];
vertex.y = points[p]['y'] - yOffset;
vertex.z = points[p]['z'];
emptyArray.vertices.push(vertex);
}
return emptyArray;
}
loadObject(objectParticles, objectPoints, name = "fu", path = "./js/3d/",callback=undefined) {
const codepenAssetUrl = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/605067/";
var that = this;
var objLoader = new THREE.OBJLoader();
objLoader.setPath(path ? path : codepenAssetUrl)
objLoader.load(name + '.obj', function (object) {
// var mesh = new THREE.Mesh( object, new THREE.MeshBasicMaterial( { color: 0xffff00 ,transparent:true} ) );
//
//that.scene2.add(mesh)
object.traverse(function (child) {
if (child instanceof THREE.Mesh) {
let scale = that.OBJ_PARAMS[name].scale;
// console.log(child)
let area = new THREE.Box3();
area.setFromObject(child);
let yOffset = (area.max.y * scale) / 2;
child.geometry.scale(scale, scale, scale);
objectPoints = THREE.GeometryUtils.randomPointsInBufferGeometry(child.geometry, that.particleCount);
that.createVertices(objectParticles, objectPoints, yOffset, 2);
}
//else if (child){
// console.log(child)
//}
});
if (callback){
callback();
}
});
}
slowDown() {
TweenMax.to(this.animationVars, 4, {
ease: TweenMax.Power2.easeOut, speed: this.normalSpeed, delay: 1
});
}
toEcho(e) {
hideResume()
this.changeOpacity(this.lines, 0, 1)
console.log(e)
this.sceneIndex = 2
this.currentAnmiation = "object";
console.log("toEcho");
this.morphTo(this.echoParticle);
this.tweenScale(0.01);
}
removeLines() {
var o = this.scene2.getObjectByName('lines');
this.scene1.remove(o);
this.scene2.remove(o);
}
moveScene(scene, duration=1.5, position = {x: 0, y: 0, z: 0}) {
TweenMax.to(scene.position, duration, position,
{
ease: TweenMax.Elastic.easeOut.config(1, 0.75),
x: position.x,
y: position.y,
z: position.z
});
}
moveParticleSystem(duration=1.5, position = {x: 0, y: 0, z: 0}) {
TweenMax.to(this.particleSystem.position, duration, position,
{
ease: TweenMax.Elastic.easeOut.config(1, 0.75),
x: position.x,
y: position.y,
z: position.z
});
}
animate() {
this.particlesStart.verticesNeedUpdate = true;
requestAnimationFrame(this.animate.bind(this));
/*
var delta = this.clock.getDelta() * spawnerOptions.timeScale;
this.tick += delta;
if ( this.tick < 0 ) this.tick = 0;
if ( delta > 0 ) {
this.optGPU.position.x = Math.sin( this.tick * this.spawnerOptions.horizontalSpeed ) * 20;
this.optGPU.position.y = Math.sin( this.tick * this.spawnerOptions.verticalSpeed ) * 10;
this.optGPU.position.z = Math.sin( this.tick * this.spawnerOptions.horizontalSpeed + spawnerOptions.verticalSpeed ) * 5;
for ( var x = 0; x < this.spawnerOptions.spawnRate * delta; x++ ) {
// Yep, that's really it. Spawning particles is super cheap, and once you spawn them, the rest of
// their lifecycle is handled entirely on the GPU, driven by a time uniform updated below
GPUParicleSystem.spawnParticle( options );
}
}
this.GPU.update( tick );
*/
this.render();
}
fullScreen() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
render() {
// console.log("mouseY :" + this.mouseY + " mouseX: " + this.mouseX)
var currentScene = null;
var currentCamera = null;
function switchCall(index, func1, func2) {
switch (index) {
case 1:
func1()
break;
case 2:
func2()
break;
}
}
switchCall(this.sceneIndex,
() => {
currentScene = this.scene;
}, () => {
//this.particleSystem.rotation.x += 0.001
this.particleSystem.rotation.y += 0.001
this.lines.rotation.y += 0.001
//this.particleSystem.rotation.z += 0.001
currentScene = this.scene2;
})
switchCall(this.cameraIndex,
() => {
// this.camera.position.x += ( this.mouseX + 200 - this.camera.position.x ) * .05;
//this.camera.position.y = this.screenDepth* .05;
//this.camera.lookAt(this.scene.position);
switchCall(this.camPositionIdx,
() => {
// this.camera.lookAt(this.scene.position);
}, () => {
// this.camera.lookAt(this.scene.position);
})
currentCamera = this.camera;
}, () => {
currentScene = this.scene2;
//this.camera2.position.x += ( this.mouseX + 0 - this.camera2.position.x ) * 0.05;
//this.camera2.position.y += ( -this.mouseY + 500 - this.camera2.position.y ) * 0.05;
//this.camera2.lookAt(this.scene2.position);
switchCall(this.camPositionIdx,
() => {
this.camera.lookAt(this.scene2.position);
}, () => {
// this.camera.lookAt(this.scene2.position);
})
//this.renderer.setClearColor(0x000000, 0);
currentCamera = this.camera2.clone();
})
this.renderer.render(currentScene, currentCamera);
}
toHat(e) {
hideResume();
fadeInEdu()
this.moveScene(this.scene2,1.5 , {x: -229, y: 70, z: -211})
this.moveParticleSystem()
this.changeOpacity(this.lines, 0.5, 1)
console.log(e);
this.tweenScale(2);
this.sceneIndex = 2
this.currentAnmiation = "object";
console.log("toObject");
this.morphTo(this.hatParticles);
}
toSphere() {
this.moveScene(this.scene2, 3)
this.moveParticleSystem()
this.tweenScale(3, {x: 70, y: 70, z: 70})
this.morphTo(this.sphereParticles);
}
toGear() {
this.loadObject(this.gearParticles, this.gearPoints, "gear","./js/3d/",transform.bind(this));
function transform () {
hideResume()
this.changeOpacity(this.lines, 0, 1)
console.log()
this.sceneIndex = 2;
this.tweenScale(3, {x: .80, y: .80, z: .80})
this.morphTo(this.gearParticles);
}
}
toAstro() {
this.moveScene(this.scene2, 1.5, {x: 269, y: 90, z: -111})
this.moveParticleSystem()
hideResume()
fadeInSkills()
this.changeOpacity(this.lines, 1, 1)
console.log("astro")
this.sceneIndex = 2
this.tweenScale(1.5, {x: 1, y: 1, z: 1})
this.morphTo(this.astroParticles);
}
toBulb() {
this.moveScene(this.scene2, 1.5, {x: 269, y: 90, z: -111})
this.moveParticleSystem(1.5,{x:14,y:170,z:-150})
hideResume()
fadeInSkills()
this.changeOpacity(this.lines, 1, 1)
console.log("bulb")
this.sceneIndex = 2
this.tweenScale(2.5, {x: 2.5, y: 2.5, z: 2.5})
this.morphTo(this.bulbParticles);
}
toBurst() {
showResume()
hideTextBoxes()
this.moveParticleSystem(1.5)
this.moveScene(this.scene2, 1)
this.sceneIndex = 2
this.changeOpacity(this.lines, 1, 1)
TweenMax.to(this.particleSystem.scale, 1.5, {x: 3.5, y: 3.5, z: 3.5}, {
ease: TweenMax.Elastic.easeOut.config(1, 0.75),
});
this.morphTo(this.burstParticles);
}
toCube() {
this.sceneIndex = 2;
this.changeOpacity(this.lines, 0, 1)
TweenMax.to(this.particleSystem.scale, 1, {x: 30, y: 30, z: 30}, {
ease: TweenMax.Elastic.easeOut.config(1, 0.75),
});
this.morphTo(this.cubeParticles);
}
}