1
1
import { ShaderUtils } from '../../../platform/graphics/shader-utils.js' ;
2
- import { BLEND_ADDITIVE , BLEND_MULTIPLICATIVE , BLEND_NORMAL } from '../../constants.js' ;
2
+ import { blendNames } from '../../constants.js' ;
3
3
import { shaderChunks } from '../chunks/chunks.js' ;
4
4
import { ShaderGenerator } from './shader-generator.js' ;
5
5
6
+ const normalTypeNames = [
7
+ 'NONE' ,
8
+ 'VERTEX' ,
9
+ 'MAP'
10
+ ] ;
11
+
6
12
class ShaderGeneratorParticle extends ShaderGenerator {
7
13
generateKey ( options ) {
8
14
const definesHash = ShaderGenerator . definesHash ( options . defines ) ;
@@ -15,95 +21,47 @@ class ShaderGeneratorParticle extends ShaderGenerator {
15
21
return key ;
16
22
}
17
23
18
- _animTex ( options ) {
19
- let vshader = '' ;
20
- vshader += options . animTexLoop ? shaderChunks . particleAnimFrameLoopVS : shaderChunks . particleAnimFrameClampVS ;
21
- vshader += shaderChunks . particleAnimTexVS ;
22
- return vshader ;
24
+ createVertexDefines ( options ) {
25
+ const vDefines = new Map ( options . defines ) ;
26
+
27
+ if ( options . mesh ) vDefines . set ( 'USE_MESH' , '' ) ;
28
+ if ( options . localSpace ) vDefines . set ( 'LOCAL_SPACE' , '' ) ;
29
+ if ( options . screenSpace ) vDefines . set ( 'SCREEN_SPACE' , '' ) ;
30
+ if ( options . animTex ) vDefines . set ( 'ANIMTEX' , '' ) ;
31
+ if ( options . soft > 0 ) vDefines . set ( 'SOFT' , '' ) ;
32
+ if ( options . stretch > 0.0 ) vDefines . set ( 'STRETCH' , '' ) ;
33
+ if ( options . customFace ) vDefines . set ( 'CUSTOM_FACE' , '' ) ;
34
+ if ( options . pack8 ) vDefines . set ( 'PACK8' , '' ) ;
35
+ if ( options . localSpace ) vDefines . set ( 'LOCAL_SPACE' , '' ) ;
36
+ if ( options . animTexLoop ) vDefines . set ( 'ANIMTEX_LOOP' , '' ) ;
37
+ if ( options . wrap ) vDefines . set ( 'WRAP' , '' ) ;
38
+ if ( options . alignToMotion ) vDefines . set ( 'ALIGN_TO_MOTION' , '' ) ;
39
+
40
+ vDefines . set ( 'NORMAL' , normalTypeNames [ options . normal ] ) ;
41
+
42
+ return vDefines ;
43
+ }
44
+
45
+ createFragmentDefines ( options ) {
46
+ const fDefines = new Map ( options . defines ) ;
47
+
48
+ if ( options . soft > 0 ) fDefines . set ( 'SOFT' , '' ) ;
49
+ if ( options . halflambert ) fDefines . set ( 'HALF_LAMBERT' , '' ) ;
50
+
51
+ fDefines . set ( 'NORMAL' , normalTypeNames [ options . normal ] ) ;
52
+ fDefines . set ( 'BLEND' , blendNames [ options . blend ] ) ;
53
+
54
+ return fDefines ;
23
55
}
24
56
25
57
createShaderDefinition ( device , options ) {
26
58
27
- const executionDefine = `#define PARTICLE_${ options . useCpu ? 'CPU' : 'GPU' } \n` ;
28
-
29
- let fshader = `#define PARTICLE\n${ executionDefine } ` ;
30
- let vshader = `#define VERTEXSHADER\n${ executionDefine } ` ;
31
-
32
- if ( options . mesh ) vshader += '#define USE_MESH\n' ;
33
- if ( options . localSpace ) vshader += '#define LOCAL_SPACE\n' ;
34
- if ( options . screenSpace ) vshader += '#define SCREEN_SPACE\n' ;
35
-
36
- if ( options . animTex ) vshader += '\nuniform vec2 animTexTilesParams;\n' ;
37
- if ( options . animTex ) vshader += '\nuniform vec4 animTexParams;\n' ;
38
- if ( options . animTex ) vshader += '\nuniform vec2 animTexIndexParams;\n' ;
39
- if ( options . normal === 2 ) vshader += '\nvarying mat3 ParticleMat;\n' ;
40
- if ( options . normal === 1 ) vshader += '\nvarying vec3 Normal;\n' ;
41
- if ( options . soft ) vshader += '\nvarying float vDepth;\n' ;
42
-
43
- const faceVS = options . customFace ? shaderChunks . particle_customFaceVS : shaderChunks . particle_billboardVS ;
44
-
45
- if ( ! options . useCpu ) {
46
- vshader += shaderChunks . particle_initVS ;
47
- vshader += ( options . pack8 ? shaderChunks . particleInputRgba8PS : shaderChunks . particleInputFloatPS ) ;
48
- if ( options . soft > 0 ) vshader += shaderChunks . screenDepthPS ;
49
- vshader += shaderChunks . particleVS ;
50
- if ( options . localSpace ) vshader += shaderChunks . particle_localShiftVS ;
51
- if ( options . animTex ) vshader += this . _animTex ( options ) ;
52
- if ( options . wrap ) vshader += shaderChunks . particle_wrapVS ;
53
- if ( options . alignToMotion ) vshader += shaderChunks . particle_pointAlongVS ;
54
- vshader += options . mesh ? shaderChunks . particle_meshVS : faceVS ;
55
- if ( options . normal === 1 ) vshader += shaderChunks . particle_normalVS ;
56
- if ( options . normal === 2 ) vshader += shaderChunks . particle_TBNVS ;
57
- if ( options . stretch > 0.0 ) vshader += shaderChunks . particle_stretchVS ;
58
- vshader += shaderChunks . particle_endVS ;
59
- if ( options . soft > 0 ) vshader += shaderChunks . particle_softVS ;
60
- } else {
61
- if ( options . soft > 0 ) vshader += shaderChunks . screenDepthPS ;
62
- vshader += shaderChunks . particle_cpuVS ;
63
- if ( options . localSpace ) vshader += shaderChunks . particle_localShiftVS ;
64
- if ( options . animTex ) vshader += this . _animTex ( options ) ;
65
- // if (options.wrap) vshader += shaderChunks.particle_wrapVS;
66
- if ( options . alignToMotion ) vshader += shaderChunks . particle_pointAlongVS ;
67
- vshader += options . mesh ? shaderChunks . particle_meshVS : faceVS ;
68
- if ( options . normal === 1 ) vshader += shaderChunks . particle_normalVS ;
69
- if ( options . normal === 2 ) vshader += shaderChunks . particle_TBNVS ;
70
- if ( options . stretch > 0.0 ) vshader += shaderChunks . particle_stretchVS ;
71
- vshader += shaderChunks . particle_cpu_endVS ;
72
- if ( options . soft > 0 ) vshader += shaderChunks . particle_softVS ;
73
- }
74
- vshader += '}\n' ;
59
+ const vDefines = this . createVertexDefines ( options ) ;
60
+ const fDefines = this . createFragmentDefines ( options ) ;
75
61
76
- if ( options . normal > 0 ) {
77
- if ( options . normal === 1 ) {
78
- fshader += '\nvarying vec3 Normal;\n' ;
79
- } else if ( options . normal === 2 ) {
80
- fshader += '\nvarying mat3 ParticleMat;\n' ;
81
- }
82
- fshader += '\nuniform vec3 lightCube[6];\n' ;
83
- }
84
- if ( options . soft ) fshader += '\nvarying float vDepth;\n' ;
85
-
86
- fshader += shaderChunks . decodePS ;
87
- fshader += '#include "gammaPS"\n' ;
88
- fshader += '#include "tonemappingPS"\n' ;
89
- fshader += '#include "fogPS"\n' ;
90
-
91
- if ( options . normal === 2 ) fshader += '\nuniform sampler2D normalMap;\n' ;
92
- if ( options . soft > 0 ) fshader += shaderChunks . screenDepthPS ;
93
- fshader += shaderChunks . particlePS ;
94
- if ( options . soft > 0 ) fshader += shaderChunks . particle_softPS ;
95
- if ( options . normal === 1 ) fshader += '\nvec3 normal = Normal;\n' ;
96
- if ( options . normal === 2 ) fshader += shaderChunks . particle_normalMapPS ;
97
- if ( options . normal > 0 ) fshader += options . halflambert ? shaderChunks . particle_halflambertPS : shaderChunks . particle_lambertPS ;
98
- if ( options . normal > 0 ) fshader += shaderChunks . particle_lightingPS ;
99
- if ( options . blend === BLEND_NORMAL ) {
100
- fshader += shaderChunks . particle_blendNormalPS ;
101
- } else if ( options . blend === BLEND_ADDITIVE ) {
102
- fshader += shaderChunks . particle_blendAddPS ;
103
- } else if ( options . blend === BLEND_MULTIPLICATIVE ) {
104
- fshader += shaderChunks . particle_blendMultiplyPS ;
105
- }
106
- fshader += shaderChunks . particle_endPS ;
62
+ const executionDefine = `PARTICLE_${ options . useCpu ? 'CPU' : 'GPU' } \n` ;
63
+ vDefines . set ( executionDefine , '' ) ;
64
+ fDefines . set ( executionDefine , '' ) ;
107
65
108
66
const includes = new Map ( Object . entries ( {
109
67
...shaderChunks ,
@@ -112,11 +70,12 @@ class ShaderGeneratorParticle extends ShaderGenerator {
112
70
113
71
return ShaderUtils . createDefinition ( device , {
114
72
name : 'ParticleShader' ,
115
- vertexCode : vshader ,
116
- fragmentCode : fshader ,
117
- fragmentDefines : options . defines ,
73
+ vertexCode : shaderChunks . particle_shaderVS ,
74
+ fragmentCode : shaderChunks . particle_shaderPS ,
75
+ fragmentDefines : fDefines ,
118
76
fragmentIncludes : includes ,
119
- vertexDefines : options . defines
77
+ vertexIncludes : includes ,
78
+ vertexDefines : vDefines
120
79
} ) ;
121
80
}
122
81
}
0 commit comments