效果图:实际效果,图中的贴图是可以动的 一、准备工作
1、静态文件
2、首先你要引入相关依赖:
threejs核心依赖: http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.min.js
兼容检测器: http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.min.js
运行帧数监测器: http://www.yanhuangxueyuan.com/versions/threejsR92/examples/js/libs/stats.min.js
轨道控制器: http://www.yanhuangxueyuan.com/versions/threejsR92/examples/js/libs/stats.min.js
3、 接下来从Vue项目中的运用来说, 以上依赖库,可以在 public/index.html 中引入,也可以下载到项目本地,然后再main.js 中import引入
二、下面开始编写代码
- 首先你需要在页面中准备好一个DOM容器, 如下
<div id="ThreeJS"></div>
- 创建变量
data() {
return {
scene: null,
camera: null,
renderer: null,
controls: null,
stats: null
}
}
- 调用时需要在页面DOM挂载完成时再调用
mounted() {
this.init()
}
- 功能函数
methods: {
init() {
var scene, renderer, camera, annie, boomer;
var clock = new THREE.Clock();
var container = document.getElementById("ThreeJS");
scene = new THREE.Scene();
var SCREEN_WIDTH = window.innerWidth,
SCREEN_HEIGHT = window.innerHeight;
var VIEW_ANGLE = 45,
ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT,
NEAR = 0.1,
FAR = 20000;
camera = new THREE.PerspectiveCamera(
VIEW_ANGLE,
ASPECT,
NEAR,
FAR
);
scene.add(camera);
camera.position.set(0, 150, 400);
camera.lookAt(scene.position);
if (!Detector.webgl) return alert('不支持')
renderer = new THREE.WebGLRenderer({
antialias: true,
})
renderer = new THREE.CanvasRenderer();
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
container.appendChild(renderer.domElement);
controls = new THREE.OrbitControls(
camera,
renderer.domElement
);
stats = new Stats();
stats.domElement.style.position = "absolute";
stats.domElement.style.bottom = "0px";
stats.domElement.style.zIndex = 100;
container.appendChild(stats.domElement);
var light = new THREE.PointLight(0xffffff);
light.position.set(0, 250, 0);
scene.add(light);
var floorTexture = new THREE.ImageUtils.loadTexture(
"images/checkerboard.jpg"
);
floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat.set(10, 10);
var floorMaterial = new THREE.MeshBasicMaterial({
map: floorTexture,
side: THREE.DoubleSide,
});
var floorGeometry = new THREE.PlaneGeometry(1000, 1000, 10, 10);
var floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.position.y = -0.5;
floor.rotation.x = Math.PI / 2;
scene.add(floor);
var skyBoxGeometry = new THREE.CubeGeometry(10000, 10000, 10000);
var skyBoxMaterial = new THREE.MeshBasicMaterial({
color: 0x9999ff,
side: THREE.BackSide,
});
var skyBox = new THREE.Mesh(skyBoxGeometry, skyBoxMaterial);
scene.fog = new THREE.FogExp2(0x9999ff, 0.00025);
var runnerTexture = new THREE.ImageUtils.loadTexture(
"images/run.png"
);
annie = new TextureAnimator(runnerTexture, 10, 1, 10, 75);
var runnerMaterial = new THREE.MeshBasicMaterial({
map: runnerTexture,
side: THREE.DoubleSide,
});
var runnerGeometry = new THREE.PlaneGeometry(50, 50, 1, 1);
var runner = new THREE.Mesh(runnerGeometry, runnerMaterial);
runner.position.set(-100, 25, 0);
scene.add(runner);
var explosionTexture = new THREE.ImageUtils.loadTexture(
"images/explosion.jpg"
);
boomer = new TextureAnimator(explosionTexture, 4, 4, 16, 55);
var explosionMaterial = new THREE.MeshBasicMaterial({
map: explosionTexture,
});
var cubeGeometry = new THREE.CubeGeometry(50, 50, 50);
var cube = new THREE.Mesh(cubeGeometry, explosionMaterial);
cube.position.set(0, 26, 0);
scene.add(cube);
function update() {
var delta = clock.getDelta();
annie.update(1000 * delta);
boomer.update(1000 * delta);
controls.update();
stats.update();
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
update();
}
animate();
function TextureAnimator(
texture,
tilesHoriz,
tilesVert,
numTiles,
tileDispDuration
) {
var tilesHorizontal = tilesHoriz;
var tilesVertical = tilesVert;
var numberOfTiles = numTiles;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1 / tilesHorizontal, 1 / tilesVertical);
var tileDisplayDuration = tileDispDuration;
var currentDisplayTime = 0;
var currentTile = 0;
this.update = function (milliSec) {
currentDisplayTime += milliSec;
while (currentDisplayTime > tileDisplayDuration) {
currentDisplayTime -= tileDisplayDuration;
currentTile++;
if (currentTile == numberOfTiles) currentTile = 0;
var currentColumn = currentTile % tilesHorizontal;
texture.offset.x = currentColumn / tilesHorizontal;
var currentRow = Math.floor(currentTile / tilesHorizontal);
texture.offset.y = currentRow / tilesVertical;
}
};
}
}
}
注:以上就是相关代码,如有疑问请在下方留言,博主会及时回复!!!
|