使用 THREE.CubeTextureLoader() 添加环境纹理,以创建立方体贴图
不使用 THREE.CubeTextureLoader()
的时候
源码
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
const scene = new THREE. Scene ( )
const camera = new THREE. PerspectiveCamera ( 100 , window. innerHeight / window. innerHeight, 1 , 50 )
camera. position. set ( 0 , 0 , 10 )
scene. add ( camera)
const axesHelper = new THREE. AxesHelper ( 5 )
scene. add ( axesHelper)
const directionLight = new THREE. DirectionalLight ( '#ffffff' , 1 )
directionLight. castShadow = true
directionLight. position. set ( 0 , 0 , 200 )
scene. add ( directionLight)
const cubeTextureLoader = new THREE. CubeTextureLoader ( )
const envMapTexture = cubeTextureLoader. load ( [
'../public/textures/environmentMaps/0/px.jpg' ,
'../public/textures/environmentMaps/0/nx.jpg' ,
'../public/textures/environmentMaps/0/py.jpg' ,
'../public/textures/environmentMaps/0/ny.jpg' ,
'../public/textures/environmentMaps/0/pz.jpg' ,
'../public/textures/environmentMaps/0/nz.jpg'
] )
scene. environment = envMapTexture
scene. background = envMapTexture
const depthMaterial = new THREE. MeshDepthMaterial ( {
depthPacking : THREE . RGBADepthPacking
} )
const textureLoader = new THREE. TextureLoader ( )
const modelTexture = textureLoader. load ( '../public/assets/model/LeePerrySmith/color.jpg' )
const normalTexture = textureLoader. load ( '../public/assets/model/LeePerrySmith/normal.jpg' )
const material = new THREE. MeshStandardMaterial ( {
map : modelTexture,
normalMap : normalTexture
} )
const gltfLoader = new GLTFLoader ( )
gltfLoader. load ( '../public/assets/model/LeePerrySmith/LeePerrySmith.glb' , gltf => {
const mesh = gltf. scene. children[ 0 ]
mesh. material = material
mesh. castShadow = true
mesh. customDepthMaterial = depthMaterial
scene. add ( mesh)
} )
const plane = new THREE. Mesh (
new THREE. PlaneGeometry ( 10 , 10 ) ,
new THREE. MeshStandardMaterial ( {
side : THREE . DoubleSide
} )
)
plane. position. set ( 0 , 0 , - 6 )
plane. receiveShadow = true
scene. add ( plane)
const renderer = new THREE. WebGLRenderer ( )
renderer. setSize ( window. innerWidth, window. innerHeight)
renderer. shadowMap. enabled = true
document. body. appendChild ( renderer. domElement)
const controls = new OrbitControls ( camera, renderer. domElement)
controls. enableDamping = true
const clock = new THREE. Clock ( )
function animate ( ) {
controls. update ( )
const time = clock. getElapsedTime ( )
requestAnimationFrame ( animate)
renderer. render ( scene, camera)
}
animate ( )
window. addEventListener ( 'resize' , ( ) => {
camera. aspect = window. innerWidth / window. innerHeight
camera. updateProjectionMatrix ( )
renderer. setSize ( window. innerWidth, window. innerHeight)
renderer. setPixelRatio ( window. devicePixelRatio)
} )