Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ar test #252

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package server

import (
"context"
"encoding/base64"
"html/template"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -78,6 +80,100 @@ func RunServer() {
http.ServeFile(w, r, "public/support.html")
})

router.HandleFunc("/ar", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.New("cube").Parse(`
<!DOCTYPE html>
<html>
<head>
<title>Three.js Cube Example</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"orbitcontrols": "https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'orbitcontrols';

let camera, scene, renderer;
let mesh;

init();
animate();

function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.z = 2;
scene = new THREE.Scene();

const loader = new THREE.TextureLoader();
const materials = [
new THREE.MeshBasicMaterial({ map: loader.load('{{.Face1}}') }),
new THREE.MeshBasicMaterial({ map: loader.load('{{.Face2}}') }),
new THREE.MeshBasicMaterial({ map: loader.load('{{.Face1}}') }),
new THREE.MeshBasicMaterial({ map: loader.load('{{.Face1}}') }),
new THREE.MeshBasicMaterial({ map: loader.load('{{.Face1}}') }),
new THREE.MeshBasicMaterial({ map: loader.load('{{.Face1}}') })
];

const geometry = new THREE.BoxGeometry();
mesh = new THREE.Mesh(geometry, materials);
scene.add(mesh);

renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize);

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = true;
}

function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}

function animate() {
requestAnimationFrame(animate);
mesh.rotation.x += 0.0010;
mesh.rotation.y += 0.0010;
renderer.render(scene, camera);
}
</script>
</body>
</html>
`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create an instance of TemplateData with the desired paths
imagebase64, _ := tobase64("assets/songstitch_logo_dark.png")
data := TemplateData{
Face1: "data:image/png;base64," + imagebase64,
Face2: "data:image/png;base64, " + imagebase64,
}
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

})

server := &http.Server{
Addr: ":8080",
Handler: router,
Expand All @@ -92,3 +188,16 @@ func RunServer() {
log.Fatal().Err(err).Msg("Startup failed")
}
}

type TemplateData struct {
Face1 string
Face2 string
}

func tobase64(file string) (string, error) {
bytes, err := os.ReadFile(file)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(bytes), nil
}