1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| let material; let dracoLoader = new DRACOLoader() dracoLoader.setDecoderPath("./draco/") const loader = new GLTFLoader() loader.setDRACOLoader(dracoLoader) loader.load("字体模型路径", (gltf) => { let text = gltf.scene.children[0] scene.add(text)
let geometry = text.geometry const position = geometry.attributes.position const vertexCount = position.count const triangleCount = vertexCount / 3 cosnt randomDirections = [] const randomStrengths = [] for (let i = 0; i< triangleCount; i++){ const dir = new THREE.Vector3( Math.random()*2 -1, Math.random()*2 -1, Math.random()*2 -1, ).normalize().toArray() randomDirections.push(dir[0],dir[1],dir[2]) const str = Math.random() randomStrengths.push(str,str,str) } const randomDirectionsAttribute = new THREE.Float32BufferAttribute( new Float32Array(randomDirections),3) geometry.setAttribute("randomDirection", randomDirectionsAttribute) const randomStrengthsAttribute = new THREE.Float32BufferAttribute( new Float32Array(randomStrengths),1) geometry.setAttribute("randomStrength", randomStrengthsAttribute) material = new THREE.ShaderMaterial({ uniforms:{time: { value: 0}}, vertexShader: ` attributr vec3 randomDirection attributr float randomStrength uniform float time; varying vec3 vPosition
void main() { vPosition = position vec3 pos = position.xyz pos += randomDirection * randomStrength * sin(time) gl_Position = projectionMatrix * modelViewMatrix * vec4(pos.xyz, 1.0) } `, fragmentShader: ` varying vec3 vPosition void main() { vec3 color = normalize(vPosition)*0.5 +0.5 color.z = color.z * 0.3 gl_FragColor = vec4(mixedColor * color * dProd, 1.0) } `, side: THREE.DoubleSide, transparent: true }) })
|