three.js 源碼注釋(四十四)Light/DirectionalLight.js
轉載請保留此句:商域無疆 - 本部落格專註於 敏捷開發及移動和物聯裝置研究:資料視覺效果、GOLANG、Html5、WEBGL、THREE.JS,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。
俺也是剛開始學,好多地兒肯定不對還請見諒.
以下代碼是THREE.JS 源碼檔案中Light/DirectionalLight.js檔案的注釋.
/** * @author mrdoob / http://mrdoob.com/ * @author alteredq / http://alteredqualia.com/ */ /*///DirectionalLight方法根據設定燈光的顏屬性color, 強度屬性intensity建立平行光光源.///DirectionalLight對象的功能函數採用定義構造的函數原型對象來實現./// NOTE: SpotLight類型燈光實現了陰影,但是需要在情境中使用MeshLambertMaterial或者MeshPhongMaterial/// 下面一些注釋直接摘抄自:http://www.cnblogs.com/yiyezhai/archive/2012/12/24/2829991.html/// Example:/// var light = new THREE.SpotLight(0xff0000,1,100,Math.PI /2,5);//建立燈光對象/// light.position.set(50,50,30);//設定位置///light.castShadow = true;//開啟陰影///light.shadowMapWidth = 1024; //陰影貼圖寬度設定為1024像素///light.shadowMapHeight = 1024; //陰影貼圖高度設定為1024像素///light.shadowCameraNear = 500;//陰影的平截頭體地區near屬性///light.shadowCameraFar = 4000;//陰影的平截頭體地區far屬性///light.shadowCameraFov = 30;//陰影的平截頭體地區fov屬性///scene.add(lignt);//加入情境*////DirectionalLight///燈光的顏色屬性///燈光的強度,預設是1///返回DirectionalLight,平行光光源.THREE.DirectionalLight = function ( color, intensity ) {THREE.Light.call( this, color );//調用Light對象的call方法,將原本屬於Light的方法交給當前對象DirectionalLight來使用.this.position.set( 0, 1, 0 );//燈光的位置屬性初始化為,0,1,0this.target = new THREE.Object3D();//建立一個目標點對象,目標點對象是一個Object3D對象.this.intensity = ( intensity !== undefined ) ? intensity : 1;//燈光的顏色屬性,如果不指定,初始化為1.(光線的密度,預設為1。因為RGB的三個值均在0~255之間,不能反映出光照的強度變化,光照越強,物體表面就更明亮。)this.castShadow = false;//布爾值,預設為false,如果設定為true,對於所有表面都會逐像元地計算其在光照方向上是否被遮擋,這會消耗大量的計算。this.onlyShadow = false;//布爾值,控制是否只產生陰影而不“照亮”物體,預設為false。這種模式也許有什麼特殊應用吧。//this.shadowCameraNear = 50;//shadowCameraNear屬性,正交投影立方體近端,定義一個範圍(正交投影立方體),不計算在範圍之外的物體的陰影,near預設是50this.shadowCameraFar = 5000;//shadowCameraFar屬性,正交投影立方體遠端,定義一個範圍(正交投影立方體),不計算在範圍之外的物體的陰影,far預設是5000this.shadowCameraLeft = - 500;//shadowCameraLeft屬性,正交投影立方體左端,定義一個範圍(正交投影立方體),不計算在範圍之外的物體的陰影,left預設是500this.shadowCameraRight = 500;//shadowCameraRight屬性,正交投影立方體右端,定義一個範圍(正交投影立方體),不計算在範圍之外的物體的陰影,right預設是500this.shadowCameraTop = 500;//shadowCameraTop屬性,正交投影立方體上端,定義一個範圍(正交投影立方體),不計算在範圍之外的物體的陰影,top預設是500this.shadowCameraBottom = - 500;//shadowCameraBottom屬性,正交投影立方體下端,定義一個範圍(正交投影立方體),不計算在範圍之外的物體的陰影,Bottom預設是500this.shadowCameraVisible = false;//shadowCameraVisible設定為true,會在情境中顯示燈光的架構,方便調試this.shadowBias = 0;//陰影貼圖的位移,this.shadowDarkness = 0.5;//陰影對物體亮度的影響,預設是0.5this.shadowMapWidth = 512; //陰影貼圖寬度,單位像素,預設512this.shadowMapHeight = 512; //陰影貼圖高度,單位像素,預設512/*對於平行光,WebGL可以使用級聯陰影貼圖(或成為平行分割陰影貼圖)有很好的陰影品質,特別是遠距離觀看。級聯陰影通過分割可視地區逐步部分變大,並使用相同的大小,在每個陰影貼圖。結果是物體接近觀看者將比更遠的物體獲得更多的陰影貼映像素。對於平行光陰影的品質和效能,陰影的距離是非常重要的。就像陰影級聯數,陰影距離可以在品質設定中設定,很容易降低陰影範圍,以減少硬體效能消耗。在陰影距離結束處,陰影將淡出,更遠的物體將沒有陰影。大多數情況下在情境中更遠地方的陰影不會引人注目!*/this.shadowCascade = false;//陰影級聯this.shadowCascadeOffset = new THREE.Vector3( 0, 0, - 1000 );//陰影級聯位移距離this.shadowCascadeCount = 2;//當使用2個陰影級聯時,整個陰影距離內,預設被分為兩塊,靠近觀察者較小的塊和遠處較大的塊this.shadowCascadeBias = [ 0, 0, 0 ];//陰影級聯位移數組this.shadowCascadeWidth = [ 512, 512, 512 ];//陰影級聯寬度數組this.shadowCascadeHeight = [ 512, 512, 512 ];//陰影級聯高度數組this.shadowCascadeNearZ = [ - 1.000, 0.990, 0.998 ];//陰影級聯近處this.shadowCascadeFarZ = [ 0.990, 0.998, 1.000 ];//陰影級聯遠處this.shadowCascadeArray = [];//陰影級聯數組//TODO: 下面這些完全沒弄明白,以後補上詳細的注釋this.shadowMap = null;//指定陰影貼圖,WebGLRenderTarget對象,這裡好像很複雜,完全沒弄明白,以後補上詳細的注釋.this.shadowMapSize = null;//陰影圖的大小,注意,這裡應符合OpenGL中對紋理的要求(2的n次方+2n) this.shadowCamera = null;//陰影貼圖相機,THREE.PerspectiveCamera對象,this.shadowMatrix = null;//陰影貼圖矩陣};/******************************************************************************************下面是DirectionalLight對象提供的功能函數定義,一部分通過prototype繼承自Light方法***************************************************************************************/THREE.DirectionalLight.prototype = Object.create( THREE.Light.prototype );//DirectionalLight對象從THREE.Light的原型繼承所有屬性方法/*clone方法///clone方法複製DirectionalLight對象*////clone///返回複製的DirectionalLight對象THREE.DirectionalLight.prototype.clone = function () {var light = new THREE.DirectionalLight();THREE.Light.prototype.clone.call( this, light );//調用THREE.Light.clone方法,複製平行光對象//複製當前燈光對象的屬性light.target = this.target.clone();light.intensity = this.intensity;light.castShadow = this.castShadow;light.onlyShadow = this.onlyShadow;//light.shadowCameraNear = this.shadowCameraNear;light.shadowCameraFar = this.shadowCameraFar;light.shadowCameraLeft = this.shadowCameraLeft;light.shadowCameraRight = this.shadowCameraRight;light.shadowCameraTop = this.shadowCameraTop;light.shadowCameraBottom = this.shadowCameraBottom;light.shadowCameraVisible = this.shadowCameraVisible;light.shadowBias = this.shadowBias;light.shadowDarkness = this.shadowDarkness;light.shadowMapWidth = this.shadowMapWidth;light.shadowMapHeight = this.shadowMapHeight;//light.shadowCascade = this.shadowCascade;light.shadowCascadeOffset.copy( this.shadowCascadeOffset );light.shadowCascadeCount = this.shadowCascadeCount;light.shadowCascadeBias = this.shadowCascadeBias.slice( 0 );light.shadowCascadeWidth = this.shadowCascadeWidth.slice( 0 );light.shadowCascadeHeight = this.shadowCascadeHeight.slice( 0 );light.shadowCascadeNearZ = this.shadowCascadeNearZ.slice( 0 );light.shadowCascadeFarZ = this.shadowCascadeFarZ.slice( 0 );return light;//返回複製的平行光的對象};上一篇http://www.bkjia.com/kf/201412/359015.html