Skip to content

Commit 1165f45

Browse files
committed
sand pack editor
1 parent 0b0c43c commit 1165f45

File tree

5 files changed

+500
-11
lines changed

5 files changed

+500
-11
lines changed

components/spStarterTempaltes.tsx

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
export const minimalRendererIndexHTML = `<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Shader Park</title>
5+
<meta charset="utf-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="stylesheet" href="/styles.css">
9+
</head>
10+
11+
<body>
12+
<canvas class="my-canvas"></canvas>
13+
<script type="module">
14+
import { sculptToMinimalRenderer } from 'https://unpkg.com/shader-park-core/dist/shader-park-core.esm.js';
15+
16+
fetch('./spCode.txt')
17+
.then(response => response.text())
18+
.then(spCode => {
19+
let canvas = document.querySelector('.my-canvas');
20+
// This converts your Shader Park code into a shader and renders it to the my-canvas element
21+
sculptToMinimalRenderer(canvas, spCode);
22+
});
23+
</script>
24+
</body>
25+
</html>
26+
`;
27+
28+
export const shaderParkStartCode = `rotateY(mouse.x * PI / 2 + time * .5);
29+
rotateX(mouse.y * PI / 2);
30+
metal(.5);
31+
shine(.4);
32+
color(getRayDirection() + .2);
33+
rotateY(getRayDirection().y * 4 + time);
34+
boxFrame(vec3(.4), .02);
35+
expand(.02);
36+
blend(nsin(time) * .6);
37+
sphere(.2);
38+
`;
39+
40+
export const minimalRendererCSS = `
41+
42+
body {
43+
font-family: helvetica, arial, sans-serif;
44+
width: 100vw;
45+
height: 100vh;
46+
margin : 0px;
47+
padding : 0px;
48+
border : 0px;
49+
}
50+
51+
canvas {
52+
width: 100%;
53+
height: 100%;
54+
margin : 0px;
55+
padding : 0px;
56+
border : 0px;
57+
background-color : transparent;
58+
}
59+
`

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"start": "next start"
77
},
88
"dependencies": {
9+
"@codesandbox/sandpack-react": "^2.6.9",
910
"@headlessui/react": "^1.6.6",
1011
"@heroicons/react": "^1.0.6",
1112
"@tailwindcss/aspect-ratio": "^0.4.0",
@@ -16,7 +17,7 @@
1617
"react-dom": "18.1.0",
1718
"react-firebase-hooks": "^5.0.3",
1819
"react-firebaseui": "^6.0.0",
19-
"shader-park-core": "^0.1.30"
20+
"shader-park-core": "^0.1.41"
2021
},
2122
"devDependencies": {
2223
"@types/node": "17.0.35",

pages/new.tsx

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { NextPage } from 'next'
2+
import SculptureCard from "../components/sculptureCard"
3+
import firebaseApp from '../firebase/clientApp'
4+
import { Sandpack } from "@codesandbox/sandpack-react";
5+
import { minimalRendererIndexHTML, shaderParkStartCode, minimalRendererCSS } from '../components/spStarterTempaltes';
6+
7+
import { ref, getDatabase, query, orderByChild } from 'firebase/database';
8+
import { useList } from 'react-firebase-hooks/database';
9+
10+
const database = getDatabase(firebaseApp);
11+
12+
const Explore: NextPage = () => {
13+
14+
var dbRef = ref(database, 'sculptures')
15+
// const dbQuery = dbRef.orderBy("timestamp");
16+
const mostViewedPosts = query(dbRef, orderByChild('favorites'));
17+
18+
19+
// let [snapshots, loading, error] = useList(ref(database, 'sculptures'));
20+
let [snapshots, loading, error] = useList(mostViewedPosts);
21+
if(!loading && snapshots) {
22+
23+
snapshots = snapshots?.reverse().slice(0, 100);
24+
console.log(snapshots[0].val())
25+
return (
26+
// <Sandpack />
27+
<Sandpack
28+
template="static"
29+
files={{
30+
"/index.html": minimalRendererIndexHTML,
31+
"/spCode.txt": shaderParkStartCode,
32+
"/styles.css" : minimalRendererCSS
33+
}}
34+
options={{
35+
// visibleFiles: ["/spCode.js", "/index.html", "/style.css"],
36+
visibleFiles: ["/spCode.txt"],
37+
initMode: "immediate",
38+
activeFile: "/spCode.txt",
39+
showLineNumbers: true, // default - true
40+
showInlineErrors: true, // default - false
41+
recompileMode: 'immediate',
42+
// recompileDelay: 300,
43+
// wrapContent: true, // default - false
44+
editorHeight: 800, // default - 300
45+
editorWidthPercentage: 50, // default - 50
46+
}}
47+
/>
48+
)
49+
50+
} else {
51+
return (
52+
<span>List: Loading...</span>
53+
)
54+
}
55+
56+
}
57+
58+
export default Explore

pages/sculpture/[id].tsx

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const Sculpture = (props: any) => {
2121
let sculp = snapshot.val();
2222
if(sculp) {
2323
shaderSource = sculp.shaderSource;
24-
console.log('sculp', sculp)
2524
if(!triggerOnce && shaderSource.length) {
2625
triggerOnce = true;
2726
const divElement = canvasRef.current;

0 commit comments

Comments
 (0)