Skip to content

Commit

Permalink
Complete particle height map example
Browse files Browse the repository at this point in the history
  • Loading branch information
dimroc committed Sep 30, 2012
1 parent 82029e1 commit 6644daa
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 27 deletions.
Binary file added images/star_height128.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 42 additions & 27 deletions index.html
Expand Up @@ -1023,6 +1023,20 @@ <h3>Render to texture</h3>
</section>
</section>

<section>
<section>
<h3>Vertex Textures</h3>
<canvas data-sample="height_map_image"></canvas>
<canvas data-sample="particle_height_map_vertex_texture"></canvas>
</section>

<section>
<h3>Height Map in Vertex Buffer</h3>
<canvas data-sample="height_map_image"></canvas>
<canvas data-sample="particle_height_map"></canvas>
</section>
</section>

<section>
<h3>John Carmack on WebGL</h3>
<br/>
Expand Down Expand Up @@ -1084,33 +1098,6 @@ <h2>Credits</h2>
<div class="progress"><span></span></div>
</div>

<script src="lib/js/head.min.js"></script>
<script src="lib/js/jquery.min.js"></script>
<script src="lib/js/jquery.hotkeys.js"></script>
<script src="lib/js/underscore.min.js"></script>
<script src="lib/js/swfobject.js"></script>
<script src="lib/js/dat.gui.js"></script>

<script src="lib/js/three.js"></script>
<script src="lib/js/three/EffectComposer.js"></script>
<script src="lib/js/three/ShaderExtras.js"></script>
<script src="lib/js/three/RenderPass.js"></script>
<script src="lib/js/three/BloomPass.js"></script>
<script src="lib/js/three/ShaderPass.js"></script>
<script src="lib/js/three/MaskPass.js"></script>

<!-- js files needed for WebGL specific samples (excluding three js) -->
<script src="lib/js/J3DI.js"></script>
<script src="lib/js/J3DIMath.js"></script>
<script src="lib/js/webgl-utils.js"></script>
<script src="lib/js/webgl-debug.js"></script>

<!-- App specific js -->
<script src="js/reveal.min.js"></script>
<script src="js/stats_bootstrap.js"></script>
<script src="js/samples.js"></script>
<script src="js/dat.gui.bootstrap.js"></script>

<!-- Shaders -->
<script id="webgl_vshader" type="x-shader/x-vertex">
uniform mat4 u_modelViewProjMatrix;
Expand Down Expand Up @@ -1210,6 +1197,34 @@ <h2>Credits</h2>
}
</script>

<script src="lib/js/head.min.js"></script>
<script src="lib/js/jquery.min.js"></script>
<script src="lib/js/jquery.hotkeys.js"></script>
<script src="lib/js/underscore.min.js"></script>
<script src="lib/js/swfobject.js"></script>
<script src="lib/js/dat.gui.js"></script>
<script src="lib/js/EventEmitter.js"></script>

<script src="lib/js/three.js"></script>
<script src="lib/js/three/EffectComposer.js"></script>
<script src="lib/js/three/ShaderExtras.js"></script>
<script src="lib/js/three/RenderPass.js"></script>
<script src="lib/js/three/BloomPass.js"></script>
<script src="lib/js/three/ShaderPass.js"></script>
<script src="lib/js/three/MaskPass.js"></script>

<!-- js files needed for WebGL specific samples (excluding three js) -->
<script src="lib/js/J3DI.js"></script>
<script src="lib/js/J3DIMath.js"></script>
<script src="lib/js/webgl-utils.js"></script>
<script src="lib/js/webgl-debug.js"></script>

<!-- App specific js -->
<script src="js/reveal.min.js"></script>
<script src="js/stats_bootstrap.js"></script>
<script src="js/samples.js"></script>
<script src="js/dat.gui.bootstrap.js"></script>

<script>

// Full list of configuration options available here:
Expand Down
3 changes: 3 additions & 0 deletions js/samples.js
Expand Up @@ -51,9 +51,12 @@
}

head.js(
"js/samples/height_map_image.js",
"js/samples/todo_lighted_spinning_textured_cube.js",
"js/samples/todo_lighted_spinning_cube.js",
"js/samples/particles.js",
"js/samples/particle_height_map.js",
"js/samples/particle_height_map_vertex_texture.js",
"js/samples/shadow_map.js",
"js/samples/render_to_texture.js",
"js/samples/blur_post_process.js",
Expand Down
38 changes: 38 additions & 0 deletions js/samples/height_map_image.js
@@ -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;
}
};
})();
84 changes: 84 additions & 0 deletions js/samples/particle_height_map.js
@@ -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);
}
};
})();
84 changes: 84 additions & 0 deletions js/samples/particle_height_map_vertex_texture.js
@@ -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);
}
};
})();

0 comments on commit 6644daa

Please sign in to comment.