ugly-game 0.15.48 → 0.15.50

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.
@@ -30,6 +30,15 @@ export interface PropMesh {
30
30
  clearcoat?: number;
31
31
  /** Analytic studio/sky reflection strength. Defaults to 1 for PBR materials. */
32
32
  reflectionStrength?: number;
33
+ /** Caller-owned, linear HDR equirectangular environment with roughness mip levels. */
34
+ environment?: GPUTexture;
35
+ /** Caller-owned OpenGL (+Y) tangent normal map, linear format. */
36
+ normalMap?: GPUTexture;
37
+ /** Caller-owned linear occlusion/roughness/metalness map (RGB). */
38
+ ormMap?: GPUTexture;
39
+ normalScale?: number;
40
+ /** Repetition for normal/ORM maps; albedo UVs are unchanged. */
41
+ detailRepeat?: number;
33
42
  positions: Float32Array;
34
43
  normals: Float32Array;
35
44
  colors: Float32Array;
@@ -34,8 +34,11 @@ struct Inst { m: mat4x4<f32>, d0: vec4<f32>, d1: vec4<f32>, d2: vec4<f32>, d3: v
34
34
  // alpha-test cutoff (0 ⇒ no test, which is every model that predates it).
35
35
  // mat = opacity, cutoff, roughness (-1 = legacy), metalness.
36
36
  // coat = clearcoat, reflection strength, unused, unused.
37
- struct Mat { mat: vec4<f32>, coat: vec4<f32> };
37
+ struct Mat { mat: vec4<f32>, coat: vec4<f32>, maps: vec4<f32> };
38
38
  @group(2) @binding(2) var<uniform> mtl: Mat;
39
+ @group(2) @binding(3) var environmentTex: texture_2d<f32>;
40
+ @group(2) @binding(4) var normalTex: texture_2d<f32>;
41
+ @group(2) @binding(5) var ormTex: texture_2d<f32>;
39
42
  @group(1) @binding(0) var shadowMap: texture_depth_2d_array; // the SAME cascaded sun shadow map the terrain cubes sample
40
43
  @group(1) @binding(1) var shadowSamp: sampler_comparison;
41
44
  struct VO { @builtin(position) pos: vec4<f32>, @location(0) world: vec3<f32>, @location(1) n: vec3<f32>, @location(2) c: vec3<f32>, @location(3) l: vec3<f32>, @location(4) uv: vec2<f32> };
@@ -196,12 +199,20 @@ fn studioEnv(r: vec3<f32>, rough: f32) -> vec3<f32> {
196
199
  var c = mix(vec3(0.12, 0.11, 0.09), vec3(0.58, 0.68, 0.80), horizon);
197
200
  let width = 0.015 + rough * rough * 0.28;
198
201
  let top = smoothstep(0.68 - width, 0.68 + width, r.y);
199
- let stripA = exp(-pow((r.x - 0.48) / (0.07 + width), 2.0)) * smoothstep(-0.1, 0.3, r.y);
200
- let stripB = exp(-pow((r.z + 0.38) / (0.045 + width), 2.0)) * smoothstep(0.0, 0.3, r.y);
202
+ // Tall softboxes extend below the horizon: eye-level vertical panels reflect
203
+ // directions below the camera, so overhead-only lights cannot reveal their gloss.
204
+ let panelHeight = smoothstep(-0.8, -0.55, r.y) * (1.0-smoothstep(0.65, 0.95, r.y));
205
+ let stripA = exp(-pow((r.x - 0.48) / (0.12 + width), 2.0)) * panelHeight;
206
+ let stripB = exp(-pow((r.z + 0.38) / (0.09 + width), 2.0)) * panelHeight;
201
207
  c += vec3(1.6, 1.5, 1.35) * top * 0.55;
202
208
  c += vec3(2.5, 2.35, 2.1) * (stripA + stripB) * (1.0 - rough * 0.7);
203
209
  return c;
204
210
  }
211
+ fn environment(r: vec3<f32>, rough: f32) -> vec3<f32> {
212
+ if (mtl.coat.z < 0.5) { return studioEnv(r,rough); }
213
+ let uv=vec2(atan2(r.z,r.x)/6.2831853+0.5,acos(clamp(r.y,-1.0,1.0))/3.14159265);
214
+ return textureSampleLevel(environmentTex,albedoSamp,uv,rough*mtl.coat.w).rgb;
215
+ }
205
216
  fn fresnel(f0: vec3<f32>, cosTheta: f32) -> vec3<f32> {
206
217
  return f0 + (vec3(1.0) - f0) * pow(1.0 - clamp(cosTheta, 0.0, 1.0), 5.0);
207
218
  }
@@ -220,7 +231,18 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
220
231
  return select(c * 12.92, 1.055 * pow(max(c, vec3(0.0)), vec3(1.0/2.4)) - 0.055, c > vec3(0.0031308));
221
232
  }
222
233
  @fragment fn fs(i: VO) -> @location(0) vec4<f32> {
223
- let N = normalize(i.n);
234
+ var N = normalize(i.n);
235
+ let materialUV=i.uv*mtl.maps.w;
236
+ let px=dpdx(i.world); let py=dpdy(i.world);
237
+ let tx=dpdx(materialUV); let ty=dpdy(materialUV);
238
+ let mapN=textureSample(normalTex,albedoSamp,materialUV).xyz*2.0-1.0;
239
+ let orm=textureSample(ormTex,albedoSamp,materialUV).rgb;
240
+ if(mtl.maps.x>0.5) {
241
+ let T=cross(py,N)*tx.x+cross(N,px)*ty.x;
242
+ let B=cross(py,N)*tx.y+cross(N,px)*ty.y;
243
+ let inv=inverseSqrt(max(max(dot(T,T),dot(B,B)),0.000001));
244
+ N=normalize(N*mapN.z+(T*mapN.x+B*mapN.y)*inv*mtl.maps.z);
245
+ }
224
246
  let ff = select(1.0, 0.5 + 0.85 * fixedFill(N, u.up.xyz), u.up.w > 0.5); // fixed fill (fall back to flat amb if no up)
225
247
  let d = clamp(dot(N, -normalize(u.sun.xyz)), 0.0, 1.0);
226
248
  let sh = mix(1.0, shadowFactor(i.world, d), u.sun.w); // u.sun.w = shadow-receive enable (0 ⇒ off, byte-identical for demo)
@@ -249,8 +271,8 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
249
271
  let nl = max(dot(N, L), 0.0);
250
272
  let nh = max(dot(N, H), 0.0);
251
273
  let vh = max(dot(V, H), 0.0);
252
- let rough = clamp(mtl.mat.z, 0.045, 1.0);
253
- let metal = clamp(mtl.mat.w, 0.0, 1.0);
274
+ let rough = clamp(mtl.mat.z * select(1.0,orm.g,mtl.maps.y>0.5), 0.045, 1.0);
275
+ let metal = clamp(mtl.mat.w * select(1.0,orm.b,mtl.maps.y>0.5), 0.0, 1.0);
254
276
  // Vertex/instance tints are display sRGB, while albedo textures use sRGB GPU
255
277
  // formats (and arrive decoded). Legacy props keep their historical path.
256
278
  let albedo = srgbToLinear(i.c) * tex.rgb;
@@ -259,11 +281,13 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
259
281
  let kd = (vec3(1.0)-F) * (1.0-metal);
260
282
  let direct = (kd * albedo / 3.14159265 + F * ggx(nh,nv,nl,rough)) * u.sunCol.xyz * nl * sh * 2.5;
261
283
  let R = reflect(-V,N);
262
- let env = studioEnv(R,rough) * mtl.coat.y;
284
+ let env = environment(R,rough) * mtl.coat.y;
263
285
  let reflected = env * fresnel(f0,nv) * (1.0-rough*0.6);
264
- let diffuse = albedo * u.amb.xyz * (0.6 + 0.4*max(N.y,0.0)) * (1.0-metal);
286
+ let ambient=mix(u.amb.xyz,environment(N,1.0)*0.45,select(0.0,0.6,mtl.coat.z>0.5));
287
+ let ao=select(1.0,orm.r,mtl.maps.y>0.5);
288
+ let diffuse = albedo * ambient * (0.6 + 0.4*max(N.y,0.0)) * (1.0-metal)*ao;
265
289
  let coatF = fresnel(vec3(0.04),nv) * mtl.coat.x;
266
- let coating = studioEnv(R,0.12) * coatF * mtl.coat.y;
290
+ let coating = environment(R,0.12) * coatF * mtl.coat.y;
267
291
  let hdr = (direct + diffuse + reflected) * (vec3(1.0)-coatF) + coating;
268
292
  // Smooth highlight compression preserves saturated paint rather than
269
293
  // clipping reflected softboxes to a flat white patch.
@@ -560,6 +584,11 @@ export class GpuPropsRenderer {
560
584
  visibility: GPUShaderStage.FRAGMENT,
561
585
  buffer: { type: 'uniform' },
562
586
  },
587
+ ...[3, 4, 5].map((binding) => ({
588
+ binding,
589
+ visibility: GPUShaderStage.FRAGMENT,
590
+ texture: { sampleType: 'float' },
591
+ })),
563
592
  ],
564
593
  });
565
594
  this.drawBGL = [bgl0, bgl1, bgl2];
@@ -818,7 +847,7 @@ export class GpuPropsRenderer {
818
847
  const alpha = m.alpha ?? 1;
819
848
  const cutout = m.cutout ?? 0;
820
849
  const matBuf = dev.createBuffer({
821
- size: 32,
850
+ size: 48,
822
851
  usage: U.UNIFORM | U.COPY_DST,
823
852
  });
824
853
  dev.queue.writeBuffer(matBuf, 0, new Float32Array([
@@ -828,8 +857,12 @@ export class GpuPropsRenderer {
828
857
  m.metalness ?? 0,
829
858
  m.clearcoat ?? 0,
830
859
  m.reflectionStrength ?? 1,
831
- 0,
832
- 0,
860
+ m.environment ? 1 : 0,
861
+ m.environment ? m.environment.mipLevelCount - 1 : 0,
862
+ m.normalMap ? 1 : 0,
863
+ m.ormMap ? 1 : 0,
864
+ m.normalScale ?? 1,
865
+ m.detailRepeat ?? 1,
833
866
  ]));
834
867
  return {
835
868
  alpha,
@@ -847,6 +880,12 @@ export class GpuPropsRenderer {
847
880
  { binding: 0, resource: (tex ?? this.white).createView() },
848
881
  { binding: 1, resource: this.sampler },
849
882
  { binding: 2, resource: { buffer: matBuf } },
883
+ {
884
+ binding: 3,
885
+ resource: (m.environment ?? this.white).createView(),
886
+ },
887
+ { binding: 4, resource: (m.normalMap ?? this.white).createView() },
888
+ { binding: 5, resource: (m.ormMap ?? this.white).createView() },
850
889
  ],
851
890
  }),
852
891
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.48",
3
+ "version": "0.15.50",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -15,6 +15,21 @@
15
15
  "files": [
16
16
  "dist"
17
17
  ],
18
+ "scripts": {
19
+ "preinstall": "npx only-allow pnpm",
20
+ "build": "tsc -p tsconfig.json && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/",
21
+ "lint": "ugly-app lint",
22
+ "lint:fix": "ugly-app lint --fix",
23
+ "test": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/*.test.mts && pnpm run test:gpu-skin",
24
+ "test:flight": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/flight*.test.mts",
25
+ "test:flight-browser": "playwright test -c playwright.flight.config.ts",
26
+ "test:flight-perf": "playwright test -c playwright.flight.config.ts --grep 'CPU and retained-memory'",
27
+ "test:types": "tsc -p tsconfig.test.json",
28
+ "test:package-types": "node tools/packageTypesSmoke.mjs",
29
+ "pretty": "prettier --write .",
30
+ "prepublishOnly": "pnpm run pretty && ugly-app lint && tsc -p tsconfig.json && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/",
31
+ "test:gpu-skin": "node --experimental-transform-types --import ./tools/tsResolve.mjs --test src/assets/gpuSkin.test.mts"
32
+ },
18
33
  "dependencies": {
19
34
  "@webgpu/types": "^0.1.40",
20
35
  "meshoptimizer": "^1.2.0"
@@ -30,19 +45,5 @@
30
45
  "typescript": "^5.6.0",
31
46
  "ugly-app": "^0.1.841",
32
47
  "wesl": "0.7.28"
33
- },
34
- "scripts": {
35
- "preinstall": "npx only-allow pnpm",
36
- "build": "tsc -p tsconfig.json && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/",
37
- "lint": "ugly-app lint",
38
- "lint:fix": "ugly-app lint --fix",
39
- "test": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/*.test.mts && pnpm run test:gpu-skin",
40
- "test:flight": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/flight*.test.mts",
41
- "test:flight-browser": "playwright test -c playwright.flight.config.ts",
42
- "test:flight-perf": "playwright test -c playwright.flight.config.ts --grep 'CPU and retained-memory'",
43
- "test:types": "tsc -p tsconfig.test.json",
44
- "test:package-types": "node tools/packageTypesSmoke.mjs",
45
- "pretty": "prettier --write .",
46
- "test:gpu-skin": "node --experimental-transform-types --import ./tools/tsResolve.mjs --test src/assets/gpuSkin.test.mts"
47
48
  }
48
- }
49
+ }