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

Assignment 5 #167

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions 05/ar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
<script src="https://raw.githack.com/AR-js-org/AR.js/dev/aframe/build/aframe-ar.js"></script>

<body style="margin : 0px; overflow: hidden;">
<a-scene embedded arjs>
<a-marker preset="hiro">
<a-entity position="0 0 0" 8 rotation="0 0 0" scale="0.05 0.05 0.05" gltf-model="tinker_shoe.glb"></a-entity>
</a-marker>
<a-entity camera></a-entity>
</a-scene>
</body>

</html>
219 changes: 219 additions & 0 deletions 05/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<html>
<head>
<style>
html, body {
background-color:#000;
margin: 0;
padding: 0;
height: 100%;
overflow: hidden !important;
}
#loading-text {
position: fixed;
width: 100%;
height: 100vh;
color: white;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
}
</style>

<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script src="https://mrdoob.github.io/stats.js/build/stats.min.js"></script>

<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@latest/build/three.module.js",
"three/addons/": "https://unpkg.com/three@latest/examples/jsm/"
}
}
</script>

<script type="module">

import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { AnaglyphEffect } from 'three/addons/effects/AnaglyphEffect.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import {Pane} from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/tweakpane.min.js';
import { VertexNormalsHelper } from 'three/addons/helpers/VertexNormalsHelper.js';

var renderer, controls, scene, camera, effect, stats;

window.onload = function() {

// remove loading text
const loadingText = document.getElementById("loading-text");
loadingText.remove();

// stats.js widget
stats = new Stats();
document.body.appendChild(stats.domElement);

// create scene
scene = new THREE.Scene();

// setup the camera
var fov = 75;
var ratio = window.innerWidth / window.innerHeight;
var zNear = 1;
var zFar = 10000;
camera = new THREE.PerspectiveCamera( fov, ratio, zNear, zFar );
camera.position.set(9.169998139359794, 9.08793637369809, 7.778155808615913);
camera.quaternion.set(-0.25015274932131615, 0.6561298429853336, 0.24560548240557592, 0.6682777702148656);

// create renderer and add canvas
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize( window.innerWidth, window.innerHeight );

// based on https://editor.p5js.org/setapolo/sketches/EjZJO54wo
effect = new AnaglyphEffect(renderer, 100);
effect.setSize(window.innerWidth, window.innerHeight);

document.body.appendChild( renderer.domElement );
window.onresize = () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
effect.setSize(window.innerWidth || 2, window.innerHeight || 2);
}

//.. setup lights
var ambientLight = new THREE.AmbientLight();
scene.add( ambientLight );

var light = new THREE.DirectionalLight( 0xffffff, 5.0 );
light.position.set( 10, 100, 10 );
scene.add( light );

window.SCENE = {
anaglyph: false,
poly: null,
rotatePoly: false,
doRotatePoly: () => {
window.SCENE.rotatePoly = !window.SCENE.rotatePoly;
},
blender: null,
rotateBlender: false,
doRotateBlender: () => {
window.SCENE.rotateBlender = !window.SCENE.rotateBlender;
},
blenderHelper: null,
blenderOldMaterial: null,
changeBlenderMaterial: () => {
if (!window.SCENE.blenderOldMaterial) {
window.SCENE.blenderOldMaterial = window.SCENE.blender.material.clone();
window.SCENE.blender.material = new THREE.MeshNormalMaterial();
} else {
window.SCENE.blender.material = window.SCENE.blenderOldMaterial.clone();
window.SCENE.blenderOldMaterial = null;
}
}
};

const loader = new GLTFLoader();
loader.load('shoe_raw.glb', function (gltf) {
scene.add(gltf.scene);
var poly = gltf.scenes[0].children[0];
poly.scale.x = 20;
poly.scale.y = 20;
poly.scale.z = 20;
window.SCENE.poly = poly;
});

loader.load('shoe.glb', function (gltf) {
scene.add(gltf.scene);
var blender = gltf.scenes[0].children[0];
blender.scale.x = 20;
blender.scale.y = 20;
blender.scale.z = 20;
blender.translateZ(-15);

// used https://threejs.org/docs/#examples/en/helpers/VertexNormalsHelper to debug
var helper = new VertexNormalsHelper(blender, .1, 'blue');
window.SCENE.blenderHelper = helper;
scene.add(helper);
helper.visible = false;
window.SCENE.blender = blender;
window.SCENE.helper = helper;
var blenderui = pane.addFolder({title: 'Blender Mesh'});
blenderui.addBinding(window.SCENE.helper, 'visible', {label: "Show Normals!"});
blenderui.addButton({title: "Rotate blender!"}).on('click', () => {
window.SCENE.doRotateBlender();
});
blenderui.addButton({title: "Change Material!"}).on('click', () => {
window.SCENE.changeBlenderMaterial();
})
});

// setup interaction
controls = new OrbitControls( camera, renderer.domElement );

var pane = new Pane();
var sceneui = pane.addFolder({title: 'Scene'});

sceneui.addBinding(light.position, 'x', {min:-100, max:100, label:'Light X'});
sceneui.addBinding(light.position, 'y', {min:-100, max:100, label:'Light Y'});
sceneui.addBinding(light.position, 'z', {min:-100, max:100, label:'Light Z'});
sceneui.addBinding(window.SCENE, 'anaglyph');

var polyui = pane.addFolder({title: 'PolyCam Mesh'});
polyui.addButton({title: 'rotate!'}).on('click', () => {
window.SCENE.doRotatePoly();
});

// call animation/rendering loop
animate();

};

function animate() {

requestAnimationFrame( animate );
controls.update();
stats.update();

if (window.SCENE.anaglyph) {
effect.render( scene, camera );
} else {
renderer.render( scene, camera );
}

if (window.SCENE.poly) {
// https://threejs.org/docs/#api/en/math/Quaternion
const q = new THREE.Quaternion();
if (window.SCENE.rotatePoly) {
q.setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI);
window.SCENE.poly.quaternion.slerp(q, 0.01);
} else {
q.identity();
window.SCENE.poly.quaternion.slerp(q, 0.01);
}
}

if (window.SCENE.blender && window.SCENE.blenderHelper) {
// https://threejs.org/docs/#api/en/math/Quaternion
const q = new THREE.Quaternion();
if (window.SCENE.rotateBlender) {
q.setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI);
window.SCENE.blender.quaternion.slerp(q, 0.01);
} else {
q.identity();
window.SCENE.blender.quaternion.slerp(q, 0.01);
}
window.SCENE.blenderHelper.update();
}


};
</script>
</head>
<body>
<div id="loading-text">
<h2>Loading...</h2>
</div>
</body>
</html>
Binary file added 05/shoe.glb
Binary file not shown.
Binary file added 05/shoe.ply
Binary file not shown.
Binary file added 05/shoe.stl
Binary file not shown.
Binary file added 05/shoe_raw.glb
Binary file not shown.
Binary file added 05/tinker_shoe.glb
Binary file not shown.