-
Notifications
You must be signed in to change notification settings - Fork 3
/
trilinear_lerp.html
212 lines (178 loc) · 6.73 KB
/
trilinear_lerp.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title></head><body>
<script type="module">
// #region IMPORTS
import Starter, { THREE } from './lib/starter.js';
import DynLineMesh from './lib/DynLineMesh.js';
import ShapePointsMesh from './lib/ShapePointsMesh.js';
import { UtilGltf2, Gltf2 } from './lib/UtilGltf2.js';
import { vec3_copy, vec3_set, vec3_buf_set }
from './lib/Maths.js';
import { Manipulator3D } from './lib/manipulator3d.es.js';
// #endregion
// #region MAIN
let App;
let Debug = {};
let Ref = {};
window.addEventListener( "load", async _=>{
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App = new Starter( { webgl2:true, grid:true } );
App.setCamera( 0, 20, 4, [0,0.5,0] );
App.add( ( Debug.ln = new DynLineMesh() ) );
App.add( ( Debug.pnt = new ShapePointsMesh() ) );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// LOAD IN MESH
const gltf = await Gltf2.fetch( './res/cauldron.gltf' );
//let mat = new THREE.MeshPhongMaterial( {color:'cyan' } );
let mat = TrilinearLatticeMaterial();
let mesh = UtilGltf2.loadMesh( gltf, null, mat );
mesh.geometry.computeBoundingBox();
App.add( mesh );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// SETUP LATTICE CONTROL
const lattice = new TrilinearLattice(
mesh.geometry.boundingBox.min.toArray(),
mesh.geometry.boundingBox.max.toArray()
);
mat.userData.cube = lattice.cube;
mat.userData.minBound = lattice.minBound;
mat.userData.maxBound = lattice.maxBound;
Ref.lattice = lattice;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 3D TRANSFORM GIZMO
Ref.man = new Manipulator3D( App.scene, App.camera, App.renderer, false );
Ref.man.setActive( true ).useScale( false ).useRotate( false ).setScaleFactor(12);
// Hook into events to disable camera controller when user does a drag action
Ref.man.on( 'dragstart', ()=>{ App.orbit.enabled = false; } );
Ref.man.on( 'dragend', ()=>{ App.orbit.enabled = true; } );
Ref.man.on( 'translate', e=>{
Ref.lattice.setPos( Ref.selected, e.detail );
});
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// LATTICE POINTS
const geo = new THREE.SphereGeometry( 0.05, 8, 8 );
mat = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
let i=0;
Ref.points = [];
for( let p of lattice.iterPoints() ){
mesh = new THREE.Mesh( geo, mat );
mesh.position.fromArray( p );
Ref.points.push( mesh );
App.add( mesh );
}
selectPoint( 7 );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App.render();
});
window.addEventListener( 'keydown', e=>{
if( e.key >= 1 && e.key <= 8 ) selectPoint( e.key );
});
function selectPoint( idx ){
const i = idx - 1;
Ref.selected = i;
Ref.man.attach( Ref.points[ i ] );
}
//#endregion
class TrilinearLattice{
// #region MAIN
minBound = [0,0,0];
maxBound = [0,0,0];
cube = [
0,0,0, 0,0,0, 0,0,0, 0,0,0,
0,0,0, 0,0,0, 0,0,0, 0,0,0,
];
constructor( min=null, max=null ){
if( min && max ) this.setBounds( min, max );
}
// #endregion
// #region SETTERS
setBounds( min, max ){
vec3_copy( this.minBound, min );
vec3_copy( this.maxBound, max );
this._updateCube();
return this;
}
// #endregion
// #region METHODS
addOffset( idx, offset ){
const i = idx * 3;
this.cube[ i+0 ] += offset[ 0 ];
this.cube[ i+1 ] += offset[ 1 ];
this.cube[ i+2 ] += offset[ 2 ];
return this;
}
setPos( idx, pos ){
const i = idx * 3;
this.cube[ i+0 ] = pos[ 0 ];
this.cube[ i+1 ] = pos[ 1 ];
this.cube[ i+2 ] = pos[ 2 ];
return this;
}
// #endregion
// #region CUBE
_updateCube(){
const [ x0, y0, z0 ] = this.minBound;
const [ x1, y1, z1 ] = this.maxBound;
const c = this.cube;
vec3_buf_set( c, 0*3, x0, y0, z0 );
vec3_buf_set( c, 1*3, x1, y0, z0 );
vec3_buf_set( c, 2*3, x0, y1, z0 );
vec3_buf_set( c, 3*3, x1, y1, z0 );
vec3_buf_set( c, 4*3, x0, y0, z1 ); // Front Plane
vec3_buf_set( c, 5*3, x1, y0, z1 );
vec3_buf_set( c, 6*3, x0, y1, z1 );
vec3_buf_set( c, 7*3, x1, y1, z1 );
}
// #endregion
// #region ITERATORS
iterPoints(){
let i = 0;
const result = { value:[0,0,0], done:false };
const next = ()=>{
if( i >= this.cube.length ) result.done = true;
else{
result.value[ 0 ] = this.cube[ i+0 ];
result.value[ 1 ] = this.cube[ i+1 ];
result.value[ 2 ] = this.cube[ i+2 ];
}
i += 3;
return result;
};
return { [Symbol.iterator](){ return { next }; } };
}
// #endregion
}
function TrilinearLatticeMaterial(){
const mat = new THREE.MeshLambertMaterial( { color: 0x707070, wireframe: false } );
mat.userData.minBound = null;
mat.userData.maxBound = null;
mat.userData.cube = null;
mat.onBeforeCompile = (sh)=>{
sh.uniforms.minBound = { value: mat.userData.minBound };
sh.uniforms.maxBound = { value: mat.userData.maxBound };
sh.uniforms.cube = { value: mat.userData.cube };
sh.vertexShader = `uniform vec3 cube[8]; uniform vec3 minBound; uniform vec3 maxBound;\n` + sh.vertexShader;
sh.vertexShader = sh.vertexShader.replace( '#include <begin_vertex>',
`#include <begin_vertex>
// https://en.wikipedia.org/wiki/Trilinear_interpolation
vec3 pos = position;
float xd = ( pos.x - minBound.x ) / ( maxBound.x - minBound.x );
float yd = ( pos.y - minBound.y ) / ( maxBound.y - minBound.y );
float zd = ( pos.z - minBound.z ) / ( maxBound.z - minBound.z );
vec3 c00 = cube[0] * ( 1.0 - xd ) + cube[1] * xd; // Back Plane
vec3 c01 = cube[2] * ( 1.0 - xd ) + cube[3] * xd;
vec3 c10 = cube[4] * ( 1.0 - xd ) + cube[5] * xd; // Forward Plane
vec3 c11 = cube[6] * ( 1.0 - xd ) + cube[7] * xd;
vec3 c0 = c00 * ( 1.0 - zd ) + c10 * zd;
vec3 c1 = c01 * ( 1.0 - zd ) + c11 * zd;
transformed = c0 * ( 1.0 - yd ) + c1 * yd;
`);
};
return mat;
}
</script>
<div style="position:fixed; top:0px; left:0px; padding:5px; background-color:#ffffff20">
To Select points use the keyboard : 1,2,3,4,5,6,7,8
</div>
</body></html>