wco_models 3.5.0.6 → 3.5.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/stylesheets/wco/main.scss +7 -3
  3. data/app/assets/stylesheets/wco/photos.scss +2 -1
  4. data/app/controllers/wco/newsvideos_controller.rb +18 -16
  5. data/app/controllers/wco/office_action_templates_controller.rb +3 -0
  6. data/app/controllers/wco/publishers_controller.rb +6 -16
  7. data/app/controllers/wco/reports_controller.rb +6 -5
  8. data/app/controllers/wco/sitemap_paths_controller.rb +14 -2
  9. data/app/controllers/wco/sites_controller.rb +1 -1
  10. data/app/controllers/wco/tags_controller.rb +1 -1
  11. data/app/jobs/wco/newspartial_speech_job.rb +12 -0
  12. data/app/jobs/wco/{newsvideo_generate_job.rb → newsvideo_combine_job.rb} +2 -2
  13. data/app/models/iro/option.rb +37 -4
  14. data/app/models/iro/priceitem.rb +62 -4
  15. data/app/models/tda/option.rb +39 -41
  16. data/app/models/wco/newspartial.rb +16 -6
  17. data/app/models/wco/newsvideo.rb +1 -1
  18. data/app/models/wco/publisher.rb +29 -8
  19. data/app/models/wco/publisher_d10.rb +50 -0
  20. data/app/models/wco/report.rb +12 -2
  21. data/app/models/wco/site.rb +1 -67
  22. data/app/models/wco/sitemap_path.rb +19 -13
  23. data/app/models/wco/tag.rb +15 -8
  24. data/app/models/wco/utils.rb +1 -1
  25. data/app/views/wco/api/newsproducer/README.txt +4 -0
  26. data/app/views/wco/api/newsproducer/_studio_blue2.js-bk +606 -0
  27. data/app/views/wco/api/newsproducer/_studio_blue2.js-bk2 +898 -0
  28. data/app/views/wco/api/newsproducer/studio_blue2.html.erb-bk +151 -0
  29. data/app/views/wco/office_action_templates/index.haml +1 -1
  30. data/app/views/wco/office_actions/_actions.haml +1 -1
  31. data/app/views/wco/publishers/_form.haml +11 -2
  32. data/app/views/wco/publishers/_post_with.haml +1 -1
  33. data/app/views/wco/reports/_form.haml +13 -2
  34. data/app/views/wco/reports/_index.haml +18 -11
  35. data/app/views/wco/reports/show.haml +6 -1
  36. data/app/views/wco/sitemap_paths/_index.haml +1 -0
  37. data/app/views/wco/sitemap_paths/show.haml +14 -0
  38. data/app/views/wco/sites/index.haml +6 -0
  39. data/app/views/wco/sites/show.haml +1 -1
  40. data/app/views/wco/tags/_form.haml +3 -0
  41. data/config/routes.rb +3 -3
  42. data/lib/shortcuts.rb +1 -1
  43. data/lib/wco_models.rb +1 -1
  44. metadata +10 -4
  45. data/app/views/wco/sitemap_paths/check.haml +0 -13
@@ -0,0 +1,898 @@
1
+
2
+ /*
3
+ **/
4
+ const logg = (a, b="", c=null) => {
5
+ if ('undefined' === typeof window) { return }
6
+ c = "string" === typeof c ? c : b.replace(/\W/g, '')
7
+ if (c.length > 0) {
8
+ window[c] = a
9
+ }
10
+ console.log(`+++ ${b}:`, a) // eslint-disable-line no-console
11
+ }
12
+
13
+ document.addEventListener("keydown", e => {
14
+ if (e.code === "Space" && !["INPUT", "TEXTAREA"].includes(e.target.tagName)) {
15
+ e.preventDefault()
16
+ }
17
+ })
18
+
19
+ const MODELS_ROOT = 'https://cdn.jsdelivr.net/gh/wasya-co/ishlib3js@0.4.7/public/vendor/models'
20
+
21
+ const AVATAR_STOR = 'avatar'
22
+ const avatars = {
23
+ brunette: { type: 'F', url: `${MODELS_ROOT}/avatars/brunette/model.glb` },
24
+ cara: { type: 'F', url: `${MODELS_ROOT}/avatars/cara/model.glb` }, // short hair
25
+ eve: { type: 'F', url: `${MODELS_ROOT}/avatars/eve/model.glb` }, // poison green
26
+
27
+ denis: { type: 'M', url: `${MODELS_ROOT}/avatars/denis/model.glb` }, // beard
28
+ gregor: { type: 'M', url: `${MODELS_ROOT}/avatars/gregor/model.glb` }, // gray shirt
29
+ }
30
+ $('select.avatar').each((_idx, el) => {
31
+ const $select = $(el)
32
+ $.each(avatars, function(name, url) {
33
+ $('<option>', {
34
+ value: name,
35
+ text: name,
36
+ }).appendTo($select)
37
+ })
38
+ try {
39
+ const saved = localStorage.getItem(`${AVATAR_STOR}-${$select.data('uid')}`)
40
+ if (saved && avatars[saved]) $select.val(saved)
41
+ } catch (error) {
42
+ console.log(error)
43
+ }
44
+ })
45
+
46
+ const SCENE_STOR = 'studio'
47
+ const scenes = await fetch(`${MODELS_ROOT}/scenes/index.json`).then(r => r.json())
48
+ $.each(scenes, (name) => {
49
+ $('<option>', { value: name, text: name }).appendTo($('select.studio'))
50
+ })
51
+ try {
52
+ const saved = localStorage.getItem( SCENE_STOR )
53
+ if (saved && scenes[saved]) $('select.studio').val(saved)
54
+ } catch (error) {
55
+ console.log(error)
56
+ }
57
+
58
+ let scene_url = scenes[$('select.studio').val()] || scenes.rick_and_morty_garage
59
+
60
+ let gestures = {
61
+ talking_1: (avatar) => `${MODELS_ROOT}/animation-library/feminine/fbx/expression/${avatar.body}_Talking_Variations_001.fbx`,
62
+ talking_4: (avatar) => `${MODELS_ROOT}/animation-library/feminine/fbx/expression/${avatar.body}_Talking_Variations_004.fbx`,
63
+ walk_1: { speed: 2.0, url: (a) => `${MODELS_ROOT}/animation-library/feminine/fbx/locomotion/${a.body}_Walk_001.fbx` },
64
+ walk_2: { speed: 2.0, url: (a) => `${MODELS_ROOT}/animation-library/feminine/fbx/locomotion/${a.body}_Walk_002.fbx` },
65
+
66
+ sitting: (a) => `${MODELS_ROOT}/animations/Sitting.fbx`,
67
+ sit_to_stand: (a) => `${MODELS_ROOT}/animations/Sit To Stand.fbx`,
68
+ stand_to_sit: (a) => `${MODELS_ROOT}/animations/Stand To Sit.fbx`,
69
+
70
+ entering_car: (a) => `${MODELS_ROOT}/animations/Entering Car.fbx`,
71
+ slow_run: { speed: 1.1, url: (a) => `${MODELS_ROOT}/animations/Slow Run.fbx`, }
72
+ };
73
+ $.each(gestures, (name) => {
74
+ $('<option>', { value: name, text: name }).appendTo($('select.gestures'))
75
+ })
76
+
77
+ let u_positions = {
78
+ '1': [0, 0, 0],
79
+ '2': [1, 0, 0],
80
+ '3': [-1, 0, 0],
81
+ }
82
+
83
+
84
+
85
+ import * as THREE from 'three'
86
+
87
+ import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'
88
+ import { FBXLoader } from 'three/addons/loaders/FBXLoader.js'
89
+ import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
90
+
91
+ import { TalkingHead } from "talkinghead"
92
+
93
+ const params = new URLSearchParams(window.location.search)
94
+
95
+ let config = {
96
+ sampleRate: 8000,
97
+ mood: 'neutral',
98
+ gain: 0.5,
99
+ lipsyncType: "visemes",
100
+ lipsyncLang: "en",
101
+ waitForAudioChunks: false,
102
+ enableMetrics: false,
103
+ };
104
+
105
+ const onMetrics = (which) => {
106
+ logg(which, 'onMetrics')
107
+ }
108
+
109
+ const onSubtitles = (which) => {
110
+ logg(which, 'onSubtitles')
111
+ }
112
+ const streamOpts = {
113
+ gain: config.gain,
114
+ lipsyncLang: config.lipsyncLang,
115
+ lipsyncType: config.lipsyncType,
116
+ metrics: config.enableMetrics ? { enabled: true, intervalHz: config.metricsInterval } : { enabled: false },
117
+ mood: config.mood,
118
+ sampleRate: config.sampleRate,
119
+ waitForAudioChunks: config.waitForAudioChunks,
120
+ }
121
+
122
+
123
+ let width = 854
124
+ let height = 480
125
+ let slug = '<ccapture>'
126
+ const fps = 30
127
+
128
+ let frame = 0
129
+ let lastAnimTime = 0
130
+ let capturing = false
131
+ let captureFrame = 0
132
+ let captureTotal = 0
133
+ const api_key = params.get('api_key')
134
+ const api_secret = params.get('api_secret')
135
+ const wco_origin = params.get('wco_origin')
136
+ const newspartial_id = params.get('newspartial_id')
137
+ let totalFrames
138
+
139
+ let camera, camera_wide, camera_1, camera_2, camera_3
140
+ let controls, renderer, scene
141
+ let head, head_1, head_2, head_3
142
+ let faceTarget = new THREE.Vector3()
143
+ const cameraTargets = {
144
+ wide: new THREE.Vector3(0.5, 1.2, 0),
145
+ av_1: new THREE.Vector3(0, 1.5, 0),
146
+ av_2: new THREE.Vector3(1, 1.5, 0),
147
+ av_3: new THREE.Vector3(-1, 1.5, 0),
148
+ }
149
+ let cameraTransition = null
150
+ const CAMERA_BLEND_MS = 900
151
+ let chunkedInput = null
152
+ const lights = {}
153
+ const LIGHT_CTRL_KEY = 'wco.studio_blue.light-ctrl'
154
+ const HEAD_CTRL_KEY = 'wco.studio_blue.head-ctrl'
155
+ const CAMERA_CTRL_KEY = 'wco.studio_blue.camera-ctrl'
156
+ var capturer = new CCapture( { format: 'webm', framerate: fps } )
157
+ const gltfLoader = new GLTFLoader()
158
+ const fbxLoader = new FBXLoader()
159
+ const loading = document.getElementById('loading')
160
+ const status = document.getElementById('status')
161
+
162
+ /*
163
+ **/
164
+ function point_camera_at_face(cam_id, _head) {
165
+ const leftEye = new THREE.Vector3()
166
+ const rightEye = new THREE.Vector3()
167
+ _head.objectLeftEye.getWorldPosition(leftEye)
168
+ _head.objectRightEye.getWorldPosition(rightEye)
169
+ faceTarget.copy(leftEye).add(rightEye).multiplyScalar(0.5)
170
+
171
+ const camPos = new THREE.Vector3(0, 0, 2)
172
+ camPos.applyQuaternion(_head.armature.quaternion)
173
+ camPos.add(faceTarget)
174
+ const _camera = cameras_fn(cam_id)
175
+ _camera.position.copy(camPos)
176
+ _camera.lookAt(faceTarget)
177
+ cameraTargets[cam_id].copy(faceTarget)
178
+ }
179
+
180
+ /*
181
+ **/
182
+ function put_feet_at_origin(_head) {
183
+ // Put feet at the origin
184
+ _head.armature.updateMatrixWorld(true)
185
+ const leftToe = new THREE.Vector3()
186
+ const rightToe = new THREE.Vector3()
187
+ _head.objectLeftToeBase.getWorldPosition(leftToe)
188
+ _head.objectRightToeBase.getWorldPosition(rightToe)
189
+ _head.armature.position.set(
190
+ -(leftToe.x + rightToe.x) / 2,
191
+ -Math.min(leftToe.y, rightToe.y),
192
+ -(leftToe.z + rightToe.z) / 2
193
+ )
194
+ _head.armature.updateMatrixWorld(true)
195
+ }
196
+
197
+ /*
198
+ **/
199
+ function rescale(model, config) {
200
+ const box = new THREE.Box3().setFromObject(model)
201
+ const size = box.getSize(new THREE.Vector3())
202
+ const currentHeight = size.y
203
+ const scale = config.height / currentHeight
204
+ model.scale.setScalar(scale)
205
+ }
206
+
207
+ function scene_cfg(s) {
208
+ if (typeof s === 'string') return { url: s, height: 3.3 }
209
+ return { url: s.url, height: s.height || 3.3 }
210
+ }
211
+
212
+ function set_shadows(root, cast, receive) {
213
+ root.traverse((obj) => {
214
+ if (obj.isMesh) {
215
+ obj.castShadow = cast
216
+ obj.receiveShadow = receive
217
+ }
218
+ })
219
+ }
220
+
221
+ /*
222
+ **/
223
+ function setup_light(scene) {
224
+ lights.ambientLight = new THREE.AmbientLight( 0xffffff, 0.6 )
225
+ scene.add( lights.ambientLight )
226
+
227
+ lights.hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444, 1.2 )
228
+ lights.hemiLight.position.set( 0, 20, 0 )
229
+ scene.add( lights.hemiLight )
230
+
231
+ lights.keyLight = new THREE.DirectionalLight( 0xffffff, 2.5 )
232
+ lights.keyLight.position.set( 5, 10, 7 )
233
+ lights.keyLight.castShadow = true
234
+ lights.keyLight.shadow.mapSize.set( 2048, 2048 )
235
+ lights.keyLight.shadow.camera.near = 0.5
236
+ lights.keyLight.shadow.camera.far = 40
237
+ lights.keyLight.shadow.camera.left = -10
238
+ lights.keyLight.shadow.camera.right = 10
239
+ lights.keyLight.shadow.camera.top = 10
240
+ lights.keyLight.shadow.camera.bottom = -10
241
+ lights.keyLight.shadow.bias = -0.0001
242
+ scene.add( lights.keyLight )
243
+
244
+ lights.fillLight = new THREE.DirectionalLight( 0xffffff, 1.2 )
245
+ lights.fillLight.position.set( -5, 4, -2 )
246
+ scene.add( lights.fillLight )
247
+
248
+ lights.rimLight = new THREE.DirectionalLight( 0xffffff, 0.8 )
249
+ lights.rimLight.position.set( 0, 6, -8 )
250
+ scene.add( lights.rimLight )
251
+
252
+ restoreLightControls()
253
+ syncLightsFromControls()
254
+ }
255
+
256
+ function restoreLightControls() {
257
+ try {
258
+ const saved = JSON.parse(localStorage.getItem(LIGHT_CTRL_KEY) || 'null')
259
+ if (!saved || typeof saved !== 'object') return
260
+ document.querySelectorAll('input.light-ctrl').forEach((input) => {
261
+ if (Object.prototype.hasOwnProperty.call(saved, input.name)) {
262
+ input.checked = !!saved[input.name]
263
+ }
264
+ })
265
+ } catch (error) {
266
+ console.log(error)
267
+ }
268
+ }
269
+
270
+ function persistLightControls() {
271
+ const saved = {}
272
+ document.querySelectorAll('input.light-ctrl').forEach((input) => {
273
+ saved[input.name] = input.checked
274
+ })
275
+ localStorage.setItem(LIGHT_CTRL_KEY, JSON.stringify(saved))
276
+ }
277
+
278
+ function restoreHeadControl() {
279
+ try {
280
+ const saved = localStorage.getItem(HEAD_CTRL_KEY)
281
+ if (!saved) return
282
+ const input = document.querySelector(`input[name=head][value="${saved}"]`)
283
+ if (input) input.checked = true
284
+ } catch (error) {
285
+ console.log(error)
286
+ }
287
+ }
288
+
289
+ function persistHeadControl(id) {
290
+ localStorage.setItem(HEAD_CTRL_KEY, String(id))
291
+ }
292
+
293
+ function restoreCameraControl() {
294
+ try {
295
+ const saved = localStorage.getItem(CAMERA_CTRL_KEY)
296
+ if (!saved) return
297
+ const input = document.querySelector(`input[name=camera][value="${saved}"]`)
298
+ if (input) input.checked = true
299
+ } catch (error) {
300
+ console.log(error)
301
+ }
302
+ }
303
+
304
+ function persistCameraControl(id) {
305
+ localStorage.setItem(CAMERA_CTRL_KEY, String(id))
306
+ }
307
+
308
+ function syncLightsFromControls() {
309
+ document.querySelectorAll('input.light-ctrl').forEach((input) => {
310
+ const light = lights[input.name]
311
+ if (light) light.visible = input.checked
312
+ })
313
+ persistLightControls()
314
+ }
315
+
316
+ function sync_shadows() {
317
+ const on = $('#is_shadows').prop('checked')
318
+ if (lights.keyLight) lights.keyLight.castShadow = on
319
+ }
320
+
321
+ function gesture_url(g, avatar) {
322
+ if (typeof g === 'function') return g(avatar)
323
+ const u = g && g.url
324
+ return typeof u === 'function' ? u(avatar) : u
325
+ }
326
+
327
+ function update_walk(this_head, dt) {
328
+ if (!this_head || !this_head.speed || !this_head.mixer || !this_head.armature) return
329
+ const dir = new THREE.Vector3()
330
+ this_head.armature.getWorldDirection(dir)
331
+ dir.y = 0
332
+ if (dir.lengthSq() === 0) return
333
+ dir.normalize()
334
+ this_head.armature.position.addScaledVector(dir, this_head.speed * dt / 1000)
335
+ }
336
+
337
+ function play_animation(config) {
338
+ logg(config, 'my play_animation()')
339
+ let this_head = heads_fn()[config.avatar_id]
340
+ if (this_head) this_head.playAnimation(gestures[config.animation_name](config))
341
+ }
342
+
343
+ function cut_to(key) {
344
+ const radio = document.querySelector(`input[name=camera][value="${key}"]`)
345
+ if (radio) radio.checked = true
346
+ setActiveCamera(key, true)
347
+ }
348
+
349
+ function move_camera(config) {
350
+ const fromKey = String(config.from)
351
+ const toKey = String(config.to)
352
+ const duration = Number(config.duration) > 0 ? Number(config.duration) : CAMERA_BLEND_MS
353
+ const src = cameras_fn(fromKey)
354
+ const dest = cameras_fn(toKey)
355
+ const startTarget = (cameraTargets[fromKey] || cameraTargets.wide).clone()
356
+ const destTarget = (cameraTargets[toKey] || cameraTargets.wide).clone()
357
+
358
+ persistCameraControl(toKey)
359
+ const radio = document.querySelector(`input[name=camera][value="${toKey}"]`)
360
+ if (radio) radio.checked = true
361
+
362
+ ;[camera_wide, camera_1, camera_2, camera_3, camera].forEach((cam) => {
363
+ if (cam) {
364
+ cam.aspect = width / height
365
+ cam.updateProjectionMatrix()
366
+ }
367
+ })
368
+
369
+ src.updateMatrixWorld()
370
+ dest.updateMatrixWorld()
371
+
372
+ camera.position.copy(src.position)
373
+ camera.quaternion.copy(src.quaternion)
374
+ camera.fov = src.fov
375
+ camera.near = dest.near
376
+ camera.far = dest.far
377
+ camera.updateProjectionMatrix()
378
+ faceTarget.copy(startTarget)
379
+ if (controls) {
380
+ controls.target.copy(startTarget)
381
+ controls.enabled = false
382
+ }
383
+
384
+ cameraTransition = {
385
+ startPos: src.position.clone(),
386
+ startQuat: src.quaternion.clone(),
387
+ startFov: src.fov,
388
+ startTarget: startTarget,
389
+ endPos: dest.position.clone(),
390
+ endQuat: dest.quaternion.clone(),
391
+ endFov: dest.fov,
392
+ endTarget: destTarget,
393
+ elapsed: 0,
394
+ duration: duration,
395
+ }
396
+ }
397
+
398
+ // window.play_animation = play_animation
399
+ window.move_camera = move_camera
400
+ window.cut_to = cut_to
401
+
402
+ let eventsIndex = 0
403
+ function tickEvents(elapsedMs) {
404
+ if (!chunkedInput || !chunkedInput.ftimes || !chunkedInput.fns) return
405
+ while (eventsIndex < chunkedInput.ftimes.length && elapsedMs >= chunkedInput.ftimes[eventsIndex]) {
406
+ const fn = chunkedInput.fns[eventsIndex]
407
+ logg(fn, 'fn')
408
+ eventsIndex++
409
+ eval(fn)
410
+ }
411
+ }
412
+
413
+
414
+
415
+ /*
416
+ **/
417
+ async function init() {
418
+ if (newspartial_id) {
419
+ try {
420
+ const url = wco_origin + '/wco/api/newspartials/' + newspartial_id + '/config.json?api_key=' + api_key + '&api_secret=' + api_secret
421
+ chunkedInput = await fetch(url).then(r => r.json())
422
+ logg(chunkedInput, 'chunkedInput')
423
+
424
+ const last = chunkedInput.wtimes.length-1
425
+ const duration_ms = chunkedInput.wtimes[last] + chunkedInput.wdurations[last]
426
+ totalFrames = duration_ms/1000*fps
427
+ width = chunkedInput.w_px
428
+ height = chunkedInput.h_px
429
+ slug = chunkedInput.slug
430
+ } catch (error) {
431
+ console.log(error)
432
+ chunkedInput = null
433
+ totalFrames = undefined
434
+ }
435
+ }
436
+
437
+ scene = new THREE.Scene()
438
+ scene.background = new THREE.Color(0x333333) // new THREE.Color(0xd3d3d3)
439
+
440
+ /* fov — Camera frustum vertical field of view.
441
+ * aspect — Camera frustum aspect ratio.
442
+ * near — Camera frustum near plane.
443
+ * far — Camera frustum far plane.
444
+ **/
445
+ camera_1 = new THREE.PerspectiveCamera( 10, width/height, 0.1, 1000 )
446
+ camera_1.position.set( 0, 1.6, 4 )
447
+ camera_1.lookAt( 0, 1.5, 0 )
448
+
449
+ camera_2 = new THREE.PerspectiveCamera( 10, width/height, 0.1, 1000 )
450
+ camera_2.position.set( 2, 1.6, 4 )
451
+ camera_2.lookAt( 1, 1.5, 0 )
452
+
453
+ camera_3 = new THREE.PerspectiveCamera( 10, width/height, 0.1, 1000 )
454
+ camera_3.position.set( -2, 1.6, 4 )
455
+ camera_3.lookAt( -1, 1.5, 0 )
456
+
457
+ camera_wide = new THREE.PerspectiveCamera( 25, width/height, 0.1, 1000 )
458
+ camera_wide.position.set( -2, 2.2, 8 )
459
+ camera_wide.lookAt( 0.5, 1.2, 0 )
460
+
461
+ camera = new THREE.PerspectiveCamera( 10, width/height, 0.1, 1000 )
462
+ camera.position.copy( camera_wide.position )
463
+ camera.quaternion.copy( camera_wide.quaternion )
464
+ camera.fov = camera_wide.fov
465
+ camera.updateProjectionMatrix()
466
+
467
+ setup_light(scene)
468
+
469
+ const grid = new THREE.GridHelper(10, 10, 0x888888, 0xbbbbbb)
470
+ grid.position.y = 0
471
+ scene.add(grid)
472
+
473
+
474
+ let studio
475
+ async function load_studio(s) {
476
+ const cfg = scene_cfg(s)
477
+ logg(cfg, 'load_studio')
478
+
479
+ if (studio && studio.parent) studio.parent.remove(studio)
480
+ studio = (await gltfLoader.loadAsync(cfg.url)).scene
481
+ rescale(studio, { height: cfg.height })
482
+ set_shadows(studio, false, true)
483
+ scene.add(studio)
484
+ }
485
+ await load_studio(scene_url)
486
+ $('select.studio').on('change', async function() {
487
+ const name = $(this).val()
488
+ if (!scenes[name]) return
489
+ localStorage.setItem(SCENE_STOR, name)
490
+ scene_url = scenes[name]
491
+ loading.textContent = 'Loading...'
492
+ try {
493
+ await load_studio(scene_url)
494
+ loading.textContent = 'loaded'
495
+ } catch (error) {
496
+ console.log(error)
497
+ loading.textContent = error.toString()
498
+ }
499
+ })
500
+
501
+
502
+ // head = new TalkingHead( document.getElementById('avatar'), {
503
+ // avatarOnly: true,
504
+ // avatarOnlyScene: scene,
505
+ // avatarOnlyCamera: camera,
506
+ // lipsyncModules: ["en"],
507
+ // dracoEnabled: true,
508
+ // })
509
+ head_1 = new TalkingHead( document.getElementById('avatar_1'), {
510
+ avatarOnly: true,
511
+ avatarOnlyScene: scene,
512
+ avatarOnlyCamera: camera_1,
513
+ lipsyncModules: ["en"],
514
+ dracoEnabled: true,
515
+ })
516
+ head_2 = new TalkingHead( document.getElementById('avatar_2'), {
517
+ avatarOnly: true,
518
+ avatarOnlyScene: scene,
519
+ avatarOnlyCamera: camera_1,
520
+ lipsyncModules: ["en"],
521
+ dracoEnabled: true,
522
+ })
523
+ head_3 = new TalkingHead( document.getElementById('avatar_3'), {
524
+ avatarOnly: true,
525
+ avatarOnlyScene: scene,
526
+ avatarOnlyCamera: camera_1,
527
+ lipsyncModules: ["en"],
528
+ dracoEnabled: true,
529
+ })
530
+
531
+
532
+
533
+
534
+ $('select.avatar').on('change', async function() {
535
+ const uid = String($(this).data('uid'))
536
+ const thisHead = heads_fn(uid)
537
+ const val = $(this).val()
538
+ localStorage.setItem(`${AVATAR_STOR}-${uid}`, val || '')
539
+ if (!thisHead) return
540
+ if (thisHead.armature && thisHead.armature.parent) thisHead.armature.parent.remove(thisHead.armature)
541
+ const avatar = avatars[val]
542
+ if (!avatar || !avatar.url) return
543
+ loading.textContent = 'Loading...'
544
+ try {
545
+ if (thisHead.armature && thisHead.armature.parent) thisHead.armature.parent.remove(thisHead.armature)
546
+ await thisHead.showAvatar({
547
+ url: avatar.url,
548
+ body: avatar.type,
549
+ avatarMood: 'neutral',
550
+ lipsyncLang: 'en'
551
+ }, (ev) => {
552
+ if ( ev.lengthComputable ) {
553
+ let val = Math.min(100,Math.round(ev.loaded/ev.total * 100 ))
554
+ loading.textContent = "Loading " + val + "%"
555
+ }
556
+ })
557
+ thisHead.armature.rotation.set(0, 0, 0)
558
+ set_shadows(thisHead.armature, true, false)
559
+ scene.add(thisHead.armature)
560
+ put_feet_at_origin(thisHead)
561
+ thisHead.armature.position.add(new THREE.Vector3(...u_positions[uid]))
562
+ point_camera_at_face(`av_${uid}`, thisHead)
563
+ await thisHead.streamStart(streamOpts, () => {}, () => {}, onSubtitles, onMetrics)
564
+ loading.textContent = 'loaded'
565
+ } catch (error) {
566
+ console.log(error)
567
+ loading.textContent = error.toString()
568
+ }
569
+ })
570
+ $('select.avatar').trigger('change')
571
+
572
+
573
+
574
+ //
575
+
576
+ renderer = new THREE.WebGLRenderer( {
577
+ alpha: false,
578
+ antialias: true,
579
+ } )
580
+ renderer.setClearColor(0xd3d3d3, 1)
581
+ renderer.setPixelRatio( 1 ); // window.devicePixelRatio )
582
+ renderer.setSize( width, height )
583
+ renderer.toneMapping = THREE.ACESFilmicToneMapping
584
+ renderer.toneMappingExposure = 1
585
+ renderer.outputColorSpace = THREE.SRGBColorSpace
586
+ renderer.shadowMap.enabled = true
587
+ renderer.shadowMap.type = THREE.PCFSoftShadowMap
588
+ document.getElementById('rotatingC').appendChild( renderer.domElement )
589
+
590
+ //
591
+
592
+ controls = new OrbitControls( camera, renderer.domElement )
593
+ controls.enableDamping = true
594
+ controls.enableRotate = true
595
+ controls.enablePan = true
596
+ controls.enableZoom = true
597
+ controls.minDistance = 0.5
598
+ controls.maxDistance = 100
599
+ controls.target.copy(faceTarget)
600
+ controls.autoRotate = false
601
+ controls.update()
602
+
603
+ restoreHeadControl()
604
+ document.querySelectorAll('input[name=head]').forEach((input) => {
605
+ input.addEventListener('change', () => {
606
+ if (!input.checked) return
607
+ setActiveHead(input.value)
608
+ })
609
+ })
610
+ const selectedHead = document.querySelector('input[name=head]:checked')
611
+ setActiveHead(selectedHead ? selectedHead.value : '1')
612
+
613
+ restoreCameraControl()
614
+ document.querySelectorAll('input[name=camera]').forEach((input) => {
615
+ input.addEventListener('change', () => {
616
+ if (!input.checked) return
617
+ setActiveCamera(input.value)
618
+ })
619
+ })
620
+ const selected = document.querySelector('input[name=camera]:checked')
621
+ setActiveCamera(selected ? selected.value : 'wide', true)
622
+
623
+ document.querySelectorAll('input.light-ctrl').forEach((input) => {
624
+ input.addEventListener('change', syncLightsFromControls)
625
+ })
626
+ syncLightsFromControls()
627
+ $('#is_shadows').on('change', sync_shadows)
628
+ sync_shadows()
629
+
630
+ //
631
+
632
+ window.addEventListener( 'resize', onWindowResize )
633
+
634
+
635
+ // Keep rendering so OrbitControls drag/damping stay live
636
+ lastAnimTime = performance.now()
637
+ renderer.setAnimationLoop(animate)
638
+ }
639
+
640
+ /*
641
+ let semafore = false
642
+ document.addEventListener('DOMContentLoaded', async function(e) {
643
+ if (!semafore) {
644
+ semafore = true
645
+ await init()
646
+ }
647
+ })
648
+ */
649
+ if (document.readyState === 'loading') {
650
+ document.addEventListener('DOMContentLoaded', init)
651
+ } else {
652
+ init()
653
+ }
654
+
655
+
656
+ function cameras_fn(which) {
657
+ switch (which) {
658
+ case 'wide':
659
+ return camera_wide
660
+ case 'av_1':
661
+ return camera_1
662
+ case 'av_2':
663
+ return camera_2
664
+ case 'av_3':
665
+ return camera_3
666
+ default:
667
+ logg('fpq - cameras_fn default - this should never happen')
668
+ return camera_wide
669
+ }
670
+ }
671
+
672
+ function heads_fn(which) {
673
+ switch (which) {
674
+ case '1':
675
+ return head_1
676
+ break;
677
+ case '2':
678
+ return head_2
679
+ break;
680
+ case '3':
681
+ return head_3
682
+ break;
683
+ default:
684
+ logg('fpp - heads_fn default - this should never happen')
685
+ return head_1
686
+ }
687
+ }
688
+
689
+ function setActiveHead(id) {
690
+ head = heads_fn(id)
691
+ persistHeadControl(id)
692
+ }
693
+
694
+ function easeInOutCubic(t) {
695
+ return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2
696
+ }
697
+
698
+ function setActiveCamera(id, instant = false) {
699
+ const key = String(id)
700
+ persistCameraControl(key)
701
+ const dest = cameras_fn(key)
702
+ const destTarget = (cameraTargets[key] || cameraTargets.wide).clone()
703
+ ;[camera_wide, camera_1, camera_2, camera_3, camera].forEach((cam) => {
704
+ if (cam) {
705
+ cam.aspect = width / height
706
+ cam.updateProjectionMatrix()
707
+ }
708
+ })
709
+
710
+ dest.updateMatrixWorld()
711
+
712
+ const endPos = dest.position.clone()
713
+ const endQuat = dest.quaternion.clone()
714
+
715
+ if (instant || !controls) {
716
+ camera.position.copy(endPos)
717
+ camera.quaternion.copy(endQuat)
718
+ camera.fov = dest.fov
719
+ camera.near = dest.near
720
+ camera.far = dest.far
721
+ camera.updateProjectionMatrix()
722
+ faceTarget.copy(destTarget)
723
+ if (controls) {
724
+ controls.target.copy(destTarget)
725
+ controls.update()
726
+ }
727
+ cameraTransition = null
728
+ return
729
+ }
730
+
731
+ cameraTransition = {
732
+ startPos: camera.position.clone(),
733
+ startQuat: camera.quaternion.clone(),
734
+ startFov: camera.fov,
735
+ startTarget: controls.target.clone(),
736
+ endPos: endPos,
737
+ endQuat: endQuat,
738
+ endFov: dest.fov,
739
+ endTarget: destTarget,
740
+ elapsed: 0,
741
+ duration: CAMERA_BLEND_MS,
742
+ }
743
+ controls.enabled = false
744
+ }
745
+
746
+ function updateCameraTransition(dt) {
747
+ if (!cameraTransition) return
748
+ cameraTransition.elapsed += dt
749
+ const u = Math.min(1, cameraTransition.elapsed / cameraTransition.duration)
750
+ const e = easeInOutCubic(u)
751
+ const tr = cameraTransition
752
+ camera.position.lerpVectors(tr.startPos, tr.endPos, e)
753
+ camera.quaternion.slerpQuaternions(tr.startQuat, tr.endQuat, e)
754
+ camera.fov = tr.startFov + (tr.endFov - tr.startFov) * e
755
+ camera.updateProjectionMatrix()
756
+ if (controls) {
757
+ controls.target.lerpVectors(tr.startTarget, tr.endTarget, e)
758
+ faceTarget.copy(controls.target)
759
+ } else {
760
+ faceTarget.lerpVectors(tr.startTarget, tr.endTarget, e)
761
+ }
762
+ if (u >= 1) {
763
+ cameraTransition = null
764
+ if (!capturing && controls) {
765
+ controls.enabled = true
766
+ controls.update()
767
+ }
768
+ }
769
+ }
770
+
771
+ function onWindowResize() {
772
+ ;[camera_wide, camera_1, camera_2, camera_3].forEach((cam) => {
773
+ if (cam) {
774
+ cam.aspect = width / height
775
+ cam.updateProjectionMatrix()
776
+ }
777
+ })
778
+ renderer.setSize( width, height )
779
+ render()
780
+ }
781
+
782
+ function render() {
783
+ renderer.render( scene, camera )
784
+ }
785
+
786
+ function animate() {
787
+ let dt
788
+ if (capturing) {
789
+ dt = 1000 / fps
790
+ } else {
791
+ const now = performance.now()
792
+ dt = lastAnimTime ? (now - lastAnimTime) : (1000 / fps)
793
+ lastAnimTime = now
794
+ if (dt > 100) dt = 100
795
+ }
796
+
797
+ if (capturing) tickEvents(captureFrame * dt)
798
+
799
+ if (head_1) head_1.animate(dt)
800
+ if (head_2) head_2.animate(dt)
801
+ if (head_3) head_3.animate(dt)
802
+ update_walk(head_1, dt)
803
+ update_walk(head_2, dt)
804
+ update_walk(head_3, dt)
805
+ updateCameraTransition(dt)
806
+ if (!cameraTransition && !capturing) controls.update()
807
+ renderer.render( scene, camera )
808
+
809
+ if (capturing) {
810
+ capturer.capture( renderer.domElement )
811
+ captureFrame++
812
+ if (captureFrame >= captureTotal) {
813
+ finishCapture()
814
+ }
815
+ }
816
+ }
817
+
818
+ async function startSpeakCapture() {
819
+ if (capturing) return
820
+ if (!head) return
821
+ if (!chunkedInput) return
822
+
823
+ const last = chunkedInput.wtimes.length - 1
824
+ const duration_ms = chunkedInput.wtimes[last] + chunkedInput.wdurations[last]
825
+ captureTotal = Math.max(1, Math.ceil(duration_ms / 1000 * fps))
826
+ captureFrame = 0
827
+ eventsIndex = 0
828
+ capturing = true
829
+ cameraTransition = null
830
+ if (controls) controls.enabled = false
831
+
832
+ capturer = new CCapture( { format: 'webm', framerate: fps } )
833
+ capturer.start()
834
+ logg('capturer.start')
835
+ status.textContent = 'capturing'
836
+ head.streamAudio(chunkedInput)
837
+ }
838
+
839
+ function finishCapture() {
840
+ capturing = false
841
+ capturer.stop()
842
+ logg('capturer.stop')
843
+ lastAnimTime = performance.now()
844
+ if (controls) controls.enabled = true
845
+
846
+ capturer.save((blob) => {
847
+ logg(blob, 'blob')
848
+ renderer.domElement.toBlob((thumb) => {
849
+ logg(thumb, 'thumb')
850
+ const form = new FormData()
851
+ form.append('video', blob, 'lips.webm')
852
+ form.append('name', slug)
853
+ form.append('thumb', thumb)
854
+ if (newspartial_id) form.append('newspartial_id', newspartial_id)
855
+
856
+ const url = wco_origin
857
+ ? (wco_origin + '/wco/api/videos/?api_key=' + api_key + '&api_secret=' + api_secret)
858
+ : '/wco/api/videos/'
859
+ fetch(url, {
860
+ method: 'POST',
861
+ body: form,
862
+ }).then(() => {
863
+ status.textContent = 'finished'
864
+ }).catch((error) => {
865
+ console.log(error)
866
+ status.textContent = 'save failed'
867
+ })
868
+ })
869
+ })
870
+ }
871
+
872
+
873
+ // Speak when clicked
874
+ $('#speak').on('click', async () => {
875
+ try {
876
+ await startSpeakCapture()
877
+ } catch (error) {
878
+ console.log(error)
879
+ capturing = false
880
+ lastAnimTime = performance.now()
881
+ if (controls) controls.enabled = true
882
+ }
883
+ })
884
+
885
+ $('#jesture').on('click', async () => {
886
+ try {
887
+ if (!head) return
888
+ const name = $('select.gestures').val()
889
+ const g = gestures[name]
890
+ const _anim = gesture_url(g, head.avatar)
891
+ head.speed = (g && typeof g === 'object' && g.speed) ? g.speed : 0
892
+ head.playAnimation( _anim )
893
+ } catch (error) {
894
+ console.log(error)
895
+ }
896
+ })
897
+
898
+ console.log('+++ loaded wco_models :: newsproducer :: studio_blue.js')