Setting the file. One moment.
Environment · Frost Sequence Camera Orbit · heygen-com/hyperframes · Skills Docs
ContentsBack to the top of the page Three Mesh BVH LICENSE
source/src/scene/Environment.ts
source/src/scene/ Environment.ts
TypeScript · 101 lines · 4 KB
tex
=
new
THREE
.
DataTexture
(data, width, height,
THREE
.RGBAFormat,
THREE
.FloatType);
7 tex.mapping = THREE .EquirectangularReflectionMapping;
8 tex.colorSpace = THREE .LinearSRGBColorSpace;
9 tex.wrapS = THREE .RepeatWrapping;
10 tex.wrapT = THREE .ClampToEdgeWrapping;
11 tex.minFilter = tex.magFilter = THREE .LinearFilter;
12 const smooth = ( a : number , b : number , x : number ) => {
13 const t = Math. max ( 0 , Math. min ( 1 , (x - a) / (b - a)));
14 return t * t * ( 3 - 2 * t);
15 };
16 const directions = Array. from ({ length: width * height }, ( _ , i ) => {
17 const u = ((i % width) + 0.5 ) / width,
18 v = (Math. floor (i / width) + 0.5 ) / height;
19 const az = (u - 0.5 ) * Math. PI * 2 ,
20 el = (v - 0.5 ) * Math. PI ;
21 return new THREE . Vector3 (
22 Math. cos (el) * Math. cos (az),
23 Math. sin (el),
24 Math. cos (el) * Math. sin (az),
25 );
26 });
27 function update ( L : any ) {
28 const card = ( color : string , power : number , el : number , az : number , w : number , h : number ) => {
29 const e = THREE .MathUtils. degToRad (el),
30 a = THREE .MathUtils. degToRad (az);
31 const normal = new THREE . Vector3 (
32 Math. cos (e) * Math. sin (a),
33 Math. sin (e),
34 Math. cos (e) * Math. cos (a),
35 );
36 const right = new THREE . Vector3 ()
37 . crossVectors ( new THREE . Vector3 ( 0 , 1 , 0 ), normal)
38 . normalize ();
39 const up = new THREE . Vector3 (). crossVectors (normal, right);
40 return { color: new THREE . Color (color), power, normal, right, up, w, h };
41 };
42 const cards = [
43 card (
44 L .key.color,
45 L .envSoftbox * 5 ,
46 L .key.elevation,
47 L .key.azimuth,
48 0.42 * L .key.size,
49 0.65 * L .key.size,
50 ),
51 card (
52 L .rimColor,
53 L .envRim * 6 ,
54 L .rimElevation,
55 L .rimAzimuth,
56 0.1 * L .rimSize,
57 0.9 * L .rimSize,
58 ),
59 card (
60 L .accentCool.color,
61 L .accentCool.reflection * 4 ,
62 L .accentCool.elevation,
63 L .accentCool.azimuth,
64 0.13 * L .accentCool.size,
65 0.75 * L .accentCool.size,
66 ),
67 card (
68 L .accentWarm.color,
69 L .accentWarm.reflection * 4 ,
70 L .accentWarm.elevation,
71 L .accentWarm.azimuth,
72 0.2 * L .accentWarm.size,
73 0.7 * L .accentWarm.size,
74 ),
75 ];
76 const sky = new THREE . Color ( L .fillColor),
77 ground = new THREE . Color ( L .fillGroundColor);
78 directions. forEach (( d , i ) => {
79 const hemisphere = smooth ( - 0.3 , 0.8 , d.y);
80 const fill = L .envFill * 0.055 ;
81 let r = (ground.r * ( 1 - hemisphere) + sky.r * hemisphere) * fill;
82 let g = (ground.g * ( 1 - hemisphere) + sky.g * hemisphere) * fill;
83 let b = (ground.b * ( 1 - hemisphere) + sky.b * hemisphere) * fill;
84 for ( const c of cards) {
85 const facing = d. dot (c.normal);
86 if (facing <= 0 ) continue ;
87 const x = Math. abs (d. dot (c.right) / facing),
88 y = Math. abs (d. dot (c.up) / facing);
89 const coverage = ( 1 - smooth (c.w * 0.45 , c.w, x)) * ( 1 - smooth (c.h * 0.45 , c.h, y));
90 const glow = c.power * coverage;
91 r += c.color.r * glow;
92 g += c.color.g * glow;
93 b += c.color.b * glow;
94 }
95 data. set ([r, g, b, 1 ], i * 4 );
96 });
97 tex.needsUpdate = true ;
98 (tex as any ).needsPMREMUpdate = true ;
99 }
100 return { texture: tex, update };
101 }