@@ -5,132 +5,130 @@ import * as p from './params.ts';
5
5
import { computeBindGroupLayout as layout , ModelData } from './schemas.ts' ;
6
6
import { projectPointOnLine } from './tgsl-helpers.ts' ;
7
7
8
- export const computeShader = tgpu [ '~unstable' ]
9
- . computeFn ( {
10
- in : { gid : d . builtin . globalInvocationId } ,
11
- workgroupSize : [ p . workGroupSize ] ,
12
- } ) ( ( input ) => {
13
- const fishIndex = input . gid . x ;
14
- // TODO: replace it with struct copy when Chromium is fixed
15
- const fishData = ModelData ( {
16
- position : layout . $ . currentFishData [ fishIndex ] . position ,
17
- direction : layout . $ . currentFishData [ fishIndex ] . direction ,
18
- scale : layout . $ . currentFishData [ fishIndex ] . scale ,
19
- variant : layout . $ . currentFishData [ fishIndex ] . variant ,
20
- applySeaDesaturation :
21
- layout . $ . currentFishData [ fishIndex ] . applySeaDesaturation ,
22
- applySeaFog : layout . $ . currentFishData [ fishIndex ] . applySeaFog ,
23
- applySinWave : layout . $ . currentFishData [ fishIndex ] . applySinWave ,
24
- } ) ;
25
- let separation = d . vec3f ( ) ;
26
- let alignment = d . vec3f ( ) ;
27
- let alignmentCount = 0 ;
28
- let cohesion = d . vec3f ( ) ;
29
- let cohesionCount = 0 ;
30
- let wallRepulsion = d . vec3f ( ) ;
31
- let rayRepulsion = d . vec3f ( ) ;
8
+ export const computeShader = tgpu [ '~unstable' ] . computeFn ( {
9
+ in : { gid : d . builtin . globalInvocationId } ,
10
+ workgroupSize : [ p . workGroupSize ] ,
11
+ } ) ( ( input ) => {
12
+ const fishIndex = input . gid . x ;
13
+ // TODO: replace it with struct copy when Chromium is fixed
14
+ const fishData = ModelData ( {
15
+ position : layout . $ . currentFishData [ fishIndex ] . position ,
16
+ direction : layout . $ . currentFishData [ fishIndex ] . direction ,
17
+ scale : layout . $ . currentFishData [ fishIndex ] . scale ,
18
+ variant : layout . $ . currentFishData [ fishIndex ] . variant ,
19
+ applySeaDesaturation :
20
+ layout . $ . currentFishData [ fishIndex ] . applySeaDesaturation ,
21
+ applySeaFog : layout . $ . currentFishData [ fishIndex ] . applySeaFog ,
22
+ applySinWave : layout . $ . currentFishData [ fishIndex ] . applySinWave ,
23
+ } ) ;
24
+ let separation = d . vec3f ( ) ;
25
+ let alignment = d . vec3f ( ) ;
26
+ let alignmentCount = 0 ;
27
+ let cohesion = d . vec3f ( ) ;
28
+ let cohesionCount = 0 ;
29
+ let wallRepulsion = d . vec3f ( ) ;
30
+ let rayRepulsion = d . vec3f ( ) ;
32
31
33
- for ( let i = 0 ; i < p . fishAmount ; i += 1 ) {
34
- if ( d . u32 ( i ) === fishIndex ) {
35
- continue ;
36
- }
32
+ for ( let i = 0 ; i < p . fishAmount ; i += 1 ) {
33
+ if ( d . u32 ( i ) === fishIndex ) {
34
+ continue ;
35
+ }
37
36
38
- // TODO: replace it with struct copy when Chromium is fixed
39
- const other = ModelData ( {
40
- position : layout . $ . currentFishData [ i ] . position ,
41
- direction : layout . $ . currentFishData [ i ] . direction ,
42
- scale : layout . $ . currentFishData [ i ] . scale ,
43
- variant : layout . $ . currentFishData [ i ] . variant ,
44
- applySeaDesaturation : layout . $ . currentFishData [ i ] . applySeaDesaturation ,
45
- applySeaFog : layout . $ . currentFishData [ i ] . applySeaFog ,
46
- applySinWave : layout . $ . currentFishData [ i ] . applySinWave ,
47
- } ) ;
48
- const dist = std . length ( std . sub ( fishData . position , other . position ) ) ;
49
- if ( dist < layout . $ . fishBehavior . separationDist ) {
50
- separation = std . add (
51
- separation ,
52
- std . sub ( fishData . position , other . position ) ,
53
- ) ;
54
- }
55
- if ( dist < layout . $ . fishBehavior . alignmentDist ) {
56
- alignment = std . add ( alignment , other . direction ) ;
57
- alignmentCount = alignmentCount + 1 ;
58
- }
59
- if ( dist < layout . $ . fishBehavior . cohesionDist ) {
60
- cohesion = std . add ( cohesion , other . position ) ;
61
- cohesionCount = cohesionCount + 1 ;
62
- }
37
+ // TODO: replace it with struct copy when Chromium is fixed
38
+ const other = ModelData ( {
39
+ position : layout . $ . currentFishData [ i ] . position ,
40
+ direction : layout . $ . currentFishData [ i ] . direction ,
41
+ scale : layout . $ . currentFishData [ i ] . scale ,
42
+ variant : layout . $ . currentFishData [ i ] . variant ,
43
+ applySeaDesaturation : layout . $ . currentFishData [ i ] . applySeaDesaturation ,
44
+ applySeaFog : layout . $ . currentFishData [ i ] . applySeaFog ,
45
+ applySinWave : layout . $ . currentFishData [ i ] . applySinWave ,
46
+ } ) ;
47
+ const dist = std . length ( std . sub ( fishData . position , other . position ) ) ;
48
+ if ( dist < layout . $ . fishBehavior . separationDist ) {
49
+ separation = std . add (
50
+ separation ,
51
+ std . sub ( fishData . position , other . position ) ,
52
+ ) ;
63
53
}
64
- if ( alignmentCount > 0 ) {
65
- alignment = std . mul ( 1 / d . f32 ( alignmentCount ) , alignment ) ;
54
+ if ( dist < layout . $ . fishBehavior . alignmentDist ) {
55
+ alignment = std . add ( alignment , other . direction ) ;
56
+ alignmentCount = alignmentCount + 1 ;
66
57
}
67
- if ( cohesionCount > 0 ) {
68
- cohesion = std . sub (
69
- std . mul ( 1 / d . f32 ( cohesionCount ) , cohesion ) ,
70
- fishData . position ,
71
- ) ;
58
+ if ( dist < layout . $ . fishBehavior . cohesionDist ) {
59
+ cohesion = std . add ( cohesion , other . position ) ;
60
+ cohesionCount = cohesionCount + 1 ;
72
61
}
73
- for ( let i = 0 ; i < 3 ; i += 1 ) {
74
- const repulsion = d . vec3f ( ) ;
75
- repulsion [ i ] = 1.0 ;
76
-
77
- const axisAquariumSize = p . aquariumSize [ i ] / 2 ;
78
- const axisPosition = fishData . position [ i ] ;
79
- const distance = p . fishWallRepulsionDistance ;
62
+ }
63
+ if ( alignmentCount > 0 ) {
64
+ alignment = std . mul ( 1 / d . f32 ( alignmentCount ) , alignment ) ;
65
+ }
66
+ if ( cohesionCount > 0 ) {
67
+ cohesion = std . sub (
68
+ std . mul ( 1 / d . f32 ( cohesionCount ) , cohesion ) ,
69
+ fishData . position ,
70
+ ) ;
71
+ }
72
+ for ( let i = 0 ; i < 3 ; i += 1 ) {
73
+ const repulsion = d . vec3f ( ) ;
74
+ repulsion [ i ] = 1.0 ;
80
75
81
- if ( axisPosition > axisAquariumSize - distance ) {
82
- const str = axisPosition - ( axisAquariumSize - distance ) ;
83
- wallRepulsion = std . sub ( wallRepulsion , std . mul ( str , repulsion ) ) ;
84
- }
76
+ const axisAquariumSize = p . aquariumSize [ i ] / 2 ;
77
+ const axisPosition = fishData . position [ i ] ;
78
+ const distance = p . fishWallRepulsionDistance ;
85
79
86
- if ( axisPosition < - axisAquariumSize + distance ) {
87
- const str = - axisAquariumSize + distance - axisPosition ;
88
- wallRepulsion = std . add ( wallRepulsion , std . mul ( str , repulsion ) ) ;
89
- }
80
+ if ( axisPosition > axisAquariumSize - distance ) {
81
+ const str = axisPosition - ( axisAquariumSize - distance ) ;
82
+ wallRepulsion = std . sub ( wallRepulsion , std . mul ( str , repulsion ) ) ;
90
83
}
91
84
92
- if ( layout . $ . mouseRay . activated === 1 ) {
93
- const proj = projectPointOnLine (
94
- fishData . position ,
95
- layout . $ . mouseRay . line ,
96
- ) ;
97
- const diff = std . sub ( fishData . position , proj ) ;
98
- const limit = p . fishMouseRayRepulsionDistance ;
99
- const str = std . pow ( 2 , std . clamp ( limit - std . length ( diff ) , 0 , limit ) ) - 1 ;
100
- rayRepulsion = std . mul ( str , std . normalize ( diff ) ) ;
85
+ if ( axisPosition < - axisAquariumSize + distance ) {
86
+ const str = - axisAquariumSize + distance - axisPosition ;
87
+ wallRepulsion = std . add ( wallRepulsion , std . mul ( str , repulsion ) ) ;
101
88
}
89
+ }
102
90
103
- fishData . direction = std . add (
104
- fishData . direction ,
105
- std . mul ( layout . $ . fishBehavior . separationStr , separation ) ,
106
- ) ;
107
- fishData . direction = std . add (
108
- fishData . direction ,
109
- std . mul ( layout . $ . fishBehavior . alignmentStr , alignment ) ,
110
- ) ;
111
- fishData . direction = std . add (
112
- fishData . direction ,
113
- std . mul ( layout . $ . fishBehavior . cohesionStr , cohesion ) ,
114
- ) ;
115
- fishData . direction = std . add (
116
- fishData . direction ,
117
- std . mul ( p . fishWallRepulsionStrength , wallRepulsion ) ,
118
- ) ;
119
- fishData . direction = std . add (
120
- fishData . direction ,
121
- std . mul ( p . fishMouseRayRepulsionStrength , rayRepulsion ) ,
91
+ if ( layout . $ . mouseRay . activated === 1 ) {
92
+ const proj = projectPointOnLine (
93
+ fishData . position ,
94
+ layout . $ . mouseRay . line ,
122
95
) ;
96
+ const diff = std . sub ( fishData . position , proj ) ;
97
+ const limit = p . fishMouseRayRepulsionDistance ;
98
+ const str = std . pow ( 2 , std . clamp ( limit - std . length ( diff ) , 0 , limit ) ) - 1 ;
99
+ rayRepulsion = std . mul ( str , std . normalize ( diff ) ) ;
100
+ }
123
101
124
- fishData . direction = std . mul (
125
- std . clamp ( std . length ( fishData . direction ) , 0.0 , 0.01 ) ,
126
- std . normalize ( fishData . direction ) ,
127
- ) ;
102
+ fishData . direction = std . add (
103
+ fishData . direction ,
104
+ std . mul ( layout . $ . fishBehavior . separationStr , separation ) ,
105
+ ) ;
106
+ fishData . direction = std . add (
107
+ fishData . direction ,
108
+ std . mul ( layout . $ . fishBehavior . alignmentStr , alignment ) ,
109
+ ) ;
110
+ fishData . direction = std . add (
111
+ fishData . direction ,
112
+ std . mul ( layout . $ . fishBehavior . cohesionStr , cohesion ) ,
113
+ ) ;
114
+ fishData . direction = std . add (
115
+ fishData . direction ,
116
+ std . mul ( p . fishWallRepulsionStrength , wallRepulsion ) ,
117
+ ) ;
118
+ fishData . direction = std . add (
119
+ fishData . direction ,
120
+ std . mul ( p . fishMouseRayRepulsionStrength , rayRepulsion ) ,
121
+ ) ;
128
122
129
- const translation = std . mul (
130
- d . f32 ( std . min ( 999 , layout . $ . timePassed ) ) / 8 ,
131
- fishData . direction ,
132
- ) ;
133
- fishData . position = std . add ( fishData . position , translation ) ;
134
- layout . $ . nextFishData [ fishIndex ] = fishData ;
135
- } )
136
- . $name ( 'compute shader' ) ;
123
+ fishData . direction = std . mul (
124
+ std . clamp ( std . length ( fishData . direction ) , 0.0 , 0.01 ) ,
125
+ std . normalize ( fishData . direction ) ,
126
+ ) ;
127
+
128
+ const translation = std . mul (
129
+ d . f32 ( std . min ( 999 , layout . $ . timePassed ) ) / 8 ,
130
+ fishData . direction ,
131
+ ) ;
132
+ fishData . position = std . add ( fishData . position , translation ) ;
133
+ layout . $ . nextFishData [ fishIndex ] = fishData ;
134
+ } ) ;
0 commit comments