|
@@ -1,7 +1,7 @@
|
|
|
<!DOCTYPE html>
|
|
|
<html lang="en">
|
|
|
<head>
|
|
|
- <title>three.js webgl - collada</title>
|
|
|
+ <title>three.js webgl - STL</title>
|
|
|
<meta charset="utf-8">
|
|
|
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
|
|
<style>
|
|
@@ -29,7 +29,7 @@
|
|
|
<body>
|
|
|
<div id="info">
|
|
|
<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> -
|
|
|
- STL loader test by <a href="https://github.com/aleeper">aleeper</a>
|
|
|
+ STL loader test by <a href="https://github.com/aleeper">aleeper</a>
|
|
|
</div>
|
|
|
|
|
|
<script src="../build/three.min.js"></script>
|
|
@@ -46,7 +46,6 @@
|
|
|
var container, stats;
|
|
|
|
|
|
var camera, scene, renderer, objects;
|
|
|
- var particleLight, pointLight;
|
|
|
|
|
|
init();
|
|
|
animate();
|
|
@@ -56,17 +55,20 @@
|
|
|
container = document.createElement( 'div' );
|
|
|
document.body.appendChild( container );
|
|
|
|
|
|
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
|
|
|
- camera.position.set( 2, 2, 3 );
|
|
|
+ camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
|
|
|
+ camera.position.set( 3, 0.5, 3 );
|
|
|
|
|
|
scene = new THREE.Scene();
|
|
|
|
|
|
+ scene.fog = new THREE.Fog( 0xffffff, 2, 15 );
|
|
|
+ scene.fog.color.setHSV( 0.06, 0.2, 0.45 );
|
|
|
+
|
|
|
// Grid
|
|
|
|
|
|
- var size = 14, step = 1;
|
|
|
+ var size = 20, step = 0.25;
|
|
|
|
|
|
var geometry = new THREE.Geometry();
|
|
|
- var material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } );
|
|
|
+ var material = new THREE.LineBasicMaterial( { color: 0x000000 } );
|
|
|
|
|
|
for ( var i = - size; i <= size; i += step ) {
|
|
|
|
|
@@ -79,43 +81,60 @@
|
|
|
}
|
|
|
|
|
|
var line = new THREE.Line( geometry, material, THREE.LinePieces );
|
|
|
+ line.position.y = -0.46;
|
|
|
scene.add( line );
|
|
|
|
|
|
+ // Ground
|
|
|
+
|
|
|
+ var plane = new THREE.Mesh( new THREE.PlaneGeometry( 40, 40 ), new THREE.MeshPhongMaterial( { ambient: 0x999999, color: 0x999999, specular: 0x101010, perPixel: true } ) );
|
|
|
+ plane.rotation.x = -Math.PI/2;
|
|
|
+ plane.position.y = -0.5;
|
|
|
+ scene.add( plane );
|
|
|
+
|
|
|
+ plane.receiveShadow = true;
|
|
|
+
|
|
|
+ // Object
|
|
|
+
|
|
|
var loader = new THREE.STLLoader();
|
|
|
loader.addEventListener( 'load', function ( event ) {
|
|
|
|
|
|
var geometry = event.content;
|
|
|
- var material = new THREE.MeshPhongMaterial( { ambient: 0xff0000, color: 0xff0000, specular: 0xffffff } );
|
|
|
+ var material = new THREE.MeshPhongMaterial( { ambient: 0xff5533, color: 0xff5533, specular: 0x111111, shininess: 200, perPixel: true } );
|
|
|
+ var mesh = new THREE.Mesh( geometry, material );
|
|
|
+
|
|
|
+ mesh.castShadow = true;
|
|
|
+ mesh.receiveShadow = true;
|
|
|
|
|
|
- scene.add( new THREE.Mesh( geometry, material ) );
|
|
|
+ scene.add( mesh );
|
|
|
|
|
|
} );
|
|
|
loader.load( './models/stl/slotted_disk.stl' );
|
|
|
|
|
|
-
|
|
|
- particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
|
|
|
- scene.add( particleLight );
|
|
|
-
|
|
|
// Lights
|
|
|
|
|
|
- scene.add( new THREE.AmbientLight( 0xcccccc ) );
|
|
|
+ scene.add( new THREE.AmbientLight( 0x777777 ) );
|
|
|
|
|
|
- var directionalLight = new THREE.DirectionalLight( 0xeeeeee );
|
|
|
- directionalLight.position.x = Math.random() - 0.5;
|
|
|
- directionalLight.position.y = Math.random() - 0.5;
|
|
|
- directionalLight.position.z = Math.random() - 0.5;
|
|
|
- directionalLight.position.normalize();
|
|
|
- scene.add( directionalLight );
|
|
|
+ addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
|
|
|
+ addShadowedLight( 0.5, 1, -1, 0xffaa00, 1 );
|
|
|
|
|
|
- pointLight = new THREE.PointLight( 0xffffff, 4 );
|
|
|
- pointLight.position = particleLight.position;
|
|
|
- scene.add( pointLight );
|
|
|
+ // renderer
|
|
|
|
|
|
- renderer = new THREE.WebGLRenderer();
|
|
|
+ renderer = new THREE.WebGLRenderer( { antialias: true, clearColor: 0x111111, clearAlpha: 1, alpha: false } );
|
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
|
|
+ renderer.setClearColor( scene.fog.color, 1 );
|
|
|
+
|
|
|
+ renderer.gammaInput = true;
|
|
|
+ renderer.gammaOutput = true;
|
|
|
+ renderer.physicallyBasedShading = true;
|
|
|
+
|
|
|
+ renderer.shadowMapEnabled = true;
|
|
|
+ renderer.shadowMapCullFrontFaces = false;
|
|
|
+
|
|
|
container.appendChild( renderer.domElement );
|
|
|
|
|
|
+ // stats
|
|
|
+
|
|
|
stats = new Stats();
|
|
|
stats.domElement.style.position = 'absolute';
|
|
|
stats.domElement.style.top = '0px';
|
|
@@ -127,6 +146,32 @@
|
|
|
|
|
|
}
|
|
|
|
|
|
+ function addShadowedLight( x, y, z, color, intensity ) {
|
|
|
+
|
|
|
+ var directionalLight = new THREE.DirectionalLight( color, intensity );
|
|
|
+ directionalLight.position.set( x, y, z )
|
|
|
+ scene.add( directionalLight );
|
|
|
+
|
|
|
+ directionalLight.castShadow = true;
|
|
|
+ //directionalLight.shadowCameraVisible = true;
|
|
|
+
|
|
|
+ var d = 1;
|
|
|
+ directionalLight.shadowCameraLeft = -d;
|
|
|
+ directionalLight.shadowCameraRight = d;
|
|
|
+ directionalLight.shadowCameraTop = d;
|
|
|
+ directionalLight.shadowCameraBottom = -d;
|
|
|
+
|
|
|
+ directionalLight.shadowCameraNear = 1;
|
|
|
+ directionalLight.shadowCameraFar = 4;
|
|
|
+
|
|
|
+ directionalLight.shadowMapWidth = 2048;
|
|
|
+ directionalLight.shadowMapHeight = 2048;
|
|
|
+
|
|
|
+ directionalLight.shadowBias = -0.005;
|
|
|
+ directionalLight.shadowDarkness = 0.15;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
function onWindowResize() {
|
|
|
|
|
|
camera.aspect = window.innerWidth / window.innerHeight;
|
|
@@ -151,16 +196,11 @@
|
|
|
|
|
|
var timer = Date.now() * 0.0005;
|
|
|
|
|
|
- camera.position.x = Math.cos( timer ) * 3;
|
|
|
- camera.position.y = 1;
|
|
|
- camera.position.z = Math.sin( timer ) * 3;
|
|
|
+ camera.position.x = Math.cos( timer ) * 5;
|
|
|
+ camera.position.z = Math.sin( timer ) * 5;
|
|
|
|
|
|
camera.lookAt( scene.position );
|
|
|
|
|
|
- particleLight.position.x = Math.sin( timer * 4 ) * 3009;
|
|
|
- particleLight.position.y = Math.cos( timer * 5 ) * 4000;
|
|
|
- particleLight.position.z = Math.cos( timer * 4 ) * 3009;
|
|
|
-
|
|
|
renderer.render( scene, camera );
|
|
|
|
|
|
}
|