The following code is used to add lighting effects:
//enable the renderer to support shadow mapsthree. Render.shadowmapenabled=true;//Set Light Shadow properties//set light to create a shadowthree. Light.castshadow=true;//if it is a debug state, you can also display the light's Debugging Help boxthree. Light.shadowcameravisible=true;//set Shadow Map qualitythree. Light.shadowmapwidth= Three. Light.shadowmapheight = 1024*4;//Setting model Properties//set the object to create a shadowthree. Mesh.castshadow=true;//sets the object to receive the shadow, that is, the shadow of the other object can be displayed on the objectthree. Mesh.receiveshadow=true;
The complete test code is as follows:
<! DOCTYPE html>var_scene,_camera,_control,_render,_ambient,_spotlight; Init (); Animate (); functionInit () {_scene=Newthree. Scene (); _camera=NewThree. Perspectivecamera (45,window.innerwidth/window.innerheight,0.1,2000); _CAMERA.POSITION.Z = 50; _render=Newthree. Webglrenderer (); //set the renderer size_render.setsize (window.innerwidth,window.innerheight); //Set renderer Shadows available_render.shadowmapenabled =true; Document.body.appendChild (_render.domelement); //scene controller for controlling cameras in the scene for easy viewing of scenes_control =Newthree. Orbitcontrols (_camera,_render.domelement); //Add ambient light_ambient =NewThree. Ambientlight (0XFFFFFF); _scene.add (_ambient); //Light Properties_spotlight =NewThree. SpotLight (0XFFFFFF); _spotlight.castshadow=true; _spotlight.shadowcameravisible=true; _spotlight.position.set (100,100,100); //set Shadow Map precision_spotlight.shadowmapwidth = _spotlight.shadowmapheight = 1024*4; _scene.add (_spotlight); //Load ModelLoadmodel (); } functionAnimate () {requestanimationframe (animate); Render (); } functionRender () {//Update camera Angle_control.update (); _render.render (_scene,_camera); } functionLoadmodel () {//call Objmtlloader to load the model varLoader =Newthree. Objmtlloader (); Loader.load (' Model/tc_tm/tc.obj ', ' MODEL/TC_TM/TC.MTL ', function(obj) {//Traverse: Callback, the model and all child models execute the function //equivalent to a children array traversing objObj.traverse (function(child) {if(Childinstanceofthree. Mesh) {//set the model to generate shadows and receive shadowsChild.castshadow =true; Child.receiveshadow=true; } }); _scene.add (obj); }); }</script></body>The effect is as follows:
Far View:
Closer Look:
Add lighting effects to Threejs scenes