@tinypoly/gpu-renderer 0.2.1 → 0.2.2

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 (63) hide show
  1. package/dist/frameLayout.d.ts.map +1 -1
  2. package/dist/frameLayout.js +2 -0
  3. package/dist/frameLayout.js.map +1 -1
  4. package/dist/grass.d.ts +106 -0
  5. package/dist/grass.d.ts.map +1 -0
  6. package/dist/grass.js +350 -0
  7. package/dist/grass.js.map +1 -0
  8. package/dist/index.d.ts +2 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +1 -0
  11. package/dist/index.js.map +1 -1
  12. package/dist/materialLayout.d.ts +6 -0
  13. package/dist/materialLayout.d.ts.map +1 -1
  14. package/dist/materialLayout.js +2 -0
  15. package/dist/materialLayout.js.map +1 -1
  16. package/dist/pathTracePass.d.ts +2 -2
  17. package/dist/pathTracePass.d.ts.map +1 -1
  18. package/dist/pathTracePass.js +11 -9
  19. package/dist/pathTracePass.js.map +1 -1
  20. package/dist/renderer.d.ts.map +1 -1
  21. package/dist/renderer.js +14 -3
  22. package/dist/renderer.js.map +1 -1
  23. package/dist/shaderProgram.d.ts.map +1 -1
  24. package/dist/shaderProgram.js +10 -1
  25. package/dist/shaderProgram.js.map +1 -1
  26. package/dist/subsurface.d.ts +2 -0
  27. package/dist/subsurface.d.ts.map +1 -1
  28. package/dist/subsurface.js +3 -2
  29. package/dist/subsurface.js.map +1 -1
  30. package/dist/wgsl/bsdf.wgsl.d.ts +1 -1
  31. package/dist/wgsl/bsdf.wgsl.d.ts.map +1 -1
  32. package/dist/wgsl/bsdf.wgsl.js +39 -3
  33. package/dist/wgsl/bsdf.wgsl.js.map +1 -1
  34. package/dist/wgsl/grass.wgsl.d.ts +14 -0
  35. package/dist/wgsl/grass.wgsl.d.ts.map +1 -0
  36. package/dist/wgsl/grass.wgsl.js +456 -0
  37. package/dist/wgsl/grass.wgsl.js.map +1 -0
  38. package/dist/wgsl/lights.wgsl.d.ts +1 -1
  39. package/dist/wgsl/lights.wgsl.d.ts.map +1 -1
  40. package/dist/wgsl/lights.wgsl.js +5 -0
  41. package/dist/wgsl/lights.wgsl.js.map +1 -1
  42. package/dist/wgsl/pathTrace.wgsl.d.ts +1 -1
  43. package/dist/wgsl/pathTrace.wgsl.d.ts.map +1 -1
  44. package/dist/wgsl/pathTrace.wgsl.js +34 -5
  45. package/dist/wgsl/pathTrace.wgsl.js.map +1 -1
  46. package/dist/wgsl/surface.wgsl.d.ts +1 -1
  47. package/dist/wgsl/surface.wgsl.d.ts.map +1 -1
  48. package/dist/wgsl/surface.wgsl.js +2 -0
  49. package/dist/wgsl/surface.wgsl.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/frameLayout.ts +2 -0
  52. package/src/grass.ts +523 -0
  53. package/src/index.ts +2 -0
  54. package/src/materialLayout.ts +7 -0
  55. package/src/pathTracePass.ts +11 -9
  56. package/src/renderer.ts +21 -6
  57. package/src/shaderProgram.ts +11 -1
  58. package/src/subsurface.ts +3 -2
  59. package/src/wgsl/bsdf.wgsl.ts +39 -3
  60. package/src/wgsl/grass.wgsl.ts +456 -0
  61. package/src/wgsl/lights.wgsl.ts +5 -0
  62. package/src/wgsl/pathTrace.wgsl.ts +34 -5
  63. package/src/wgsl/surface.wgsl.ts +2 -0
@@ -660,6 +660,11 @@ fn lightTransmittance(position: vec3<f32>, chosen: LightSample, rng: ptr<functio
660
660
  return vec3<f32>(1.0);
661
661
  }
662
662
 
663
+ // A blade of grass on the way blocks the light.
664
+ if (GRASS && chosen.occlude && traceGrass(position, chosen.direction, chosen.distance).found) {
665
+ return vec3<f32>(0.0);
666
+ }
667
+
663
668
  if (chosen.occlude) {
664
669
  // Most blockers are plainly opaque, and for those any triangle on the way settles it: the search stops at the
665
670
  // first one found instead of looking for the closest. Anything that lets light through goes the long way below.
@@ -132,7 +132,8 @@ fn emissionWeight(hit: Hit, direction: vec3<f32>, bsdfPdf: f32, lightsOwnDirect:
132
132
  * volume, a toon ramp read all round, a clearcoat that may face what its base turns from, and transmission.
133
133
  */
134
134
  fn treeNormal(surface: Surface) -> vec3<f32> {
135
- if (surface.kind == KIND_VOLUME || surface.kind == KIND_TOON || surface.clearcoat > 0.0 || surface.transmission > 0.0) {
135
+ if (surface.kind == KIND_VOLUME || surface.kind == KIND_TOON || surface.clearcoat > 0.0 || surface.transmission > 0.0
136
+ || (GRASS && any(surface.translucent > vec3<f32>(0.0)))) {
136
137
  return vec3<f32>(0.0);
137
138
  }
138
139
 
@@ -171,7 +172,14 @@ fn directLight(
171
172
  let toon = surface.kind == KIND_TOON;
172
173
  // Three's shadow map leaves a toon mesh's own far side unshadowed; tracing through the mesh would not.
173
174
  let skip = select(NO_MATERIAL, surface.material, toon && dot(surface.normal, chosen.direction) < 0.1);
174
- let through = lightTransmittance(position, chosen, rng, skip, medium);
175
+ var shadowFrom = position;
176
+
177
+ // Light from behind a translucent surface is looked for from its far side, past the surface itself.
178
+ if (GRASS && any(surface.translucent > vec3<f32>(0.0)) && dot(chosen.direction, surface.facing) < 0.0) {
179
+ shadowFrom = position - (surface.normal + surface.facing) * RAY_EPSILON;
180
+ }
181
+
182
+ let through = lightTransmittance(shadowFrom, chosen, rng, skip, medium);
175
183
 
176
184
  if (all(through <= vec3<f32>(0.0))) {
177
185
  return vec3<f32>(0.0);
@@ -457,7 +465,20 @@ fn traceRadiance(
457
465
  }
458
466
 
459
467
  for (var step = 0u; step < MAX_STEPS + MAX_WALK; step = step + 1u) {
460
- let hit = traceScene(origin, direction, limit);
468
+ var hit = traceScene(origin, direction, limit);
469
+ // A blade of grass in front of whatever the ray hit. A walk inside a material never meets one.
470
+ var grass: GrassHit;
471
+
472
+ if (GRASS && !(SUBSURFACE_WALK && inMedium)) {
473
+ grass = traceGrass(origin, direction, hit.distance);
474
+
475
+ if (grass.found) {
476
+ hit.distance = grass.distance;
477
+ hit.triangle = GRASS_TRIANGLE;
478
+ hit.u = 0.0;
479
+ hit.v = 0.0;
480
+ }
481
+ }
461
482
 
462
483
  // A step of the walk inside a material that ends in the medium: the light scatters there, keeping each colour's
463
484
  // share of it. It needs three numbers, which a plain hash gives for a fraction of what the sequence costs.
@@ -570,7 +591,7 @@ fn traceRadiance(
570
591
  let point = origin + direction * travel;
571
592
  var m = 0u;
572
593
 
573
- if (!scattering) {
594
+ if (!scattering && !(GRASS && grass.found)) {
574
595
  m = materialIndexOf(hit.triangle);
575
596
  }
576
597
 
@@ -582,6 +603,8 @@ fn traceRadiance(
582
603
  direction = -surface.normal;
583
604
  } else if (VOLUMES && scattering) {
584
605
  surface = volumePoint(direction, volumeAlbedo, volumeG);
606
+ } else if (GRASS && grass.found) {
607
+ surface = grassSurface(grass, direction);
585
608
  } else {
586
609
 
587
610
  // A volume's boundary is not a surface: the ray goes in or out, and the medium is what it meets.
@@ -730,7 +753,8 @@ fn traceRadiance(
730
753
  if (primary) {
731
754
  alpha = 1.0;
732
755
 
733
- if (!scattering && !fogged && afterMaterial == NO_MATERIAL && materials[m].afterProgram != NO_PROGRAM) {
756
+ if (!scattering && !(GRASS && grass.found) && !fogged && afterMaterial == NO_MATERIAL
757
+ && materials[m].afterProgram != NO_PROGRAM) {
734
758
  afterMaterial = m;
735
759
  afterTriangle = hit.triangle;
736
760
  afterBary = bary;
@@ -893,6 +917,11 @@ fn traceRadiance(
893
917
  escapeIntensity = surface.envMapIntensity;
894
918
  next = scattered.direction;
895
919
  nextOrigin = position;
920
+
921
+ // Through a translucent surface, the path carries on from its far side.
922
+ if (GRASS && dot(next, surface.facing) < 0.0 && any(surface.translucent > vec3<f32>(0.0))) {
923
+ nextOrigin = point - surface.facing * RAY_EPSILON;
924
+ }
896
925
  primary = false;
897
926
  straight = false;
898
927
  bounce = bounce + 1u;
@@ -64,6 +64,8 @@ struct Surface {
64
64
  material: u32,
65
65
  // Share of the diffuse layer that scatters inside the material instead of at its surface.
66
66
  subsurface: f32,
67
+ // What a translucent surface lets through to its far side, diffusely: grass blades. Zero for everything else.
68
+ translucent: vec3<f32>,
67
69
  };
68
70
 
69
71
  /** Barycentric weights of the two trailing vertices, for a point drawn uniformly over a triangle's area. */