three实现三维场景字母文字下落地面动画效果代码
代码语言:html
所属分类:三维
代码描述:three实现三维场景字母文字下落地面动画效果代码,无数字母缓慢下落到纸面上排列整齐。
代码标签: three 三维 场景 字母 文字 下落 地面 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> body{ overflow: hidden; margin: 0; } </style> </head> <body > <script type="importmap"> { "imports": { "three": "//repo.bfw.wiki/bfwrepo/js/module/three/build/163/three.module.js", "three/addons/": "//repo.bfw.wiki/bfwrepo/js/module/three/examples/163/jsm/" } } </script> <script type="module"> import * as THREE from "three"; import { OrbitControls } from "three/addons/controls/OrbitControls.js"; import * as TWEEN from "three/addons/libs/tween.module.js"; import { SimplexNoise } from "three/addons/math/SimplexNoise.js"; let simplex = new SimplexNoise(); class TextTerrain extends THREE.Object3D { constructor(anisotropy) { super(); let alphabet = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"]; let textTexture = (() => { let c = document.createElement("canvas"); let ctx = c.getContext("2d"); let texSize = 2048; c.width = texSize; c.height = texSize; ctx.clearRect(0, 0, texSize, texSize); let dim = 8; let dimStep = texSize / dim; for (let i = 0; i < alphabet.length; i++) { //console.log(alphabet.length) let tileX = i % 8; let tileY = Math.floor(i / 8); let x = (tileX + 0.5) * dimStep; let y = texSize - (tileY + 0.5) * dimStep; //console.log(x, y); ctx.fillStyle = `rgba(0, 0, 0, 1)`; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.font = `bold ${dimStep * 0.9}px Arial`; ctx.fillText(alphabet[i], x, y); } let tex = new THREE.CanvasTexture(c); tex.colorSpace = "srgb"; tex.anisotropy = anisotropy; return tex; })(); let tileDim = 200; let g = new THREE.PlaneGeometry(); g.setAttribute("letterIdx", new THREE.InstancedBufferAttribute(new Float32Array(Array.from({ length: tileDim * tileDim }, () => { return THREE.MathUtils.randInt(0, alphabet.length - 1) })), 1)); //console.log(g); let m = new THREE.MeshBasicMaterial({ map: textTexture, transparent: true, alphaTest: 0.01, side: THREE.DoubleSide, //forceSinglePass: true, onBeforeCompile: shader => { shader.vertexShader = ` attribute float letterIdx; varying float vLetterIdx; ${shader.vertexShader} `.replace( `#include <uv_vertex>`, `#include <uv_vertex> vLetterIdx = letterIdx; ` ); //console.log(shader.vertexShader); shader.fragmentShader = ` varying float vLetterIdx; ${shader.fragmentShader} `.replace( `#include <map_fragment>`, ` float letterIdx = floor(vLetterIdx + 0.1); float tileStep = 1. / 8.; float u = mod(letterIdx, 8.); float v = floor(letterIdx / 8.); vec2 iUv = (vec2(u, v) + vMapUv) * tileStep; vec4 sampledDiffuseColor = texture2D( map, iUv ); diffuseColor *= sampledDiffuseColor; ` ); //console.log(shader.fragmentShader); } }); let io = new THREE.InstancedMesh(g, m, tileDim * tileDim); this.instancedMesh = io; this.dummy = new THREE.Object3D(); this.finals = []; let tri = new THREE.Triangle(); let n = new THREE.Vector3(); let la = new THREE.Vector3(); function getY(x, z) { return simplex.noise(x * 0.01, z * 0.01) * 7.5; } let setFinals = () => { let y0 = getY(this.dummy.position.x, this.dummy.position.z); let y1 = getY(this.dummy.position.x, this.dummy.position.z - 1); le.........完整代码请登录后点击上方下载按钮下载查看
网友评论0