|
| 1 | +<!DOCTYPE HTML> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js - cube map WebGL</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <style type="text/css"> |
| 7 | + body { |
| 8 | + background:#000; |
| 9 | + color:#fff; |
| 10 | + padding:0; |
| 11 | + margin:0; |
| 12 | + overflow:hidden; |
| 13 | + font-family:georgia; |
| 14 | + text-align:center; |
| 15 | + } |
| 16 | + a { color: #ff0080; text-decoration: none; } |
| 17 | + a:hover { color: #0080ff; } |
| 18 | + |
| 19 | + canvas { pointer-events:none; z-index:10; position:relative; } |
| 20 | + #log { position:absolute; top:50px; text-align:left; display:block; z-index:100; pointer-events:none; } |
| 21 | + #d { text-align:center; margin:1em auto -9.5em; z-index:1000; position:relative; display:block; |
| 22 | + background:rgba(0,0,0,0.5); padding:0.5em; width:400px; border-radius:15px; -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,0.5) } |
| 23 | + </style> |
| 24 | + </head> |
| 25 | + |
| 26 | + <body> |
| 27 | + <div id="d"> |
| 28 | + <p><a href="http://github.com/mrdoob/three.js">Three.js</a> cube map refraction demo |
| 29 | + |
| 30 | + <p>Lucy model from <a href="http://graphics.stanford.edu/data/3Dscanrep/">Stanford 3d scanning repository</a> |
| 31 | + <p>Texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank">Humus</a> |
| 32 | + </div> |
| 33 | + |
| 34 | + <pre id="log"></pre> |
| 35 | + |
| 36 | + <script type="text/javascript" src="../build/Three.js"></script> |
| 37 | + |
| 38 | + <script type="text/javascript" src="../src/extras/primitives/Sphere.js"></script> |
| 39 | + <script type="text/javascript" src="../src/extras/primitives/Plane.js"></script> |
| 40 | + <script type="text/javascript" src="../src/extras/io/Loader.js"></script> |
| 41 | + |
| 42 | + <script type="text/javascript" src="js/Stats.js"></script> |
| 43 | + |
| 44 | + <script type="text/javascript"> |
| 45 | + |
| 46 | + var FLOOR = -250; |
| 47 | + |
| 48 | + var container, stats; |
| 49 | + |
| 50 | + var camera, scene, webglRenderer; |
| 51 | + var cameraCube, sceneCube; |
| 52 | + |
| 53 | + var mesh, zmesh, lightMesh, geometry; |
| 54 | + |
| 55 | + var directionalLight, pointLight; |
| 56 | + |
| 57 | + var mouseX = 0; |
| 58 | + var mouseY = 0; |
| 59 | + |
| 60 | + var windowHalfX = window.innerWidth / 2; |
| 61 | + var windowHalfY = window.innerHeight / 2; |
| 62 | + |
| 63 | + document.addEventListener('mousemove', onDocumentMouseMove, false); |
| 64 | + |
| 65 | + init(); |
| 66 | + setInterval( loop, 1000 / 60 ); |
| 67 | + |
| 68 | + function init() { |
| 69 | + |
| 70 | + container = document.createElement('div'); |
| 71 | + document.body.appendChild(container); |
| 72 | + |
| 73 | + camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 100000 ); |
| 74 | + camera.position.z = 2000; |
| 75 | + |
| 76 | + cameraCube = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 100000 ); |
| 77 | + |
| 78 | + scene = new THREE.Scene(); |
| 79 | + sceneCube = new THREE.Scene(); |
| 80 | + |
| 81 | + // LIGHTS |
| 82 | + |
| 83 | + var ambient = new THREE.AmbientLight( 0xffffff ); |
| 84 | + scene.addLight( ambient ); |
| 85 | + |
| 86 | + pointLight = new THREE.PointLight( 0xffffff, 2 ); |
| 87 | + scene.addLight( pointLight ); |
| 88 | + |
| 89 | + // light representation |
| 90 | + sphere = new Sphere( 100, 16, 8, 1 ); |
| 91 | + lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color:0xffaa00 } ) ); |
| 92 | + lightMesh.position = pointLight.position; |
| 93 | + lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05; |
| 94 | + scene.addObject(lightMesh); |
| 95 | + |
| 96 | + // material samples |
| 97 | + |
| 98 | + |
| 99 | + var r = "textures/cube/Park3Med/"; |
| 100 | + |
| 101 | + var urls = [ r + "px.jpg", r + "nx.jpg", |
| 102 | + r + "py.jpg", r + "ny.jpg", |
| 103 | + r + "pz.jpg", r + "nz.jpg" ]; |
| 104 | + |
| 105 | + var images = loadImageArray( urls ); |
| 106 | + var cubeMaterial3 = new THREE.MeshBasicMaterial( { color: 0xccddff, env_map: new THREE.TextureCube( images, THREE.RefractionMap ), refraction_ratio: 0.98, reflectivity:0.9 } ); |
| 107 | + var cubeMaterial2 = new THREE.MeshBasicMaterial( { color: 0xccfffd, env_map: new THREE.TextureCube( images, THREE.RefractionMap ), refraction_ratio: 0.985 } ); |
| 108 | + var cubeMaterial1 = new THREE.MeshBasicMaterial( { color: 0xffffff, env_map: new THREE.TextureCube( images, THREE.RefractionMap ), refraction_ratio: 0.98 } ) |
| 109 | + |
| 110 | + createCube( 10000, images ); |
| 111 | + |
| 112 | + webglRenderer = new THREE.WebGLRenderer( scene ); |
| 113 | + webglRenderer.setSize( window.innerWidth, window.innerHeight ); |
| 114 | + webglRenderer.autoClear = false; |
| 115 | + container.appendChild( webglRenderer.domElement ); |
| 116 | + |
| 117 | + stats = new Stats(); |
| 118 | + stats.domElement.style.position = 'absolute'; |
| 119 | + stats.domElement.style.top = '0px'; |
| 120 | + stats.domElement.style.zIndex = 100; |
| 121 | + container.appendChild( stats.domElement ); |
| 122 | + |
| 123 | + var loader = new THREE.Loader(); |
| 124 | + loader.loadBinary( 'obj/lucy/Lucy100k_bin.js', function( geometry ) { createScene( geometry, cubeMaterial1, cubeMaterial2, cubeMaterial3 ) }, "obj/lucy" ); |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + function addMesh( geometry, scale, x, y, z, rx, ry, rz, material ) { |
| 129 | + |
| 130 | + mesh = new THREE.Mesh( geometry, material ); |
| 131 | + mesh.scale.x = mesh.scale.y = mesh.scale.z = scale; |
| 132 | + mesh.position.x = x; |
| 133 | + mesh.position.y = y; |
| 134 | + mesh.position.z = z; |
| 135 | + mesh.rotation.x = rx; |
| 136 | + mesh.rotation.y = ry; |
| 137 | + mesh.rotation.z = rz; |
| 138 | + scene.addObject(mesh); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + function addCubeMesh( geometry, scale, x, y, z, rx, ry, rz, material ) { |
| 143 | + |
| 144 | + mesh = new THREE.Mesh( geometry, material ); |
| 145 | + mesh.position.x = x; |
| 146 | + mesh.position.y = y; |
| 147 | + mesh.position.z = z; |
| 148 | + mesh.rotation.x = rx; |
| 149 | + mesh.rotation.y = ry; |
| 150 | + mesh.rotation.z = rz; |
| 151 | + mesh.scale.x = mesh.scale.y = mesh.scale.z = scale; |
| 152 | + sceneCube.addObject(mesh); |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + function createCube( size, images ) { |
| 157 | + |
| 158 | + var hsize = size/2, plane = new Plane( size, size ), pi2 = Math.PI/2, pi = Math.PI; |
| 159 | + |
| 160 | + addCubeMesh( plane, 1, 0, 0, -hsize, 0, 0, 0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[5] ) } ) ); |
| 161 | + addCubeMesh( plane, 1, -hsize, 0, 0, 0, pi2, 0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[0] ) } ) ); |
| 162 | + addCubeMesh( plane, 1, hsize, 0, 0, 0, -pi2, 0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[1] ) } ) ); |
| 163 | + addCubeMesh( plane, 1, 0, hsize, 0, pi2, 0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[2] ) } ) ); |
| 164 | + addCubeMesh( plane, 1, 0, -hsize, 0, -pi2, 0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[3] ) } ) ); |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + function createScene( geometry, m1, m2, m3 ) { |
| 169 | + |
| 170 | + var s = 1.5, z = -1000; |
| 171 | + |
| 172 | + addMesh( geometry, s, 0, 0, z, 0,0,0, m1 ); |
| 173 | + addMesh( geometry, s, -900, 0, z, 0,0,0, m2 ); |
| 174 | + addMesh( geometry, s, 900, 0, z, 0,0,0, m3 ); |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + function loadImageArray( urls ) { |
| 179 | + |
| 180 | + var i, images = []; |
| 181 | + |
| 182 | + images.loadCount = 0; |
| 183 | + |
| 184 | + for ( i = 0; i < urls.length; ++i ) { |
| 185 | + |
| 186 | + images[i] = new Image(); |
| 187 | + images[i].loaded = 0; |
| 188 | + images[i].onload = function() { images.loadCount += 1; this.loaded = 1;/*log( images.loadCount );*/ } |
| 189 | + images[i].src = urls[i]; |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + return images; |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + function onDocumentMouseMove(event) { |
| 198 | + |
| 199 | + mouseX = ( event.clientX - windowHalfX ) * 4; |
| 200 | + mouseY = ( event.clientY - windowHalfY ) * 4; |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | + var r = 0; |
| 205 | + |
| 206 | + function loop() { |
| 207 | + |
| 208 | + camera.position.x += ( mouseX - camera.position.x ) * .05; |
| 209 | + camera.position.y += ( - mouseY - camera.position.y ) * .05; |
| 210 | + |
| 211 | + cameraCube.target.position.x = - camera.position.x; |
| 212 | + cameraCube.target.position.y = - camera.position.y; |
| 213 | + cameraCube.target.position.z = - camera.position.z; |
| 214 | + |
| 215 | + |
| 216 | + lightMesh.position.x = 1500 * Math.cos( r ); |
| 217 | + lightMesh.position.z = 1500 * Math.sin( r ); |
| 218 | + |
| 219 | + r += 0.01; |
| 220 | + |
| 221 | + webglRenderer.clear(); |
| 222 | + webglRenderer.render( sceneCube, cameraCube ); |
| 223 | + webglRenderer.render( scene, camera ); |
| 224 | + |
| 225 | + stats.update(); |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | + function log(text) { |
| 230 | + |
| 231 | + var e = document.getElementById("log"); |
| 232 | + e.innerHTML = text + "<br/>" + e.innerHTML; |
| 233 | + |
| 234 | + } |
| 235 | + </script> |
| 236 | + |
| 237 | + </body> |
| 238 | +</html> |
0 commit comments