forked from CQIITKGP/CQIITKGP.github.io
-
Notifications
You must be signed in to change notification settings - Fork 10
/
hover.js
158 lines (134 loc) · 4.48 KB
/
hover.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var hoverEffect = function(opts) {
var THREE = window.THREE;
var vertex = `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`;
var fragment = `
varying vec2 vUv;
uniform float dispFactor;
uniform sampler2D disp;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform float angle1;
uniform float angle2;
uniform float intensity1;
uniform float intensity2;
mat2 getRotM(float angle) {
float s = sin(angle);
float c = cos(angle);
return mat2(c, -s, s, c);
}
void main() {
vec4 disp = texture2D(disp, vUv);
vec2 dispVec = vec2(disp.r, disp.g);
vec2 distortedPosition1 = vUv + getRotM(angle1) * dispVec * intensity1 * dispFactor;
vec2 distortedPosition2 = vUv + getRotM(angle2) * dispVec * intensity2 * (1.0 - dispFactor);
vec4 _texture1 = texture2D(texture1, distortedPosition1);
vec4 _texture2 = texture2D(texture2, distortedPosition2);
gl_FragColor = mix(_texture1, _texture2, dispFactor);
}
`;
function firstDefined() {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] !== undefined) return arguments[i];
}
}
var parent = opts.parent;
var dispImage = opts.displacementImage;
var image1 = opts.image1;
var image2 = opts.image2;
var intensity1 = firstDefined(opts.intensity1, opts.intensity, 1);
var intensity2 = firstDefined(opts.intensity2, opts.intensity, 1);
var commonAngle = firstDefined(opts.angle, Math.PI / 4); // 45 degrees by default, so grayscale images work correctly
var angle1 = firstDefined(opts.angle1, commonAngle);
var angle2 = firstDefined(opts.angle2, -commonAngle*3);
var speedIn = firstDefined(opts.speedIn, opts.speed, 1.6);
var speedOut = firstDefined(opts.speedOut, opts.speed, 1.2);
var userHover = firstDefined(opts.hover, true);
var easing = firstDefined(opts.easing, Expo.easeOut);
if (!parent) {
console.warn('Parent missing');
return;
}
if (!(image1 && image2 && dispImage)) {
console.warn('One or more images are missing');
return;
}
var scene = new THREE.Scene();
var camera = new THREE.OrthographicCamera(
parent.offsetWidth / -2,
parent.offsetWidth / 2,
parent.offsetHeight / 2,
parent.offsetHeight / -2,
1,
1000
);
camera.position.z = 1;
var renderer = new THREE.WebGLRenderer({ antialias: false });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setClearColor(0xffffff, 0.0);
renderer.setSize(parent.offsetWidth, parent.offsetHeight);
parent.appendChild(renderer.domElement);
var render = function() {
// This will be called by the TextureLoader as well as TweenMax.
renderer.render(scene, camera);
};
var loader = new THREE.TextureLoader();
loader.crossOrigin = '';
var texture1 = loader.load(image1, render);
var texture2 = loader.load(image2, render);
var disp = loader.load(dispImage, render);
disp.wrapS = disp.wrapT = THREE.RepeatWrapping;
texture1.magFilter = texture2.magFilter = THREE.LinearFilter;
texture1.minFilter = texture2.minFilter = THREE.LinearFilter;
var mat = new THREE.ShaderMaterial({
uniforms: {
intensity1: { type: 'f', value: intensity1 },
intensity2: { type: 'f', value: intensity2 },
dispFactor: { type: 'f', value: 0.0 },
angle1: { type: 'f', value: angle1 },
angle2: { type: 'f', value: angle2 },
texture1: { type: 't', value: texture1 },
texture2: { type: 't', value: texture2 },
disp: { type: 't', value: disp },
},
vertexShader: vertex,
fragmentShader: fragment,
transparent: true,
opacity: 1.0,
});
var geometry = new THREE.PlaneBufferGeometry(parent.offsetWidth, parent.offsetHeight, 1);
var object = new THREE.Mesh(geometry, mat);
scene.add(object);
function transitionIn() {
TweenMax.to(mat.uniforms.dispFactor, speedIn, {
value: 1,
ease: easing,
onUpdate: render,
onComplete: render,
});
}
function transitionOut() {
TweenMax.to(mat.uniforms.dispFactor, speedOut, {
value: 0,
ease: easing,
onUpdate: render,
onComplete: render,
});
}
if (userHover) {
parent.addEventListener('mouseenter', transitionIn);
parent.addEventListener('touchstart', transitionIn);
parent.addEventListener('mouseleave', transitionOut);
parent.addEventListener('touchend', transitionOut);
}
window.addEventListener('resize', function(e) {
renderer.setSize(parent.offsetWidth, parent.offsetHeight);
});
this.next = transitionIn;
this.previous = transitionOut;
};