-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimS.html
162 lines (142 loc) · 5.1 KB
/
animS.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<title>model</title>
</head>
<!-- стили -->
<style>
* {
margin: 0;
padding: 0;
}
.container {
width: 100vw;
height: 100vh;
}
</style>
<body>
<!-- контейнер для обьекта -->
<div class="container"></div>
<!-- библиотека threejs -->
<script src="build/three.min.js"></script>
<!-- основной код модели -->
<script type="module">
// импорт нужных библиотек
import { OrbitControls } from "./examples/jsm/controls/OrbitControls.js"
import { GLTFLoader } from "./examples/jsm/loaders/GLTFLoader.js"
import { SkeletonUtils } from "./examples/jsm/utils/SkeletonUtils.js"
let scene
let camera
let renderer
// константа анимации
const mixers = []
// время - для анимации
const clock = new THREE.Clock()
function init() {
let container = document.querySelector(".container")
scene = new THREE.Scene()
scene.background = new THREE.Color("#bbb")
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(800, 800),
new THREE.MeshLambertMaterial({ color: "rgb(240,240,240)" })
)
ground.rotation.x = -Math.PI / 2
ground.receiveShadow = true
scene.add(ground)
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 5000)
camera.position.z = 140
camera.position.y = 100
camera.position.x = 200
renderer = new THREE.WebGLRenderer({})
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.shadowMap.enabled = true
container.appendChild(renderer.domElement)
const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
controls.target.set(0, 80, 0)
controls.update()
controls.minDistance = 100
controls.maxDistance = 900
const ambient = new THREE.AmbientLight(0xffffff, 0.25)
scene.add(ambient)
const dirLight = new THREE.DirectionalLight("rgb(255,234,229)", 1)
dirLight.position.set(1000, 2000, 1000)
scene.add(dirLight)
dirLight.castShadow = true
dirLight.shadow.mapSize.width = 2048
dirLight.shadow.mapSize.height = 2048
dirLight.shadow.camera.near = 0.5
dirLight.shadow.camera.far = 5000
const d = 500
dirLight.shadow.camera.left = -d
dirLight.shadow.camera.right = d
dirLight.shadow.camera.top = d
dirLight.shadow.camera.bottom = -d
/*
const dirLightHelper = new THREE.DirectionalLightHelper(dirLight, 10)
scene.add(dirLightHelper)
*/
const loader = new GLTFLoader()
loader.load(
"model/gltf/Soldier.glb",
gltf => {
//надо для работы тени
gltf.scene.traverse(function (object) {
if (object.isMesh) object.castShadow = true
})
//1 модель с бегом
const model1 = SkeletonUtils.clone(gltf.scene)
model1.position.x = -60
model1.position.y = 0
model1.scale.x = 100
model1.scale.y = 100
model1.scale.z = 100
model1.rotation.y = Math.PI
//2 модель с шагом
const model2 = SkeletonUtils.clone(gltf.scene)
model2.position.x = 60
model2.position.y = 0
model2.scale.x = 100
model2.scale.y = 100
model2.scale.z = 100
model2.rotation.y = Math.PI
scene.add(model1, model2)
//выводим все свойства обьекта
console.dir(gltf)
console.dir(model1)
console.dir(model2)
//добавляем анимацию бега
const mixer1 = new THREE.AnimationMixer(model1)
mixer1.clipAction(gltf.animations[1]).play()
//добавляем анимацию шага
const mixer2 = new THREE.AnimationMixer(model2)
mixer2.clipAction(gltf.animations[3]).play()
//добавляем в mixers
mixers.push(mixer1, mixer2)
},
undefined,
function (error) {
console.log("Error: " + error)
}
)
window.addEventListener("resize", onWindowResize)
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
}
function animate() {
requestAnimationFrame(animate)
controls.update()
//перебираем все анимации mixers
const delta = clock.getDelta()
for (const mixer of mixers) mixer.update(delta)
renderer.render(scene, camera)
}
animate()
}
init()
</script>
</body>
</html>