-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
gsplat-many.mjs
174 lines (144 loc) · 5.89 KB
/
gsplat-many.mjs
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
import * as pc from 'playcanvas';
/**
* @typedef {import('../../options.mjs').ExampleOptions} ExampleOptions
* @param {import('../../options.mjs').ExampleOptions} options - The example options.
* @returns {Promise<pc.AppBase>} The example application.
*/
async function example({ canvas, deviceType, assetPath, scriptsPath, glslangPath, twgslPath, pcx, files }) {
const gfxOptions = {
deviceTypes: [deviceType],
glslangUrl: glslangPath + 'glslang.js',
twgslUrl: twgslPath + 'twgsl.js',
// disable antialiasing as gaussian splats do not benefit from it and it's expensive
antialias: false
};
const device = await pc.createGraphicsDevice(canvas, gfxOptions);
const createOptions = new pc.AppOptions();
createOptions.graphicsDevice = device;
createOptions.mouse = new pc.Mouse(document.body);
createOptions.touch = new pc.TouchDevice(document.body);
createOptions.componentSystems = [
pc.RenderComponentSystem,
pc.CameraComponentSystem,
pc.LightComponentSystem,
pc.ScriptComponentSystem,
pc.GSplatComponentSystem
];
createOptions.resourceHandlers = [
// @ts-ignore
pc.TextureHandler,
// @ts-ignore
pc.ContainerHandler,
// @ts-ignore
pc.ScriptHandler,
// @ts-ignore
pc.GSplatHandler
];
const app = new pc.AppBase(canvas);
app.init(createOptions);
// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
// Ensure canvas is resized when window changes size
const resize = () => app.resizeCanvas();
window.addEventListener('resize', resize);
app.on('destroy', () => {
window.removeEventListener('resize', resize);
});
const assets = {
gallery: new pc.Asset('gallery', 'container', { url: assetPath + 'models/vr-gallery.glb' }),
guitar: new pc.Asset('gsplat', 'gsplat', { url: assetPath + 'splats/guitar.ply' }),
biker: new pc.Asset('gsplat', 'gsplat', { url: assetPath + 'splats/biker.ply' }),
orbit: new pc.Asset('script', 'script', { url: scriptsPath + 'camera/orbit-camera.js' })
};
const assetListLoader = new pc.AssetListLoader(Object.values(assets), app.assets);
assetListLoader.load(() => {
app.start();
app.scene.toneMapping = pc.TONEMAP_ACES;
// get the instance of the gallery and set up with render component
const galleryEntity = assets.gallery.resource.instantiateRenderEntity();
app.root.addChild(galleryEntity);
// Create an Entity with a camera component
const camera = new pc.Entity();
camera.addComponent("camera", {
clearColor: new pc.Color(0.2, 0.2, 0.2)
});
camera.setLocalPosition(-3, 1, 2);
const createSplatInstance = (name, resource, px, py, pz, scale, vertex, fragment) => {
const splat = resource.instantiate({
debugRender: false,
fragment: fragment,
vertex: vertex
});
splat.name = name;
splat.setLocalPosition(px, py, pz);
splat.setLocalScale(scale, scale, scale);
app.root.addChild(splat);
return splat;
};
const guitar = createSplatInstance('guitar', assets.guitar.resource, 0, 0.8, 0, 0.4, files['shader.vert'], files['shader.frag']);
const biker1 = createSplatInstance('biker1', assets.biker.resource, -1.5, 0.05, 0, 0.7);
// clone the biker and add the clone to the scene
const biker2 = biker1.clone();
biker2.setLocalPosition(1.5, 0.05, 0);
biker2.rotate(0, 150, 0);
app.root.addChild(biker2);
// add orbit camera script with a mouse and a touch support
camera.addComponent("script");
camera.script.create("orbitCamera", {
attributes: {
inertiaFactor: 0.2,
focusEntity: guitar,
distanceMax: 60,
frameOnStart: false
}
});
camera.script.create("orbitCameraInputMouse");
camera.script.create("orbitCameraInputTouch");
app.root.addChild(camera);
let currentTime = 0;
app.on("update", function (dt) {
currentTime += dt;
const material = guitar.gsplat.material;
material.setParameter('uTime', currentTime);
});
});
return app;
}
export class GSplatManyExample {
static CATEGORY = 'Loaders';
static example = example;
static FILES = {
'shader.vert': /* glsl */`
uniform float uTime;
varying float height;
void main(void)
{
// evaluate center of the splat in object space
vec3 centerLocal = evalCenter();
// modify it
float heightIntensity = centerLocal.y * 0.2;
centerLocal.x += sin(uTime * 5.0 + centerLocal.y) * 0.3 * heightIntensity;
// output y-coordinate
height = centerLocal.y;
// evaluate the rest of the splat using world space center
vec4 centerWorld = matrix_model * vec4(centerLocal, 1.0);
gl_Position = evalSplat(centerWorld);
}
`,
'shader.frag': /* glsl */`
uniform float uTime;
varying float height;
void main(void)
{
// get splat color and alpha
gl_FragColor = evalSplat();
// modify it
vec3 gold = vec3(1.0, 0.85, 0.0);
float sineValue = abs(sin(uTime * 5.0 + height));
float blend = smoothstep(0.9, 1.0, sineValue);
gl_FragColor.xyz = mix(gl_FragColor.xyz, gold, blend);
}
`
};
}