javascript - Three.js SpotLight class on an animating JSON mesh -


i'm trying grasp on three.js light classes. i've tinkered around three.js example in attempt 3d mesh model screen , add basic rotate animation it.

how achieve static light source on moving object? light reflected object seems follow path along rotation.

here's code: http://codepen.io/jagomez8/pen/bzbyez

i've switched out various other light classes think issue lies in meshphongmaterial. when apply flatshading material renders desired result except flat gets.the relevant code on line 105.

if ( ! detector.webgl ) detector.addgetwebglmessage();          var renderer = new three.webglrenderer();          var camera = new three.perspectivecamera( 35, window.innerwidth / window.innerheight, 1, 1000 );          var controls = new three.orbitcontrols( camera, renderer.domelement );          var scene = new three.scene();         var egg;          var matfloor = new three.meshphongmaterial();         var matbox = new three.meshphongmaterial( { color: 0x4080ff } );          var geofloor = new three.boxgeometry( 2000, 1, 2000 );         var geobox = new three.boxgeometry( 3, 1, 2 );          var mshfloor = new three.mesh( geofloor, matfloor );         var mshbox = new three.mesh( geobox, matbox );          var ambient = new three.ambientlight( 0xffffff, 0.1 );          var spotlight = new three.spotlight( 0xffffff, 1 );         var lighthelper;          var gui, guielements, param = { color: '0xffffff' };          function init() {              renderer.shadowmap.enabled = true;             renderer.shadowmap.type = three.pcfsoftshadowmap;              renderer.gammainput = true;             renderer.gammaoutput = true;              camera.position.set( 65, 8, - 10 );              spotlight.position.set( 15, 40, 35 );             spotlight.castshadow = true;             spotlight.angle = math.pi / 4;             spotlight.penumbra = 0.05;             spotlight.decay = 2;             spotlight.distance = 200;             spotlight.shadow.mapsize.width = 1024;             spotlight.shadow.mapsize.height = 1024;             spotlight.shadow.camera.near = 1;             spotlight.shadow.camera.far = 200;              lighthelper = new three.spotlighthelper( spotlight );              matfloor.color.set( 0x808080 );              mshfloor.receiveshadow = true;             mshfloor.position.set( 0, - 0.05, 0 );              mshbox.castshadow = true;             mshbox.position.set( 40, 1.8, 0 );              scene.add( camera );             scene.add( mshfloor );             scene.add( mshbox );             scene.add( ambient );             scene.add( spotlight );             scene.add( new three.axishelper( 10 ) );             scene.add( lighthelper );              document.body.appendchild( renderer.domelement );             renderer.setsize( window.innerwidth, window.innerheight );              controls.addeventlistener( 'change', render );             controls.mindistance = 20;             controls.maxdistance = 500;             controls.maxpolarangle = math.pi / 2;             controls.enablepan = false;             controls.target.copy( mshbox.position );             controls.update();              window.addeventlistener( 'resize', onresize, false );               var manager = new three.loadingmanager();             manager.onprogress = function( item, loaded, total ) {                  console.log( item, loaded, total );              };              var onprogress = function( xhr ) {                  if ( xhr.lengthcomputable ) {                      var percentcomplete = xhr.loaded / xhr.total * 100;                     console.log( math.round( percentcomplete, 2 ) + '% downloaded' );                  }              };              var onerror = function( xhr ) {             };              var loader = new three.jsonloader( manager );             loader.load( 'http://alexgdm.com/egg.json', function( geometry, material ) {                 ///****3d mesh***///                 egg = new three.mesh( geometry, new three.meshphongmaterial( { ambient: 0x050505, color: 0x0033ff, specular: 0x555555, shininess: 30/*, shading: three.flatshading */}  )  );                 egg.position.set(0, 1, 1);                   scene.add( egg );                 animate();               }, onprogress, onerror );          }          function onresize() {              renderer.setsize( window.innerwidth, window.innerheight );             camera.aspect = ( window.innerwidth / window.innerheight );             camera.updateprojectionmatrix();          }          function render() {              lighthelper.update(); // required             renderer.render( scene, camera );          }          function animate() {             requestanimationframe( animate );             egg.rotation.y += 0.01;              renderer.render( scene, camera );         }          function cleargui() {              if ( gui ) gui.destroy();              gui = new dat.gui();              gui.open();          }          function buildgui() {              cleargui();              addgui( 'light color', spotlight.color.gethex(), function( val ) {                  spotlight.color.sethex( val );                 render();              }, true );              addgui( 'intensity', spotlight.intensity, function( val ) {                  spotlight.intensity = val;                 render();              }, false, 0, 2 );              addgui( 'distance', spotlight.distance, function( val ) {                  spotlight.distance = val;                 render();              }, false, 0, 200 );              addgui( 'angle', spotlight.angle, function( val ) {                  spotlight.angle = val;                 render();              }, false, 0, math.pi / 3 );              addgui( 'penumbra', spotlight.penumbra, function( val ) {                  spotlight.penumbra = val;                 render();              }, false, 0, 1 );              addgui( 'decay', spotlight.decay, function( val ) {                  spotlight.decay = val;                 render();              }, false, 1, 2 );          }          function addgui( name, value, callback, iscolor, min, max ) {              var node;             param[ name ] = value;              if ( iscolor ) {                  node = gui.addcolor( param, name ).onchange( function() {                      callback( param[ name ] );                  } );              } else if ( typeof value == 'object' ) {                  node = gui.add( param, name, value ).onchange( function() {                      callback( param[ name ] );                  } );              } else {                  node = gui.add( param, name, min, max ).onchange( function() {                      callback( param[ name ] );                  } );              }              return node;          }          init();          buildgui();          render(); 

your egg-shaped model invalid. in particular, vertex normals not correct.

it appears vertex normals have y-component , z-component switched. try remapping them, , setting .y = -.z, , .z = .y.

or, call

geometry.computevertexnormals(); 

that should result in reasonable vertex normals.

three.vertexnormalshelper can used view normals, if want.

three.js r.77


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -