Skip to content
This repository
tree: c42d04f7cc
Fetching contributors…

Cannot retrieve contributors at this time

file 361 lines (235 sloc) 8.435 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
<!doctype html>
<html lang="en">
<head>
<title>three.js webgl - baked illumination</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background:#000;
color:#000;
padding:0;
margin:0;
overflow:hidden;
font-family:monospace;
font-size:13px;
text-align:center;
}
a { color:skyblue }
#info {
position: absolute;
top: 0px; width: 100%;
color: #fff;
background:rgba(0,0,0,0.65);
padding: 5px;
text-align: center;
z-index:100;
display:block;
}
</style>
</head>

<body>
<div id="info">
<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> webgl - baked + real-time illumination -
using <a href="http://code.google.com/p/js-openctm/">js-openctm</a> -
camaro by <a href="http://www.turbosquid.com/3d-models/blender-camaro/411348" target="_blank">dskfnwn</a> -
skybox by <a href="http://ict.debevec.org/~debevec/" target="_blank">Paul Debevec</a>
</div>

<script src="../build/Three.js"></script>

<script src="js/loaders/ctm/lzma.js"></script>
<script src="js/loaders/ctm/ctm.js"></script>
<script src="js/loaders/ctm/CTMLoader.js"></script>

<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>

<script>

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

var SCREEN_WIDTH = window.innerWidth;
var SCREEN_HEIGHT = window.innerHeight;
var FLOOR = -250;

var container, stats;

var camera, scene, controls;
var renderer;

var mesh;

var textureCube;
var cameraCube, sceneCube;

var loader;

var mouseX = 0, mouseY = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

init();
animate();

function init() {

container = document.createElement( 'div' );
document.body.appendChild( container );

// SCENE

scene = new THREE.Scene();

// CAMERA

camera = new THREE.PerspectiveCamera( 25, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
camera.position.set( 185, 40, 170 );
scene.add( camera );

controls = new THREE.TrackballControls( camera );
controls.dynamicDampingFactor = 0.25;


// SKYBOX

sceneCube = new THREE.Scene();
cameraCube = new THREE.PerspectiveCamera( 25, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
sceneCube.add( cameraCube );

var r = "textures/cube/pisa/";
var urls = [ r + "px.png", r + "nx.png",
r + "py.png", r + "ny.png",
r + "pz.png", r + "nz.png" ];


textureCube = THREE.ImageUtils.loadTextureCube( urls );

var shader = THREE.ShaderUtils.lib[ "cube" ];
shader.uniforms[ "tCube" ].texture = textureCube;

var material = new THREE.ShaderMaterial( {

fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: shader.uniforms,
depthWrite: false

} ),

mesh = new THREE.Mesh( new THREE.CubeGeometry( 100, 100, 100 ), material );
mesh.flipSided = true;
sceneCube.add( mesh );

// LIGHTS

var light = new THREE.PointLight( 0xffffff, 1 );
light.position.set( 2, 5, 1 );
light.position.multiplyScalar( 30 );
scene.add( light );

THREE.ColorUtils.adjustHSV( light.color, 0, -0.75, 0 );

var light = new THREE.PointLight( 0xffffff, 0.75 );
light.position.set( -12, 4.6, 2.4 );
light.position.multiplyScalar( 30 );
scene.add( light );

THREE.ColorUtils.adjustHSV( light.color, 0, -0.5, 0 );

scene.add( new THREE.AmbientLight( 0x050505 ) );

// RENDERER

renderer = new THREE.WebGLRenderer( { antialias: true, clearColor: 0xffffff, clearAlpha: 1 } );
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
renderer.domElement.style.position = "relative";

renderer.autoClear = false;

container.appendChild( renderer.domElement );

//

renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.physicallyBasedShading = true;

// STATS

stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '5px';
stats.domElement.style.zIndex = 100;
container.appendChild( stats.domElement );

stats.domElement.children[ 0 ].children[ 0 ].style.color = "#aaa";
stats.domElement.children[ 0 ].style.background = "transparent";
stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";

// EVENTS

window.addEventListener( 'resize', onWindowResize, false );
window.addEventListener( 'mousemove', onDocumentMouseMove, false );

// LOADER

var start = Date.now();

// classic way via BinaryLoader
/*
loader = new THREE.BinaryLoader( true );
document.body.appendChild( loader.statusDomElement );

var callbackScene = function( geometry ) {

createScene( geometry, 0, 0, 0, 30 );

var end = Date.now();

console.log( "load time:", end - start, "ms" );

};

loader.load( "obj/camaro_bake/camaro_bake.js", callbackScene );

*/
// new way via CTMLoader and separate parts

var useWorker = true,
useBuffers = true;

loaderCTM = new THREE.CTMLoader( renderer.context, true );
document.body.appendChild( loaderCTM.statusDomElement );

var position = new THREE.Vector3( -105, -78, -40 );
var scale = new THREE.Vector3( 30, 30, 30 );

loaderCTM.loadParts( "models/ctm/camaro/camaro.js", function( geometries, materials ) {

hackMaterials( materials );

for ( var i = 0; i < geometries.length; i ++ ) {

var mesh = new THREE.Mesh( geometries[ i ], materials[ i ] );
mesh.position = position;
mesh.scale = scale;
mesh.doubleSided = true;
scene.add( mesh );

}

loaderCTM.statusDomElement.style.display = "none";

var end = Date.now();

console.log( "load time:", end - start, "ms" );

}, useWorker, useBuffers );

}

//

function hackMaterials( materials ) {

for ( var i = 0; i < materials.length; i ++ ) {

var m = materials[ i ];

if ( m.name.indexOf( "Body" ) !== -1 ) {

var mm = new THREE.MeshPhongMaterial( { map: m.map } );

mm.envMap = textureCube;
mm.combine = THREE.MixOperation;
mm.reflectivity = 0.75;

materials[ i ] = mm;

} else if ( m.name.indexOf( "mirror" ) !== -1 ) {

var mm = new THREE.MeshPhongMaterial( { map: m.map } );

mm.envMap = textureCube;
mm.combine = THREE.MultiplyOperation;

materials[ i ] = mm;

} else if ( m.name.indexOf( "glass" ) !== -1 ) {

var mm = new THREE.MeshPhongMaterial( { map: m.map } );

mm.envMap = textureCube;
mm.color.copy( m.color );
mm.combine = THREE.MixOperation;
mm.reflectivity = 0.25;
mm.opacity = m.opacity;
mm.transparent = true;

materials[ i ] = mm;

} else if ( m.name.indexOf( "Material.001" ) !== -1 ) {

var mm = new THREE.MeshPhongMaterial( { map: m.map } );

mm.shininess = 30;
mm.color.setHex( 0x404040 );
mm.metal = true;
mm.perPixel = true;

materials[ i ] = mm;

}

}

}

//

function createScene( geometry, x, y, z, s ) {

loader.statusDomElement.style.display = "none";

THREE.GeometryUtils.center( geometry );
hackMaterials( geometry.materials );

var material = new THREE.MeshFaceMaterial();

mesh = new THREE.Mesh( geometry, material );
mesh.position.set( x, y, z );
mesh.scale.set( s, s, s );
mesh.doubleSided = true;
scene.add( mesh );

}

//

function onWindowResize( event ) {

SCREEN_WIDTH = window.innerWidth;
SCREEN_HEIGHT = window.innerHeight;

renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );

camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
camera.updateProjectionMatrix();

cameraCube.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
cameraCube.updateProjectionMatrix();

}


function onDocumentMouseMove(event) {

mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY );

}

//

function animate() {

requestAnimationFrame( animate );

render();
stats.update();

}

function render() {

controls.update();

cameraCube.rotation.copy( camera.rotation );

renderer.clear();
renderer.render( sceneCube, cameraCube );
renderer.render( scene, camera );

}

</script>

</body>
</html>
Something went wrong with that request. Please try again.