Three.js通過RectAreaLight創(chuàng)造平面區(qū)域光

一渺尘、RectAreaLight的特性:
1、不支持陰影,無法通過設(shè)置castShadow來產(chǎn)生陰影昌抠。
2、RectAreaLight所散發(fā)的光源只能影響MeshStandardMaterial和MeshPhysicalMaterial兩種材質(zhì)鲁僚,其他材質(zhì)不受該光照的影響炊苫。
二、效果圖:
三冰沙、引入步驟:
1侨艾、引入RectAreaLight:
import {RectAreaLight} from 'three'
2、創(chuàng)造RectAreaLight并加入場(chǎng)景
 this.areaLight = new RectAreaLight(0xffffff, 1, 25, 25);
 this.areaLight.position.set(0, 0, 8);
 this.scene.add(this.areaLight);
3拓挥、創(chuàng)建兩個(gè)矩形平面作為平面區(qū)域光的載體
  const rectLightMesh = new Mesh(new PlaneGeometry(10, 10), new MeshBasicMaterial({ color:0xffffff, side:BackSide }));
  this.areaLight.add(rectLightMesh);

 const rectLightMeshBack = new Mesh(new PlaneGeometry(10, 10), new MeshBasicMaterial({ color:'#000000' }));
 rectLightMesh.add(rectLightMeshBack);

創(chuàng)建兩個(gè)矩形平面的原因是為了保證平面光的光源的指向是同一個(gè)方向唠梨。而且需要注意的是必須設(shè)置side = side:BackSide,這樣做是為了保證方面的平面緩沖幾何體能夠上色侥啤。

四当叭、完整源代碼:
import { OrbitControls } from '../../node_modules/three/examples/jsm/controls/OrbitControls';//控制器
import Stats from 'stats.js';
import * as dat from 'dat.gui';
import { Mesh, PerspectiveCamera, Scene, WebGLRenderer, AmbientLight, MeshBasicMaterial, PlaneGeometry, BoxGeometry, MeshPhongMaterial,  Color,  RectAreaLight, PlaneBufferGeometry, BackSide, MeshStandardMaterial,  } from 'three';
export class LightProbeDemo {
    private camera: PerspectiveCamera;
    private scene: Scene;
    private renderer: WebGLRenderer;
    private stats: Stats;
    private dat: any;
    private params!: any;
    private planeGeometry!: PlaneBufferGeometry;
    private envlight!: AmbientLight;
    private areaLight!: RectAreaLight;
    constructor() {
        // 創(chuàng)建場(chǎng)景
        this.scene = new Scene();
        this.stats = new Stats();
        // 操作器
        this.dat = new dat.GUI();
        // 創(chuàng)建渲染器
        this.renderer = new WebGLRenderer({ antialias: true });
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        this.renderer.setPixelRatio(window.devicePixelRatio); //設(shè)備像素比 可以清晰物體
        this.renderer.setClearColor(0xEEEEEE, 1); //設(shè)置背景顏色
        this.renderer.shadowMap.enabled = true;
        document.body.appendChild(this.renderer.domElement);



        // 創(chuàng)建相機(jī)
        this.camera = new PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 10000);
        this.camera.position.set(10, 40, 40);
        this.camera.lookAt(this.scene.position);
        // 平面
        this.planeGeometry = new PlaneBufferGeometry(1600, 1400);
        // console.log(this.planeGeometry);
        
        const planeMaterial = new MeshPhongMaterial({
            specular: 0xffffff,
            color: 0xeeffff,
            shininess: 100,
            // side:DoubleSide
        });
        const planeMesh = new Mesh(this.planeGeometry, planeMaterial);
        planeMesh.rotation.x = -0.5 * Math.PI;
        planeMesh.position.y = -2;
        this.scene.add(planeMesh);

     
        const box = new BoxGeometry(5, 5, 5);
        const material = new MeshStandardMaterial({ color: '#A00000' });
        const boxmesh = new Mesh(box, material);
        boxmesh.rotation.y = 0.1*Math.PI;
        this.scene.add(boxmesh);

        // AreaLight
        this.areaLight = new RectAreaLight(0xffffff, 1, 25, 25);
        this.areaLight.position.set(0, 0, 10);
        this.scene.add(this.areaLight);
        // AreaLight載體
        const rectLightMesh = new Mesh(new PlaneGeometry(10, 10), new MeshBasicMaterial({ color:0xffffff, side:BackSide }));
        this.areaLight.add(rectLightMesh);

        const rectLightMeshBack = new Mesh(new PlaneGeometry(10, 10), new MeshBasicMaterial({ color:'#000000' }));
        rectLightMesh.add(rectLightMeshBack);

        // 環(huán)境光
        this.envlight = new AmbientLight(0xffffff, 0.7);

        this.scene.add(this.envlight);


        this.params = {
            envIntensity: 0.7,
            ambientLight: this.envlight.color.getStyle(),
            visible: false,
            
        };
        this.dat.add(this.params, 'envIntensity', 0.1, 1.0).onChange(e => {
            this.envlight.intensity = e;

        }).name('環(huán)境光強(qiáng)度');
        this.dat.addColor(this.params, 'ambientLight').onChange(e => {
            console.log(e);
            this.envlight.color = new Color(e);
        }).name('環(huán)境光顏色');

        this.dat.add(this.params, 'visible').onChange(e => {
            this.envlight.visible = !e;
        }).name('是否顯示環(huán)境光');


        // fps顯示器
        this.stats.showPanel(0);
        document.body.appendChild(this.stats.dom);
        // 相機(jī)控制器
        new OrbitControls(this.camera, this.renderer.domElement);
        window.addEventListener('resize', () => this.onWindowResize());
        this.render();
    }


    // 窗口變化
    private onWindowResize() {
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        this.camera.aspect = window.innerWidth / window.innerHeight;
        this.camera.updateProjectionMatrix(); //相機(jī)屬性發(fā)生變化更新投影矩陣
    }
    // 渲染
    private render() {
        this.stats.begin();

        window.requestAnimationFrame(() => this.render());
        this.renderer.render(this.scene, this.camera);
        this.stats.end();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末茬故,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蚁鳖,更是在濱河造成了極大的恐慌磺芭,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件醉箕,死亡現(xiàn)場(chǎng)離奇詭異钾腺,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)琅攘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門垮庐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人坞琴,你說我怎么就攤上這事哨查。” “怎么了剧辐?”我有些...
    開封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵寒亥,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我荧关,道長(zhǎng)溉奕,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任忍啤,我火速辦了婚禮加勤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘同波。我一直安慰自己鳄梅,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開白布未檩。 她就那樣靜靜地躺著戴尸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪冤狡。 梳的紋絲不亂的頭發(fā)上孙蒙,一...
    開封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音悲雳,去河邊找鬼挎峦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛合瓢,可吹牛的內(nèi)容都是我干的浑测。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼迁央!你這毒婦竟也來了掷匠?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤岖圈,失蹤者是張志新(化名)和其女友劉穎讹语,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蜂科,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡顽决,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了导匣。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片才菠。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖贡定,靈堂內(nèi)的尸體忽然破棺而出赋访,到底是詐尸還是另有隱情,我是刑警寧澤缓待,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布蚓耽,位于F島的核電站,受9級(jí)特大地震影響旋炒,放射性物質(zhì)發(fā)生泄漏步悠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一瘫镇、第九天 我趴在偏房一處隱蔽的房頂上張望月幌。 院中可真熱鬧生真,春花似錦潦匈、人聲如沸咐柜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至睁壁,卻和暖如春背苦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背潘明。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工行剂, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人钳降。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓厚宰,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子铲觉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容