generated from mohnishlandge/threejs-template
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
164 lines (147 loc) · 3.17 KB
/
index.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
159
160
161
162
163
164
import './style/main.css'
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
/**
* GUI Controls
*/
import * as dat from 'dat.gui'
const gui = new dat.GUI()
/**
* Base
*/
// Canvas
const canvas = document.querySelector('canvas.webgl')
// Scene
const scene = new THREE.Scene()
/**
* Object
*/
const geometry = new THREE.IcosahedronGeometry(20, 1)
const material = new THREE.MeshNormalMaterial()
// Material Props.
material.wireframe = true
// Create Mesh & Add To Scene
const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)
/**
* Sizes
*/
const sizes = {
width: window.innerWidth,
height: window.innerHeight,
}
window.addEventListener('resize', () => {
// Update sizes
sizes.width = window.innerWidth
sizes.height = window.innerHeight
// Update camera
camera.aspect = sizes.width / sizes.height
camera.updateProjectionMatrix()
// Update renderer
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})
/**
* Camera
*/
// Base camera
const camera = new THREE.PerspectiveCamera(
75,
sizes.width / sizes.height,
0.001,
5000
)
camera.position.x = 1
camera.position.y = 1
camera.position.z = 40
scene.add(camera)
// Controls
const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true
controls.enabled = true
controls.autoRotate = true
// controls.enableZoom = false
controls.enablePan = false
controls.dampingFactor = 0.05
controls.maxDistance = 1000
controls.minDistance = 30
/**
* Renderer
*/
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
antialias: true,
alpha: true,
})
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
/**
* Animation GSAP
*/
gsap.registerPlugin(ScrollTrigger)
ScrollTrigger.defaults({
scrub: 3,
ease: 'none',
})
const sections = document.querySelectorAll('.section')
gsap.from(mesh.position, {
y: 1,
duration: 1,
ease: 'expo',
})
gsap.from('h1', {
yPercent: 100,
autoAlpha: 0,
ease: 'back',
delay: 0.3,
})
gsap.to(mesh.rotation, {
x: Math.PI * 2,
scrollTrigger: {
trigger: sections[1],
},
})
gsap.to(mesh.scale, {
x: 2,
y: 2,
scrollTrigger: {
trigger: sections[2],
},
})
gsap.to(mesh.rotation, {
y: Math.PI * 2,
scrollTrigger: {
trigger: sections[3],
},
})
/**
* Animate
*/
const clock = new THREE.Clock()
const tick = () => {
const elapsedTime = clock.getElapsedTime()
//mesh.rotation.x += 0.01 * Math.sin(1)
//mesh.rotation.y += 0.01 * Math.sin(1)
//mesh.rotation.z += 0.01 * Math.sin(1)
// Update controls
controls.update()
// Render
renderer.render(scene, camera)
// Call tick again on the next frame
window.requestAnimationFrame(tick)
}
/*------------------------------
MouseMove
------------------------------*/
function onMouseMove(e) {
const x = e.clientX
const y = e.clientY
gsap.to(scene.rotation, {
y: gsap.utils.mapRange(0, window.innerWidth, 1, -1, x),
x: gsap.utils.mapRange(0, window.innerHeight, 1, -1, y),
})
}
window.addEventListener('mousemove', onMouseMove)
tick()