電子工程系應 用 電 子 組電 腦 遊 戲 設 計 組
Unity遊戲程式設計(09) 3D物件與光源設定
吳錫修
March 7, 2017
shap
e th
e fu
ture
選單命令GameObject>3D Object內可找到內建3D物件
Cube 立方體
Sphere 球體
Capsule 膠囊
Cylinder 圓柱體
Plane 平面
Quad 四邊形
內建3D物件
2 Wu, ShyiShiou Dept. of E.E., NKUT
Plane
Quad
Cylinder
Capsule
Cube Sphere
shap
e th
e fu
ture
選單命令Assets> Import Package> Effects
勾選LightCookies
勾選LightFlares
滙入光源素材
3 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
由Transform屬性調整物件位置 (Position)、角度 (Rotation)、及大小 (Scale)
場景3D座標系統
4 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
光源決定遊戲場景明暗與氛圍,使場景中的物體呈現出正確的顏色,及產生陰影效果
選單命令GameObject>Light內可找到光源物件
Directional Light 方向光
Point Light 點光源
Spotlight 聚光燈
Area Light 區域光
場景預設會加入方向光
設置光源 1/5
5 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
Directional Light (方向光)
⼀個在無限遠處的光源,平行的照射在場景中,會影響場景中的所有物件,類似自然界中日光的照明效果
最不耗費圖形處理器資源的光源
因為是無限遠處的光源,Position屬性不會影響照射效果
設置光源 2/5
6 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
Point Light (點光源)
⼀個向四面八方發出光線,影響其範圍內的所有物件,類似燈泡的照明效果
點光源較耗用圖形處理器資源
設置光源 3/5
7 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
Spotlight (聚光燈)
朝某⼀方向在錐形範圍照射光線,位於錐形範圍內的物件會受到光線照射,類似舞台上的探照燈
聚光燈較耗用運算資源
設置光源 4/5
8 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
Area Light (區域光)
Area Light,俗稱面燈。由矩形單⼀面射出光線
不能產生即時光照的效果,僅能用於光影貼圖烘焙 (Light baking)
設置光源 5/5
9 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
Type 選擇光源類型
Spot 聚光燈
Directional 方向光
Point 點光源
Area (baked only) 區域光
Baking 光照計算方式
Realtime 即時計算光照效果
Baked 執行光影貼圖時計算光照效果
Mixed 混用
Range 光線照射距離,適用點光源與聚光燈
Spot Angle 聚光燈角度 (1~179),控制光源錐形範圍,適用聚光燈
光源屬性 1/5
10 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
Color 燈光顏色,預設為白光
Intensity 光源強度,聚光燈與點光源預設值為1,方向光預設值為0.5
Cookie 光源加上⼀個有 Alpha 通道的紋理光罩
Cookie Size 縮放Cookie光罩,適用方向光
光源屬性 2/5
11 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
Shadow Type 陰影類型
No Shadow 關閉陰影
Hard Shadow 輪廓清晰的影子
Soft Shadow 柔邊輪廓的影子,較耗運算資源
Strength 陰影強度,1最明顯,0消失
Resolution 陰影解析度
Use Quality Settings 套用Edit>Project Settings>Quality設定
Low Resolution 低解析度
Medium Resolution 中解析度
High Resolution 高解析度
Very High Resolution 超高解析度
光源屬性 3/5
12 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
Bias 微調陰影的位置與陰影定義點,避免產生shadow acne情形。太大的Bias值可能導致GameObject附近的陰影區域被錯誤地點亮,而造成⼀個斷開的陰影,使GameObject看起来好像是離開地面
Normal Bias 微調陰影的位置與陰影定義點
Shadow Near Plane 設定渲染陰影光源距離,光距離GameObjects小於設定值時不會投射陰影
光源屬性 4/5
13 Wu, ShyiShiou Dept. of E.E., NKUT
shadow acne bias值太大
shap
e th
e fu
ture
Draw Halo 開啟光源之光暈效果
Flare 設定光源眩光效果
Render Mode 光源渲染模式
Auto 根據附近燈光的亮度和專案Quality設定,在運行時決定渲染方式
Important 逐像素進行渲染,可以得到最明顯的視覺效果
Not Important 快速渲染
Culling Mask 設定光源影響的圖層
光源屬性 5/5
14 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
新增場景,檔名Lighting
Directional Light
Intensity = 0.3
Main Camera
Position (x, y ,z) = (0, 2, -9)
Rotation (x, y, z) = (5, 0, 0)
選單命令GameObject>3D Object>Plane
Position (x, y, z) = (0, -1, 0)
選單命令GameObject>3D Object>Sphere
Position (x, y, z) = (0, 0, 0)
Scale (x, y, z) = (2, 2, 2)
光源設定練習 1/3
15 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
選單命令GameObject>Light>Point Light
Position (x, y, z) = (4, 3, 0)
Color = red
選單命令GameObject>Light>Spotlight
Position (x, y, z) = (4, 4, 0)
Rotation (x, y, z) = (125, -90, 0)
Spot Angle = 50
Intensity = 2
Shadow Type = Soft Shadows
光源設定練習2/3
16 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
測試結果
光源設定練習3/3
17 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
新增場景,檔名Flare
Directional Light
Intensity = 0.3
Flare = Sun
Main Camera
Position (x, y ,z) = (0, 2, -9)
Rotation (x, y, z) = (5, 0, 0)
選單命令GameObject>3D Object>Plane
Position (x, y, z) = (0, -1, 0)
選單命令GameObject>3D Object>Sphere
Position (x, y, z) = (0, 0, 0)
Scale (x, y, z) = (2, 2, 2)
眩光效果練習 1/4
18 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
選單命令GameObject>Light>Point Light
Position (x, y, z) = (4, 3, 0)
Color = red
Flare = 50mmZoom
選單命令GameObject>Light>Spotlight
Position (x, y, z) = (4, 4, 0)
Rotation (x, y, z) = (125, -90, 0)
Spot Angle = 50
Intensity = 2
Shadow Type = Soft Shadows
Flare = FlareSmall
眩光效果練習 2/4
19 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
Main Camera加入腳本程式
public class CameraControl : MonoBehaviour {public float x;public float y;public float xSpeed = 1;public float ySpeed = 1;private Quaternion rotationEuler;void LateUpdate(){
x += Input.GetAxis ("Mouse X") * xSpeed ;y -= Input.GetAxis ("Mouse Y") * xSpeed ;rotationEuler = Quaternion.Euler (y, x, 0);transform.rotation = rotationEuler;
}}
眩光效果練習 3/4
20 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
測試結果
眩光效果練習 4/4
21 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
光影貼圖是⼀種增加靜態場景光照效果的技術
事先將光影效果經由計算產生貼圖,渲染到場景中的靜態物件,減少遊戲執行階段的燈光照明計算,在遊戲執行時以較少的處理器資源就能使靜態場景看起來更真實
光影貼圖
22 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
場景物件
光影貼圖練習 1/7
23 Wu, ShyiShiou Dept. of E.E., NKUT
Sphere
Plane
Area Light - Left
Area Light - Middle
Area Light - Right
Directional Light
shap
e th
e fu
ture
光影貼圖練習 2/7
24 Wu, ShyiShiou Dept. of E.E., NKUT
光影貼圖前
光影貼圖後
光影貼圖後(關閉方向光源)
shap
e th
e fu
ture
新增場景,檔名Light baking
Directional Light
Rotation (x, y, z) = (50, -30, 0)
Intensity = 0.4
Main Camera
Position (x, y ,z) = (0, 4, -10)
Rotation (x, y, z) = (12, 0, 0)
光影貼圖練習 3/7
25 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
選單命令GameObject>3D Object>Sphere
Position (x, y, z) = (0, 1.5, 0)
Scale (x, y, z) = (3, 3, 3)
選單命令GameObject>3D Object>Plane
Position (x, y, z) = (0, 0, 0)
Scale (x, y, z) = (1, 1, 1)
選單命令GameObject>Light>Area Light
Position (x, y, z) = (5, 2, -3)
Rotation (x, y, z) = (0, 0, 0)
Width=5, Height=5
Color = Red
光影貼圖練習 4/7
26 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
選單命令GameObject>Light>Area Light
Position (x, y, z) = (0, 2, -3)
Rotation (x, y, z) = (0, 0, 0)
Width=5, Height=5
Color = Green
選單命令GameObject>Light>Area Light
Position (x, y, z) = (-5, 2, -3)
Rotation (x, y, z) = (0, 0, 0)
Width=5, Height=5
Color = Blue
光影貼圖練習 5/7
27 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
將要烘焙的物件 (Shpere及Plane) 設定為Static
將要加入光影貼圖運算的光源Baking屬性設定為Baked
光影貼圖練習 6/7
28 Wu, ShyiShiou Dept. of E.E., NKUT
shap
e th
e fu
ture
選單命令Window>Lighting
取消勾選Auto
點擊Build按鈕
光影貼圖練習 7/7
29 Wu, ShyiShiou Dept. of E.E., NKUT