-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
255 lines (222 loc) · 8.37 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>netcdf three.js - volume rendering of netcdf datasets</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="./main.css">
<!-- <script src='./js/netcdfjs.min.js'></script> -->
</head>
<body>
<div id="info">
<a href="http://threejs.org/" target="_blank" rel="noopener">three.js</a>
- Volume rendering of netcdf dataset (mip / isosurface)
</div>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
}
}
</script>
<script src="../dist/netcdf_three.js"></script>
<script>
// netcdfjs
const nc3 = netcdf_three.netcdf_three;
</script>
<script type="module">
/*
* @author mbredif / http://github.com/mbredif
*/
import * as THREE from 'three';
import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
import WebGL from 'three/addons/capabilities/WebGL.js';
const WEBGL = WebGL;
import { GUI } from './dat.gui.js';
if ( WEBGL.isWebGL2Available() === false ) {
document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
}
var renderer,
scene,
camera,
controls,
material,
mesh,
planeMeshX,
planeMeshY,
planeMeshZ,
samplingMaterial,
config,
colormaps,
variables = {},
guidim;
init();
function updatePlanes(texture) {
const x = texture.image.width;
const y = texture.image.height;
const z = texture.image.depth;
planeMeshX.scale.set(z, y, 1 );
planeMeshX.quaternion.set(0,1,0,1).normalize();
planeMeshX.position.set(0, y / 2, z / 2);
planeMeshY.scale.set(x, z, 1 );
planeMeshY.quaternion.set(1,0,0,-1).normalize();
planeMeshY.position.set(x / 2, 0, z / 2);
planeMeshZ.scale.set(x, y, 1 );
planeMeshZ.position.set(x / 2, y / 2, 0 );
}
function init() {
scene = new THREE.Scene();
// Create renderer
var canvas = document.createElement( 'canvas' );
var context = canvas.getContext( 'webgl2', { alpha: false, antialias: false } );
renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// Create camera (The volume renderer does not work very well with perspective yet)
var h = 512; // frustum height
var aspect = window.innerWidth / window.innerHeight;
camera = new THREE.OrthographicCamera( - h * aspect / 2, h * aspect / 2, h / 2, - h / 2, 1, 1000 );
camera.position.set( 64, 64, 64 );
camera.up.set( 0, 0, 1 ); // z is up
// Create controls
controls = new OrbitControls( camera, renderer.domElement );
controls.addEventListener( 'change', render );
controls.target.set( 0, 0, 0 );
controls.minZoom = 2;
controls.maxZoom = 10;
controls.update();
// Colormap textures
colormaps = {
viridis: new THREE.TextureLoader().load( 'textures/cm_viridis.png', render ),
gray: new THREE.TextureLoader().load( 'textures/cm_gray.png', render )
};
// Lighting is baked into the shader a.t.m.
// var dirLight = new DirectionalLight( 0xffffff );
// The gui for interaction
config = { clim1: 0.05, clim2: 0., renderstyle: 'iso',
iso_threshold: 0.5, iso_shininess: 40,
iso_ambient_color: '#336633',
iso_diffuse_color: '#cc3333',
iso_specular_color: '#ffffff',
colormap: 'viridis',
planeX: 0, planeY: 0, planeZ: 0, dimension: "" };
var gui = new GUI();
guidim = gui.add( config, 'dimension', [] ).onChange( onDimensionChanged );
gui.add( config, 'clim1', 0., 1., 0.00001 ).onChange( onConfigChanged );
gui.add( config, 'clim2', 0., 1., 0.00001 ).onChange( onConfigChanged );
gui.add( config, 'colormap', { gray: 'gray', viridis: 'viridis' } ).onChange( onConfigChanged );
gui.add( config, 'renderstyle', { mip: 'mip', iso: 'iso' } ).onChange( onConfigChanged );
gui.add( config, 'iso_threshold', 0., 1., 0.00001 ).onChange( onConfigChanged );
gui.addColor( config, 'iso_ambient_color' ).onChange( onConfigChanged );
gui.addColor( config, 'iso_diffuse_color' ).onChange( onConfigChanged );
gui.addColor( config, 'iso_specular_color' ).onChange( onConfigChanged );
gui.add( config, 'iso_shininess', 0., 100, 0.001 ).onChange( onConfigChanged );
gui.add( config, 'planeX' ).onChange( onConfigChanged );
gui.add( config, 'planeY' ).onChange( onConfigChanged );
gui.add( config, 'planeZ' ).onChange( onConfigChanged );
samplingMaterial = nc3.createSamplingMaterial(config);
mesh = nc3.createMesh(config);
material = mesh.material;
var planeGeom = new THREE.PlaneGeometry( 1, 1 );
planeMeshX = new THREE.Mesh( planeGeom, samplingMaterial );
planeMeshY = new THREE.Mesh( planeGeom, samplingMaterial );
planeMeshZ = new THREE.Mesh( planeGeom, samplingMaterial );
scene.add(mesh);
scene.add(planeMeshX);
scene.add(planeMeshY);
scene.add(planeMeshZ);
// Load the data ...
fetch('./data/A2003.1.WENO5.002.nc', { method: "GET" })
.then(nc3.readNetcdfHeader)
.then(header => loadHeader(header, 'THT'));
window.addEventListener( 'resize', onWindowResize, false );
document.addEventListener( 'drop', onDrop );
document.addEventListener( 'dragover', onDragOver );
}
function loadHeader(reader, dim) {
// console.log(header); // just for debug
variables = reader.variables.filter(v => v.dimensions.length==3); // keep volume variables only
const dimensions = variables.map(v => v.name).sort();
guidim = guidim.options(dimensions).onChange( onDimensionChanged(reader) );
guidim.setValue(dim || dimensions[0]); // will trigger onDimensionChanged()
}
function onDragOver(event) { event.preventDefault(); }
function onDrop(event) {
event.preventDefault();
const files = [];
if (event.dataTransfer.items) {
for (var i = 0; i < event.dataTransfer.items.length; i++) {
if (event.dataTransfer.items[i].kind === 'file')
files.push(event.dataTransfer.items[i].getAsFile());
}
} else {
files = event.dataTransfer.files;
}
if(files.length>1)
console.warn('multiple files have been dropped, showing only one');
if(files.length>0) {
console.log('loading ' + files[0].name);
files[0].arrayBuffer().then(buffer => loadHeader(nc3.instanceNetcdfReader(buffer)));
}
}
function onDimensionChanged(reader) {
function updateTexture(texture) {
const variable = variables.find(val => val.name === texture.name);
if (!variable) {
// the decoded variable is no longer present in the main file (eg another file has been loaded), skipping
return;
}
variables.find(val => val.name === texture.name).texture = texture; // cache it
mesh.updateTexture(texture);
samplingMaterial.updateTexture(texture);
updatePlanes(texture);
controls.target.set( texture.image.width/ 2, texture.image.height / 2, texture.image.depth / 2 );
onConfigChanged();
}
return (value) => {
const variable = variables.find(val => val.name === value);
if (variable.texture) {
// use cached texture
updateTexture(variable.texture);
return;
}
nc3.fetchVolume(reader, value)
.then(nc3.normalizeVolume)
.then(nc3.createTexture)
.then(updateTexture)
};
}
function onConfigChanged() {
config.cm = colormaps[ config.colormap ];
if (mesh) mesh.updateConfig(config);
if (samplingMaterial) samplingMaterial.updateConfig(config);
if (planeMeshX) {
planeMeshX.position.x = config.planeX;
planeMeshX.updateMatrixWorld();
}
if (planeMeshY) {
planeMeshY.position.y = config.planeY;
planeMeshY.updateMatrixWorld();
}
if (planeMeshZ) {
planeMeshZ.position.z = config.planeZ;
planeMeshZ.updateMatrixWorld();
}
render();
}
function onWindowResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
var aspect = window.innerWidth / window.innerHeight;
var frustumHeight = camera.top - camera.bottom;
camera.left = - frustumHeight * aspect / 2;
camera.right = frustumHeight * aspect / 2;
camera.updateProjectionMatrix();
render();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body></html>