-
Notifications
You must be signed in to change notification settings - Fork 0
/
orbitControls.html
113 lines (94 loc) · 3.53 KB
/
orbitControls.html
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
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html>
<head>
<title>Example 01.01 - Basic skeleton</title>
<script type="text/javascript" src="../libs/three.js"></script>
<script type="text/javascript" src="../libs/OrbitControls.js"></script>
<style>
body {
/* set margin to 0 and overflow to hidden, to
use the complete page */
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<!-- Div which will hold the Output -->
环境光 AmbientLight
环境光的颜色会直接影响整个环境的色彩
<div id="WebGL-output">
</div>
<script type="text/javascript">
function init() {
const scene = new THREE.Scene();
const axis = new THREE.AxisHelper(20)
scene.add(axis)
const camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.x = -20;
camera.position.y = 40;
camera.position.z = 40;
camera.lookAt(scene.position);
// camera.lookAt(scene.position)
// camera.lookAt(new THREE.Vector3(0,0,0))
// 平面
const planeGeometry = new THREE.PlaneGeometry(60, 20,1,1)
// const planeMaterial = new THREE.MeshLambertMaterial({ color: 0x049ef4})
const planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
const plane = new THREE.Mesh(planeGeometry, planeMaterial)
plane.receiveShadow = true
plane.rotation.x = -0.5 * Math.PI
plane.position.x = 0;
plane.position.y = 0;
plane.position.z = 0;
scene.add(plane)
// camera.lookAt(plane.position)
// 球体
const sphereGeometry = new THREE.SphereGeometry(4, 20, 20)
const sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff});
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial)
sphere.position.x = 20;
sphere.position.y = 2;
sphere.position.z = 0;
sphere.castShadow = true
scene.add(sphere)
// 灯光
var ambiColor = "#e3239f";
var ambientLight = new THREE.AmbientLight(ambiColor);
scene.add(ambientLight);
// 聚光灯
const spotLight = new THREE.SpotLight( 0xffff00 );
// spotLight.position.x = -30
// spotLight.position.y = 50
// spotLight.position.z = 0
spotLight.position.set(-40, 60, -10);
spotLight.castShadow = true
scene.add(spotLight)
const spotLightHelper = new THREE.SpotLightHelper( spotLight );
scene.add( spotLightHelper );
const renderer = new THREE.WebGLRenderer()
renderer.setClearColorHex();
renderer.setClearColor(new THREE.Color(0xEEEEEE));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true
const wrapper = document.querySelector('#WebGL-output');
wrapper.appendChild(renderer.domElement)
const controls = new THREE.OrbitControls( camera, renderer.domElement );
let step = 0;
function render() {
step += 0.02;
sphere.position.x = 10 + Math.cos(step) * 10
sphere.position.y = 2 + Math.abs(Math.sin(step)) * 5
// sphere.position.y = 2 + Math.sin(step) * 5
controls.update()
requestAnimationFrame(render)
renderer.render(scene, camera);
}
render()
// renderer.render(scene, camera);
console.log('scene', scene)
}
window.onload = init
</script>
</body>
</html>