-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmokeSimple.html
183 lines (157 loc) · 5.42 KB
/
SmokeSimple.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SmokeSimple</title>
</head>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
}
.info {
position: fixed;
}
</style>
<body>
</body>
<div id="info"></div>
<script id="dfvert" type="x-shader/x-vertex">
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<script id="fragShader" type="x-shader/x-fragment">
uniform vec2 res;//The width and height of our screen
uniform sampler2D bufferTexture;//Our input texture
uniform vec3 smokeSource;//The x,y are the posiiton. The z is the power/density
void main() {
vec2 uv = gl_FragCoord.xy / res; // Normalized screen coordinates
gl_FragColor = vec4(uv, 0.5, 1.0); // Gradient based on screen position
}
</script>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from "three";
var scene;
var camera;
var renderer;
function scene_setup() {
//This is the basic scene setup
scene = new THREE.Scene();
var width = window.innerWidth;
var height = window.innerHeight;
//Note that we're using an orthographic camera here rather than a prespective
camera = new THREE.OrthographicCamera(width / - 2, width / 2, height / 2, height / - 2, 1, 1000);
camera.position.z = 2;
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
//Initialize the Threejs scene
scene_setup();
var bufferScene;
var textureA;
var textureB;
var bufferMaterial;
var plane;
var bufferObject;
var finalMaterial;
var quad;
function buffer_texture_setup() {
//Create buffer scene
bufferScene = new THREE.Scene();
//Create 2 buffer textures
textureA = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, {
minFilter: THREE.LinearFilter,
magFilter: THREE.NearestFilter,
format: THREE.RGBAFormat
});
textureB = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, {
minFilter: THREE.LinearFilter,
magFilter: THREE.NearestFilter,
format: THREE.RGBAFormat
});
//Pass textureA to shader
bufferMaterial = new THREE.ShaderMaterial({
uniforms: {
bufferTexture: { type: "t", value: textureA },
res: { type: 'v2', value: new THREE.Vector2(window.innerWidth, window.innerHeight) },//Keeps the resolution
smokeSource: { type: "v3", value: new THREE.Vector3(0, 0, 0) }///This keeps the position of the mouse and whether it was clicked or not
},
fragmentShader: document.getElementById('fragShader').innerHTML
});
plane = new THREE.PlaneGeometry(window.innerWidth, window.innerHeight);
bufferObject = new THREE.Mesh(plane, bufferMaterial);
bufferScene.add(bufferObject);
//Draw textureB to screen
finalMaterial = new THREE.ShaderMaterial({
uniforms: {
outMap: {
value: textureB
}
},
vertexShader: document.getElementById('dfvert').innerHTML,
fragmentShader: `
uniform sampler2D outMap;
varying vec2 vUv;
void main() {
vec4 color = texture2D(outMap, vUv);
gl_FragColor = vec4(color.rgb, 1.0); // Pass through the texture color
}
`
})
quad = new THREE.Mesh(plane, finalMaterial);
scene.add(quad);
}
buffer_texture_setup();
//Send position of smoke source with value
var mouseDown = false;
function UpdateMousePosition(X, Y) {
var mouseX = X;
var mouseY = window.innerHeight - Y;
bufferMaterial.uniforms.smokeSource.value.x = mouseX;
bufferMaterial.uniforms.smokeSource.value.y = mouseY;
}
document.onmousemove = function (event) {
UpdateMousePosition(event.clientX, event.clientY)
}
document.onmousedown = function (event) {
mouseDown = true;
bufferMaterial.uniforms.smokeSource.value.z = 0.5; // Increase density for stronger smoke
}
document.onmouseup = function (event) {
mouseDown = false;
bufferMaterial.uniforms.smokeSource.value.z = 0.0; // No smoke when mouse is not pressed
}
//Render everything!
function render() {
requestAnimationFrame(render);
// Draw to textureB
renderer.setRenderTarget(textureB);
renderer.render(bufferScene, camera);
// Swap textureA and B
var t = textureA;
textureA = textureB;
textureB = t;
finalMaterial.uniforms.outMap.value = textureB;
bufferMaterial.uniforms.bufferTexture.value = textureA;
console.log(textureB,textureA)
// Finally, draw to the screen
renderer.setRenderTarget(null);
renderer.render(scene, camera);
}
render();
</script>
</html>