Ai
2 Star 0 Fork 0

mirrors_googlecodelabs/webvr-codelab

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
demo.js 3.14 KB
一键复制 编辑 原始数据 按行查看 历史
Sam Thorogood 提交于 2017-05-10 06:55 +08:00 . Initial codelab release
/*
Copyright 2017 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';
class Demo {
static get CAMERA_SETTINGS () {
return {
viewAngle: 45,
near: 0.1,
far: 10000
};
}
constructor () {
this._width;
this._height;
this._renderer;
this._camera;
this._aspect;
this._settings;
this._box;
this._container = document.querySelector('#container');
this.clearContainer();
this.createRenderer();
this._onResize = this._onResize.bind(this);
this._update = this._update.bind(this);
this._onResize();
this.createCamera();
this.createScene();
this.createMeshes();
this._addEventListeners();
requestAnimationFrame(this._update);
}
_update () {
const ROTATION_VALUE = 4;
const time = window.performance.now() * 0.0001;
this._box.rotation.x = Math.sin(time) * ROTATION_VALUE;
this._box.rotation.y = Math.cos(time) * ROTATION_VALUE;
this._render();
}
_render () {
this._renderer.render(this._scene, this._camera);
requestAnimationFrame(this._update);
}
_onResize () {
this._width = window.innerWidth;
this._height = window.innerHeight;
this._aspect = this._width / this._height;
this._renderer.setSize(this._width, this._height);
if (!this._camera) {
return;
}
this._camera.aspect = this._aspect;
this._camera.updateProjectionMatrix();
}
_addEventListeners () {
window.addEventListener('resize', this._onResize);
}
clearContainer () {
this._container.innerHTML = '';
}
createRenderer () {
this._renderer = new THREE.WebGLRenderer();
this._container.appendChild(this._renderer.domElement);
}
createCamera () {
this._settings = Demo.CAMERA_SETTINGS;
this._camera = new THREE.PerspectiveCamera(
this._settings.viewAngle,
this._aspect,
this._settings.near,
this._settings.far
);
}
createScene () {
this._scene = new THREE.Scene();
}
createMeshes () {
const WIDTH = 1;
const HEIGHT = 1;
const DEPTH = 1;
// Box.
const boxGeometry = new THREE.BoxGeometry(WIDTH, HEIGHT, DEPTH);
const boxMaterial = new THREE.MeshNormalMaterial();
this._box = new THREE.Mesh(boxGeometry, boxMaterial);
this._box.position.z = -3;
// Room.
const roomGeometry = new THREE.BoxGeometry(10, 2, 10, 10, 2, 10);
const roomMaterial = new THREE.MeshBasicMaterial({
wireframe: true,
opacity: 0.3,
transparent: true,
side: THREE.BackSide
});
const room = new THREE.Mesh(roomGeometry, roomMaterial);
this._scene.add(this._box);
this._scene.add(room);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_googlecodelabs/webvr-codelab.git
git@gitee.com:mirrors_googlecodelabs/webvr-codelab.git
mirrors_googlecodelabs
webvr-codelab
webvr-codelab
master

搜索帮助