二、 燈光模塊的使用方法。
2.1 定向光
代碼清單如下:
final DirectionalLight directionalLight = new DirectionalLight();
directionalLight.setLookAt(MathC.Vector.ZERO);
directionalLight.setPower(1.5f);
directionalLight.enableLookAt();
addLight(directionalLight);
Material sphereMaterial = new Material();
SpecularMethod.Phong phongMethod = new SpecularMethod.Phong();
phongMethod.setShininess(180);
sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
sphereMaterial.setSpecularMethod(phongMethod);
sphereMaterial.enableLighting(true);
- 首先創(chuàng)建一個定向光對象,這個是不會陌生的,這里不在贅述。
- 設(shè)置定向光源的方向,setLookAt(MathC.Vector.ZERO),可以直接設(shè)置規(guī)定好的幾個方向,可以點擊這個枚舉類中查看一下,有z軸負(fù)方向跟x軸正方向等,也可以設(shè)置一個方向向量,setLookAt(float x, float y, float z)。
@param x y z 這個接口接受一個方向向量。 - setPower(1.5f)設(shè)置光源的半徑,也就是光源的半徑是多少。
@param radio 該參數(shù)接受一個float類型的參數(shù)來規(guī)定光源的半徑是多少。 - addLight(directionalLight)將光源對象加入到渲染隊列中進行渲染。
- 創(chuàng)建一個材質(zhì)對象,準(zhǔn)備將光源的渲染對象插入到材質(zhì)對象的著色器中,進行光源的渲染。
- 創(chuàng)建一個phone光源模型,這里不在贅述,知道需要這么創(chuàng)建就可以了。
- 設(shè)定模型對象表面的粗糙度,這參數(shù)跟鏡面光的強度有關(guān)系,如果粗糙度越低,那么鏡面光越強,鏡面光照也被稱為高光。
@param shininess 表示模型表面粗糙度的參數(shù)。 - 創(chuàng)建一個Lambert光照模型,這樣光照的強度會隨著距離的遠(yuǎn)近來調(diào)節(jié),這是lambert光照模型(知道步驟即可)并將其設(shè)置到模型的材質(zhì)中。
- 將phone光照模型設(shè)置到模型的材質(zhì)中,并且打開光源的開光,將光源渲染加入到場景渲染中。
說明:
將創(chuàng)建好的材質(zhì)設(shè)置到模型材質(zhì)中,這樣就可以給模型添加燈光了。這一點可以直接參考前面是如果給ui設(shè)置材質(zhì)函數(shù)的設(shè)置,這里不在贅述這些步驟了。
2.2 定向光
代碼清單如下:
PointLight pointLight = new PointLight();
pointLight.translateAbs(0,0,0);
pointLight.setPower(1.5f);
pointLight.setLookAt(0, 0, 0);
addLight(pointLight);
Material sphereMaterial = new Material();
sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
SpecularMethod.Phong phongMethod = new SpecularMethod.Phong();
phongMethod.setShininess(180);
sphereMaterial.setSpecularMethod(phongMethod);
sphereMaterial.setAmbientIntensity(0, 0, 0);
sphereMaterial.enableLighting(true);
注意:接口在上面大部分都介紹過,這邊都不在贅述一些重復(fù)的接口了,下面就介紹一些上面沒有介紹過的接口。
- translateAbs(0,0,0)設(shè)置光源的位置,因為光照對象及ANode的子類,當(dāng)然你擁有模型的某些屬性。
@param x y z 設(shè)置光源在3D場景中的位置。 - setLookAt(0, 0, 0)設(shè)置光源的觀測點,也就是光源的直照的點。
- setAmbientIntensity(0, 0, 0)設(shè)置環(huán)境光的強度,這個值是環(huán)境光的設(shè)置接口參數(shù)。
@param r g b 三個顏色通道的顏色,這邊直接設(shè)置0-1之間就可以。
說明:這里介紹了接口的使用,可以直接查看接口中的方法,這樣就可以更好的使用。
2.2 聚光燈
final SpotLight spotLight = new SpotLight();
spotLight.setPower(1.5f);
spotLight.enableLookAt();
spotLight.setPosition(0, 4.0, 0.0);
spotLight.setLookAt(0, 0, 0);
addLight(spotLight);
Material sphereMaterial = new Material();
sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
SpecularMethod.Phong phongMethod = new SpecularMethod.Phong();
phongMethod.setShininess(180);
sphereMaterial.setSpecularMethod(phongMethod);
sphereMaterial.setAmbientIntensity(0, 0, 0);
sphereMaterial.enableLighting(true);
注意:大部分的接口都介紹過了,這里不在贅述了,可以查看上面的接口設(shè)置。
- setCameraW(float[] cameraW)這個接口可以將攝像機與定向光與聚光燈綁定到一起。
@param cameraW 這個參數(shù)是攝像機的w向量值。