three实现简单三维人物角色修改配件效果代码

代码语言:html

所属分类:三维

代码描述:three实现简单三维人物角色修改配件效果代码

代码标签: three 三维 人物 角色 修改 配件

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

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

<head>

  <meta charset="UTF-8">
  

  
  
  
<style>
html,
body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.background {
  background-color: #242424;
}

.attachment {
  content: " ";
  position: absolute;
  display: inline-flex;
}

.attachment-legs {
  left: 60%;
  top: 70%;
}

.attachment-weapon {
  left: 34%;
  top: 36%;
}

.attachment {
  width: 3rem;
  height: 3rem;
  border: 1px solid grey;
  cursor: pointer;
}
.attachment:hover .attachment__button-content {
  margin: 5%;
  height: 90%;
  width: 90%;
  background-color: rgba(255, 255, 255, 0.3);
}

.attachment__button-content {
  position: relative;
  margin: 10%;
  height: 80%;
  width: 80%;
  background-color: rgba(255, 255, 255, 0.1);
  transition: background-color 0.15s ease, margin 0.15s ease, height 0.15s ease, width 0.15s ease;
}

.add-sign-plus {
  transition: transform 0.2s ease;
}

.add-sign {
  color: lightgrey;
  top: 50%;
  left: 50%;
  position: absolute;
  transform: translate(-50%, -50%);
  font-size: 1.75rem;
}

.options {
  transition: opacity 0.2s ease;
  opacity: 0;
  visibility: hidden;
}

.attachment-options-legs {
  left: calc(60% + 3.1rem);
  top: 70%;
}

.attachment-options-weapon {
  left: calc(34% - 6.3rem);
  top: 36%;
}

.attachment-options {
  display: flex;
  position: absolute;
  height: min-content;
  width: auto;
  height: 3rem;
  cursor: pointer;
}

.option {
  position: relative;
  margin-left: 1px;
  height: 80%;
  width: 100%;
  height: 100%;
  transition: background-color 0.15s ease, margin 0.15s ease, height 0.15s ease, width 0.15s ease;
  opacity: 0.5;
  transition: opacity 0.2s ease;
  background-color: rgba(255, 255, 255, 0.1);
  border: 1px solid #4e4e4e;
}
.option:hover {
  opacity: 1;
}

.pegleg-svg,
.leg-svg,
.sword-svg,
.axe-svg {
  width: 30px;
  height: 30px;
  margin-left: 0.55rem;
  margin-right: 0.5rem;
  margin-top: 0.5rem;
}

.tip {
  position: absolute;
  left: 50%;
  top: 1rem;
  transform: translateX(-50%);
  text-transform: uppercase;
  font-family: sans-serif;
  font-weight: 600;
  opacity: 0.35;
  font-size: 0.75rem;
}
</style>



</head>

<body>
  <div id="background" class="background"></div>

<div class="tip">Drag the screen to look around</div>

<div class="attachment attachment-weapon" onclick="toggleWeaponsMenu()">
  <div class="attachment__button-content">
    <div class="add-sign">
      <div class="add-sign-plus add-sign-weapon">
        +
      </div>
    </div>
  </div>
</div>

<div class="options weapon-options">
  <div class="attachment-options attachment-options-weapon">
    <div class="option" onclick="applyWeapon(0)">
    <svg class="axe-svg" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 20"><path d="M7 1a1 1 0 112 0v18a1 1 0 11-2 0V1z" fill="#fff"/><path fill-rule="evenodd" clip-rule="evenodd" d="M14.5 2.618a.5.5 0 00-.724-.447L9.275 4.42a.5.5 0 00.048.916l4.501 1.688a.5.5 0 00.676-.468V2.618zm-1.17-1.342a1.5 1.5 0 012.17 1.342v3.939a1.5 1.5 0 01-2.027 1.404L8.972 6.273c-1.227-.46-1.316-2.16-.144-2.746l4.501-2.25z" fill="#fff"/><path fill-rule="evenodd" clip-rule="evenodd" d="M1.5 2.618a.5.5 0 01.724-.447l4.501 2.25a.5.5 0 01-.048.916L2.176 7.025a.5.5 0 01-.676-.468V2.618zm1.17-1.342A1.5 1.5 0 00.5 2.618v3.939a1.5 1.5 0 002.027 1.404l4.501-1.688c1.227-.46 1.316-2.16.144-2.746l-4.501-2.25z" fill="#fff"/></svg>
    
    </div>
    <div class="option" onclick="applyWeapon(1)">
    <svg class="sword-svg" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M14.5 6.382V16.5h-5V6.378c.56-1.389 1.049-2.392 1.493-3.043.474-.695.795-.838.976-.835.19.003.52.168 1.005.87.453.657.951 1.654 1.526 3.012zm-3 12.118h1V21a.5.5 0 01-1 0v-2.5z" stroke="#fff"/></svg></div>
  </div>
</div>



<div class="attachment attachment-legs" onclick="toggleLegsMenu()">
  <div class="attachment__button-content">
    <div class="add-sign">
      <div class="add-sign-plus add-sign-legs">
        +
      </div>
    </div>
  </div>
</div>

<div class="options leg-options">
  <div class="attachment-options attachment-options-legs">
    <div class="option" onclick="applyLegs(1)">
    <svg class="pegleg-svg" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill-rule="evenodd" clip-rule="evenodd" d="M17 4H7v3h10V4zM7 3a1 1 0 00-1 1v3a1 1 0 001 1h10a1 1 0 001-1V4a1 1 0 00-1-1H7z" fill="#fff"/><path fill-rule="evenodd" clip-rule="evenodd" d="M14 8h-4v4h4V8zm-4-1a1 1 0 00-1 1v4a1 1 0 001 1h4a1 1 0 001-1V8a1 1 0 00-1-1h-4z" fill="#fff"/><path d="M11 13h2v7h-2v-7zM10 20h4v1h-4v-1z" fill="#fff"/></svg>
    </div>
    <div class="option" onclick="applyLegs(0)">
    <svg class="leg-svg" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill-rule="evenodd" clip-rule="evenodd" d="M6 4.5A1.5 1.5 0 017.5 3H12a1.5 1.5 0 011.5 1.5v10a.5.5 0 00.5.5h3a1.5 1.5 0 011.5 1.5v3A1.5 1.5 0 0117 21H7.5A1.5 1.5 0 016 19.5v-15zM7.5 4a.5.5 0 00-.5.5V9h5.5V4.5A.5.5 0 0012 4H7.5zm5 6H7v9.5a.5.5 0 00.5.5H17a.5.5 0 00.5-.5v-3a.5.5 0 00-.5-.5h-3a1.5 1.5 0 01-1.5-1.5V10z" fill="#fff"/></svg>
    </div>
  </div>
</div>
    
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.110.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/GLTFLoader.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/OrbitControls.108.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three-js-csg.js"></script>
      <script >
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ alpha: true });
const controls = new THREE.OrbitControls(camera, renderer.domElement);

// Materials
const materialDarkest = new THREE.MeshPhongMaterial({ color: 0x33281b });
const materialDark = new THREE.MeshPhongMaterial({ color: 0x664e31 });
const materialLight = new THREE.MeshPhongMaterial({ color: 0xa3835b });
const steelMaterial = new THREE.MeshPhongMaterial({ color: 0x878787 });
const skinMaterial = new THREE.MeshPhongMaterial({
  color: 0xffdbac,
  flatShading: false });


// Canvas Renderer
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true;
renderer.shadowMapSoft = true;

renderer.shadowCameraNear = 1;
renderer.shadowCameraFar = 500;
renderer.shadowCameraFov = 60;

renderer.shadowMapBias = 0.05;
renderer.shadowMapDarkness = 1;
renderer.shadowMapWidth = 512;
renderer.shadowMapHeight = 512;
document.getElementById('background').appendChild(renderer.domElement);
camera.position.z = 12;

// Weapons
function axe() {
  const axeHandleGeo = new THREE.BoxGeometry(7, 0.25, 0.25);
  const handle = new THREE.Mesh(axeHandleGeo, materialLight);
  const axeShape = new THREE.Shape();

  axeShape.moveTo(0, 0.15);
  axeShape.lineTo(1, 1);
  axeShape.lineTo(1.25, 0.5);
  axeShape.lineTo(1.25, -0.5);
  axeShape.lineTo(1, -1);
  axeShape.lineTo(0, -0.15);

  const extrudeSettings = {
    steps: 2,
    depth: 0.05,
    bevelEnabled: true,
    bevelThickness: 0.25,
    bevelSize: 0.5,
    bevelOffset: 0,
    bevelSegments: 1 };


  const axeGeo = new THREE.ExtrudeBufferGeometry(axeShape, extrudeSettings);
  const buttGeo = new THREE.BoxGeometry(0.3, 0.3, 0.3);
  const butt1 = new THREE.Mesh(buttGeo, steelMaterial);
  const butt2 = new THREE.Mesh(buttGeo, steelMaterial);
  const butt3 = new THREE.Mesh(buttGeo, steelMaterial);
  const axe1 = new THREE.Mesh(axeGeo, steelMaterial);
  const axe2 = new THREE.Mesh(axeGeo, steelMaterial);

  axe1.castShadow = true;
  axe2.castShadow = true;
  handle.castShadow = true;

  const group = new THREE.Group();

  group.add(handle);
  group.add(axe1);
  group.add(axe2);
  group.add(butt1);
  group.add(butt2);
  group.add(butt3);

  axe1.position.set(2.75, 0.4, 0);
  axe1.rotation.z = Math.PI / 2;
  axe2.position.set(2.75, -0.4, 0);
  axe2.rotation.z = -Math.PI / 2;
  butt2.position.set(-3.5, 0, 0);
  butt3.position.set(3.5, 0, 0);

  return group;
}

function sword() {
  const handleGeo1 = new THREE.BoxGeometry(1.5, 0.25, 0.25);
  const handle1 = new THREE.Mesh(handleGeo1, materialLight);

  const handleGeo2 = new THREE.BoxGeometry(0.25, 2, 0.25);
  const handle2 = new THREE.Mesh(handleGeo2, materialLight);

  const shape = new THREE.Shape();
  const extrudeSettings = {
    steps: 2,
    depth: 0.05,
    bevelEnabled: true,
    bevelThickness: 0.25,
    bevelSize: 0.5,
    bevelOffset: 0,
    bevelSegments: 1 };


  shape.moveTo(0, 0.1);
  shape.lineTo(4, 0.5);
  shape.lineTo(4.5, 0);
  shape.lineTo(4, -0.5);
  shape.lineTo(0, -0.1);

  const bladeGeo = new THREE.ExtrudeBufferGeometry(shape, extrudeSettings);
  const blade = new THREE.Mesh(bladeGeo, steelMaterial);

  const group = new THREE.Group();

  handle1.position.set(-0.85, 0.0, 0);
  blade.position.set(0.5, 0, 0);

  handle1.castShadow = true;
  blade.castShadow = true;
  handle2.castShadow = true;

  group.add(blade);
  group.add(handle1);
  group.add(handle2);

  group.position.set(-0.5, 0, -0.05);
  group.rotation.x = Math.PI / 6;

  return group;
}

// Legs 
function pegLeg() {
  const pegLegGeo = new THREE.BoxGeometry(0.5, 1.8, 0.5);
  const leg = new THREE.Mesh(pegLegGeo, materialLight);

  const stumpUpperGeo = new THREE.BoxGeometry(1, 0.75, 1);
  const stumpUpper = new THREE.Mesh(stumpUpperGeo, materialLight);

  const stumpMaterial = new THREE.MeshPhongMaterial({ color: 0x26211a });
  const stumpGeo = new THREE.BoxGeometry(0.6, 0.2, 0.6);
  const stump = new THREE.Mesh(stumpGeo, stumpMaterial);

  const group = new THREE.Group();

  stump.position.set(1, -4.65, -0.34);
  leg.position.set(1, -3.75, -0..........完整代码请登录后点击上方下载按钮下载查看

网友评论0