three 基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 lines
10 KiB

2 years ago
2 years ago
import { OrbitControls } from 'jsm/controls/OrbitControls.js';
2 years ago
2 years ago
import { CSS3DRenderer, CSS3DObject, CSS3DSprite } from 'jsm/renderers/CSS3DRenderer.js';
import css3dlabel from './units/css3dlabel.js'
2 years ago
2 years ago
var APP = {
Player: function () {
2 years ago
var controls, camera, glScene, cssScene, glRenderer, cssRenderer;
2 years ago
var waters = [];
//CSS 标签列表
var BIG_LABEL_ARR = [];
2 years ago
//刷新函数列表
var FUN_ARR = [];
2 years ago
camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
1,
10000);
camera.position.set(-10, 5, -10);
glRenderer = createGlRenderer();
cssRenderer = createCssRenderer();
controls = new OrbitControls(camera, glRenderer.domElement);
// controls = new OrbitControls(camera, glRenderer.domElement);
document.body.appendChild(cssRenderer.domElement);
document.body.appendChild(glRenderer.domElement);
// glRenderer.domElement.appendChild(cssRenderer.domElement);
glScene = new THREE.Scene();
2 years ago
cssScene = new THREE.Scene();
2 years ago
// for (var i = 0; i < 20; i++) {
// create3dPage(
// 1020, 680,
// new THREE.Vector3(-2 - i, 1 + i / 3, -4.2 + i),
// new THREE.Vector3(0, Math.PI, 0),
// 'https://zuoben.blog.csdn.net/');
// }
// initModel();
createLights();
window.camera = camera;
window.scene = glScene;
2 years ago
2 years ago
2 years ago
function createGlRenderer() {
var glRenderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
2 years ago
glRenderer.setClearColor(0x34495E);
2 years ago
glRenderer.setPixelRatio(window.devicePixelRatio);
glRenderer.setSize(window.innerWidth, window.innerHeight);
glRenderer.domElement.style.position = 'absolute';
glRenderer.domElement.style.zIndex = 1;
glRenderer.domElement.style.top = 0;
return glRenderer;
}
function createCssRenderer() {
2 years ago
var cssRenderer = new CSS3DRenderer();
2 years ago
cssRenderer.setSize(window.innerWidth, window.innerHeight);
cssRenderer.domElement.style.position = 'absolute';
glRenderer.domElement.style.zIndex = 0;
cssRenderer.domElement.style.top = 0;
return cssRenderer;
}
function initDraws(child) { }
function initFrames(child) {
child.material = new THREE.MeshBasicMaterial({
color: 0x7f5816,
})
}
function initStairs(child) {
child.material = new THREE.MeshStandardMaterial({
color: 0xd1cdb7,
})
child.material.roughness = 0.5
child.material.metalness = 0.6
}
function initWalls(child) {
child.material = new THREE.MeshStandardMaterial({
color: 0xffffff,
})
child.material.roughness = 0.5
child.material.metalness = 0.6
}
function create3dPage(w, h, position, rotation, url) {
var plane = createPlane(
w, h,
position,
rotation);
glScene.add(plane);
var cssObject = createCssObject(
w, h,
position,
rotation,
url);
cssScene.add(cssObject);
}
function createLights() {
2 years ago
var directionalLight = new THREE.DirectionalLight(0xe0ffff);
2 years ago
directionalLight.position.set(-.5, .5, -1.5).normalize();
glScene.add(directionalLight);
2 years ago
const light = new THREE.AmbientLight("#afafaf");
2 years ago
glScene.add(light);
2 years ago
// const light1 = new THREE.PointLight(0xe0ffff, 0.1, 20)
// light1.position.set(-2, 3, 2)
// glScene.add(light1)
2 years ago
2 years ago
// const light2 = new THREE.PointLight(0xe0ffff, 0.1, 20)
// light2.position.set(0, 3, -6)
// glScene.add(light2)
2 years ago
2 years ago
// const light3 = new THREE.PointLight(0xe0ffff, 0.1, 20)
// light3.position.set(-12, 3, 6)
// glScene.add(light3)
2 years ago
2 years ago
// const light4 = new THREE.PointLight(0xe0ffff, 0.1, 20)
// light4.position.set(-12, 4, -4)
// glScene.add(light4)
2 years ago
2 years ago
// const light5 = new THREE.PointLight(0xe0ffff, 0.1, 20)
// light5.position.set(12, 4, -8)
// glScene.add(light5)
2 years ago
2 years ago
// const light6 = new THREE.PointLight(0xe0ffff, 0.1, 20)
// light6.position.set(12, 4, 0)
// glScene.add(light6)
2 years ago
2 years ago
// const light7 = new THREE.PointLight(0xe0ffff, 0.1, 20)
// light7.position.set(12, 4, 8)
// glScene.add(light7)
2 years ago
}
2 years ago
this.setClearColor = function (color) {
glRenderer.setClearColor(color);
2 years ago
}
//position = { x: 0, y: 0, z: 0 }, rotation= { x: 0, y: 0, z: 0 }
2 years ago
this.addModels = function (value) {
2 years ago
glScene.add(value);
};
this.setCamera = function (value) {
camera = value;
camera.aspect = this.width / this.height;
camera.updateProjectionMatrix();
// this.camera = camera;
window.camera = camera;
};
this.setScene = function (value) {
2 years ago
glScene = value;
window.scene = glScene
2 years ago
};
2 years ago
this.addWater = function (water) {
2 years ago
waters.push(water);
}
this.setPixelRatio = function (pixelRatio) {
glRenderer.setPixelRatio(pixelRatio);
};
this.setSize = function (width, height) {
this.width = width;
this.height = height;
if (camera) {
camera.aspect = this.width / this.height;
camera.updateProjectionMatrix();
}
glRenderer.setSize(this.width, this.height);
cssRenderer.setSize(this.width, this.height);
};
2 years ago
2 years ago
var time, startTime, prevTime;
2 years ago
2 years ago
function animate() {
2 years ago
2 years ago
// controls.update();
glRenderer.render(glScene, camera);
cssRenderer.render(cssScene, camera);
2 years ago
waters.forEach(water => {
// 播放特效
water.material.uniforms['time'].value += 1.0 / 60.0;
});
FUN_ARR.forEach( fun=> {
// 播放特效
fun();
});
2 years ago
2 years ago
}
2 years ago
var objects = []
2 years ago
this.play = function () {
2 years ago
2 years ago
// if (renderer.xr.enabled) dom.append(vrButton);
// startTime = prevTime = performance.now();
// document.addEventListener('keydown', onKeyDown);
// document.addEventListener('keyup', onKeyUp);
// document.addEventListener('pointerdown', onPointerDown);
// document.addEventListener('pointerup', onPointerUp);
// document.addEventListener('pointermove', onPointerMove);
// dispatch(events.start, arguments);
glRenderer.setAnimationLoop(animate);
2 years ago
// const image = document.createElement('img');
// image.addEventListener('load', function () {
2 years ago
2 years ago
// for (let i = 0; i < 2; i++) {
2 years ago
2 years ago
// const object = new CSS3DSprite(image.cloneNode());
// object.position.x = Math.random() * 4 - 2,
// object.position.y = Math.random() * 4 - 2,
// object.position.z = Math.random() * 4 - 2;
// object.scale.set(0.01, 0.01, 0.01);
// glScene.add(object);
2 years ago
2 years ago
// objects.push(object);
2 years ago
2 years ago
// }
2 years ago
2 years ago
// //transition();
2 years ago
2 years ago
// });
// image.src = '/assets/textures/sprite.png';
2 years ago
2 years ago
// cssScene.add(createSpriteShape());
// cssScene.add(createBox13DLabel());
2 years ago
2 years ago
};
this.stop = function () {
if (renderer.xr.enabled) vrButton.remove();
// document.removeEventListener('keydown', onKeyDown);
// document.removeEventListener('keyup', onKeyUp);
// document.removeEventListener('pointerdown', onPointerDown);
// document.removeEventListener('pointerup', onPointerUp);
// document.removeEventListener('pointermove', onPointerMove);
dispatch(events.stop, arguments);
glRenderer.setAnimationLoop(null);
2 years ago
2 years ago
};
this.dispose = function () {
glRenderer.dispose();
cssRenderer.dispose();
camera = undefined;
glScene = undefined;
cssScene = undefined;
};
//
2 years ago
this.create3dPage = function (w, h, position, rotation, url) {
2 years ago
var plane = css3dlabel.createPlane(
w, h,
position,
rotation);
glScene.add(plane);
var cssObject = css3dlabel.createCssObject(
w, h,
position,
rotation,
url);
cssScene.add(cssObject);
}
2 years ago
const defaultScale = 80
2 years ago
this.create3dLabel = function (parent, text, w = 100, h = 20, position, rotation) {
2 years ago
// let sprite =createBox13DLabel()
// glScene.add(sprite)
// return ;
let txtlength = text.length
2 years ago
var width = defaultScale * txtlength;
2 years ago
var height = defaultScale + 15;
2 years ago
2 years ago
var plane = css3dlabel.createPlane(
width, height,
position,
rotation);
2 years ago
2 years ago
glScene.add(plane);
var cssObject = css3dlabel.createCssLabel(text,
2 years ago
width, height,
2 years ago
position,
rotation);
// if(parent){
// parent.add(cssObject);
2 years ago
BIG_LABEL_ARR.push(cssObject);
2 years ago
// }else{
2 years ago
cssScene.add(cssObject);
2 years ago
// }
2 years ago
//cssObject.element.textContent = 'your data'
2 years ago
}
2 years ago
// this.create3dButton = function (parent, text, w = 100, h = 20, position, rotation,click) {
// let txtlength = text.length
// var width = defaultScale * txtlength;
// var height = defaultScale + 15;
// var plane = css3dlabel.createPlane(
// width, height,
// position,
// rotation);
// glScene.add(plane);
// var cssObject = css3dlabel.createButton(text,
// position,
// rotation,click);
// // if(parent){
// // parent.add(cssObject);
// // }else{
// cssScene.add(cssObject);
// // }
// }
2 years ago
function createSpriteShape() {
/*1、创建一个画布,记得设置画布的宽高,否则将使用默认宽高,有可能会导致图像显示变形*/
let canvas = document.createElement("canvas");
canvas.width = 120;
canvas.height = 120;
/*2、创建图形,这部分可以去看w3c canvas教程*/
let ctx = canvas.getContext("2d");
ctx.fillStyle = "#ff0000";
ctx.arc(50, 50, 50, 0, 2 * Math.PI);
ctx.fill();
/*3、将canvas作为纹理,创建Sprite*/
let texture = new THREE.Texture(canvas);
texture.needsUpdate = true; //注意这句不能少
let material = new THREE.SpriteMaterial({
color: 0xff00ff,//设置精灵矩形区域颜色
rotation: Math.PI / 4,//旋转精灵对象45度,弧度值
map: texture,//设置精灵纹理贴图
});
let mesh = new THREE.Sprite(material);
2 years ago
mesh.position.x = 2;
2 years ago
mesh.position.y = 2;
/*4、放大图片,每个精灵有自己的大小,默认情况下都是很小的,如果你不放大,基本是看不到的*/
//mesh.scale.set(100,100,1);
return mesh;
}
function createBox13DLabel() {
const div = document.createElement("div");
div.className = "red-box-label";
div.textContent = "红色正方体";
// CSS3DSprite;CSS3DObject
const earthLabel = new CSS3DSprite(div);
earthLabel.position.set(1, 0.2, 0.5);
earthLabel.scale.set(0.01, 0.01, 0.01);
return earthLabel;
}
2 years ago
this.updateLabel = function (name, text) {
BIG_LABEL_ARR.forEach(tmplable => {
tmplable.element.textContent = text;
})
}
2 years ago
this.addRefeshFunction = function(fun){
FUN_ARR.push(fun);
}
2 years ago
}
};
export { APP };