three实现三维点线发光构成头骨骷颅动画效果代码

代码语言:html

所属分类:三维

代码描述:three实现三维点线发光构成头骨骷颅动画效果代码,加载了obj三维模型,结合了MeshSurfaceSampler、TrackballControls、EffectComposer、RenderPass、UnrealBloomPass、LuminosityHighPassShader、CopyShader、ShaderPass、gsap后期合成。

代码标签: three 三维 点线 发光 构成 头骨 骷颅 动画

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

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
<style>
    body {
  margin: 0;
  overflow: hidden;
  background: #000;
}
</style>

</head>
<body>
<!-- partial:index.partial.html -->
<script type="x-shader/x-vertex" id="vertexshader">
  attribute float size;
  attribute vec3 color;
  attribute float fade;

  varying vec3 vColor;

  void main() {
    vColor = color;
    vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
    gl_PointSize = size;
    gl_Position = projectionMatrix * mvPosition;
  }
</script>

<script type="x-shader/x-fragment" id="fragmentshader">
  uniform sampler2D pointTexture;
  varying vec3 vColor;
  void main() {
    gl_FragColor = vec4(vColor, 1.0);
    gl_FragColor = gl_FragColor * texture2D(pointTexture, gl_PointCoord);
  }
</script>
<!-- partial -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.128.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/MeshSurfaceSampler.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/OBJLoader.min.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/TrackballControls.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/EffectComposer.110.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/RenderPass.110.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/UnrealBloomPass.110.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/LuminosityHighPassShader.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/CopyShader.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/ShaderPass.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.9.1.js"></script>

<script  >
    console.clear();

const pixelRatio = 2;

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
  60,
  window.innerWidth / window.innerHeight,
  0.001,
  1000
);
camera.position.z = 2.7;

const renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(pixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const renderScene = new THREE.RenderPass(scene, camera);

const bloomPass = new THREE.UnrealBloomPass(
  new THREE.Vector2(window.innerWidth, window.innerHeight),
  1.5,
  0.4,
  0.85
);
bloomPass.threshold = 0;
bloomPass.strength = 0.6;

const composer = new THREE.EffectComposer(renderer);
composer.setPixelRatio(pixelRatio);
composer.addPass(renderScene);
composer.addPass(bloomPass);

const controls = new THREE.TrackballControls(camera, renderer.domElement);
controls.noPan = true;
controls.rotateSpeed = 1.5;
controls.maxDistance = 3.5;
controls.minDistance = 0.3;

const group = new THREE.Group();
scene.add(group);

const sparkles = [];
const sparklesGeometry = new THREE.BufferGeometry();
const sparklesMaterial = new THREE.ShaderMaterial({
  uniforms: {
    pointTexture: {
      value: new THREE.TextureLoader().load(
        "//repo.bfw.wiki/bfwrepo/icon/5de90f964d501.png"
      )
    }
  },
  vertexShader: document.getElementById("vertexshader").textContent,
  fragmentShader: document.getElementById("fragmentshader").textContent,
  blending: THREE.AdditiveBlending,
  alphaTest: 1.0,
  transparent: true
});
const points = new THREE.Points(sparklesGeometry, sparklesMaterial);
group.add(points);

let sampler = null;
const lines = [];
let colors = [
  new THREE.Color("#CFD6DE"),
  new THREE.Color("#1EE3CF"),
  new THREE.Color("#6B48FF"),
  new THREE.Color("#125D98")
];
let galaxyColors = [
  new THREE.Color("#CFD6DE").multiplyScalar(0.5),
  new THREE.Color("#1EE3CF").multiplyScalar(0.5),
  new THREE.Color("#6B48FF").multiplyScalar(0.5),
  new THREE.Color("#125D98").multiplyScalar(0.5)
];
function dots() {
  sampler = new THREE.MeshSurfaceSampler(skull).build();

  for (let i = 0; i < 8; i++) {
    const linesMaterial = new THREE.LineBasicMaterial({
      color: colors[i % 4],
      transparent: true,
      opacity: 1
    });
    const linesMesh = new THREE.Line(new THREE.BufferGeometry(), linesMaterial);
    linesMesh.coordinates = [];
    linesMesh.previous = null;
    lines.push(linesMesh);
    group.add(linesMesh);
  }
  requestAnimationFrame(render);
}

let skull = null;
const loader = new THREE.OBJLoader();
loader.load(
  "//repo.bfw.wiki/bfwrepo/threemodel/skull_model.obj",
  (obj) => {
    skull = obj.children[0];
    dots();
  },
  (xhr) => console.log((xhr.loaded / xhr.total) * 100 + "% loaded"),
  (err) => console.log("An error happened", err)
);

let safe = 0;
const p1 = new THREE.Vector3();
function nextDot(line) {
  let ok = false;.........完整代码请登录后点击上方下载按钮下载查看

网友评论0