forked from hakimel/reveal.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete particle height map example
- Loading branch information
Showing
6 changed files
with
251 additions
and
27 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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 @@ | ||
(function() { | ||
|
||
function HeightMapCanvas() { | ||
var that = this; | ||
var eventEmitter = new EventEmitter(); | ||
var image = new Image(); | ||
|
||
this.addListener = function(event, listener) { | ||
return eventEmitter.addListener(event, listener); | ||
}; | ||
|
||
this.imageData = null; | ||
|
||
this.initialize = function(canvas) { | ||
context = canvas.getContext('2d'); | ||
|
||
image.onload = function() { | ||
canvas.width = image.width; | ||
canvas.height = image.height; | ||
context.drawImage(image, 0, 0); | ||
|
||
that.imageData = context.getImageData(0, 0, image.width, image.height); | ||
eventEmitter.emitEvent("onload", [canvas]); | ||
}; | ||
|
||
image.src = "images/star_height128.jpg"; | ||
}; | ||
} | ||
|
||
window.samples.height_map_image = { | ||
initialize: function(canvas) { | ||
var heightMapCanvas = new HeightMapCanvas(); | ||
heightMapCanvas.initialize(canvas); | ||
|
||
return heightMapCanvas; | ||
} | ||
}; | ||
})(); |
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,84 @@ | ||
(function() { | ||
|
||
function ParticleSample() { | ||
var container, | ||
camera, | ||
scene, | ||
renderer, | ||
particles, | ||
geometry, | ||
materials = [], | ||
parameters, | ||
i, | ||
h, | ||
color; | ||
|
||
var mouseX = 0, mouseY = 0; | ||
|
||
var width = sample_defaults.width * 2; | ||
var height = sample_defaults.height * 2; | ||
|
||
this.initialize = function(canvas) { | ||
camera = new THREE.PerspectiveCamera( 30, width / height, 1, 1000 ); | ||
camera.lookAt( new THREE.Vector3(5, -20, -10) ); | ||
camera.position.set( 0, 250, 150); | ||
|
||
scene = new THREE.Scene(); | ||
|
||
var instance = { active: false }; | ||
|
||
var heightMapImageSample = $("canvas[data-sample=height_map_image]").data("instance"); | ||
heightMapImageSample.addListener("onload", function() { | ||
|
||
geometry = new THREE.Geometry(); | ||
|
||
for ( i = 0; i < 128; i ++ ) { | ||
for ( j = 0; j < 128; j ++ ) { | ||
|
||
var vertex = new THREE.Vector3(); | ||
vertex.x = i; | ||
vertex.z = j; | ||
|
||
var dataPosition = i * 128 * 4 + j * 4; | ||
vertex.y = heightMapImageSample.imageData.data[dataPosition] / 255.0 * 64; | ||
|
||
geometry.vertices.push( vertex ); | ||
} | ||
} | ||
|
||
var color = [0.4, 1.0, 0]; | ||
var size = 2; | ||
|
||
material = new THREE.ParticleBasicMaterial( { size: size } ); | ||
material.color.setHSV( color[0], color[1], color[2] ); | ||
|
||
particles = new THREE.ParticleSystem( geometry, material ); | ||
|
||
scene.add( particles ); | ||
|
||
var axisHelper = new THREE.AxisHelper(); | ||
scene.add( axisHelper ); | ||
|
||
renderer = new THREE.WebGLRenderer({canvas: canvas}); | ||
renderer.setSize( width, height ); | ||
|
||
function animate() { | ||
requestAnimationFrame( animate ); | ||
if(!instance.active || sample_defaults.paused) return; | ||
renderer.render( scene, camera ); | ||
} | ||
|
||
animate(); | ||
}); | ||
|
||
return instance; | ||
}; | ||
} | ||
|
||
window.samples.particle_height_map = { | ||
initialize: function(canvas) { | ||
var particleSample = new ParticleSample(); | ||
return particleSample.initialize(canvas); | ||
} | ||
}; | ||
})(); |
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,84 @@ | ||
(function() { | ||
|
||
function ParticleSample() { | ||
var container, | ||
camera, | ||
scene, | ||
renderer, | ||
particles, | ||
geometry, | ||
materials = [], | ||
parameters, | ||
i, | ||
h, | ||
color; | ||
|
||
var mouseX = 0, mouseY = 0; | ||
|
||
var width = sample_defaults.width * 2; | ||
var height = sample_defaults.height * 2; | ||
|
||
this.initialize = function(canvas) { | ||
camera = new THREE.PerspectiveCamera( 30, width / height, 1, 1000 ); | ||
camera.lookAt( new THREE.Vector3(5, -20, -10) ); | ||
camera.position.set( 0, 250, 150); | ||
|
||
scene = new THREE.Scene(); | ||
|
||
var instance = { active: false }; | ||
|
||
var heightMapImageSample = $("canvas[data-sample=height_map_image]").data("instance"); | ||
heightMapImageSample.addListener("onload", function() { | ||
|
||
geometry = new THREE.Geometry(); | ||
|
||
for ( i = 0; i < 128; i ++ ) { | ||
for ( j = 0; j < 128; j ++ ) { | ||
|
||
var vertex = new THREE.Vector3(); | ||
vertex.x = i; | ||
vertex.z = j; | ||
|
||
var dataPosition = i * 128 * 4 + j * 4; | ||
vertex.y = heightMapImageSample.imageData.data[dataPosition] / 255.0 * 64; | ||
|
||
geometry.vertices.push( vertex ); | ||
} | ||
} | ||
|
||
var color = [0.4, 1.0, 0]; | ||
var size = 2; | ||
|
||
material = new THREE.ParticleBasicMaterial( { size: size } ); | ||
material.color.setHSV( color[0], color[1], color[2] ); | ||
|
||
particles = new THREE.ParticleSystem( geometry, material ); | ||
|
||
scene.add( particles ); | ||
|
||
var axisHelper = new THREE.AxisHelper(); | ||
scene.add( axisHelper ); | ||
|
||
renderer = new THREE.WebGLRenderer({canvas: canvas}); | ||
renderer.setSize( width, height ); | ||
|
||
function animate() { | ||
requestAnimationFrame( animate ); | ||
if(!instance.active || sample_defaults.paused) return; | ||
renderer.render( scene, camera ); | ||
} | ||
|
||
animate(); | ||
}); | ||
|
||
return instance; | ||
}; | ||
} | ||
|
||
window.samples.particle_height_map_vertex_texture = { | ||
initialize: function(canvas) { | ||
var particleSample = new ParticleSample(); | ||
return particleSample.initialize(canvas); | ||
} | ||
}; | ||
})(); |