html5 vr效果,HTML5 Three.js 虚拟现实小实验(VR Experiment)

本文介绍了如何使用HTML5和Three.js库创建虚拟现实(VR)体验。通过设置和更新视锥体、射线投射器、碰撞检测以及交互效果,演示了如何创建一个VR实验,包括注视点跟踪、聚焦效果和对象交互。
摘要由CSDN通过智能技术生成

HTML

导入代码模板:

/*! Reticulum - v1.0.12 - 2015-08-17

* https://gqpbj.github.io/examples/basic.html

*

* Copyright (c) 2015 Godfrey Q;

* Licensed under the MIT license */

var Reticulum = (function() {

var INTERSECTED = null;

var collisionList = [];

var raycaster;

var vector;

var clock;

var reticle = {};

var fuse = {};

var frustum;

var cameraViewProjectionMatrix;

//Settings from user

var settings = {

camera: null, //Required

proximity: false

};

//Vibrate

var vibrate = navigator.vibrate ? navigator.vibrate.bind(navigator) : function() {};

//Fuse

fuse.initiate = function(options) {

var parameters = options || {};

this.visible = parameters.visible !== false; //default to true;

this.duration = parameters.duration || 2.5;

this.vibratePattern = parameters.vibrate || 100;

this.color = parameters.color || 0x000000;

this.innerRadius = parameters.innerRadius || reticle.innerRadiusTo;

this.outerRadius = parameters.outerRadius || reticle.outerRadiusTo;

this.phiSegments = 3;

this.thetaSegments = 32;

this.thetaStart = Math.PI / 2;

//var geometry = new THREE.CircleGeometry( reticle.outerRadiusTo, 32, Math.PI/2, 0 );

var geometry = new THREE.RingGeometry(this.innerRadius, this.outerRadius, this.thetaSegments, this.phiSegments, this.thetaStart, 0);

//Make Mesh

this.mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({

color: this.color,

side: THREE.BackSide,

fog: false

}));

//Set mesh visibility

this.mesh.visible = this.visible;

//Change position and rotation of fuse

this.mesh.position.z = 0.0001; // Keep in front of reticle

this.mesh.rotation.y = 180 * (Math.PI / 180); //Make it clockwise

//Add to reticle

reticle.mesh.add(this.mesh);

//geometry.dispose();

};

fuse.out = function() {

this.active = false;

this.mesh.visible = false;

this.update(0);

}

fuse.over = function() {

this.active = true;

this.update(0);

this.mesh.visible = this.visible;

}

fuse.update = function(elapsed) {

if (!this.active) return;

//--RING

var gazedTime = elapsed / this.duration;

var thetaLength = gazedTime * (Math.PI * 2);

var vertices = this.mesh.geometry.vertices;

var radius = this.innerRadius;

var radiusStep = ((this.outerRadius - this.innerRadius) / this.phiSegments);

var count = 0;

for (var i = 0; i < this.phiSegments + 1; i++) {

for (var o = 0; o < this.thetaSegments + 1; o++) {

var vertex = vertices[count];

var segment = this.thetaStart + o / this.thetaSegments * thetaLength;

vertex.x = radius * Math.cos(segment);

vertex.y = radius * Math.sin(segment);

count++;

}

radius += radiusStep;

}

this.mesh.geometry.verticesNeedUpdate = true;

//Disable fuse if reached 100%

if (gazedTime >= 1) {

this.active = false;

}

//--RING EOF

}

//Reticle

reticle.initiate = function(options) {

var parameters = options || {};

this.active = true;

this.visible = parameters.visible !== false; //default to true;

this.far = parameters.far || settings.camera.far - 10.0;

this.color = parameters.color || 0xcc0000;

this.innerRadius = parameters.innerRadius || 0.0001;

this.outerRadius = parameters.outerRadius || 0.003;

this.worldPosition = new THREE.Vector3();

this.ignoreInvisible = parameters.ignoreInvisible !== false; //default to true;

//Hover

this.innerRadiusTo = parameters.hover.innerRadius || 0.02;

this.outerRadiusTo = parameters.hover.outerRadius || 0.024;

this.colorTo = parameters.hover.color || this.color;

this.vibratePattern = parameters.hover.vibrate || 50;

this.hit = false;

//Animation options

this.speed = parameters.hover.speed || 5;

this.moveSpeed = 0;

//Geometry

var geometry = new THREE.RingGeometry(this.innerRadius, this.outerRadius, 32, 3, 0, Math.PI * 2);

var geometryScale = new THREE.RingGeometry(this.innerRadiusTo, this.outerRadiusTo, 32, 3, 0, Math.PI * 2);

//Add Morph Targets for scale animation

geometry.morphTargets.push({

name: "target1",

vertices: geometryScale.vertices

});

//Make Mesh

this.mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({

color: this.color,

morphTargets: true,

fog: false

}));

this.mesh.visible = this.visible;

//set depth and scale

this.setDepthAndScale();

//Add to camera

settings.camera.add(this.mesh);

};

//Sets the depth and scale of the reticle - reduces eyestrain and depth issues

reticle.setDepthAndScale = function(depth) {

var cross

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值