-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
152 lines (124 loc) · 4.16 KB
/
index.js
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
import { Scene, PerspectiveCamera, WebGLRenderer, Color, SphereGeometry, TorusKnotGeometry, TextBufferGeometry } from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { createSculptureWithGeometry, sculptToThreeJSMaterial } from "/node_modules/shader-park-core/dist/shader-park-core.esm.js";
import { spCode } from "./src/spCode.js";
import { initUIInteractions } from "./src/ui.js";
import { createEditor } from "./src/editor.js";
import { Pane } from "tweakpane";
// import { font } from "./src/helvetiker_regular1.typeface.json";
// const fonts = JSON.parse(font);
// query parameters
const urlSearchParams = new URLSearchParams(window.location.search);
const qParams = Object.fromEntries(urlSearchParams.entries());
// starter code and its UI init
const startCode = ("code" in qParams) ? decodeURI(qParams["code"]) : spCode();
const state = { code: startCode };
initUIInteractions(state);
// Sizes
const sizes = {
width: window.innerWidth,
height: window.innerHeight
}
window.addEventListener("resize", () =>
{
sizes.width = window.innerWidth;
sizes.height = window.innerHeight;
camera.aspect = sizes.width / sizes.height;
camera.updateProjectionMatrix();
renderer.setSize(sizes.width, sizes.height);
})
// Scene
const scene = new Scene();
// Camera
const camera = new PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 1000);
camera.position.z = 2;
// Renderer
const renderer = new WebGLRenderer({ antialias: true, transparent: true });
renderer.setSize(sizes.width, sizes.height);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setClearColor(new Color(1, 1, 1), 0);
document.body.appendChild(renderer.domElement);
// Controls
const controlConfig = {
enableDamping : true,
dampingFactor : 0.25,
zoomSpeed : 0.5,
rotateSpeed : 0.5
};
const controls = new OrbitControls(camera, renderer.domElement, controlConfig);
// Geometry
const sphereGeometry = new SphereGeometry(2, 45, 45);
const torusGeometry = new TorusKnotGeometry(2, .3, 100, 40);
torusGeometry.computeBoundingSphere();
torusGeometry.center();
const geometry = ("torus" in qParams) ? torusGeometry : sphereGeometry;
// Shader Park Setup
const params = {};
params.time = 0;
params.test = { "x":.2, "y": .4 };
params.scale = ("scale" in qParams) ? qParams["scale"] : 1.0;
const mesh = createSculptureWithGeometry(geometry, state.code, () => ( {
time: params.time,
_scale: params.scale
} ));
state.mesh = mesh;
scene.add(mesh);
// const pane = new Pane();
// pane.addInput(
// params, "test",
// {min: 0, max: 2 }
// );
if("text" in qParams) {
const loader = new FontLoader();
const mesh = state.mesh;
loader.load( "./helvetiker_regular1.typeface.json", function ( font ) {
mesh.geometry = new TextBufferGeometry( qParams["text"], {
font: font,
size: 2,
height: .1,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: .01,
bevelSize: .1,
bevelOffset: 0,
bevelSegments: 1
} );
mesh.geometry.computeBoundingSphere();
mesh.geometry.center();
});
}
const uniformsToExclude = { "sculptureCenter": 0, "msdf": 0, "opacity": 0, "time": 0, "stepSize": 0, "_scale" : 1, "resolution": 0};
const onCodeChange = (code) => {
state.code = code;
try {
// const newMesh = createSculpture(code, () => ( {
// time: params.time,
// } ));
// scene.remove(state.mesh);
// scene.add(newMesh);
// state.mesh = newMesh;
const mesh = state.mesh;
mesh.material = sculptToThreeJSMaterial(code);
let uniforms = mesh.material.uniformDescriptions;
uniforms = uniforms.filter(uniform => !(uniform.name in uniformsToExclude));
console.log(uniforms);
} catch (error) {
console.error(error);
}
}
// Editor
const editor = createEditor(state.code, onCodeChange);
const codeContainer = document.querySelector(".code-container");
codeContainer.appendChild(editor.dom);
// Add parameters to window
window.applications = {}
window.applications.controls = controls;
window.applications.editor = editor;
// render for each frame
const render = () => {
requestAnimationFrame(render);
params.time += 0.01;
controls.update();
renderer.render(scene, camera);
};
render();