Skip to content

Commit 09d0780

Browse files
authored
Merge pull request #16827 from Mugen87/dev34
Examples: More module examples.
2 parents b8622d9 + 827e18d commit 09d0780

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2332
-1983
lines changed

examples/files.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ var files = {
318318
"webgldeferred_animation"
319319
],
320320
"webgl2": [
321-
"webgl2_loader_gltf",
322321
"webgl2_materials_texture2darray",
323322
"webgl2_materials_texture3d",
324323
"webgl2_multisampled_renderbuffers",

examples/webgl2_loader_gltf.html

Lines changed: 0 additions & 131 deletions
This file was deleted.

examples/webgl2_materials_texture2darray.html

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,25 @@
5454
<a href="https://www.codeproject.com/info/cpol10.aspx" target="_blank" rel="noopener">CPOL</a>
5555
</div>
5656

57-
<script src="../build/three.js"></script>
58-
<!--<script src="js/controls/OrbitControls.js"></script>-->
59-
6057
<script src="js/libs/jszip.min.js"></script>
61-
<script src="js/libs/stats.min.js"></script>
6258
<script src="js/WebGL.js"></script>
6359

64-
<script>
60+
<script type="module">
61+
import {
62+
DataTexture2DArray,
63+
FileLoader,
64+
Mesh,
65+
PerspectiveCamera,
66+
PlaneBufferGeometry,
67+
RedFormat,
68+
Scene,
69+
ShaderMaterial,
70+
Vector2,
71+
UnsignedByteType,
72+
WebGLRenderer
73+
} from "../build/three.module.js";
74+
75+
import Stats from './jsm/libs/stats.module.js';
6576

6677
if ( WEBGL.isWebGL2Available() === false ) {
6778

@@ -84,40 +95,40 @@
8495
var container = document.createElement( 'div' );
8596
document.body.appendChild( container );
8697

87-
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
98+
camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
8899
camera.position.z = 70;
89100

90-
scene = new THREE.Scene();
101+
scene = new Scene();
91102

92103
// width 256, height 256, depth 109, 8-bit, zip archived raw data
93104

94-
new THREE.FileLoader()
105+
new FileLoader()
95106
.setResponseType( 'arraybuffer' )
96107
.load( 'textures/3d/head256x256x109.zip', function ( data ) {
97108

98109
var zip = new JSZip( data );
99110
var array = zip.files[ 'head256x256x109' ].asUint8Array();
100111

101-
var texture = new THREE.DataTexture2DArray( array, 256, 256, 109 );
112+
var texture = new DataTexture2DArray( array, 256, 256, 109 );
102113

103-
texture.format = THREE.RedFormat;
104-
texture.type = THREE.UnsignedByteType;
114+
texture.format = RedFormat;
115+
texture.type = UnsignedByteType;
105116

106117
texture.needsUpdate = true;
107118

108-
var material = new THREE.ShaderMaterial( {
119+
var material = new ShaderMaterial( {
109120
uniforms: {
110121
diffuse: { value: texture },
111122
depth: { value: 0 },
112-
size: { value: new THREE.Vector2( planeWidth, planeHeight ) }
123+
size: { value: new Vector2( planeWidth, planeHeight ) }
113124
},
114125
vertexShader: document.getElementById( 'vs' ).textContent.trim(),
115126
fragmentShader: document.getElementById( 'fs' ).textContent.trim()
116127
} );
117128

118-
var geometry = new THREE.PlaneBufferGeometry( planeWidth, planeHeight );
129+
var geometry = new PlaneBufferGeometry( planeWidth, planeHeight );
119130

120-
mesh = new THREE.Mesh( geometry, material );
131+
mesh = new Mesh( geometry, material );
121132

122133
scene.add( mesh );
123134

@@ -128,7 +139,7 @@
128139
var canvas = document.createElement( 'canvas' );
129140
var context = canvas.getContext( 'webgl2' );
130141

131-
renderer = new THREE.WebGLRenderer( { antialias: true, canvas: canvas, context: context } );
142+
renderer = new WebGLRenderer( { antialias: true, canvas: canvas, context: context } );
132143
renderer.setPixelRatio( window.devicePixelRatio );
133144
renderer.setSize( window.innerWidth, window.innerHeight );
134145
container.appendChild( renderer.domElement );

examples/webgl2_materials_texture3d.html

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,37 @@
1313
</div>
1414
<div id="inset"></div>
1515

16-
<script src="../build/three.js"></script>
17-
18-
<script src="js/controls/OrbitControls.js"></script>
19-
20-
<script src="js/misc/Volume.js"></script>
21-
<script src="js/loaders/NRRDLoader.js"></script>
22-
<script src="js/shaders/VolumeShader.js"></script>
23-
2416
<script src="js/WebGL.js"></script>
25-
<script src="js/libs/gunzip.min.js"></script>
26-
<script src="js/libs/dat.gui.min.js"></script>
2717

28-
<script>
18+
<script type="module">
19+
import {
20+
BackSide,
21+
BoxBufferGeometry,
22+
DataTexture3D,
23+
FloatType,
24+
LinearFilter,
25+
Mesh,
26+
OrthographicCamera,
27+
RedFormat,
28+
Scene,
29+
ShaderMaterial,
30+
TextureLoader,
31+
UniformsUtils,
32+
WebGLRenderer
33+
} from "../build/three.module.js";
34+
35+
import { GUI } from './jsm/libs/dat.gui.module.js';
36+
import { OrbitControls } from './jsm/controls/OrbitControls.js';
37+
import { NRRDLoader } from './jsm/loaders/NRRDLoader.js';
38+
import { VolumeRenderShader1 } from './jsm/shaders/VolumeShader.js';
2939

3040
if ( WEBGL.isWebGL2Available() === false ) {
3141

3242
document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
3343

3444
}
3545

36-
var container,
37-
renderer,
46+
var renderer,
3847
scene,
3948
camera,
4049
controls,
@@ -46,69 +55,69 @@
4655

4756
function init() {
4857

49-
scene = new THREE.Scene();
58+
scene = new Scene();
5059

5160
// Create renderer
5261
var canvas = document.createElement( 'canvas' );
5362
var context = canvas.getContext( 'webgl2' );
54-
renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
63+
renderer = new WebGLRenderer( { canvas: canvas, context: context } );
5564
renderer.setPixelRatio( window.devicePixelRatio );
5665
renderer.setSize( window.innerWidth, window.innerHeight );
5766
document.body.appendChild( renderer.domElement );
5867

5968
// Create camera (The volume renderer does not work very well with perspective yet)
6069
var h = 512; // frustum height
6170
var aspect = window.innerWidth / window.innerHeight;
62-
camera = new THREE.OrthographicCamera( - h * aspect / 2, h * aspect / 2, h / 2, - h / 2, 1, 1000 );
71+
camera = new OrthographicCamera( - h * aspect / 2, h * aspect / 2, h / 2, - h / 2, 1, 1000 );
6372
camera.position.set( 0, 0, 128 );
6473
camera.up.set( 0, 0, 1 ); // In our data, z is up
6574

6675
// Create controls
67-
controls = new THREE.OrbitControls( camera, renderer.domElement );
76+
controls = new OrbitControls( camera, renderer.domElement );
6877
controls.addEventListener( 'change', render );
6978
controls.target.set( 64, 64, 128 );
7079
controls.minZoom = 0.5;
7180
controls.maxZoom = 4;
7281
controls.update();
7382

74-
// scene.add( new THREE.AxesHelper( 128 ) );
83+
// scene.add( new AxesHelper( 128 ) );
7584

7685
// Lighting is baked into the shader a.t.m.
77-
// var dirLight = new THREE.DirectionalLight( 0xffffff );
86+
// var dirLight = new DirectionalLight( 0xffffff );
7887

7988
// The gui for interaction
8089
volconfig = { clim1: 0, clim2: 1, renderstyle: 'iso', isothreshold: 0.15, colormap: 'viridis' };
81-
var gui = new dat.GUI();
90+
var gui = new GUI();
8291
gui.add( volconfig, 'clim1', 0, 1, 0.01 ).onChange( updateUniforms );
8392
gui.add( volconfig, 'clim2', 0, 1, 0.01 ).onChange( updateUniforms );
8493
gui.add( volconfig, 'colormap', { gray: 'gray', viridis: 'viridis' } ).onChange( updateUniforms );
8594
gui.add( volconfig, 'renderstyle', { mip: 'mip', iso: 'iso' } ).onChange( updateUniforms );
8695
gui.add( volconfig, 'isothreshold', 0, 1, 0.01 ).onChange( updateUniforms );
8796

8897
// Load the data ...
89-
new THREE.NRRDLoader().load( "models/nrrd/stent.nrrd", function ( volume ) {
98+
new NRRDLoader().load( "models/nrrd/stent.nrrd", function ( volume ) {
9099

91100
// Texture to hold the volume. We have scalars, so we put our data in the red channel.
92101
// THREEJS will select R32F (33326) based on the RedFormat and FloatType.
93102
// Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE
94103
// TODO: look the dtype up in the volume metadata
95-
var texture = new THREE.DataTexture3D( volume.data, volume.xLength, volume.yLength, volume.zLength );
96-
texture.format = THREE.RedFormat;
97-
texture.type = THREE.FloatType;
98-
texture.minFilter = texture.magFilter = THREE.LinearFilter;
104+
var texture = new DataTexture3D( volume.data, volume.xLength, volume.yLength, volume.zLength );
105+
texture.format = RedFormat;
106+
texture.type = FloatType;
107+
texture.minFilter = texture.magFilter = LinearFilter;
99108
texture.unpackAlignment = 1;
100109
texture.needsUpdate = true;
101110

102111
// Colormap textures
103112
cmtextures = {
104-
viridis: new THREE.TextureLoader().load( 'textures/cm_viridis.png', render ),
105-
gray: new THREE.TextureLoader().load( 'textures/cm_gray.png', render )
113+
viridis: new TextureLoader().load( 'textures/cm_viridis.png', render ),
114+
gray: new TextureLoader().load( 'textures/cm_gray.png', render )
106115
};
107116

108117
// Material
109-
var shader = THREE.VolumeRenderShader1;
118+
var shader = VolumeRenderShader1;
110119

111-
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
120+
var uniforms = UniformsUtils.clone( shader.uniforms );
112121

113122
uniforms[ "u_data" ].value = texture;
114123
uniforms[ "u_size" ].value.set( volume.xLength, volume.yLength, volume.zLength );
@@ -117,18 +126,18 @@
117126
uniforms[ "u_renderthreshold" ].value = volconfig.isothreshold; // For ISO renderstyle
118127
uniforms[ "u_cmdata" ].value = cmtextures[ volconfig.colormap ];
119128

120-
material = new THREE.ShaderMaterial( {
129+
material = new ShaderMaterial( {
121130
uniforms: uniforms,
122131
vertexShader: shader.vertexShader,
123132
fragmentShader: shader.fragmentShader,
124-
side: THREE.BackSide // The volume shader uses the backface as its "reference point"
133+
side: BackSide // The volume shader uses the backface as its "reference point"
125134
} );
126135

127136
// Mesh
128-
var geometry = new THREE.BoxBufferGeometry( volume.xLength, volume.yLength, volume.zLength );
137+
var geometry = new BoxBufferGeometry( volume.xLength, volume.yLength, volume.zLength );
129138
geometry.translate( volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5 );
130139

131-
var mesh = new THREE.Mesh( geometry, material );
140+
var mesh = new Mesh( geometry, material );
132141
scene.add( mesh );
133142

134143
render();

0 commit comments

Comments
 (0)