threejs实现三维恐龙滑板动画效果代码

代码语言:html

所属分类:三维

代码描述:threejs实现三维恐龙滑板动画效果代码

代码标签: three 恐龙 三维 滑板

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">
  

  
<style>
body {
  margin: 0;
}
.world {
  position: absolute;
  overflow: hidden;
  width: 100%;
  height: 100%;
  background-color: #65BDCC;
}
.toggle-music {
  position: absolute;
  top: 1rem;
  left: 1rem;
  width: 3rem;
  height: 3rem;
  background: url('//repo.bfw.wiki/bfwrepo/icon/60d1c2f3e1d1e.png') center center / 70% no-repeat;
  cursor: pointer;
}
.music-off {
  background: url('//repo.bfw.wiki/bfwrepo/icon/60d1c2e3eff9a.gif') center center / 60% no-repeat;
}
</style>



</head>

<body  >
  <div class="world"></div><a class="toggle-music"></a>
<audio class="world-music" src="//repo.bfw.wiki/bfwrepo/sound/61397f63d3f7c.mp3"></audio>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.84.js"></script>
      <script>
'use strict';

let scene,
camera,
renderer,
raycaster,
mouseDown,
world,
night = false;

let ground,
city,
dino;

let width,
height;

let speed = 0.05;

function init() {
  width = window.innerWidth,
  height = window.innerHeight;

  scene = new THREE.Scene();
  camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 50);
  camera.position.set(-2, 2, 12);
  camera.lookAt(scene.position);

  renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(width, height);
  renderer.shadowMap.enabled = true;
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;

  addLights();
  drawGround();
  drawCity();
  drawDino();

  world = document.querySelector('.world');
  world.appendChild(renderer.domElement);

  document.addEventListener('mousedown', onMouseDown);
  document.addEventListener('mouseup', onMouseUp);
  document.addEventListener('touchstart', onTouchStart);
  document.addEventListener('touchend', onTouchEnd);
  window.addEventListener('resize', onResize);
}

function addLights() {
  const light = new THREE.HemisphereLight();
  scene.add(light);

  const directLight1 = new THREE.DirectionalLight(0xffffff, 0.3);
  directLight1.castShadow = true;
  directLight1.position.set(20, 10, 18);
  scene.add(directLight1);

  const directLight2 = new THREE.DirectionalLight(0xffffff, 0.8);
  directLight2.castShadow = true;
  directLight2.position.set(-14, 6, 14);
  scene.add(directLight2);
}

function drawGround() {
  ground = new THREE.Mesh(new THREE.PlaneGeometry(90, 30), new THREE.MeshStandardMaterial({
    color: 0x375076,
    roughness: 1 }));

  ground.rotation.x = -Math.PI / 2;
  ground.position.y = -0.6;
  ground.receiveShadow = true;
  scene.add(ground);
}

function drawCity() {
  city = new City();
  scene.add(city.group);
}

function drawDino() {
  dino = new Dino();
  scene.add(dino.group);
}

function onResize() {
  width = window.innerWidth;
  height = window.innerHeight;
  camera.aspect = width / height;
  camera.updateProjectionMatrix();
  renderer.setSize(width, height);
}

function onMouseDown(event) {
  mouseDown = true;
}
function onTouchStart(event) {
  if (event.target.classList[0] === 'toggle-music') return;
  event.preventDefault();
  mouseDown = true;
  if (speed !== 0.1) speed = 0.1;
}

function onMouseUp() {
  mouseDown = false;
}
function onTouc.........完整代码请登录后点击上方下载按钮下载查看

网友评论0