-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ephebe.js
115 lines (95 loc) 路 3.2 KB
/
Ephebe.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
import * as THREE from 'three'
import glsl from 'glslify'
import assets from '../lib/AssetManager'
import { wireUniform } from '../lib/Controls'
import { addUniforms, customizeVertexShader, customizeFragmentShader } from '../lib/customizeShader'
const ephebeKey = assets.queue({
url: 'assets/ephebe_twerking.glb',
type: 'gltf',
})
const envmapKey = assets.queue({
url: 'assets/envMaps/49TH_STREET.exr',
type: 'envmap',
pmrem: true,
})
export function Ephebe(webgl, options = {}) {
const gltf = assets.get(ephebeKey)
// BUG gltf.scene.clone() doesn't clone skinning
const scene = gltf.scene
let ephebe
scene.traverse((child) => {
if (child.isMesh) {
ephebe = child
}
})
// do not make it disappear when camera is close
ephebe.frustumCulled = false
const mixer = new THREE.AnimationMixer(scene)
const clip = gltf.animations[0]
mixer.clipAction(clip).play()
ephebe.material = new THREE.MeshPhysicalMaterial({
skinning: true,
roughness: 0.25,
metalness: 1,
envMap: assets.get(envmapKey),
})
addUniforms(ephebe.material, {
time: { value: 0 },
// powerFactor: { value: webgl.controls.ephebe.powerFactor },
// multiplicator: { value: webgl.controls.ephebe.multiplicator },
powerFactor: wireUniform(ephebe.material, () => webgl.controls.ephebe.powerFactor),
multiplicator: wireUniform(ephebe.material, () => webgl.controls.ephebe.multiplicator),
})
customizeVertexShader(ephebe.material, {
head: glsl`
out vec3 vTransformed;
`,
// after skinning, export the position in world space
'#include <skinning_vertex>': glsl`
#include <skinning_vertex>
vTransformed = vec3(modelMatrix * vec4(transformed, 1.0));
`,
// like MeshNormalMaterial, but the normals are relative to world not camera
// transformedNormal is later outputted to the fragment shader as vNormal
transformedNormal: glsl`
transformedNormal = mat3(modelMatrix) * objectNormal;
`,
})
customizeFragmentShader(ephebe.material, {
head: glsl`
uniform float time;
uniform float powerFactor;
uniform float multiplicator;
in vec3 vTransformed;
#pragma glslify: hsl2rgb = require(glsl-hsl2rgb)
#pragma glslify: blendOverlay = require(glsl-blend/overlay)
`,
main: glsl`
vec3 cameraDirection = normalize(cameraPosition - vTransformed);
// 1 when facing the camera, 0 when perpendicular
float fresnel = dot(vNormal, cameraDirection);
float iridescence = pow(fresnel, powerFactor) * multiplicator;
// circle the whole hue wheel,
// the function looks like this /|/|/|/|/
float f = mod(iridescence + time, 1.0);
vec3 iridescentColor = hsl2rgb(f, 1.0, 0.5);
`,
diffuse: glsl`
diffuse = iridescentColor;
`,
// blend it again on top of all the lighting with
// overlay blend mode
gl_FragColor: glsl`
gl_FragColor.rgb = blendOverlay(gl_FragColor.rgb, iridescentColor);
`,
})
scene.scale.multiplyScalar(0.25)
scene.rotateY(Math.PI)
scene.translateX(-0.1)
scene.translateY(0.15)
webgl.onUpdate((dt) => {
mixer.update(dt)
ephebe.material.uniforms.time.value += dt * webgl.controls.ephebe.speed
})
return scene
}