|
| 1 | +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <script type="text/javascript" src="../lib/three.js"></script> |
| 5 | + <script type="text/javascript" src="../lib/stat.js"></script> |
| 6 | + |
| 7 | + <script type="text/javascript"> |
| 8 | + var requestAnimationFrame = window.requestAnimationFrame |
| 9 | + || window.mozRequestAnimationFrame |
| 10 | + || window.webkitRequestAnimationFrame |
| 11 | + || window.msRequestAnimationFrame; |
| 12 | + window.requestAnimationFrame = requestAnimationFrame; |
| 13 | + |
| 14 | + var scene = null; |
| 15 | + var camera = null; |
| 16 | + var renderer = null; |
| 17 | + |
| 18 | + var id = null; |
| 19 | + |
| 20 | + var stat = null; |
| 21 | + |
| 22 | + var ballMesh = null; |
| 23 | + var ballRadius = 0.5; |
| 24 | + var isMoving = false; |
| 25 | + var maxHeight = 5; |
| 26 | + |
| 27 | + var v = 0; |
| 28 | + var a = -0.01; |
| 29 | + |
| 30 | + function init() { |
| 31 | + stat = new Stats(); |
| 32 | + stat.domElement.style.position = 'absolute'; |
| 33 | + stat.domElement.style.right = '0px'; |
| 34 | + stat.domElement.style.top = '0px'; |
| 35 | + document.body.appendChild(stat.domElement); |
| 36 | + |
| 37 | + renderer = new THREE.WebGLRenderer({ |
| 38 | + canvas: document.getElementById('mainCanvas') |
| 39 | + }); |
| 40 | + scene = new THREE.Scene(); |
| 41 | + |
| 42 | + camera = new THREE.OrthographicCamera(-5, 5, 3.75, -3.75, 0.1, 100); |
| 43 | + camera.position.set(5, 10, 20); |
| 44 | + camera.lookAt(new THREE.Vector3(0, 3, 0)); |
| 45 | + scene.add(camera); |
| 46 | + |
| 47 | + // ball |
| 48 | + ballMesh = new THREE.Mesh(new THREE.SphereGeometry(ballRadius, 16, 8), |
| 49 | + new THREE.MeshLambertMaterial({ |
| 50 | + color: 0xffff00 |
| 51 | + })); |
| 52 | + ballMesh.position.y = ballRadius; |
| 53 | + scene.add(ballMesh); |
| 54 | + |
| 55 | + // plane |
| 56 | + var texture = THREE.ImageUtils.loadTexture('../img/chess.png', {}, function() { |
| 57 | + renderer.render(scene, camera); |
| 58 | + }); |
| 59 | + texture.wrapS = texture.wrapT = THREE.RepeatWrapping; |
| 60 | + texture.repeat.set(4, 4); |
| 61 | + var plane = new THREE.Mesh(new THREE.PlaneGeometry(5, 5), |
| 62 | + new THREE.MeshLambertMaterial({map: texture})); |
| 63 | + plane.rotation.x = -Math.PI / 2; |
| 64 | + scene.add(plane); |
| 65 | + |
| 66 | + var light = new THREE.DirectionalLight(0xffffff); |
| 67 | + light.position.set(10, 10, 15); |
| 68 | + scene.add(light); |
| 69 | + |
| 70 | + id = requestAnimationFrame(draw); |
| 71 | + } |
| 72 | + |
| 73 | + function draw() { |
| 74 | + stat.begin(); |
| 75 | + |
| 76 | + if (isMoving) { |
| 77 | + ballMesh.position.y += v; |
| 78 | + v += a; |
| 79 | + |
| 80 | + if (ballMesh.position.y <= ballRadius) { |
| 81 | + // hit plane |
| 82 | + v = -v * 0.9; |
| 83 | + } |
| 84 | + |
| 85 | + if (Math.abs(v) < 0.001) { |
| 86 | + // stop moving |
| 87 | + isMoving = false; |
| 88 | + ballMesh.position.y = ballRadius; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + renderer.render(scene, camera); |
| 93 | + |
| 94 | + id = requestAnimationFrame(draw); |
| 95 | + |
| 96 | + stat.end(); |
| 97 | + } |
| 98 | + |
| 99 | + function stop() { |
| 100 | + if (id !== null) { |
| 101 | + cancelAnimationFrame(id); |
| 102 | + id = null; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + function drop() { |
| 107 | + isMoving = true; |
| 108 | + ballMesh.position.y = maxHeight; |
| 109 | + v = 0; |
| 110 | + } |
| 111 | + </script> |
| 112 | + </head> |
| 113 | + |
| 114 | + <body onload="init()"> |
| 115 | + <canvas id="mainCanvas" width="400px" height="300px" ></canvas> |
| 116 | + <button id="dropBtn" onclick="drop()">Drop</button> |
| 117 | + </body> |
| 118 | +</html> |
0 commit comments