Skip to content

Commit 196721e

Browse files
authored
Add Demo Splat Dissolve Effects (#218)
* Demo Splat Dissolve Effects * Fly scan attribution * Update fly splat url
1 parent 937262b commit 196721e

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

examples.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
'splat-painter': '/examples/splat-painter/index.html',
238238
'splat-reveal-effects': '/examples/splat-reveal-effects/index.html',
239239
'splat-shader-effects': '/examples/splat-shader-effects/index.html',
240+
'splat-dissolve-effects': '/examples/splat-dissolve-effects/index.html',
240241
'splat-transitions': '/examples/splat-transitions/index.html',
241242
'stochastic': '/examples/stochastic/index.html',
242243
'sogs': '/examples/sogs/index.html',
@@ -440,6 +441,7 @@
440441
<a href="#splat-painter" data-example="splat-painter" class="example-link">Splat Painter</a>
441442
<a href="#splat-reveal-effects" data-example="splat-reveal-effects" class="example-link">Splat Reveal Effects</a>
442443
<a href="#splat-shader-effects" data-example="splat-shader-effects" class="example-link">Splat Shader Effects</a>
444+
<a href="#splat-dissolve-effects" data-example="splat-dissolve-effects" class="example-link">Splat Dissolve Effects</a>
443445
<a href="#splat-transitions" data-example="splat-transitions" class="example-link">Splat Transitions</a>
444446
<a href="#stochastic" data-example="stochastic" class="example-link">Stochastic splat sorting</a>
445447
<a href="#sogs" data-example="sogs" class="example-link">SOGS Compression</a>

examples/assets.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"url": "https://sparkjs.dev/assets/images/butterfly.png",
1616
"directory": "images"
1717
},
18+
"fly.spz": {
19+
"url": "https://sparkjs.dev/assets/splats/fly.spz",
20+
"directory": "splats"
21+
},
1822
"butterfly.spz": {
1923
"url": "https://sparkjs.dev/assets/splats/butterfly.spz",
2024
"directory": "splats"
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Spark • Hello World</title>
8+
<style>
9+
body {
10+
margin: 0;
11+
}
12+
</style>
13+
</head>
14+
15+
<body>
16+
<script type="importmap">
17+
{
18+
"imports": {
19+
"three": "/examples/js/vendor/three/build/three.module.js",
20+
"@sparkjsdev/spark": "/dist/spark.module.js"
21+
}
22+
}
23+
</script>
24+
<script type="module">
25+
import * as THREE from "three";
26+
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
27+
import { SplatMesh, dyno } from "@sparkjsdev/spark";
28+
import { getAssetFileURL } from "/examples/js/get-asset-url.js";
29+
30+
const flyPos = new THREE.Vector3(0, 0, -0.5);
31+
const scene = new THREE.Scene();
32+
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
33+
const renderer = new THREE.WebGLRenderer();
34+
const controls = new OrbitControls(camera, renderer.domElement);
35+
controls.target.copy(flyPos);
36+
controls.enablePan = false;
37+
renderer.setSize(window.innerWidth, window.innerHeight);
38+
document.body.appendChild(renderer.domElement);
39+
40+
window.addEventListener('resize', onWindowResize, false);
41+
function onWindowResize() {
42+
camera.aspect = window.innerWidth / window.innerHeight;
43+
camera.updateProjectionMatrix();
44+
renderer.setSize(window.innerWidth, window.innerHeight);
45+
}
46+
47+
/*
48+
Asset: fly.spz
49+
Author: Danny Bittel (https://danybittel.ch/)
50+
License: License: CC BY 4.0
51+
Source: https://www.patreon.com/cw/DanyBittel
52+
*/
53+
const splatURL = await getAssetFileURL("fly.spz");
54+
const fly = new SplatMesh({ url: splatURL });
55+
const animateT = dyno.dynoFloat(0);
56+
fly.quaternion.set(1, 0, 0, 0);
57+
fly.position.copy(flyPos);
58+
scene.add(fly);
59+
60+
fly.objectModifier = dyno.dynoBlock({ gsplat: dyno.Gsplat }, { gsplat: dyno.Gsplat }, ({ gsplat }) => {
61+
const d = new dyno.Dyno({
62+
inTypes: {
63+
gsplat: dyno.Gsplat,
64+
t: "float",
65+
},
66+
outTypes: { gsplat: dyno.Gsplat },
67+
globals: () => [
68+
dyno.unindent(`
69+
vec3 hash(vec3 p) {
70+
return fract(sin(p*123.456)*123.456);
71+
}
72+
`),
73+
],
74+
statements: ({ inputs, outputs }) =>
75+
dyno.unindentLines(`
76+
${outputs.gsplat} = ${inputs.gsplat};
77+
vec3 localPos = ${inputs.gsplat}.center;
78+
vec3 hashVal = hash(localPos);
79+
float startTime = hashVal.x * 100.0;
80+
float shouldOscillate = step(startTime, ${inputs.t});
81+
float oscillation = sin(${inputs.t} * 2.0 + hashVal.y * 6.28) * 0.5 + 0.5; // 0 to 1
82+
vec4 whiteColor = vec4(1.0, 1.0, 1.0, 1.0);
83+
vec3 moveDirection = normalize(vec3(
84+
1.0,
85+
(hashVal.y - 0.5) * 0.9,
86+
(hashVal.z - 0.5) * 0.9
87+
));
88+
float randomSpeed = fract(sin(dot(${inputs.gsplat}.center, vec3(12., 78., 45.))) * 43758.);
89+
float moveAmount = ${inputs.t} * 0.1 * randomSpeed * shouldOscillate;
90+
${outputs.gsplat}.center = ${inputs.gsplat}.center + moveDirection * moveAmount;
91+
${outputs.gsplat}.rgba = mix(${inputs.gsplat}.rgba,
92+
mix(${inputs.gsplat}.rgba, whiteColor, oscillation),
93+
shouldOscillate);
94+
// Slow fade out
95+
${outputs.gsplat}.rgba.w *= mix(1.0, 1.0 - clamp(moveAmount / 2.0, 0.0, 1.0), shouldOscillate);
96+
`),
97+
});
98+
99+
gsplat = d.apply({
100+
gsplat,
101+
t: animateT,
102+
}).gsplat;
103+
104+
return { gsplat };
105+
});
106+
107+
108+
let startTime = 0;
109+
const animate = (time) => {
110+
if (startTime === 0) startTime = time;
111+
const elapsedTime = (time - startTime) / 1000;
112+
113+
controls.update();
114+
animateT.value = elapsedTime;
115+
renderer.render(scene, camera);
116+
fly.needsUpdate = true;
117+
};
118+
renderer.setAnimationLoop(animate);
119+
</script>
120+
</body>
121+
122+
</html>

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ <h2>Examples</h2>
145145
<li><a href="/examples/splat-painter/">Splat Painter</a></li>
146146
<li><a href="/examples/splat-reveal-effects/">Splat Reveal Effects</a></li>
147147
<li><a href="/examples/splat-shader-effects/">Splat Shader Effects</a></li>
148+
<li><a href="/examples/splat-dissolve-effects/">Splat Dissolve Effects</a></li>
148149
<li><a href="/examples/splat-transitions/">Splat Transitions</a></li>
149150
<li><a href="/examples/webxr/">WebXR</a></li>
150151
<li><a href="/examples/glsl/">GLSL Shaders</a></li>

0 commit comments

Comments
 (0)