要在螢幕上顯示 3D 圖形,在 Three.js 中需要以下的基本元素:
不過在 Three.js 中有一些材質並不需要考慮光源即可被渲染在畫面上,例如 MeshBasicMaterial
、 MeshNormalMaterial
,能夠與光源互動的材質一般會用 MeshLambertMaterial
或 MeshPhongMaterial
。
在 Three.js 中提供了包括環境光(AmbientLight)、點光源(PointLight)、 聚光燈 (SpotLight)、方向光(DirectionalLight)、半球光(HemisphereLight)等多種光源。
The [DirectionalLight](<https://threejs.org/docs/#api/lights/DirectionalLight>)
is designed to mimic a distant source of light such as the sun. Light rays from a DirectionalLight
don’t fade with distance. All objects in the scene will be illuminated equally brightly no matter where they are placed - even behind the light
The light rays of a DirectionalLight
are parallel and shine from a position and towards a target
. By default, the target is placed at the center of our scene (the point (0,0,0)), so as we move the light around it will always shine towards the center.
Three.js 中有不只一種渲染器,但一般而言都是使用 WebGLRenderer
這個渲染器,它能支援較多功能,像是材質、陰影等等,在後面的程式碼中會看到常透過渲染器來設定預設背景顏色、場景尺寸、陰影效果