Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Improved webgl_worker_offscreencanvas example.
- Loading branch information
Showing
5 changed files
with
186 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var interval = null; | ||
|
||
var button = document.getElementById( 'button' ); | ||
button.addEventListener( 'click', function () { | ||
|
||
if ( interval === null ) { | ||
|
||
interval = setInterval( jank, 1000 / 60 ); | ||
|
||
button.textContent = 'STOP JANK'; | ||
|
||
} else { | ||
|
||
clearInterval( interval ); | ||
interval = null; | ||
|
||
button.textContent = 'START JANK'; | ||
result.textContent = ''; | ||
|
||
} | ||
|
||
} ); | ||
|
||
var result = document.getElementById( 'result' ); | ||
|
||
function jank() { | ||
|
||
var number = 0; | ||
|
||
for ( var i = 0; i < 10000000; i ++ ) { | ||
|
||
number += Math.random(); | ||
|
||
} | ||
|
||
result.textContent = number; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
self.importScripts( '../../../build/three.js' ); | ||
self.importScripts( './scene.js' ); | ||
|
||
self.onmessage = function ( message ) { | ||
|
||
var data = message.data; | ||
init( data.drawingSurface, data.width, data.height, data.pixelRatio, data.path ); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
var camera, scene, renderer, group; | ||
|
||
function init( canvas, width, height, pixelRatio, path ) { | ||
|
||
camera = new THREE.PerspectiveCamera( 40, width / height, 1, 1000 ); | ||
camera.position.z = 200; | ||
|
||
scene = new THREE.Scene(); | ||
scene.fog = new THREE.Fog( 0x444466, 100, 400 ); | ||
scene.background = new THREE.Color( 0x444466 ); | ||
|
||
group = new THREE.Group(); | ||
scene.add( group ); | ||
|
||
// we don't use ImageLoader since it has a DOM dependency (HTML5 image element) | ||
|
||
var loader = new THREE.ImageBitmapLoader().setPath( path ); | ||
loader.load( 'textures/matcaps/matcap-porcelain-white.jpg', function ( imageBitmap ) { | ||
|
||
var texture = new THREE.CanvasTexture( imageBitmap ); | ||
|
||
var geometry = new THREE.IcosahedronBufferGeometry( 5, 3 ); | ||
var materials = [ | ||
new THREE.MeshMatcapMaterial( { color: 0xaa24df, matcap: texture } ), | ||
new THREE.MeshMatcapMaterial( { color: 0x605d90, matcap: texture } ), | ||
new THREE.MeshMatcapMaterial( { color: 0xe04a3f, matcap: texture } ), | ||
new THREE.MeshMatcapMaterial( { color: 0xe30456, matcap: texture } ) | ||
]; | ||
|
||
for ( var i = 0; i < 100; i ++ ) { | ||
|
||
var material = materials[ i % materials.length ]; | ||
var mesh = new THREE.Mesh( geometry, material ); | ||
mesh.position.x = random() * 200 - 100; | ||
mesh.position.y = random() * 200 - 100; | ||
mesh.position.z = random() * 200 - 100; | ||
mesh.scale.setScalar( random() + 1 ); | ||
group.add( mesh ); | ||
|
||
} | ||
|
||
renderer = new THREE.WebGLRenderer( { antialias: true, canvas: canvas } ); | ||
renderer.setPixelRatio( pixelRatio ); | ||
renderer.setSize( width, height, false ); | ||
|
||
animate(); | ||
|
||
} ); | ||
|
||
} | ||
|
||
function animate() { | ||
|
||
// group.rotation.x = Date.now() / 4000; | ||
group.rotation.y = - Date.now() / 4000; | ||
|
||
renderer.render( scene, camera ); | ||
|
||
if ( self.requestAnimationFrame ) { | ||
|
||
self.requestAnimationFrame( animate ); | ||
|
||
} else { | ||
|
||
// Firefox | ||
|
||
} | ||
|
||
} | ||
|
||
// PRNG | ||
|
||
var seed = 1; | ||
function random() { | ||
var x = Math.sin(seed++) * 10000; | ||
return x - Math.floor(x); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d58d00b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new "jank" feature is really cool! Clearly demonstrates the advantages of rendering within a worker 👍
d58d00b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it for Chrome Dev Summit's keynote 🤓
https://www.youtube.com/watch?v=zPHyxvPT0gg&t=958