-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02-spot-light-shadow.js
106 lines (80 loc) · 2.69 KB
/
02-spot-light-shadow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as THREE from 'three';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls';
import { AmbientLight } from 'three';
let renderer, camera, scene;
let axesHelper;
let ambientLight, spotLight;
let plane, cylinder;
let controls;
initRenderer();
initCamera();
initScene();
//initAxesHelper();
initControls();
initAmbientlisLight();
initSpotLight();
initMeshes();
initShadow();
render();
window.addEventListener('resize', function(){//渲染结果随着窗体的变化而变化(浏览器变窄了,渲染窗口也变窄)
camera.aspect = window.innerWidth/this.window.innerHeight;
camera.updateProjectionMatrix();
renderer,setSize(window.innerWidth, this.window.innerHeight);
})
function initRenderer() {
renderer = new THREE.WebGL1Renderer();
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
function initCamera(){
camera = new THREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(0,120,200);
camera.lookAt(0,0,0);
}
function initScene(){
scene = new THREE.Scene();
}
function initAxesHelper(){
axesHelper = new THREE.AxesHelper(50);
scene.add(axesHelper);
}
function initAmbientlisLight(){//背景灯
ambientLight = new THREE.AmbientLight(0xffffff, 0.2);
scene.add(ambientLight);
}
function initSpotLight(){//聚光灯
spotLight = new THREE.SpotLight(0xffffff, 1);
spotLight.position.set(-10, 80, 0);
spotLight.angle = Math.PI/6;
spotLight.penumbra = 0.4;
scene.add(spotLight);
}
function initMeshes(){
//创建平面
const geometryPlane = new THREE.PlaneGeometry(2000, 2000);
const materialPlane = new THREE.MeshPhongMaterial({color:0x808080});
plane = new THREE.Mesh(geometryPlane, materialPlane);
plane.rotation.x = -Math.PI/2;
plane.position.set(0,-10,0);
scene.add(plane);
//创建圆柱体
const geometryCylinder = new THREE.CylinderGeometry(5, 5, 2, 24, 1, false);
const materialCylinder = new THREE.MeshPhongMaterial({color:0x8080ff});
cylinder = new THREE.Mesh(geometryCylinder, materialCylinder);
cylinder.position.set(0,10,0);
scene.add(cylinder);
}
function initControls(){//控制器可用鼠标旋转
controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change',render);
}
function initShadow(){//创建物体投影
cylinder.castShadow = true;//物体
plane.receiveShadow = true;//平面接收
spotLight.castShadow = true;//光
renderer.shadowMap.enabled = true;//渲染器渲染出来
}
function render(){
renderer.render(scene,camera);
}