ugly-game 0.15.49 → 0.15.51

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,23 @@ 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;
42
+ /** Albedo tiling, independent of detailRepeat. Defaults to 1. */
43
+ albedoRepeat?: number;
44
+ /** Dielectric F0 multiplier in [0,1]. Metals retain their conductor response. */
45
+ specularIntensity?: number;
46
+ /** Soft grazing-angle fiber reflection, 0..1; default 0. */
47
+ sheen?: number;
48
+ /** Roughness of the clear lacquer layer; default 0.12. */
49
+ clearcoatRoughness?: number;
33
50
  positions: Float32Array;
34
51
  normals: Float32Array;
35
52
  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>, surface: 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> };
@@ -205,6 +208,11 @@ fn studioEnv(r: vec3<f32>, rough: f32) -> vec3<f32> {
205
208
  c += vec3(2.5, 2.35, 2.1) * (stripA + stripB) * (1.0 - rough * 0.7);
206
209
  return c;
207
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
+ }
208
216
  fn fresnel(f0: vec3<f32>, cosTheta: f32) -> vec3<f32> {
209
217
  return f0 + (vec3(1.0) - f0) * pow(1.0 - clamp(cosTheta, 0.0, 1.0), 5.0);
210
218
  }
@@ -223,7 +231,18 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
223
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));
224
232
  }
225
233
  @fragment fn fs(i: VO) -> @location(0) vec4<f32> {
226
- 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
+ }
227
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)
228
247
  let d = clamp(dot(N, -normalize(u.sun.xyz)), 0.0, 1.0);
229
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)
@@ -231,7 +250,7 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
231
250
  // Albedo modulates the vertex colour rather than replacing it, so the StyleGuide role tint and the
232
251
  // per-instance hue jitter still act on a textured prop — and an untextured one samples the 1x1
233
252
  // white fallback and is unchanged.
234
- let tex = textureSample(albedoTex, albedoSamp, i.uv);
253
+ let tex = textureSample(albedoTex, albedoSamp, i.uv * mtl.surface.w);
235
254
  // ALPHA TEST, before any shading is paid for. mtl.mat.y is 0 for every model that does not ask for
236
255
  // one, and tex.a is 1 on the white fallback, so an untextured or opaque model never discards.
237
256
  if (tex.a < mtl.mat.y) { discard; }
@@ -252,22 +271,28 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
252
271
  let nl = max(dot(N, L), 0.0);
253
272
  let nh = max(dot(N, H), 0.0);
254
273
  let vh = max(dot(V, H), 0.0);
255
- let rough = clamp(mtl.mat.z, 0.045, 1.0);
256
- 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);
257
276
  // Vertex/instance tints are display sRGB, while albedo textures use sRGB GPU
258
277
  // formats (and arrive decoded). Legacy props keep their historical path.
259
278
  let albedo = srgbToLinear(i.c) * tex.rgb;
260
- let f0 = mix(vec3(0.04), albedo, metal);
279
+ let f0 = mix(vec3(0.04 * mtl.surface.x), albedo, metal);
261
280
  let F = fresnel(f0, vh);
262
281
  let kd = (vec3(1.0)-F) * (1.0-metal);
263
282
  let direct = (kd * albedo / 3.14159265 + F * ggx(nh,nv,nl,rough)) * u.sunCol.xyz * nl * sh * 2.5;
264
283
  let R = reflect(-V,N);
265
- let env = studioEnv(R,rough) * mtl.coat.y;
284
+ let env = environment(R,rough) * mtl.coat.y;
266
285
  let reflected = env * fresnel(f0,nv) * (1.0-rough*0.6);
267
- 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;
268
289
  let coatF = fresnel(vec3(0.04),nv) * mtl.coat.x;
269
- let coating = studioEnv(R,0.12) * coatF * mtl.coat.y;
270
- let hdr = (direct + diffuse + reflected) * (vec3(1.0)-coatF) + coating;
290
+ let coating = environment(R,mtl.surface.z) * coatF * mtl.coat.y;
291
+ // A soft fiber lobe distinguishes woven upholstery from a glossy dielectric.
292
+ // Neutral defaults preserve the existing PBR response in other consumers.
293
+ let sheen = albedo * mtl.surface.y * (u.sunCol.xyz * nl * sh * pow(1.0-vh,5.0)
294
+ + ambient * pow(1.0-nv,3.0) * 0.65) * (1.0-metal) * ao;
295
+ let hdr = (direct + diffuse * (1.0-mtl.surface.y*0.12) + reflected + sheen) * (vec3(1.0)-coatF) + coating;
271
296
  // Smooth highlight compression preserves saturated paint rather than
272
297
  // clipping reflected softboxes to a flat white patch.
273
298
  lit = linearToSrgb(hdr / (vec3(1.0) + hdr * 0.35));
@@ -563,6 +588,11 @@ export class GpuPropsRenderer {
563
588
  visibility: GPUShaderStage.FRAGMENT,
564
589
  buffer: { type: 'uniform' },
565
590
  },
591
+ ...[3, 4, 5].map((binding) => ({
592
+ binding,
593
+ visibility: GPUShaderStage.FRAGMENT,
594
+ texture: { sampleType: 'float' },
595
+ })),
566
596
  ],
567
597
  });
568
598
  this.drawBGL = [bgl0, bgl1, bgl2];
@@ -821,7 +851,7 @@ export class GpuPropsRenderer {
821
851
  const alpha = m.alpha ?? 1;
822
852
  const cutout = m.cutout ?? 0;
823
853
  const matBuf = dev.createBuffer({
824
- size: 32,
854
+ size: 64,
825
855
  usage: U.UNIFORM | U.COPY_DST,
826
856
  });
827
857
  dev.queue.writeBuffer(matBuf, 0, new Float32Array([
@@ -831,8 +861,16 @@ export class GpuPropsRenderer {
831
861
  m.metalness ?? 0,
832
862
  m.clearcoat ?? 0,
833
863
  m.reflectionStrength ?? 1,
834
- 0,
835
- 0,
864
+ m.environment ? 1 : 0,
865
+ m.environment ? m.environment.mipLevelCount - 1 : 0,
866
+ m.normalMap ? 1 : 0,
867
+ m.ormMap ? 1 : 0,
868
+ m.normalScale ?? 1,
869
+ m.detailRepeat ?? 1,
870
+ Math.max(0, Math.min(1, m.specularIntensity ?? 1)),
871
+ Math.max(0, Math.min(1, m.sheen ?? 0)),
872
+ Math.max(0.045, Math.min(1, m.clearcoatRoughness ?? 0.12)),
873
+ m.albedoRepeat ?? 1,
836
874
  ]));
837
875
  return {
838
876
  alpha,
@@ -850,6 +888,12 @@ export class GpuPropsRenderer {
850
888
  { binding: 0, resource: (tex ?? this.white).createView() },
851
889
  { binding: 1, resource: this.sampler },
852
890
  { binding: 2, resource: { buffer: matBuf } },
891
+ {
892
+ binding: 3,
893
+ resource: (m.environment ?? this.white).createView(),
894
+ },
895
+ { binding: 4, resource: (m.normalMap ?? this.white).createView() },
896
+ { binding: 5, resource: (m.ormMap ?? this.white).createView() },
853
897
  ],
854
898
  }),
855
899
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.49",
3
+ "version": "0.15.51",
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
+ }