-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
136 lines (121 loc) · 5.88 KB
/
index.html
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
<body>
<style>
canvas { background-image: url(back.webp); }
a.github { display: block; position: fixed; right: 0; bottom: 0; }
div.controller.desc { display: block; line-height: 1.3; text-align: justify; padding: 3px 25px 7px 15px; border-left: 4px solid yellowgreen; border-radius: 2px; background-color: #333; }
div.controller.desc a { color: cornflowerblue; }
div.controller.desc em { font-style: normal; font-weight: bold; color: yellowgreen; }
div.controller.desc code { font: inherit; color: navajowhite; }
</style>
<script src="https://docs.opencv.org/4.6.0/opencv.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { FourPointsControls } from './build/fpc.js';
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/GLTFLoader.js';
import { GUI } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/libs/lil-gui.module.min.js';
import { mergeVertices } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/utils/BufferGeometryUtils.js';
import snarkdown from 'https://cdn.jsdelivr.net/npm/snarkdown/dist/snarkdown.es.js';
const camera = new THREE.PerspectiveCamera( 42, 750 / 500, 0.5, 50 );
camera.position.z = 5;
const scene = new THREE.Scene();
scene.add( new THREE.AmbientLight( '#960' ) );
const light = new THREE.DirectionalLight( '#69F', 4 );
light.position.y = -1;
scene.add( light );
const geometry = new THREE.BoxGeometry( 2, 2, 2 );
geometry.translate( 0, 0, 1 );
const mesh = new THREE.BoxHelper( new THREE.Mesh( geometry ), 0x77ff00 );
const water = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), new THREE.MeshBasicMaterial( {
map: new THREE.TextureLoader().load( 'water.jpg', function( texture ) {
texture.anisotropy = 8; texture.encoding = THREE.sRGBEncoding;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
} )
} ) );
water.visible = false;
mesh.add( water );
new GLTFLoader().load( 'duck.glb', function( gltf ) {
gltf.scene.scale.setScalar( 0.7 );
gltf.scene.rotation.x = Math.PI / 2;
gltf.scene.traverse( fixDuckingNormals );
mesh.add( gltf.scene );
} );
const x = new THREE.Vector3(), y = x.clone(), z = y.clone();
const renderer = new THREE.WebGLRenderer( { alpha: true, antialias: true } );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.setSize( 750, 500 );
renderer.setAnimationLoop( function( time ) {
if( mesh.children.length > 1 ) {
mesh.children[1].rotation.y = ( time % 10000 ) * 6.2831853e-4;
}
if( water.visible ) {
controls.matrixWorld.extractBasis( x, y, z );
water.material.map.offset.set( x.y, y.y ).multiplyScalar( 1e-3 * time );
}
renderer.render( scene, camera );
} );
document.body.appendChild( renderer.domElement );
const controls = FourPointsControls( camera, renderer.domElement );
controls.add( mesh );
scene.add( controls );
controls.points[0].set( -0.17, -0.45 );
controls.points[1].set( 0.18, -0.58 );
controls.points[2].set( -0.04, -0.91 );
controls.points[3].set( -0.43, -0.71 );
const descriptions = {};
fetch( 'README.md' ).then( function( response ) { return response.text() } ).then(
function( readme ) {
[...( readme
.substr( readme.indexOf( 'Methods' ) )
.replaceAll( /\r|\n/g, '🦆' )
).matchAll( /ls\.([^`]+)[^🦆]+🦆+([^🦆]+)/gu )
].forEach(
function( array ) {
descriptions[ array[1] ] = array[2];
}
);
const gui = new GUI();
gui.add( camera, 'fov', 25, 125, 1 ).onChange( function() {
camera.updateProjectionMatrix();
} );
gui.add( controls, 'method', {
exact3: FourPointsControls.exact3,
'exact3.average': FourPointsControls.exact3.average,
exact4: FourPointsControls.exact4,
'exact4.diamond': FourPointsControls.exact4.diamond,
opencv: FourPointsControls.opencv,
} ).onChange( function() {
const methodName = this.domElement.querySelector( 'select' ).value;
const div = document.createElement( 'div' );
div.className = 'controller desc';
div.innerHTML = snarkdown(
descriptions[methodName].replaceAll( '_', '🦆' )
).replaceAll( '🦆', '_' );
const nextSibling = this.domElement.nextSibling;
if( nextSibling && nextSibling.className === div.className ) {
nextSibling.remove();
}
this.domElement.after( div );
} ).setValue( controls.method );
gui.add( water, 'visible' ).name( 'water' ).onChange( function() {
light.intensity = water.visible ? 2 : 4;
} );
}
);
function fixDuckingNormals( o ) {
if( o.geometry ) {
delete o.geometry.attributes.normal;
o.geometry = mergeVertices( o.geometry );
o.geometry.computeVertexNormals();
}
}
</script>
<a class="github" href="https://github.com/makc/four-points-controls">
<img alt="github" src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" width="100" height="100" />
</a>