-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex_shader.html
250 lines (205 loc) · 8.41 KB
/
index_shader.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
<!DOCTYPE html>
<html>
<head>
<title>Shader-Test</title>
<meta content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" name="viewport">
<link href="base.css" rel="stylesheet" type="text/css">
</head>
<body>
<!-- <script type="text/javascript" src="https://unpkg.com/@babel/[email protected]/dist/polyfill.js"></script>-->
<!-- <script type="text/javascript" src="https://unpkg.com/[email protected]"></script>-->
<script src="./libs/nifi-reader-min.js" type="text/javascript"></script>
<script type="module">
// import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
import * as THREE from './build/three.module.js';
import Stats from './libs/stats.module.js';
// import fragment from "./shader/fragment.glsl";
// import vertex from "./shader/vertex.glsl";
// import NiftiReader from './libs/nifi-reader-min.js';
import {TrackballControls} from './controls/TrackballControls.js';
import {VTKLoader} from './loaders/VTKLoader.js';
import {OrbitControls} from 'https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js';
import {GLTFLoader} from 'https://unpkg.com/[email protected]/examples/jsm/loaders/GLTFLoader.js';
import {ARButton} from 'https://unpkg.com/three/examples/jsm/webxr/ARButton.js';
let container, stats;
let camera, controls, scene, renderer;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 1e10);
camera.position.z = 0.2;
scene = new THREE.Scene();
scene.add(camera);
// // light
//
// const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x000000, 1 );
// scene.add( hemiLight );
//
// const dirLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
// dirLight.position.set( 2, 2, 2 );
// scene.add( dirLight );
// create shader material
let material = new THREE.ShaderMaterial({
extensions: {
derivatives: "#extension GL_OES_standard_derivatives : enable"
},
side: THREE.DoubleSide,
uniforms: {
time: {value: 0},
resolution: {value: new THREE.Vector4()},
},
vertexShader: vertex(),
fragmentShader: fragment()
});
// vertex shader here
function vertex() {
return `
uniform float time;
varying vec2 vScreenSpace;
varying vec2 vUv;
varying vec3 vPosition;
varying vec3 vNormal;
float PI = 3.141592653589793238;
void main() {
vUv = uv;
vPosition = position;
vNormal = normal;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
vScreenSpace = gl_Position.xy/gl_Position.w;
}
`
}
// fragment shader here
function fragment() {
return `
uniform float time;
uniform float progress;
uniform sampler2D texture1;
uniform vec4 resolution;
varying vec2 vScreenSpace;
varying vec3 vNormal;
varying vec2 vUv;
varying vec3 vPosition;
float PI = 3.141592653589793238;
float threshold(float edge0, float edge1, float x) {
return clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
}
float hash(vec3 p) {
p = fract(p * 0.3183099 + .1);
p *= 17.0;
return fract(p.x * p.y * p.z * (p.x + p.y + p.z));
}
float noise(in vec3 x) {
vec3 i = floor(x);
vec3 f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(mix(hash(i + vec3(0, 0, 0)), hash(i + vec3(1, 0, 0)), f.x), mix(hash(i + vec3(0, 1, 0)), hash(i + vec3(1, 1, 0)), f.x), f.y), mix(mix(hash(i + vec3(0, 0, 1)), hash(i + vec3(1, 0, 1)), f.x), mix(hash(i + vec3(0, 1, 1)), hash(i + vec3(1, 1, 1)), f.x), f.y), f.z);
}
float rand(float n){return fract(sin(n) * 43758.5453123);}
float noise(float p) {
float fl = floor(p);
float fc = fract(p);
return mix(rand(fl), rand(fl + 1.0), fc);
}
void main() {
float light = dot(vNormal, normalize(vec3(1.)));
// strokes
float stroke = cos((vScreenSpace.x - vScreenSpace.y )*1000.);
float smallnoise = noise(500.*vec3(vScreenSpace,time));
float bignoise = noise(15.*vec3(vScreenSpace,time));
stroke += (smallnoise*2. -1.) + (bignoise*2. -1.);
stroke = 1. - smoothstep(2.*light-2.,2.*light+2., stroke);
gl_FragColor = vec4(vScreenSpace,0.,1.);
gl_FragColor = vec4(vNormal,1.);
gl_FragColor = vec4(vec3(light),1.);
gl_FragColor = vec4(vec3(stroke),1.);
// gl_FragColor = vec4(vec3(smallnoise),1.);
// gl_FragColor = vec4(vec3(noise(100.*vec3(vScreenSpace,1.))),1.);
}
`
}
const loader = new VTKLoader();
loader.load("./resources/rh.vtk", function (geometry) {
geometry.center();
geometry.computeVertexNormals();
// const material = new THREE.MeshLambertMaterial( { color: 0xffffff } );
const mesh = new THREE.Mesh(geometry, material);
// mesh.position.set( - 0.075, 0.005, 0 );
mesh.scale.multiplyScalar(0.2);
scene.add(mesh);
mesh.rotate = 0.05;
mesh.material.uniforms.time.value = 0.05;
});
// const data = "./resources/E_vec.nii"
// this._arrayBuffer = data.buffer;
// if (NiftiReader.isNIFTI(this._arrayBuffer)) {
// this._dataSet = NiftiReader.readHeader(this._arrayBuffer);
// this._niftiImage = NiftiReader.readImage(this._dataSet, this._arrayBuffer);
// } else {
// const error = new Error('parsers.nifti could not parse the file');
// throw error;
// }
// function readNIFTI(buf) {
// var header = NiftiReader.readHeader(buf);
// var logger = document.getElementById('results');
// logger.innerText = header.toFormattedString();
// }
//
// function readFile(file) {
// var blob = makeSlice(file, 0, file.size);
// var reader = new FileReader();
// reader.onloadend = function (evt) {
// if (evt.target.readyState === FileReader.DONE) {
// readNIFTI(file.name, evt.target.result);
// }
// };
// reader.readAsArrayBuffer(blob);
// }
// readFile("./resources/E_vec.nii");
// renderer
renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.xr.enabled = true;
renderer.physicallyCorrectLights = true;
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.setClearColor(0xeeeeee, 1);
container = document.createElement('div');
document.body.appendChild(container);
container.appendChild(renderer.domElement);
document.body.appendChild(ARButton.createButton(renderer));
// function onSelect() {
// var material = new THREE.MeshPhongMaterial( { color: 0xffffff * Math.random() } );
// var mesh = new THREE.Mesh( geometry, material );
// mesh.position.set( 0, 0, - 0.3 ).applyMatrix4( controller.matrixWorld );
// mesh.quaternion.setFromRotationMatrix( controller.matrixWorld );
// scene.add( mesh );
// }
// controller = renderer.xr.getController( 0 );
// controller.addEventListener( 'select', onSelect );
// scene.add( controller );
// controls
controls = new TrackballControls(camera, renderer.domElement);
controls.minDistance = 50;
controls.maxDistance = 500;
controls.rotateSpeed = 20;
stats = new Stats();
container.appendChild(stats.dom);
//
window.addEventListener('resize', onWindowResize);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
controls.handleResize();
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
stats.update();
}
</script>
</body>
</html>