@@ -10,7 +10,7 @@ use bevy_ecs::{
1010} ;
1111use bevy_math:: {
1212 curve:: { EaseFunction , EasingCurve , Interval } ,
13- ops, Curve , FloatPow , U8Vec3 , Vec3 , VectorSpace ,
13+ ops, Curve , FloatPow , U8Vec3 , Vec3 , Vec4 , VectorSpace ,
1414} ;
1515use bevy_reflect:: TypePath ;
1616use bevy_render:: {
@@ -143,18 +143,14 @@ impl ScatteringMedium {
143143 ScatteringTerm {
144144 absorption : Vec3 :: ZERO ,
145145 scattering : Vec3 :: new ( 5.802e-6 , 13.558e-6 , 33.100e-6 ) ,
146- // falloff: Falloff::Exponential { scale: 12.5 }, //TODO: check if matches reference
147- falloff : Falloff :: Linear ,
146+ falloff : Falloff :: Exponential { scale : 12.5 } , //TODO: check if matches reference
148147 phase : PhaseFunction :: Rayleigh ,
149- // phase: PhaseFunction::Isotropic,
150148 } ,
151149 ScatteringTerm {
152150 absorption : Vec3 :: splat ( 3.996e-6 ) ,
153151 scattering : Vec3 :: splat ( 0.444e-6 ) ,
154- // falloff: Falloff::Exponential { scale: 83.5 }, //TODO: check if matches reference
155- falloff : Falloff :: Linear ,
152+ falloff : Falloff :: Exponential { scale : 83.5 } , //TODO: check if matches reference
156153 phase : PhaseFunction :: Mie { bias : 0.8 } ,
157- // phase: PhaseFunction::Isotropic,
158154 } ,
159155 ScatteringTerm {
160156 absorption : Vec3 :: new ( 0.650e-6 , 1.881e-6 , 0.085e-6 ) ,
@@ -264,10 +260,8 @@ pub struct GpuScatteringMedium {
264260 pub terms : SmallVec < [ ScatteringTerm ; 1 ] > ,
265261 pub falloff_resolution : u32 ,
266262 pub phase_resolution : u32 ,
267- pub density_max : Vec3 ,
268263 pub density_lut : Texture ,
269264 pub density_lut_view : TextureView ,
270- pub scattering_max : Vec3 ,
271265 pub scattering_lut : Texture ,
272266 pub scattering_lut_view : TextureView ,
273267}
@@ -283,55 +277,33 @@ impl RenderAsset for GpuScatteringMedium {
283277 ( render_device, render_queue) : & mut SystemParamItem < Self :: Param > ,
284278 _previous_asset : Option < & Self > ,
285279 ) -> Result < Self , PrepareAssetError < Self :: SourceAsset > > {
286- let mut density: Vec < Vec3 > = Vec :: with_capacity ( source_asset. falloff_resolution as usize ) ;
287- let mut density_lut_data: Vec < u8 > =
288- Vec :: with_capacity ( 2 * source_asset. falloff_resolution as usize * size_of :: < u32 > ( ) ) ;
289-
290- let mut density_max = Vec3 :: ZERO ;
280+ let mut density: Vec < Vec4 > =
281+ Vec :: with_capacity ( 2 * source_asset. falloff_resolution as usize ) ;
291282
292283 density. extend ( ( 0 ..source_asset. falloff_resolution ) . map ( |i| {
293284 let falloff = ( i as f32 + 0.5 ) / source_asset. falloff_resolution as f32 ;
294285
295- let absorption = source_asset
286+ source_asset
296287 . terms
297288 . iter ( )
298- . map ( |term| term. absorption * term. falloff . sample ( falloff) )
299- . sum ( ) ;
300-
301- density_max = density_max. max ( absorption) ;
302- absorption
289+ . map ( |term| term. absorption . extend ( 0.0 ) * term. falloff . sample ( falloff) )
290+ . sum :: < Vec4 > ( )
303291 } ) ) ;
304292
305293 density. extend ( ( 0 ..source_asset. falloff_resolution ) . map ( |i| {
306294 let falloff = ( i as f32 + 0.5 ) / source_asset. falloff_resolution as f32 ;
307295
308- let scattering = source_asset
296+ source_asset
309297 . terms
310298 . iter ( )
311- . map ( |term| term. scattering * term. falloff . sample ( falloff) )
312- . sum ( ) ;
313-
314- density_max = density_max. max ( scattering) ;
315- scattering
316- } ) ) ;
317-
318- density_lut_data. extend ( density. iter ( ) . flat_map ( |absorption| {
319- ( * absorption * 255.0 / density_max)
320- . as_u8vec3 ( )
321- . extend ( 0 )
322- . to_array ( )
299+ . map ( |term| term. scattering . extend ( 0.0 ) * term. falloff . sample ( falloff) )
300+ . sum :: < Vec4 > ( )
323301 } ) ) ;
324302
325- let mut scattering: Vec < Vec3 > = Vec :: with_capacity (
303+ let mut scattering: Vec < Vec4 > = Vec :: with_capacity (
326304 source_asset. falloff_resolution as usize * source_asset. phase_resolution as usize ,
327305 ) ;
328- let mut scattering_lut_data: Vec < u8 > = Vec :: with_capacity (
329- source_asset. falloff_resolution as usize
330- * source_asset. phase_resolution as usize
331- * size_of :: < u32 > ( ) ,
332- ) ;
333306
334- let mut scattering_max = Vec3 :: ZERO ;
335307 scattering. extend (
336308 ( 0 ..source_asset. falloff_resolution * source_asset. phase_resolution ) . map ( |raw_i| {
337309 let i = raw_i % source_asset. phase_resolution ;
@@ -340,28 +312,18 @@ impl RenderAsset for GpuScatteringMedium {
340312 let phase = ( j as f32 + 0.5 ) / source_asset. phase_resolution as f32 ;
341313 let neg_l_dot_v = phase * 2.0 - 1.0 ;
342314
343- let scattering = source_asset
315+ source_asset
344316 . terms
345317 . iter ( )
346318 . map ( |term| {
347- term. scattering
319+ term. scattering . extend ( 0.0 )
348320 * term. falloff . sample ( falloff)
349321 * term. phase . sample ( neg_l_dot_v)
350322 } )
351- . sum ( ) ;
352-
353- scattering_max = scattering_max. max ( scattering) ;
354- scattering
323+ . sum :: < Vec4 > ( )
355324 } ) ,
356325 ) ;
357326
358- scattering_lut_data. extend ( scattering. iter ( ) . flat_map ( |scattering| {
359- ( * scattering * 255.0 / scattering_max)
360- . as_u8vec3 ( )
361- . extend ( 0 )
362- . to_array ( )
363- } ) ) ;
364-
365327 let density_lut = render_device. create_texture_with_data (
366328 render_queue,
367329 & TextureDescriptor {
@@ -379,12 +341,12 @@ impl RenderAsset for GpuScatteringMedium {
379341 mip_level_count : 1 ,
380342 sample_count : 1 ,
381343 dimension : TextureDimension :: D2 ,
382- format : TextureFormat :: Rgba8Unorm ,
344+ format : TextureFormat :: Rgba32Float ,
383345 usage : TextureUsages :: TEXTURE_BINDING ,
384346 view_formats : & [ ] ,
385347 } ,
386348 TextureDataOrder :: LayerMajor ,
387- density_lut_data . as_slice ( ) ,
349+ bytemuck :: cast_slice ( density . as_slice ( ) ) ,
388350 ) ;
389351
390352 let density_lut_view = density_lut. create_view ( & TextureViewDescriptor {
@@ -414,12 +376,12 @@ impl RenderAsset for GpuScatteringMedium {
414376 mip_level_count : 1 ,
415377 sample_count : 1 ,
416378 dimension : TextureDimension :: D2 ,
417- format : TextureFormat :: Rgba8Unorm ,
379+ format : TextureFormat :: Rgba32Float ,
418380 usage : TextureUsages :: TEXTURE_BINDING ,
419381 view_formats : & [ ] ,
420382 } ,
421383 TextureDataOrder :: LayerMajor ,
422- scattering_lut_data . as_slice ( ) ,
384+ bytemuck :: cast_slice ( scattering . as_slice ( ) ) ,
423385 ) ;
424386
425387 let scattering_lut_view = scattering_lut. create_view ( & TextureViewDescriptor {
@@ -436,10 +398,8 @@ impl RenderAsset for GpuScatteringMedium {
436398 terms : source_asset. terms ,
437399 falloff_resolution : source_asset. falloff_resolution ,
438400 phase_resolution : source_asset. phase_resolution ,
439- density_max,
440401 density_lut,
441402 density_lut_view,
442- scattering_max,
443403 scattering_lut,
444404 scattering_lut_view,
445405 } )
0 commit comments