@wolffo/three-fire 1.0.1
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.
- package/LICENSE +21 -0
- package/README.md +194 -0
- package/dist/Fire.d.ts +31 -0
- package/dist/Fire.d.ts.map +1 -0
- package/dist/FireComponent.d.ts +30 -0
- package/dist/FireComponent.d.ts.map +1 -0
- package/dist/FireShader.d.ts +43 -0
- package/dist/FireShader.d.ts.map +1 -0
- package/dist/index.d.ts +288 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.js +457 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +463 -0
- package/dist/index.js.map +1 -0
- package/dist/types/Fire.d.ts +115 -0
- package/dist/types/Fire.d.ts.map +1 -0
- package/dist/types/FireComponent.d.ts +101 -0
- package/dist/types/FireComponent.d.ts.map +1 -0
- package/dist/types/FireShader.d.ts +73 -0
- package/dist/types/FireShader.d.ts.map +1 -0
- package/dist/types/index.d.ts +33 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +71 -0
- package/src/Fire.png +0 -0
- package/src/Fire.ts +208 -0
- package/src/FireComponent.tsx +188 -0
- package/src/FireShader.ts +223 -0
- package/src/index.ts +38 -0
@@ -0,0 +1,223 @@
|
|
1
|
+
import { Vector3, Vector4, Color, Matrix4, Texture } from 'three'
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Uniforms interface for the fire shader
|
5
|
+
*/
|
6
|
+
export interface FireShaderUniforms {
|
7
|
+
/** Fire texture (grayscale mask) */
|
8
|
+
fireTex: { value: Texture | null }
|
9
|
+
/** Fire color tint */
|
10
|
+
color: { value: Color }
|
11
|
+
/** Current time for animation */
|
12
|
+
time: { value: number }
|
13
|
+
/** Random seed for fire variation */
|
14
|
+
seed: { value: number }
|
15
|
+
/** Inverse model matrix for ray marching */
|
16
|
+
invModelMatrix: { value: Matrix4 }
|
17
|
+
/** Scale of the fire object */
|
18
|
+
scale: { value: Vector3 }
|
19
|
+
/** Noise scaling parameters [x, y, z, time] */
|
20
|
+
noiseScale: { value: Vector4 }
|
21
|
+
/** Fire shape intensity */
|
22
|
+
magnitude: { value: number }
|
23
|
+
/** Noise lacunarity (frequency multiplier) */
|
24
|
+
lacunarity: { value: number }
|
25
|
+
/** Noise gain (amplitude multiplier) */
|
26
|
+
gain: { value: number }
|
27
|
+
}
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Volumetric fire shader using ray marching and simplex noise
|
31
|
+
*
|
32
|
+
* Based on "Real-Time procedural volumetric fire" by Alfred et al.
|
33
|
+
* Uses simplex noise for turbulence and ray marching for volume rendering.
|
34
|
+
*
|
35
|
+
* @example
|
36
|
+
* ```ts
|
37
|
+
* const material = new ShaderMaterial({
|
38
|
+
* defines: FireShader.defines,
|
39
|
+
* uniforms: FireShader.uniforms,
|
40
|
+
* vertexShader: FireShader.vertexShader,
|
41
|
+
* fragmentShader: FireShader.fragmentShader,
|
42
|
+
* transparent: true
|
43
|
+
* })
|
44
|
+
* ```
|
45
|
+
*/
|
46
|
+
export const FireShader = {
|
47
|
+
defines: {
|
48
|
+
ITERATIONS: '20',
|
49
|
+
OCTAVES: '3',
|
50
|
+
},
|
51
|
+
|
52
|
+
uniforms: {
|
53
|
+
fireTex: { value: null },
|
54
|
+
color: { value: new Color(0xeeeeee) },
|
55
|
+
time: { value: 0.0 },
|
56
|
+
seed: { value: 0.0 },
|
57
|
+
invModelMatrix: { value: new Matrix4() },
|
58
|
+
scale: { value: new Vector3(1, 1, 1) },
|
59
|
+
noiseScale: { value: new Vector4(1, 2, 1, 0.3) },
|
60
|
+
magnitude: { value: 1.3 },
|
61
|
+
lacunarity: { value: 2.0 },
|
62
|
+
gain: { value: 0.5 },
|
63
|
+
} as FireShaderUniforms,
|
64
|
+
|
65
|
+
vertexShader: /* glsl */ `
|
66
|
+
varying vec3 vWorldPos;
|
67
|
+
|
68
|
+
void main() {
|
69
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
70
|
+
vWorldPos = (modelMatrix * vec4(position, 1.0)).xyz;
|
71
|
+
}
|
72
|
+
`,
|
73
|
+
|
74
|
+
fragmentShader: /* glsl */ `
|
75
|
+
uniform vec3 color;
|
76
|
+
uniform float time;
|
77
|
+
uniform float seed;
|
78
|
+
uniform mat4 invModelMatrix;
|
79
|
+
uniform vec3 scale;
|
80
|
+
uniform vec4 noiseScale;
|
81
|
+
uniform float magnitude;
|
82
|
+
uniform float lacunarity;
|
83
|
+
uniform float gain;
|
84
|
+
uniform sampler2D fireTex;
|
85
|
+
|
86
|
+
varying vec3 vWorldPos;
|
87
|
+
|
88
|
+
// GLSL simplex noise function by ashima
|
89
|
+
vec3 mod289(vec3 x) {
|
90
|
+
return x - floor(x * (1.0 / 289.0)) * 289.0;
|
91
|
+
}
|
92
|
+
|
93
|
+
vec4 mod289(vec4 x) {
|
94
|
+
return x - floor(x * (1.0 / 289.0)) * 289.0;
|
95
|
+
}
|
96
|
+
|
97
|
+
vec4 permute(vec4 x) {
|
98
|
+
return mod289(((x * 34.0) + 1.0) * x);
|
99
|
+
}
|
100
|
+
|
101
|
+
vec4 taylorInvSqrt(vec4 r) {
|
102
|
+
return 1.79284291400159 - 0.85373472095314 * r;
|
103
|
+
}
|
104
|
+
|
105
|
+
float snoise(vec3 v) {
|
106
|
+
const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);
|
107
|
+
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
|
108
|
+
|
109
|
+
vec3 i = floor(v + dot(v, C.yyy));
|
110
|
+
vec3 x0 = v - i + dot(i, C.xxx);
|
111
|
+
|
112
|
+
vec3 g = step(x0.yzx, x0.xyz);
|
113
|
+
vec3 l = 1.0 - g;
|
114
|
+
vec3 i1 = min(g.xyz, l.zxy);
|
115
|
+
vec3 i2 = max(g.xyz, l.zxy);
|
116
|
+
|
117
|
+
vec3 x1 = x0 - i1 + C.xxx;
|
118
|
+
vec3 x2 = x0 - i2 + C.yyy;
|
119
|
+
vec3 x3 = x0 - D.yyy;
|
120
|
+
|
121
|
+
i = mod289(i);
|
122
|
+
vec4 p = permute(permute(permute(
|
123
|
+
i.z + vec4(0.0, i1.z, i2.z, 1.0))
|
124
|
+
+ i.y + vec4(0.0, i1.y, i2.y, 1.0))
|
125
|
+
+ i.x + vec4(0.0, i1.x, i2.x, 1.0));
|
126
|
+
|
127
|
+
float n_ = 0.142857142857;
|
128
|
+
vec3 ns = n_ * D.wyz - D.xzx;
|
129
|
+
|
130
|
+
vec4 j = p - 49.0 * floor(p * ns.z * ns.z);
|
131
|
+
|
132
|
+
vec4 x_ = floor(j * ns.z);
|
133
|
+
vec4 y_ = floor(j - 7.0 * x_);
|
134
|
+
|
135
|
+
vec4 x = x_ * ns.x + ns.yyyy;
|
136
|
+
vec4 y = y_ * ns.x + ns.yyyy;
|
137
|
+
vec4 h = 1.0 - abs(x) - abs(y);
|
138
|
+
|
139
|
+
vec4 b0 = vec4(x.xy, y.xy);
|
140
|
+
vec4 b1 = vec4(x.zw, y.zw);
|
141
|
+
|
142
|
+
vec4 s0 = floor(b0) * 2.0 + 1.0;
|
143
|
+
vec4 s1 = floor(b1) * 2.0 + 1.0;
|
144
|
+
vec4 sh = -step(h, vec4(0.0));
|
145
|
+
|
146
|
+
vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
|
147
|
+
vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
|
148
|
+
|
149
|
+
vec3 p0 = vec3(a0.xy, h.x);
|
150
|
+
vec3 p1 = vec3(a0.zw, h.y);
|
151
|
+
vec3 p2 = vec3(a1.xy, h.z);
|
152
|
+
vec3 p3 = vec3(a1.zw, h.w);
|
153
|
+
|
154
|
+
vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
|
155
|
+
p0 *= norm.x;
|
156
|
+
p1 *= norm.y;
|
157
|
+
p2 *= norm.z;
|
158
|
+
p3 *= norm.w;
|
159
|
+
|
160
|
+
vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
|
161
|
+
m = m * m;
|
162
|
+
return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));
|
163
|
+
}
|
164
|
+
|
165
|
+
float turbulence(vec3 p) {
|
166
|
+
float sum = 0.0;
|
167
|
+
float freq = 1.0;
|
168
|
+
float amp = 1.0;
|
169
|
+
|
170
|
+
for(int i = 0; i < OCTAVES; i++) {
|
171
|
+
sum += abs(snoise(p * freq)) * amp;
|
172
|
+
freq *= lacunarity;
|
173
|
+
amp *= gain;
|
174
|
+
}
|
175
|
+
|
176
|
+
return sum;
|
177
|
+
}
|
178
|
+
|
179
|
+
vec4 samplerFire(vec3 p, vec4 scale) {
|
180
|
+
vec2 st = vec2(sqrt(dot(p.xz, p.xz)), p.y);
|
181
|
+
|
182
|
+
if(st.x <= 0.0 || st.x >= 1.0 || st.y <= 0.0 || st.y >= 1.0) {
|
183
|
+
return vec4(0.0);
|
184
|
+
}
|
185
|
+
|
186
|
+
p.y -= (seed + time) * scale.w;
|
187
|
+
p *= scale.xyz;
|
188
|
+
|
189
|
+
st.y += sqrt(st.y) * magnitude * turbulence(p);
|
190
|
+
|
191
|
+
if(st.y <= 0.0 || st.y >= 1.0) {
|
192
|
+
return vec4(0.0);
|
193
|
+
}
|
194
|
+
|
195
|
+
return texture2D(fireTex, st);
|
196
|
+
}
|
197
|
+
|
198
|
+
vec3 localize(vec3 p) {
|
199
|
+
return (invModelMatrix * vec4(p, 1.0)).xyz;
|
200
|
+
}
|
201
|
+
|
202
|
+
void main() {
|
203
|
+
vec3 rayPos = vWorldPos;
|
204
|
+
vec3 rayDir = normalize(rayPos - cameraPosition);
|
205
|
+
float rayLen = 0.0288 * length(scale.xyz);
|
206
|
+
|
207
|
+
vec4 col = vec4(0.0);
|
208
|
+
|
209
|
+
for(int i = 0; i < ITERATIONS; i++) {
|
210
|
+
rayPos += rayDir * rayLen;
|
211
|
+
vec3 lp = localize(rayPos);
|
212
|
+
lp.y += 0.5;
|
213
|
+
lp.xz *= 2.0;
|
214
|
+
col += samplerFire(lp, noiseScale);
|
215
|
+
}
|
216
|
+
|
217
|
+
// Apply color tint to the fire
|
218
|
+
col.rgb *= color;
|
219
|
+
col.a = col.r;
|
220
|
+
gl_FragColor = col;
|
221
|
+
}
|
222
|
+
`,
|
223
|
+
} as const
|
package/src/index.ts
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Modern TypeScript volumetric fire effect for Three.js and React Three Fiber
|
3
|
+
*
|
4
|
+
* This package provides both vanilla Three.js classes and React Three Fiber components
|
5
|
+
* for creating realistic volumetric fire effects using ray marching shaders.
|
6
|
+
*
|
7
|
+
* @example Vanilla Three.js
|
8
|
+
* ```ts
|
9
|
+
* import { FireMesh } from '@wolffo/three-fire'
|
10
|
+
*
|
11
|
+
* const fire = new FireMesh({ fireTex: texture })
|
12
|
+
* scene.add(fire)
|
13
|
+
* fire.update(time)
|
14
|
+
* ```
|
15
|
+
*
|
16
|
+
* @example React Three Fiber
|
17
|
+
* ```tsx
|
18
|
+
* import { Fire } from '@wolffo/three-fire'
|
19
|
+
*
|
20
|
+
* <Canvas>
|
21
|
+
* <Fire texture="/fire.png" color="orange" />
|
22
|
+
* </Canvas>
|
23
|
+
* ```
|
24
|
+
*/
|
25
|
+
|
26
|
+
// Vanilla Three.js exports
|
27
|
+
/** Fire mesh class for vanilla Three.js usage */
|
28
|
+
export { Fire as FireMesh, type FireProps as FireMeshProps } from './Fire'
|
29
|
+
/** Fire shader definition and uniforms */
|
30
|
+
export { FireShader, type FireShaderUniforms } from './FireShader'
|
31
|
+
|
32
|
+
// React Three Fiber exports
|
33
|
+
/** React component for fire effect */
|
34
|
+
export { FireComponent, useFire, type FireRef, type FireProps } from './FireComponent'
|
35
|
+
|
36
|
+
// Default export (React component)
|
37
|
+
/** Default Fire component for React Three Fiber */
|
38
|
+
export { FireComponent as Fire } from './FireComponent'
|