summaryrefslogtreecommitdiff
path: root/site/assets/js/app/face.js
blob: 1818e9aa954e62081fd1604645e76e915f63aec4 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* eslint-disable */
var faceInit = function () {
  var container = document.querySelector("#face_container")
  var camera, controls, scene, renderer
  var mouse = new THREE.Vector2(0.5, 0.5)
  var mouseTarget = new THREE.Vector2(0.5, 0.5)
  var POINT_SCALE = 1.8
  var FACE_POINT_COUNT = 68
  var SWAP_TIME = 500
  var cubes = [], meshes = []
  var currentFace = document.querySelector('.currentFace')
  var faceBuffer = (function () {
    var a = new Array(FACE_POINT_COUNT)
    for (let i = 0; i < FACE_POINT_COUNT; i++) {
      a[i] = new THREE.Vector3()
    }
    return a
  })()
  var last_t = 0, start_t = 0
  var bgColor = 0x000000 // 0x191919
  var colors = [
    0xff3333,
    0xff8833,
    0xffff33,
    0x338833,
    0x3388ff,
    0x3333ff,
    0x8833ff,
    0xff3388,
    0xffffff,
  ]
  var swapping = false, swap_count = 0, swapFrom, swapTo, face_names, faces
  init()

  function init() {
    fetch("/assets/data/3dlm_0_10.json")
      .then(req => req.json())
      .then(data => {
        face_names = Object.keys(data)
        faces = face_names.map(name => recenter(data[name]))
        setup()
        build(faces[0])
        updateFace(faces[0])
        setCurrentFace(face_names[0])
        swapTo = faces[0]
        animate()
      })
  }
  function setup() {
    var w = window.innerWidth * 2/3
    var h = Math.min(window.innerWidth / 2, window.innerHeight * 0.7)
    camera = new THREE.PerspectiveCamera(70, w/h, 1, 10000)
    camera.position.x = 0
    camera.position.y = 0
    camera.position.z = 200

    scene = new THREE.Scene()
    // scene.background = new THREE.Color(bgColor)

    renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })
    renderer.setPixelRatio(window.devicePixelRatio)
    renderer.setSize(w, h)
    renderer.setClearColor(0x000000, 0);
    container.appendChild(renderer.domElement)
    document.body.addEventListener('mousemove', onMouseMove)
    // renderer.domElement.addEventListener('mousedown', swap)
    // oktween.add({
    //   obj: el.style,
    //   units: "px",
    //   from: { left: 0 },
    //   to: { left: 100 },
    //   duration: 1000,
    //   easing: oktween.easing.circ_out,
    //   update: function(obj){
    //     console.log(obj.left)
    //   }
    //   finished: function(){
    //     console.log("done")
    //   }
    // })
    swap()
  }
  function build(points) {
    var matrix = new THREE.Matrix4()
    var quaternion = new THREE.Quaternion()

    for (var i = 0; i < FACE_POINT_COUNT; i++) {
      var p = points[i]
      var geometry = new THREE.BoxBufferGeometry()
      var position = new THREE.Vector3(p[0], p[1], p[2])
      var rotation = new THREE.Euler()
      var scale = new THREE.Vector3()
      var color = new THREE.Color()
      scale.x = scale.y = scale.z = POINT_SCALE
      quaternion.setFromEuler(rotation, false)
      matrix.compose(position, quaternion, scale)
      geometry.applyMatrix(matrix)
      material = new THREE.MeshBasicMaterial({ color: color.setHex(0xffffff) })
      cube = new THREE.Mesh(geometry, material)
      scene.add(cube)
      cubes.push(cube)
    }

    meshes = getLineGeometry(points).map((geometry, i) => {
      var color = new THREE.Color()
      var material = new MeshLineMaterial({
        color: color.setHex(colors[i % colors.length]),
      })
      var line = new MeshLine()
      line.setGeometry(geometry, _ => 1.5)
      var mesh = new THREE.Mesh(line.geometry, material)
      mesh.geometry.dynamic = true
      scene.add(mesh)
      return [line, mesh]
    })
  }
  function lerpPoints(n, A, B, C) {
    for (let i = 0, len = A.length; i < len; i++) {
      lerpPoint(n, A[i], B[i], C[i])
    }
  }
  function lerpPoint(n, A, B, C) {
    C.x = lerp(n, A.x, B.x)
    C.y = lerp(n, A.y, B.y)
    C.z = lerp(n, A.z, B.z)
  }
  function lerp(n, a, b) {
    return (b-a) * n + a
  }
  function swap(){
    swap_count = (swap_count + 1) % faces.length
    swapFrom = swapTo || faces[0]
    swapTo = faces[swap_count]
    setCurrentFace(face_names[swap_count])
    oktween.add({
      from: { n: 0 },
      to: { n: 1 },
      duration: 1000,
      easing: oktween.easing.quad_in_out,
      update: function(obj){
        lerpPoints(obj.n, swapFrom, swapTo, faceBuffer)
        updateFace(faceBuffer)
      },
      finished: function(){
        setTimeout(swap, 2000)
      }
    })
  }
  function setCurrentFace(name) {
    name = name.replace('.png', '').split('_').filter(s => !s.match(/\d+/)).join(' ')
    currentFace.innerHTML = name
  }
  function updateFace(points) {
    updateCubeGeometry(points)
    updateLineGeometry(points)
  }
  function updateCubeGeometry(points) {
    cubes.forEach((cube, i) => {
      const p = points[i]
      cube.position.set(p.x, p.y, p.z)
    })
  }
  function updateLineGeometry(points) {
    getLineGeometry(points).map((geometry, i) => {
      var [line, mesh] = meshes[i]
      line.setGeometry(geometry, _ => 1.5)
      mesh.geometry.vertices = line.geometry.vertices
      mesh.geometry.verticesNeedUpdate = true
    })
  }
  function getLineGeometry(points) {
    return [
      points.slice(0, 17),
      points.slice(17, 22),
      points.slice(22, 27),
      points.slice(27, 31),
      points.slice(31, 36),
      points.slice(36, 42),
      points.slice(42, 48),
      points.slice(48)
    ].map((a, i) => {
      var geometry = new THREE.Geometry()
      a.forEach(p => geometry.vertices.push(p))
      if (i > 4) {
        geometry.vertices.push(a[0])
      }
      return geometry
    })
  }
  function getBounds(obj) {
    return obj.reduce((a, p) => {
      return [
        Math.min(a[0], p[0]),
        Math.max(a[1], p[0]),
        Math.min(a[2], p[1]),
        Math.max(a[3], p[1]),
        Math.min(a[4], p[2]),
        Math.max(a[5], p[2]),
      ]
    }, [Infinity, -Infinity, Infinity, -Infinity, Infinity, -Infinity])
  }
  function recenter(obj) {
    const bounds = getBounds(obj)
    const x_width = (bounds[1] - bounds[0]) / 2
    const y_width = (bounds[3] - bounds[2]) / -3
    const z_width = (bounds[5] - bounds[4]) / 2
    return obj.map(p => {
      p[0] = p[0] - bounds[0] - x_width
      p[1] = -p[1] + bounds[1] + y_width
      p[2] = p[2] - bounds[2] + z_width
      return new THREE.Vector3(p[0], p[1], p[2])
    })
  }
  //
  function onMouseMove(e) {
    mouse.x = e.clientX / window.innerWidth
    mouse.y = e.clientY / window.innerHeight
  }
  function animate(t) {
    requestAnimationFrame(animate)
    renderer.render(scene, camera)
    // scene.rotation.y += 0.01 * Math.PI
    mouseTarget.x += (mouse.x - mouseTarget.x) * 0.1
    mouseTarget.y += (mouse.y - mouseTarget.y) * 0.1
    scene.rotation.x = (mouseTarget.y - 0.5) * Math.PI / 2
    scene.rotation.y = (mouseTarget.x - 0.5) * Math.PI
    // scene.rotation.y += 0.01
    last_t = t
  }
}
faceInit()