three实现三维圣诞礼物模拟重力下落效果代码

代码语言:html

所属分类:三维

代码描述:three实现三维圣诞礼物模拟重力下落效果代码

代码标签: three 三维 圣诞 礼物 模拟 重力 下落

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

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

<head>
  <meta charset="UTF-8">
  

  
  
  
<style>
body { 
	margin: 0;
	overflow: hidden;
	background: #fff5eb;
}
#info {
	position: fixed;
	top: 10px;
	width: 100%;
	text-align: center;
	color: black;
	font-family: Arial;
}
#error {
	position: fixed;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	color: black;
	font-family: Arial;
	text-align: center;
	display: none;
}

#controls {
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 100;
  display: flex;
  align-items: center;
}

.btn {
  background-color: rgba(0, 0, 0, 0.4);
  border: none;
  color: rgba(255, 255, 255, 0.4);
  padding: 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 2px 2px;
  cursor: pointer;
  border-radius: 5px;
  transition: background-color 0.3s;
}
.btn:hover {
  background-color: rgba(0, 0, 0, 0.2);
  color: rgba(0, 0, 0, 1);
}
#fullscreenBtn {
  font-size: 20px;
}
</style>



  
  
</head>

<body translate="no">
  <div id="info">Wrap up gift box drops<br>Click and drag to rotate view</div>
<div id="error">Unable to load gift box model. Falling back to basic cubes.</div>

<div id="controls">
  <button id="fullscreenBtn" class="btn" title="Toggle Fullscreen">⤢</button>
</div>

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.133.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/GLTFLoader.133.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/OrbitControls.133.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/dat.gui-min.js"></script>
  
      <script >
let scene,camera,renderer,boxes = [];
let params = {
  numBoxes: 20,
  speed: 1,
  shape: 'Gift',
  shapes: {
    'Gift': '//repo.bfw.wiki/bfwrepo/threemodel/chiris/4008356_giftbox.gltf',
    'Turbo Granny': "//repo.bfw.wiki/bfwrepo/threemodel/chiris/DanDaDan_Turbo_Granny.glb",
    'Duck': '//repo.bfw.wiki/bfwrepo/threemodel/chiris/Duck.gltf',
    'Figure': '//repo.bfw.wiki/bfwrepo/threemodel/chiris/RiggedFigure.gltf',
    'Cheese Burger': "//repo.bfw.wiki/bfwrepo/threemodel/chiris/Double_Cheese_Burger.glb",
    'Cube': 'basic',
    'Sphere': 'sphere',
    'Cone': 'cone',
    'Cylinder': 'cylinder',
    'Torus': 'torus',
    'TorusKnot': 'torusKnot',
    'Octahedron': 'octahedron',
    'Icosahedron': 'icosahedron',
    'Dodecahedron': 'dodecahedron' } };



function initGUI() {
  const gui = new dat.GUI();
  gui.add(params, 'numBoxes', 1, 50).step(1).onChange(resetBoxes);
  gui.add(params, 'speed', 0.1, 3).step(0.1);
  gui.add(params, 'shape', Object.keys(params.shapes)).onChange(resetBoxes);
}

function resetBoxes() {
  // Remove all existing boxes
  boxes.forEach(box => scene.remove(box));
  boxes = [];

  // Create new boxes with current settings
  let boxIndex = 0;
  function createNextBox() {
    if (boxIndex < params.numBoxes) {
      if (params.shapes[params.shape] === 'basic' ||
      params.shapes[params.shape] === 'sphere' ||
      params.shapes[params.shape] === 'cone' ||
      params.shapes[params.shape] === 'cylinder' ||
      params.shapes[params.shape] === 'torus' ||
      params.shapes[params.shape] === 'torusKnot' ||
      params.shapes[params.shape] === 'octahedron' ||
      params.shapes[params.shape] === 'icosahedron' ||
      params.shapes[params.shape] === 'dodecahedron') {
        createBasicShape(params.shapes[params.shape]);
      } else {
        loadAndCreateModel(params.shapes[params.shape]);
      }
      boxIndex++;
      setTimeout(createNextBox, 250);
    }
  }
  createNextBox();
}

function init() {
  scene = new THREE.Scene();
  scene.background = new THREE.Color(0xfff5eb);
  camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  renderer = new THREE.WebGLRenderer({ antialias: true });
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.shadowMap.enabled = true;
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  renderer.shadowIntensity = 0.5;
  document.body.appendChild(renderer.domElement);

  const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
  scene.add(ambientLight);
  const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
  directionalLight.position.set(5, 5, 5);
  directionalLight.castShadow = true;
  directionalLight.shadow.mapSize.width = 2048;
  directionalLight.shadow.mapSize.height = 2048;
  directionalLight.shadow.camera.near = 0.5;
  directionalLight.shadow.camera.far = 50;
  directionalLight.shadow.camera.left = -25;
  directionalLight.shadow.camera.right = 25;
  directionalLight.shadow.camera.top = 25;
  directionalLight.shadow.camera.bottom = -25;
  directionalLight.shadow.opacity = 0.5;
  scene.add(directionalLight);

  const floorGeometry = new THREE.PlaneGeometry(50, 50);
  const floorMaterial = new THREE.MeshStandardMaterial({
    color: 0xff9999,
    metalness: 0.3,
    roughness: 0..........完整代码请登录后点击上方下载按钮下载查看

网友评论0