Add lighting effects to Threejs scenes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.