@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
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 typeWolffo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
# Three fire
|
2
|
+
|
3
|
+
[](https://github.com/username/threeFire/actions)
|
4
|
+
[](https://badge.fury.io/js/@wolffo%2Fthree-fire)
|
5
|
+
|
6
|
+
Modern TypeScript volumetric fire effect for Three.js and React Three Fiber.
|
7
|
+
|
8
|
+

|
9
|
+
|
10
|
+
## Features
|
11
|
+
|
12
|
+
- 🔥 Volumetric fire effect using ray marching
|
13
|
+
- 📦 TypeScript support with full type definitions
|
14
|
+
- ⚛️ React Three Fiber component
|
15
|
+
- 🎛️ Configurable parameters (iterations, octaves, noise scale, etc.)
|
16
|
+
- 🚀 Modern Three.js compatibility (r150+)
|
17
|
+
- 📱 Optimized for performance
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
```bash
|
22
|
+
npm install @wolffo/three-fire
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### React Three Fiber (Recommended)
|
28
|
+
|
29
|
+
```tsx
|
30
|
+
import { Canvas } from '@react-three/fiber'
|
31
|
+
import { Fire } from '@wolffo/three-fire'
|
32
|
+
|
33
|
+
function App() {
|
34
|
+
return (
|
35
|
+
<Canvas>
|
36
|
+
<Fire
|
37
|
+
texture="/fire-texture.png"
|
38
|
+
color={0xff4400}
|
39
|
+
scale={[2, 3, 2]}
|
40
|
+
position={[0, 0, 0]}
|
41
|
+
/>
|
42
|
+
</Canvas>
|
43
|
+
)
|
44
|
+
}
|
45
|
+
```
|
46
|
+
|
47
|
+
### With custom parameters
|
48
|
+
|
49
|
+
```tsx
|
50
|
+
import { Fire, useFire } from '@wolffo/three-fire'
|
51
|
+
|
52
|
+
function CustomFire() {
|
53
|
+
const fireRef = useFire()
|
54
|
+
|
55
|
+
return (
|
56
|
+
<Fire
|
57
|
+
ref={fireRef.ref}
|
58
|
+
texture="/fire-texture.png"
|
59
|
+
color={0xff6600}
|
60
|
+
magnitude={1.5}
|
61
|
+
lacunarity={2.5}
|
62
|
+
gain={0.7}
|
63
|
+
iterations={25}
|
64
|
+
octaves={4}
|
65
|
+
onUpdate={(fire, time) => {
|
66
|
+
// Custom update logic
|
67
|
+
fire.fireColor.setHSL((time * 0.1) % 1, 1, 0.5)
|
68
|
+
}}
|
69
|
+
/>
|
70
|
+
)
|
71
|
+
}
|
72
|
+
```
|
73
|
+
|
74
|
+
### Vanilla Three.js
|
75
|
+
|
76
|
+
```ts
|
77
|
+
import { FireMesh } from '@wolffo/three-fire'
|
78
|
+
import { Scene, TextureLoader } from 'three'
|
79
|
+
|
80
|
+
const scene = new Scene()
|
81
|
+
const textureLoader = new TextureLoader()
|
82
|
+
|
83
|
+
// Load fire texture
|
84
|
+
const fireTexture = textureLoader.load('/fire-texture.png')
|
85
|
+
|
86
|
+
// Create fire effect
|
87
|
+
const fire = new FireMesh({
|
88
|
+
fireTex: fireTexture,
|
89
|
+
color: 0xff4400,
|
90
|
+
magnitude: 1.3,
|
91
|
+
iterations: 20,
|
92
|
+
octaves: 3
|
93
|
+
})
|
94
|
+
|
95
|
+
scene.add(fire)
|
96
|
+
|
97
|
+
// Animation loop
|
98
|
+
function animate() {
|
99
|
+
requestAnimationFrame(animate)
|
100
|
+
|
101
|
+
fire.update(performance.now() / 1000)
|
102
|
+
renderer.render(scene, camera)
|
103
|
+
}
|
104
|
+
animate()
|
105
|
+
```
|
106
|
+
|
107
|
+
## API Reference
|
108
|
+
|
109
|
+
### FireComponent Props
|
110
|
+
|
111
|
+
| Prop | Type | Default | Description |
|
112
|
+
|------|------|---------|-------------|
|
113
|
+
| `texture` | `string \| Texture` | - | Fire texture URL or Three.js Texture object |
|
114
|
+
| `color` | `Color \| string \| number` | `0xeeeeee` | Fire color |
|
115
|
+
| `iterations` | `number` | `20` | Ray marching iterations (higher = better quality, lower performance) |
|
116
|
+
| `octaves` | `number` | `3` | Noise octaves for turbulence |
|
117
|
+
| `noiseScale` | `[number, number, number, number]` | `[1, 2, 1, 0.3]` | Noise scaling factors |
|
118
|
+
| `magnitude` | `number` | `1.3` | Fire shape intensity |
|
119
|
+
| `lacunarity` | `number` | `2.0` | Noise lacunarity |
|
120
|
+
| `gain` | `number` | `0.5` | Noise gain |
|
121
|
+
| `autoUpdate` | `boolean` | `true` | Auto-update time from useFrame |
|
122
|
+
| `onUpdate` | `(fire, time) => void` | - | Custom update callback |
|
123
|
+
|
124
|
+
### FireMesh Class
|
125
|
+
|
126
|
+
```ts
|
127
|
+
class FireMesh extends Mesh {
|
128
|
+
constructor(props: FireMeshProps)
|
129
|
+
|
130
|
+
// Methods
|
131
|
+
update(time?: number): void
|
132
|
+
|
133
|
+
// Properties
|
134
|
+
time: number
|
135
|
+
fireColor: Color
|
136
|
+
magnitude: number
|
137
|
+
lacunarity: number
|
138
|
+
gain: number
|
139
|
+
}
|
140
|
+
```
|
141
|
+
|
142
|
+
## Fire Texture
|
143
|
+
|
144
|
+
You need to provide a fire texture similar to the one shown below:
|
145
|
+
|
146
|
+

|
147
|
+
|
148
|
+
The texture should be a grayscale gradient that defines the fire's density distribution.
|
149
|
+
|
150
|
+
## Performance Tips
|
151
|
+
|
152
|
+
- Lower `iterations` for better performance (try 10-15 for mobile)
|
153
|
+
- Reduce `octaves` to 2 for simpler noise
|
154
|
+
- Use texture compression for the fire texture
|
155
|
+
- Consider using LOD (Level of Detail) for distant fires
|
156
|
+
|
157
|
+
## Development
|
158
|
+
|
159
|
+
```bash
|
160
|
+
# Install dependencies
|
161
|
+
npm install
|
162
|
+
|
163
|
+
# Build the package
|
164
|
+
npm run build
|
165
|
+
|
166
|
+
# Type checking
|
167
|
+
npm run typecheck
|
168
|
+
|
169
|
+
# Run tests
|
170
|
+
npm test
|
171
|
+
```
|
172
|
+
|
173
|
+
## Publishing
|
174
|
+
|
175
|
+
This package uses automated GitHub Actions for publishing. See [PUBLISHING.md](./PUBLISHING.md) for detailed instructions.
|
176
|
+
|
177
|
+
Quick release commands:
|
178
|
+
```bash
|
179
|
+
npm run release:patch # 1.0.0 → 1.0.1
|
180
|
+
npm run release:minor # 1.0.0 → 1.1.0
|
181
|
+
npm run release:major # 1.0.0 → 2.0.0
|
182
|
+
```
|
183
|
+
|
184
|
+
## Credits
|
185
|
+
|
186
|
+
Based on the original THREE.Fire by [mattatz](https://github.com/mattatz/THREE.Fire)
|
187
|
+
|
188
|
+
- Real-Time procedural volumetric fire - http://dl.acm.org/citation.cfm?id=1230131
|
189
|
+
- webgl-noise - https://github.com/ashima/webgl-noise
|
190
|
+
- Three.js - https://threejs.org/
|
191
|
+
|
192
|
+
## License
|
193
|
+
|
194
|
+
MIT
|
package/dist/Fire.d.ts
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
import { Mesh, ShaderMaterial, Texture, Color } from 'three';
|
2
|
+
import { FireShaderUniforms } from './FireShader';
|
3
|
+
export interface FireProps {
|
4
|
+
fireTex: Texture;
|
5
|
+
color?: Color | string | number;
|
6
|
+
iterations?: number;
|
7
|
+
octaves?: number;
|
8
|
+
noiseScale?: [number, number, number, number];
|
9
|
+
magnitude?: number;
|
10
|
+
lacunarity?: number;
|
11
|
+
gain?: number;
|
12
|
+
}
|
13
|
+
export declare class Fire extends Mesh {
|
14
|
+
material: ShaderMaterial & {
|
15
|
+
uniforms: FireShaderUniforms;
|
16
|
+
};
|
17
|
+
private _time;
|
18
|
+
constructor({ fireTex, color, iterations, octaves, noiseScale, magnitude, lacunarity, gain, }: FireProps);
|
19
|
+
update(time?: number): void;
|
20
|
+
get time(): number;
|
21
|
+
set time(value: number);
|
22
|
+
get fireColor(): Color;
|
23
|
+
set fireColor(color: Color | string | number);
|
24
|
+
get magnitude(): number;
|
25
|
+
set magnitude(value: number);
|
26
|
+
get lacunarity(): number;
|
27
|
+
set lacunarity(value: number);
|
28
|
+
get gain(): number;
|
29
|
+
set gain(value: number);
|
30
|
+
}
|
31
|
+
//# sourceMappingURL=Fire.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Fire.d.ts","sourceRoot":"","sources":["../src/Fire.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EAEJ,cAAc,EACd,OAAO,EACP,KAAK,EAMN,MAAM,OAAO,CAAA;AACd,OAAO,EAAc,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAE7D,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,CAAA;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;IAC7C,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,qBAAa,IAAK,SAAQ,IAAI;IACb,QAAQ,EAAE,cAAc,GAAG;QAAE,QAAQ,EAAE,kBAAkB,CAAA;KAAE,CAAA;IAC1E,OAAO,CAAC,KAAK,CAAI;gBAEL,EACV,OAAO,EACP,KAAgB,EAChB,UAAe,EACf,OAAW,EACX,UAA2B,EAC3B,SAAe,EACf,UAAgB,EAChB,IAAU,GACX,EAAE,SAAS;IAkCL,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI;IAWlC,IAAW,IAAI,IAAI,MAAM,CAExB;IAED,IAAW,IAAI,CAAC,KAAK,EAAE,MAAM,EAG5B;IAED,IAAW,SAAS,IAAI,KAAK,CAE5B;IAED,IAAW,SAAS,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,EAElD;IAED,IAAW,SAAS,IAAI,MAAM,CAE7B;IAED,IAAW,SAAS,CAAC,KAAK,EAAE,MAAM,EAEjC;IAED,IAAW,UAAU,IAAI,MAAM,CAE9B;IAED,IAAW,UAAU,CAAC,KAAK,EAAE,MAAM,EAElC;IAED,IAAW,IAAI,IAAI,MAAM,CAExB;IAED,IAAW,IAAI,CAAC,KAAK,EAAE,MAAM,EAE5B;CACF"}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { ReactThreeFiber } from '@react-three/fiber';
|
3
|
+
import { Fire as FireMesh, FireProps as FireMeshProps } from './Fire';
|
4
|
+
declare module '@react-three/fiber' {
|
5
|
+
interface ThreeElements {
|
6
|
+
fire: ReactThreeFiber.Object3DNode<FireMesh, typeof FireMesh>;
|
7
|
+
}
|
8
|
+
}
|
9
|
+
export interface FireProps extends Omit<FireMeshProps, 'fireTex'> {
|
10
|
+
/** Fire texture URL or texture object */
|
11
|
+
texture: string | THREE.Texture;
|
12
|
+
/** Auto-update time from useFrame (default: true) */
|
13
|
+
autoUpdate?: boolean;
|
14
|
+
/** Custom update function */
|
15
|
+
onUpdate?: (fire: FireMesh, time: number) => void;
|
16
|
+
children?: React.ReactNode;
|
17
|
+
}
|
18
|
+
export interface FireRef {
|
19
|
+
/** Fire mesh instance */
|
20
|
+
fire: FireMesh | null;
|
21
|
+
/** Update fire manually */
|
22
|
+
update: (time?: number) => void;
|
23
|
+
}
|
24
|
+
export declare const FireComponent: React.ForwardRefExoticComponent<FireProps & React.RefAttributes<FireRef>>;
|
25
|
+
export declare const useFire: () => {
|
26
|
+
ref: React.RefObject<FireRef>;
|
27
|
+
fire: FireMesh | null;
|
28
|
+
update: (time?: number) => void | undefined;
|
29
|
+
};
|
30
|
+
//# sourceMappingURL=FireComponent.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"FireComponent.d.ts","sourceRoot":"","sources":["../src/FireComponent.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA2D,MAAM,OAAO,CAAA;AAC/E,OAAO,EAAoB,eAAe,EAAa,MAAM,oBAAoB,CAAA;AACjF,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,SAAS,IAAI,aAAa,EAAE,MAAM,QAAQ,CAAA;AASrE,OAAO,QAAQ,oBAAoB,CAAC;IAClC,UAAU,aAAa;QACrB,IAAI,EAAE,eAAe,CAAC,YAAY,CAAC,QAAQ,EAAE,OAAO,QAAQ,CAAC,CAAA;KAC9D;CACF;AAED,MAAM,WAAW,SAAU,SAAQ,IAAI,CAAC,aAAa,EAAE,SAAS,CAAC;IAC/D,yCAAyC;IACzC,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC,OAAO,CAAA;IAC/B,qDAAqD;IACrD,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,6BAA6B;IAC7B,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IACjD,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;CAC3B;AAED,MAAM,WAAW,OAAO;IACtB,yBAAyB;IACzB,IAAI,EAAE,QAAQ,GAAG,IAAI,CAAA;IACrB,2BAA2B;IAC3B,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;CAChC;AAED,eAAO,MAAM,aAAa,2EAsEzB,CAAA;AAKD,eAAO,MAAM,OAAO;;;oBAKA,MAAM;CAEzB,CAAA"}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
import { Vector3, Vector4, Color, Matrix4, Texture } from 'three';
|
2
|
+
export interface FireShaderUniforms {
|
3
|
+
fireTex: {
|
4
|
+
value: Texture | null;
|
5
|
+
};
|
6
|
+
color: {
|
7
|
+
value: Color;
|
8
|
+
};
|
9
|
+
time: {
|
10
|
+
value: number;
|
11
|
+
};
|
12
|
+
seed: {
|
13
|
+
value: number;
|
14
|
+
};
|
15
|
+
invModelMatrix: {
|
16
|
+
value: Matrix4;
|
17
|
+
};
|
18
|
+
scale: {
|
19
|
+
value: Vector3;
|
20
|
+
};
|
21
|
+
noiseScale: {
|
22
|
+
value: Vector4;
|
23
|
+
};
|
24
|
+
magnitude: {
|
25
|
+
value: number;
|
26
|
+
};
|
27
|
+
lacunarity: {
|
28
|
+
value: number;
|
29
|
+
};
|
30
|
+
gain: {
|
31
|
+
value: number;
|
32
|
+
};
|
33
|
+
}
|
34
|
+
export declare const FireShader: {
|
35
|
+
readonly defines: {
|
36
|
+
readonly ITERATIONS: "20";
|
37
|
+
readonly OCTAVES: "3";
|
38
|
+
};
|
39
|
+
readonly uniforms: FireShaderUniforms;
|
40
|
+
readonly vertexShader: "\n varying vec3 vWorldPos;\n\n void main() {\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n vWorldPos = (modelMatrix * vec4(position, 1.0)).xyz;\n }\n ";
|
41
|
+
readonly fragmentShader: "\n uniform vec3 color;\n uniform float time;\n uniform float seed;\n uniform mat4 invModelMatrix;\n uniform vec3 scale;\n uniform vec4 noiseScale;\n uniform float magnitude;\n uniform float lacunarity;\n uniform float gain;\n uniform sampler2D fireTex;\n\n varying vec3 vWorldPos;\n\n // GLSL simplex noise function by ashima\n vec3 mod289(vec3 x) {\n return x - floor(x * (1.0 / 289.0)) * 289.0;\n }\n\n vec4 mod289(vec4 x) {\n return x - floor(x * (1.0 / 289.0)) * 289.0;\n }\n\n vec4 permute(vec4 x) {\n return mod289(((x * 34.0) + 1.0) * x);\n }\n\n vec4 taylorInvSqrt(vec4 r) {\n return 1.79284291400159 - 0.85373472095314 * r;\n }\n\n float snoise(vec3 v) {\n const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);\n const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);\n\n vec3 i = floor(v + dot(v, C.yyy));\n vec3 x0 = v - i + dot(i, C.xxx);\n\n vec3 g = step(x0.yzx, x0.xyz);\n vec3 l = 1.0 - g;\n vec3 i1 = min(g.xyz, l.zxy);\n vec3 i2 = max(g.xyz, l.zxy);\n\n vec3 x1 = x0 - i1 + C.xxx;\n vec3 x2 = x0 - i2 + C.yyy;\n vec3 x3 = x0 - D.yyy;\n\n i = mod289(i);\n vec4 p = permute(permute(permute(\n i.z + vec4(0.0, i1.z, i2.z, 1.0))\n + i.y + vec4(0.0, i1.y, i2.y, 1.0))\n + i.x + vec4(0.0, i1.x, i2.x, 1.0));\n\n float n_ = 0.142857142857;\n vec3 ns = n_ * D.wyz - D.xzx;\n\n vec4 j = p - 49.0 * floor(p * ns.z * ns.z);\n\n vec4 x_ = floor(j * ns.z);\n vec4 y_ = floor(j - 7.0 * x_);\n\n vec4 x = x_ * ns.x + ns.yyyy;\n vec4 y = y_ * ns.x + ns.yyyy;\n vec4 h = 1.0 - abs(x) - abs(y);\n\n vec4 b0 = vec4(x.xy, y.xy);\n vec4 b1 = vec4(x.zw, y.zw);\n\n vec4 s0 = floor(b0) * 2.0 + 1.0;\n vec4 s1 = floor(b1) * 2.0 + 1.0;\n vec4 sh = -step(h, vec4(0.0));\n\n vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;\n vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;\n\n vec3 p0 = vec3(a0.xy, h.x);\n vec3 p1 = vec3(a0.zw, h.y);\n vec3 p2 = vec3(a1.xy, h.z);\n vec3 p3 = vec3(a1.zw, h.w);\n\n vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));\n p0 *= norm.x;\n p1 *= norm.y;\n p2 *= norm.z;\n p3 *= norm.w;\n\n vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);\n m = m * m;\n return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));\n }\n\n float turbulence(vec3 p) {\n float sum = 0.0;\n float freq = 1.0;\n float amp = 1.0;\n\n for(int i = 0; i < OCTAVES; i++) {\n sum += abs(snoise(p * freq)) * amp;\n freq *= lacunarity;\n amp *= gain;\n }\n\n return sum;\n }\n\n vec4 samplerFire(vec3 p, vec4 scale) {\n vec2 st = vec2(sqrt(dot(p.xz, p.xz)), p.y);\n\n if(st.x <= 0.0 || st.x >= 1.0 || st.y <= 0.0 || st.y >= 1.0) {\n return vec4(0.0);\n }\n\n p.y -= (seed + time) * scale.w;\n p *= scale.xyz;\n\n st.y += sqrt(st.y) * magnitude * turbulence(p);\n\n if(st.y <= 0.0 || st.y >= 1.0) {\n return vec4(0.0);\n }\n\n return texture2D(fireTex, st);\n }\n\n vec3 localize(vec3 p) {\n return (invModelMatrix * vec4(p, 1.0)).xyz;\n }\n\n void main() {\n vec3 rayPos = vWorldPos;\n vec3 rayDir = normalize(rayPos - cameraPosition);\n float rayLen = 0.0288 * length(scale.xyz);\n\n vec4 col = vec4(0.0);\n\n for(int i = 0; i < ITERATIONS; i++) {\n rayPos += rayDir * rayLen;\n vec3 lp = localize(rayPos);\n lp.y += 0.5;\n lp.xz *= 2.0;\n col += samplerFire(lp, noiseScale);\n }\n\n col.a = col.r;\n gl_FragColor = col;\n }\n ";
|
42
|
+
};
|
43
|
+
//# sourceMappingURL=FireShader.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"FireShader.d.ts","sourceRoot":"","sources":["../src/FireShader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAEjE,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE;QAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAAA;KAAE,CAAA;IAClC,KAAK,EAAE;QAAE,KAAK,EAAE,KAAK,CAAA;KAAE,CAAA;IACvB,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;IACvB,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;IACvB,cAAc,EAAE;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,CAAA;IAClC,KAAK,EAAE;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,CAAA;IACzB,UAAU,EAAE;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,CAAA;IAC9B,SAAS,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;IAC5B,UAAU,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;IAC7B,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;CACxB;AAED,eAAO,MAAM,UAAU;;;;;uBAiBhB,kBAAkB;;;CA8Jf,CAAA"}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,288 @@
|
|
1
|
+
import { Texture, Color, Matrix4, Vector3, Vector4, Mesh, ShaderMaterial } from 'three';
|
2
|
+
import React from 'react';
|
3
|
+
import { ReactThreeFiber } from '@react-three/fiber';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Uniforms interface for the fire shader
|
7
|
+
*/
|
8
|
+
interface FireShaderUniforms {
|
9
|
+
/** Fire texture (grayscale mask) */
|
10
|
+
fireTex: {
|
11
|
+
value: Texture | null;
|
12
|
+
};
|
13
|
+
/** Fire color tint */
|
14
|
+
color: {
|
15
|
+
value: Color;
|
16
|
+
};
|
17
|
+
/** Current time for animation */
|
18
|
+
time: {
|
19
|
+
value: number;
|
20
|
+
};
|
21
|
+
/** Random seed for fire variation */
|
22
|
+
seed: {
|
23
|
+
value: number;
|
24
|
+
};
|
25
|
+
/** Inverse model matrix for ray marching */
|
26
|
+
invModelMatrix: {
|
27
|
+
value: Matrix4;
|
28
|
+
};
|
29
|
+
/** Scale of the fire object */
|
30
|
+
scale: {
|
31
|
+
value: Vector3;
|
32
|
+
};
|
33
|
+
/** Noise scaling parameters [x, y, z, time] */
|
34
|
+
noiseScale: {
|
35
|
+
value: Vector4;
|
36
|
+
};
|
37
|
+
/** Fire shape intensity */
|
38
|
+
magnitude: {
|
39
|
+
value: number;
|
40
|
+
};
|
41
|
+
/** Noise lacunarity (frequency multiplier) */
|
42
|
+
lacunarity: {
|
43
|
+
value: number;
|
44
|
+
};
|
45
|
+
/** Noise gain (amplitude multiplier) */
|
46
|
+
gain: {
|
47
|
+
value: number;
|
48
|
+
};
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* Volumetric fire shader using ray marching and simplex noise
|
52
|
+
*
|
53
|
+
* Based on "Real-Time procedural volumetric fire" by Alfred et al.
|
54
|
+
* Uses simplex noise for turbulence and ray marching for volume rendering.
|
55
|
+
*
|
56
|
+
* @example
|
57
|
+
* ```ts
|
58
|
+
* const material = new ShaderMaterial({
|
59
|
+
* defines: FireShader.defines,
|
60
|
+
* uniforms: FireShader.uniforms,
|
61
|
+
* vertexShader: FireShader.vertexShader,
|
62
|
+
* fragmentShader: FireShader.fragmentShader,
|
63
|
+
* transparent: true
|
64
|
+
* })
|
65
|
+
* ```
|
66
|
+
*/
|
67
|
+
declare const FireShader: {
|
68
|
+
readonly defines: {
|
69
|
+
readonly ITERATIONS: "20";
|
70
|
+
readonly OCTAVES: "3";
|
71
|
+
};
|
72
|
+
readonly uniforms: FireShaderUniforms;
|
73
|
+
readonly vertexShader: "\n varying vec3 vWorldPos;\n\n void main() {\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n vWorldPos = (modelMatrix * vec4(position, 1.0)).xyz;\n }\n ";
|
74
|
+
readonly fragmentShader: "\n uniform vec3 color;\n uniform float time;\n uniform float seed;\n uniform mat4 invModelMatrix;\n uniform vec3 scale;\n uniform vec4 noiseScale;\n uniform float magnitude;\n uniform float lacunarity;\n uniform float gain;\n uniform sampler2D fireTex;\n\n varying vec3 vWorldPos;\n\n // GLSL simplex noise function by ashima\n vec3 mod289(vec3 x) {\n return x - floor(x * (1.0 / 289.0)) * 289.0;\n }\n\n vec4 mod289(vec4 x) {\n return x - floor(x * (1.0 / 289.0)) * 289.0;\n }\n\n vec4 permute(vec4 x) {\n return mod289(((x * 34.0) + 1.0) * x);\n }\n\n vec4 taylorInvSqrt(vec4 r) {\n return 1.79284291400159 - 0.85373472095314 * r;\n }\n\n float snoise(vec3 v) {\n const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);\n const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);\n\n vec3 i = floor(v + dot(v, C.yyy));\n vec3 x0 = v - i + dot(i, C.xxx);\n\n vec3 g = step(x0.yzx, x0.xyz);\n vec3 l = 1.0 - g;\n vec3 i1 = min(g.xyz, l.zxy);\n vec3 i2 = max(g.xyz, l.zxy);\n\n vec3 x1 = x0 - i1 + C.xxx;\n vec3 x2 = x0 - i2 + C.yyy;\n vec3 x3 = x0 - D.yyy;\n\n i = mod289(i);\n vec4 p = permute(permute(permute(\n i.z + vec4(0.0, i1.z, i2.z, 1.0))\n + i.y + vec4(0.0, i1.y, i2.y, 1.0))\n + i.x + vec4(0.0, i1.x, i2.x, 1.0));\n\n float n_ = 0.142857142857;\n vec3 ns = n_ * D.wyz - D.xzx;\n\n vec4 j = p - 49.0 * floor(p * ns.z * ns.z);\n\n vec4 x_ = floor(j * ns.z);\n vec4 y_ = floor(j - 7.0 * x_);\n\n vec4 x = x_ * ns.x + ns.yyyy;\n vec4 y = y_ * ns.x + ns.yyyy;\n vec4 h = 1.0 - abs(x) - abs(y);\n\n vec4 b0 = vec4(x.xy, y.xy);\n vec4 b1 = vec4(x.zw, y.zw);\n\n vec4 s0 = floor(b0) * 2.0 + 1.0;\n vec4 s1 = floor(b1) * 2.0 + 1.0;\n vec4 sh = -step(h, vec4(0.0));\n\n vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;\n vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;\n\n vec3 p0 = vec3(a0.xy, h.x);\n vec3 p1 = vec3(a0.zw, h.y);\n vec3 p2 = vec3(a1.xy, h.z);\n vec3 p3 = vec3(a1.zw, h.w);\n\n vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));\n p0 *= norm.x;\n p1 *= norm.y;\n p2 *= norm.z;\n p3 *= norm.w;\n\n vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);\n m = m * m;\n return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));\n }\n\n float turbulence(vec3 p) {\n float sum = 0.0;\n float freq = 1.0;\n float amp = 1.0;\n\n for(int i = 0; i < OCTAVES; i++) {\n sum += abs(snoise(p * freq)) * amp;\n freq *= lacunarity;\n amp *= gain;\n }\n\n return sum;\n }\n\n vec4 samplerFire(vec3 p, vec4 scale) {\n vec2 st = vec2(sqrt(dot(p.xz, p.xz)), p.y);\n\n if(st.x <= 0.0 || st.x >= 1.0 || st.y <= 0.0 || st.y >= 1.0) {\n return vec4(0.0);\n }\n\n p.y -= (seed + time) * scale.w;\n p *= scale.xyz;\n\n st.y += sqrt(st.y) * magnitude * turbulence(p);\n\n if(st.y <= 0.0 || st.y >= 1.0) {\n return vec4(0.0);\n }\n\n return texture2D(fireTex, st);\n }\n\n vec3 localize(vec3 p) {\n return (invModelMatrix * vec4(p, 1.0)).xyz;\n }\n\n void main() {\n vec3 rayPos = vWorldPos;\n vec3 rayDir = normalize(rayPos - cameraPosition);\n float rayLen = 0.0288 * length(scale.xyz);\n\n vec4 col = vec4(0.0);\n\n for(int i = 0; i < ITERATIONS; i++) {\n rayPos += rayDir * rayLen;\n vec3 lp = localize(rayPos);\n lp.y += 0.5;\n lp.xz *= 2.0;\n col += samplerFire(lp, noiseScale);\n }\n\n // Apply color tint to the fire\n col.rgb *= color;\n col.a = col.r;\n gl_FragColor = col;\n }\n ";
|
75
|
+
};
|
76
|
+
|
77
|
+
/**
|
78
|
+
* Properties for creating a Fire instance
|
79
|
+
*/
|
80
|
+
interface FireProps$1 {
|
81
|
+
/** Fire texture (grayscale mask defining fire shape) */
|
82
|
+
fireTex: Texture;
|
83
|
+
/** Fire color tint (default: 0xeeeeee) */
|
84
|
+
color?: Color | string | number;
|
85
|
+
/** Ray marching iterations - higher = better quality, lower performance (default: 20) */
|
86
|
+
iterations?: number;
|
87
|
+
/** Noise octaves for turbulence (default: 3) */
|
88
|
+
octaves?: number;
|
89
|
+
/** Noise scaling parameters [x, y, z, time] (default: [1, 2, 1, 0.3]) */
|
90
|
+
noiseScale?: [number, number, number, number];
|
91
|
+
/** Fire shape intensity (default: 1.3) */
|
92
|
+
magnitude?: number;
|
93
|
+
/** Noise lacunarity - frequency multiplier (default: 2.0) */
|
94
|
+
lacunarity?: number;
|
95
|
+
/** Noise gain - amplitude multiplier (default: 0.5) */
|
96
|
+
gain?: number;
|
97
|
+
}
|
98
|
+
/**
|
99
|
+
* Volumetric fire effect using ray marching shaders
|
100
|
+
*
|
101
|
+
* Creates a procedural fire effect that renders as a translucent volume.
|
102
|
+
* The fire shape is defined by a grayscale texture, with white areas being
|
103
|
+
* the most dense part of the fire.
|
104
|
+
*
|
105
|
+
* @example
|
106
|
+
* ```ts
|
107
|
+
* const texture = textureLoader.load('fire.png')
|
108
|
+
* const fire = new Fire({
|
109
|
+
* fireTex: texture,
|
110
|
+
* color: 0xff4400,
|
111
|
+
* magnitude: 1.5
|
112
|
+
* })
|
113
|
+
* scene.add(fire)
|
114
|
+
*
|
115
|
+
* // In animation loop
|
116
|
+
* fire.update(time)
|
117
|
+
* ```
|
118
|
+
*/
|
119
|
+
declare class Fire extends Mesh {
|
120
|
+
material: ShaderMaterial & {
|
121
|
+
uniforms: FireShaderUniforms;
|
122
|
+
};
|
123
|
+
private _time;
|
124
|
+
/**
|
125
|
+
* Creates a new Fire instance
|
126
|
+
*
|
127
|
+
* @param props - Configuration options for the fire effect
|
128
|
+
*/
|
129
|
+
constructor({ fireTex, color, iterations, octaves, noiseScale, magnitude, lacunarity, gain, }: FireProps$1);
|
130
|
+
/**
|
131
|
+
* Updates the fire animation and matrix uniforms
|
132
|
+
*
|
133
|
+
* Call this method in your animation loop to animate the fire effect.
|
134
|
+
*
|
135
|
+
* @param time - Current time in seconds (optional)
|
136
|
+
*
|
137
|
+
* @example
|
138
|
+
* ```ts
|
139
|
+
* function animate() {
|
140
|
+
* fire.update(performance.now() / 1000)
|
141
|
+
* renderer.render(scene, camera)
|
142
|
+
* requestAnimationFrame(animate)
|
143
|
+
* }
|
144
|
+
* ```
|
145
|
+
*/
|
146
|
+
update(time?: number): void;
|
147
|
+
/**
|
148
|
+
* Current animation time in seconds
|
149
|
+
*/
|
150
|
+
get time(): number;
|
151
|
+
set time(value: number);
|
152
|
+
/**
|
153
|
+
* Fire color tint
|
154
|
+
*
|
155
|
+
* @example
|
156
|
+
* ```ts
|
157
|
+
* fire.fireColor = 'orange'
|
158
|
+
* fire.fireColor = 0xff4400
|
159
|
+
* fire.fireColor = new Color(1, 0.5, 0)
|
160
|
+
* ```
|
161
|
+
*/
|
162
|
+
get fireColor(): Color;
|
163
|
+
set fireColor(color: Color | string | number);
|
164
|
+
/**
|
165
|
+
* Fire shape intensity
|
166
|
+
*
|
167
|
+
* Higher values create more dramatic fire shapes.
|
168
|
+
* Range: 0.5 - 3.0, Default: 1.3
|
169
|
+
*/
|
170
|
+
get magnitude(): number;
|
171
|
+
set magnitude(value: number);
|
172
|
+
/**
|
173
|
+
* Noise lacunarity (frequency multiplier)
|
174
|
+
*
|
175
|
+
* Controls how much the frequency increases for each noise octave.
|
176
|
+
* Range: 1.0 - 4.0, Default: 2.0
|
177
|
+
*/
|
178
|
+
get lacunarity(): number;
|
179
|
+
set lacunarity(value: number);
|
180
|
+
/**
|
181
|
+
* Noise gain (amplitude multiplier)
|
182
|
+
*
|
183
|
+
* Controls how much the amplitude decreases for each noise octave.
|
184
|
+
* Range: 0.1 - 1.0, Default: 0.5
|
185
|
+
*/
|
186
|
+
get gain(): number;
|
187
|
+
set gain(value: number);
|
188
|
+
}
|
189
|
+
|
190
|
+
declare module '@react-three/fiber' {
|
191
|
+
interface ThreeElements {
|
192
|
+
fire: ReactThreeFiber.Object3DNode<Fire, typeof Fire>;
|
193
|
+
}
|
194
|
+
}
|
195
|
+
/**
|
196
|
+
* Props for the Fire React component
|
197
|
+
*/
|
198
|
+
interface FireProps extends Omit<FireProps$1, 'fireTex'> {
|
199
|
+
/** Fire texture URL or Three.js Texture object */
|
200
|
+
texture: string | Texture;
|
201
|
+
/** Auto-update time from useFrame (default: true) */
|
202
|
+
autoUpdate?: boolean;
|
203
|
+
/** Custom update function called each frame */
|
204
|
+
onUpdate?: (fire: Fire, time: number) => void;
|
205
|
+
/** Child components */
|
206
|
+
children?: React.ReactNode;
|
207
|
+
/** Position in 3D space */
|
208
|
+
position?: [number, number, number];
|
209
|
+
/** Rotation in radians */
|
210
|
+
rotation?: [number, number, number];
|
211
|
+
/** Scale factor (uniform or per-axis) */
|
212
|
+
scale?: [number, number, number] | number;
|
213
|
+
}
|
214
|
+
/**
|
215
|
+
* Ref interface for imperative fire control
|
216
|
+
*/
|
217
|
+
interface FireRef {
|
218
|
+
/** Fire mesh instance */
|
219
|
+
fire: Fire | null;
|
220
|
+
/** Update fire animation manually */
|
221
|
+
update: (time?: number) => void;
|
222
|
+
}
|
223
|
+
/**
|
224
|
+
* React Three Fiber component for volumetric fire effect
|
225
|
+
*
|
226
|
+
* Creates a procedural fire effect that can be easily integrated into R3F scenes.
|
227
|
+
* The component automatically handles texture loading, animation updates, and
|
228
|
+
* provides props for all fire parameters.
|
229
|
+
*
|
230
|
+
* @example
|
231
|
+
* ```tsx
|
232
|
+
* <Canvas>
|
233
|
+
* <Fire
|
234
|
+
* texture="/fire.png"
|
235
|
+
* color="orange"
|
236
|
+
* magnitude={1.5}
|
237
|
+
* scale={[2, 3, 2]}
|
238
|
+
* position={[0, 0, 0]}
|
239
|
+
* />
|
240
|
+
* </Canvas>
|
241
|
+
* ```
|
242
|
+
*
|
243
|
+
* @example With custom animation
|
244
|
+
* ```tsx
|
245
|
+
* <Fire
|
246
|
+
* texture="/fire.png"
|
247
|
+
* onUpdate={(fire, time) => {
|
248
|
+
* fire.fireColor.setHSL((time * 0.1) % 1, 1, 0.5)
|
249
|
+
* }}
|
250
|
+
* />
|
251
|
+
* ```
|
252
|
+
*/
|
253
|
+
declare const FireComponent: React.ForwardRefExoticComponent<FireProps & React.RefAttributes<FireRef>>;
|
254
|
+
/**
|
255
|
+
* Hook for easier access to fire instance and controls
|
256
|
+
*
|
257
|
+
* Provides a ref and helper methods for controlling fire imperatively.
|
258
|
+
*
|
259
|
+
* @returns Object with ref, fire instance, and update method
|
260
|
+
*
|
261
|
+
* @example
|
262
|
+
* ```tsx
|
263
|
+
* function MyComponent() {
|
264
|
+
* const fireRef = useFire()
|
265
|
+
*
|
266
|
+
* const handleClick = () => {
|
267
|
+
* if (fireRef.fire) {
|
268
|
+
* fireRef.fire.magnitude = 2.0
|
269
|
+
* }
|
270
|
+
* }
|
271
|
+
*
|
272
|
+
* return (
|
273
|
+
* <Fire ref={fireRef.ref} texture="/fire.png" />
|
274
|
+
* )
|
275
|
+
* }
|
276
|
+
* ```
|
277
|
+
*/
|
278
|
+
declare const useFire: () => {
|
279
|
+
/** Ref to pass to Fire component */
|
280
|
+
ref: React.RefObject<FireRef>;
|
281
|
+
/** Fire mesh instance (null until mounted) */
|
282
|
+
fire: Fire | null;
|
283
|
+
/** Update fire animation manually */
|
284
|
+
update: (time?: number) => void | undefined;
|
285
|
+
};
|
286
|
+
|
287
|
+
export { FireComponent as Fire, FireComponent, Fire as FireMesh, FireShader, useFire };
|
288
|
+
export type { FireProps$1 as FireMeshProps, FireProps, FireRef, FireShaderUniforms };
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,KAAK,SAAS,IAAI,aAAa,EAAE,MAAM,QAAQ,CAAA;AAC1E,OAAO,EAAE,UAAU,EAAE,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAGlE,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAGtF,OAAO,EAAE,aAAa,IAAI,IAAI,EAAE,MAAM,iBAAiB,CAAA"}
|