Permalink
Browse files

merged

  • Loading branch information...
2 parents af91788 + 94e068e commit b11949718efd97783e59de19039e95d677d1a911 @yomotsu committed Jul 4, 2012
Showing with 324 additions and 0 deletions.
  1. +3 −0 README.md
  2. +13 −0 index.html
  3. +8 −0 lib/Stats.js
  4. +300 −0 physijs_tetris/index.html
View
@@ -0,0 +1,3 @@
+# See Live Examples
+
+[from here](http://yomotsu.github.com/threejs-examples/)
View
@@ -8,6 +8,12 @@
html{
font-size:16px;
font-family: 'Dosis', sans-serif;
+<<<<<<< HEAD
+=======
+}
+html,
+.contents a{
+>>>>>>> gh-pages
background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAANCAAAAACBJWHFAAAAk0lEQVQIHQXBwWEAMQgDMPZftU0wGBPofSpZcnV6qrxGt1bPwJvfu6yU546rDcFNviN4rba87I548/WPr4rsvtbIjsyQEIqmy15eMNHf9LkV6LHYcY8p1tt7ME8WVcRGMRVJJWmEa9tRSJ+/vlHmXHV8OMge9OBYSpmh8VJXB5/bxq0o5P4BBBGfzQgPGtYQ+cutf1ga3nO49mrYAAAAAElFTkSuQmCC);
}
body{
@@ -119,7 +125,10 @@
display:block;
position:relative;
z-index:3;
+<<<<<<< HEAD
background:url(_bg.png);
+=======
+>>>>>>> gh-pages
-webkit-transform:translate3d(0, 0, 0);
-moz-transform:translate3d(0, 0, 0);
-ms-transform:translate3d(0, 0, 0);
@@ -230,7 +239,11 @@
<header>
<nav>
<ul>
+<<<<<<< HEAD
<li><a href="github.com/yomotsu/">github</a></li>
+=======
+ <li><a href="http://github.com/yomotsu/">github</a></li>
+>>>>>>> gh-pages
<li><a href="http://twitter.com/yomotsu/">twitter</a></li>
<li><a href="http://yomotsu.net">blog</a></li>
<li><a href="http://www.pxgrid.com">PixelGrid</a></li>
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,300 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>Tetris w/ THREE.js and Physijs</title>
+<style>
+body{
+ margin:0;
+ padding:0;
+ overflow:hidden;
+}
+.info{
+ color:#fff;
+ font-family:Helvetica, Arial, sans-serif;
+ position:absolute;
+ top:60px;
+ left:0;
+ z-index:10;
+ padding:10px;
+ background:rgba(0,0,0,.7);
+}
+</style>
+<script src="../lib/Three_r49.js"></script>
+<script src="../lib/Stats.js"></script>
+<script src="physi.js"></script>
+<script>
+window.addEventListener( 'DOMContentLoaded', function(){
+ Physijs.scripts.worker = 'physijs_worker.js';
+ Physijs.scripts.ammo = 'ammo.js';
+ var activeMesh = null;
+ var activeMeshAxis = 0;
+
+ var width = window.innerWidth;
+ var height = window.innerHeight;
+
+ var scene = new Physijs.Scene; // instead of "new THREE.Scene()"
+
+ var camera = new THREE.PerspectiveCamera( 40, width / height, 1, 1000 );
+ camera.position.set( 30, 30, 180 );
+ //camera.lookAt( scene.position );
+ camera.lookAt(new THREE.Vector3(0, 50, 0));
+ scene.add( camera );
+
+ var renderer = new THREE.WebGLRenderer({ antialias: true });
+ renderer.setSize( width, height );
+ renderer.shadowMapEnabled = true;
+ renderer.shadowMapSoft = true;
+ document.body.appendChild( renderer.domElement );
+
+ var stats = new Stats();
+ stats.domElement.style.position = 'absolute';
+ stats.domElement.style.top = '0px';
+ stats.domElement.style.zIndex = 100;
+ document.body.appendChild( stats.domElement );
+
+ var light = new THREE.DirectionalLight( 0xFFFFFF );
+ light.position.set( 20, 40, 15 );
+ light.target.position.copy( scene.position );
+ light.castShadow = true;
+ light.shadowCameraLeft = -60;
+ light.shadowCameraTop = -60;
+ light.shadowCameraRight = 60;
+ light.shadowCameraBottom = 60;
+ light.shadowCameraNear = 20;
+ light.shadowCameraFar = 200;
+ light.shadowBias = -.0001
+ light.shadowMapWidth = light.shadowMapHeight = 2048;
+ light.shadowDarkness = .7;
+ scene.add( light );
+
+ // Ground
+ var ground_material = Physijs.createMaterial(
+ new THREE.MeshLambertMaterial({ map: THREE.ImageUtils.loadTexture( 'bg.jpg' ) }),
+ 1.0, // high friction
+ 1.0 // low restitution
+ );
+ ground_material.map.wrapS = ground_material.map.wrapT = THREE.RepeatWrapping;
+ ground_material.map.repeat.set( 3, 3 );
+
+ var ground = new Physijs.BoxMesh(
+ new THREE.CubeGeometry(100, 1, 100),
+ ground_material,
+ 0 // mass
+ );
+ ground.receiveShadow = true;
+ scene.add( ground );
+
+ render();
+ createBlock();
+ document.addEventListener('keypress',function(e){
+ console.log(e.keyCode)
+ if(e.keyCode === 32 && !!activeMesh){ //Space key
+ e.preventDefault();
+ activeMesh.position.x += activeMeshAxis;
+ activeMesh.rotation.z += 90 * Math.PI / 180;
+ activeMesh.__dirtyPosition = true;
+ activeMesh.__dirtyRotation = true;
+ } else if(e.keyCode === 97){ //A key
+ e.preventDefault();
+ activeMesh.position.x -= 5;
+ activeMesh.__dirtyPosition = true;
+ } else if(e.keyCode === 100){ //D key
+ e.preventDefault();
+ activeMesh.position.x += 5;
+ activeMesh.__dirtyPosition = true;
+ }
+ },false);
+
+ function createBlock(){
+ var blocks =[
+
+ //l-tetromino
+ function(){
+ var material = new THREE.MeshLambertMaterial({ color: 0x00ffff });
+ var parent = new Physijs.BoxMesh( new THREE.CubeGeometry( 5, 20, 5 ), material );
+
+ parent.position.set(0,100,0)
+ parent.castShadow = true;
+ scene.add( parent );
+ activeMesh = parent;
+
+ var active = true;
+ parent.addEventListener( 'collision', function() {
+ if(active){
+ active = false;
+ createBlock();
+ }
+ });
+ },
+
+ //O-tetromino
+ function(){
+ var material = new THREE.MeshLambertMaterial({ color: 0xffff00 });
+ var parent = new Physijs.BoxMesh( new THREE.CubeGeometry( 10, 10, 5 ), material );
+
+ parent.position.set(2.5,100,0)
+ parent.castShadow = true;
+ scene.add( parent );
+ activeMesh = parent;
+
+ var active = true;
+ parent.addEventListener( 'collision', function() {
+ if(active){
+ active = false;
+ createBlock();
+ }
+ });
+ },
+
+ //T-tetromino
+ function(){
+ var material = new THREE.MeshLambertMaterial({ color: 0xff00ff });
+ var parent = new Physijs.BoxMesh( new THREE.CubeGeometry( 15, 5, 5 ), material );
+ var child1 = new Physijs.BoxMesh( new THREE.CubeGeometry( 5, 5, 5 ), material );
+ child1.position.set( 0, -5, 0 );
+ child1.castShadow = true;
+ parent.add( child1 );
+
+ parent.position.set(0,100,0)
+ parent.castShadow = true;
+ scene.add( parent );
+ activeMesh = parent;
+
+ var active = true;
+ parent.addEventListener( 'collision', function() {
+ if(active){
+ active = false;
+ createBlock();
+ }
+ });
+ },
+
+ //J-tetromino
+ function(){
+ var material = new THREE.MeshLambertMaterial({ color: 0x0000ff });
+ var parent = new Physijs.BoxMesh( new THREE.CubeGeometry( 10, 5, 5 ), material );
+ var child1 = new Physijs.BoxMesh( new THREE.CubeGeometry( 5, 10, 5 ), material );
+ child1.position.set( 7.5, 2.5, 0 );
+ child1.castShadow = true;
+ parent.add( child1 );
+
+ parent.position.set(2.5,100,0)
+ parent.castShadow = true;
+ scene.add( parent );
+ activeMesh = parent;
+
+ var active = true;
+ parent.addEventListener( 'collision', function() {
+ if(active){
+ active = false;
+ activeMesh = null;
+ createBlock();
+ }
+ });
+ },
+
+ //L-tetromino
+ function(){
+ var material = new THREE.MeshLambertMaterial({ color: 0xff9900 });
+ var parent = new Physijs.BoxMesh( new THREE.CubeGeometry( 10, 5, 5 ), material );
+ var child1 = new Physijs.BoxMesh( new THREE.CubeGeometry( 5, 10, 5 ), material );
+ child1.position.set( -7.5, 2.5, 0 );
+ child1.castShadow = true;
+ parent.add( child1 );
+
+ parent.position.set(2.5,100,0)
+ parent.castShadow = true;
+ scene.add( parent );
+ activeMesh = parent;
+
+ var active = true;
+ parent.addEventListener( 'collision', function() {
+ if(active){
+ active = false;
+ activeMesh = null;
+ createBlock();
+ }
+ });
+ },
+
+ //S-tetromino
+ function(){
+ var material = new THREE.MeshLambertMaterial({ color: 0x00ff00 });
+ var parent = new Physijs.BoxMesh( new THREE.CubeGeometry( 10, 5, 5 ), material );
+ var child1 = new Physijs.BoxMesh( new THREE.CubeGeometry( 10, 5, 5 ), material );
+ child1.position.set( -5, 5, 0 );
+ child1.castShadow = true;
+ parent.add( child1 );
+
+ parent.position.set(2.5,100,0)
+ parent.castShadow = true;
+ scene.add( parent );
+ activeMesh = parent;
+
+ var active = true;
+ parent.addEventListener( 'collision', function() {
+ if(active){
+ active = false;
+ activeMesh = null;
+ createBlock();
+ }
+ });
+ },
+
+ //Z-tetromino
+ function(){
+ var material = new THREE.MeshLambertMaterial({ color: 0xff0000 });
+ var parent = new Physijs.BoxMesh( new THREE.CubeGeometry( 10, 5, 5 ), material );
+ var child1 = new Physijs.BoxMesh( new THREE.CubeGeometry( 10, 5, 5 ), material );
+ child1.position.set( -5, -5, 0 );
+ child1.castShadow = true;
+ parent.add( child1 );
+
+ parent.position.set(2.5,100,0)
+ parent.castShadow = true;
+ scene.add( parent );
+ activeMesh = parent;
+
+ var active = true;
+ parent.addEventListener( 'collision', function() {
+ if(active){
+ active = false;
+ activeMesh = null;
+ createBlock();
+ }
+ });
+ },
+ ];
+
+ var rand = Math.floor (Math.random () * blocks.length);
+ blocks[rand]();
+ }
+
+ function checkBottom(){
+ if(activeMesh && activeMesh.position.y < -30){
+ active = false;
+ activeMesh = null;
+ createBlock();
+ }
+ }
+
+ function render() {
+ scene.simulate(undefined, 2);
+ renderer.render( scene, camera);
+ stats.update();
+ checkBottom();
+ requestAnimationFrame( render );
+ };
+
+}, false);
+</script>
+</head>
+<body>
+<div class="info">
+[A] key : to move left<br>
+[D] key : to move right<br>
+[Space] key : to rotate
+</div>
+
+</body>
+</html>

0 comments on commit b119497

Please sign in to comment.