This repository was archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathwebgl_nodes_points.html
210 lines (136 loc) · 5.83 KB
/
webgl_nodes_points.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D webgl - node particles</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> - webgl node particles example
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"v3d": "../build/v3d.module.js",
"v3d/addons/": "./jsm/",
"three/nodes": "./jsm/nodes/Nodes.js"
}
}
</script>
<script type="module">
import * as v3d from 'v3d';
import * as Nodes from 'three/nodes';
import Stats from 'v3d/addons/libs/stats.module.js';
import { GUI } from 'v3d/addons/libs/lil-gui.module.min.js';
import { TeapotGeometry } from 'v3d/addons/geometries/TeapotGeometry.js';
import { OrbitControls } from 'v3d/addons/controls/OrbitControls.js';
import { nodeFrame } from 'v3d/addons/renderers/webgl/nodes/WebGLNodes.js';
let camera, scene, renderer, stats;
init();
animate();
function init() {
camera = new v3d.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 2, 2000);
camera.position.x = 0;
camera.position.y = 100;
camera.position.z = -300;
scene = new v3d.Scene();
scene.fog = new v3d.FogExp2(0x000000, 0.001);
// geometries
const teapotGeometry = new TeapotGeometry(50, 7);
const sphereGeometry = new v3d.SphereGeometry(50, 130, 16);
const geometry = new v3d.BufferGeometry();
// buffers
const speed = [];
const intensity = [];
const size = [];
const positionAttribute = teapotGeometry.getAttribute('position');
const particleCount = positionAttribute.count;
for (let i = 0; i < particleCount; i++) {
speed.push(20 + Math.random() * 50);
intensity.push(Math.random() * .15);
size.push(30 + Math.random() * 230);
}
geometry.setAttribute('position', positionAttribute);
geometry.setAttribute('targetPosition', sphereGeometry.getAttribute('position'));
geometry.setAttribute('particleSpeed', new v3d.Float32BufferAttribute(speed, 1));
geometry.setAttribute('particleIntensity', new v3d.Float32BufferAttribute(intensity, 1));
geometry.setAttribute('particleSize', new v3d.Float32BufferAttribute(size, 1));
// maps
// Forked from: https://answers.unrealengine.com/questions/143267/emergency-need-help-with-fire-fx-weird-loop.html
const fireMap = new v3d.TextureLoader().load('textures/sprites/firetorch_1.jpg');
// nodes
const targetPosition = new Nodes.AttributeNode('targetPosition', 'vec3');
const particleSpeed = new Nodes.AttributeNode('particleSpeed', 'float');
const particleIntensity = new Nodes.AttributeNode('particleIntensity', 'float');
const particleSize = new Nodes.AttributeNode('particleSize', 'float');
const time = new Nodes.TimerNode();
const spriteSheetCount = new Nodes.ConstNode(new v3d.Vector2(6, 6));
const fireUV = new Nodes.SpriteSheetUVNode(
spriteSheetCount, // count
new Nodes.PointUVNode(), // uv
new Nodes.OperatorNode('*', time, particleSpeed) // current frame
);
const fireSprite = new Nodes.TextureNode(fireMap, fireUV);
const fire = new Nodes.OperatorNode('*', fireSprite, particleIntensity);
const lerpPosition = new Nodes.UniformNode(0);
const positionNode = new Nodes.MathNode(Nodes.MathNode.MIX, new Nodes.PositionNode(Nodes.PositionNode.LOCAL), targetPosition, lerpPosition);
// material
const material = new Nodes.PointsNodeMaterial({
depthWrite: false,
transparent: true,
sizeAttenuation: true,
blending: v3d.AdditiveBlending
});
material.colorNode = fire;
material.sizeNode = particleSize;
material.positionNode = positionNode;
const particles = new v3d.Points(geometry, material);
scene.add(particles);
// renderer
renderer = new v3d.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// stats
stats = new Stats();
document.body.appendChild(stats.dom);
// gui
const gui = new GUI();
const guiNode = { lerpPosition: 0 };
gui.add(material, 'sizeAttenuation').onChange(function() {
material.needsUpdate = true;
});
gui.add(guiNode, 'lerpPosition', 0, 1).onChange(function() {
lerpPosition.value = guiNode.lerpPosition;
});
gui.open();
// controls
const controls = new OrbitControls(camera, renderer.domElement);
controls.maxDistance = 1000;
controls.update();
// events
window.addEventListener('resize', onWindowResize);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
//
function animate() {
requestAnimationFrame(animate);
render();
stats.update();
}
function render() {
nodeFrame.update();
renderer.render(scene, camera);
}
</script>
</body>
</html>