Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="relative w-screen h-screen">
  <div class="kinetic-text w-full h-full bg-blue-1"></div>
  <div class="font">
    <font>
      <info face="OpenSans-SemiBold" size="42" bold="0" italic="0" charset="" unicode="1" stretchH="100" smooth="1" aa="1" padding="0,0,0,0" spacing="0,0" />
      <common lineHeight="45" base="34" scaleW="2048" scaleH="2048" pages="1" packed="0" alphaChnl="0" redChnl="0" greenChnl="0" blueChnl="0" />
      <pages>
        <page id="0" file="OpenSans-SemiBold.png" />
      </pages>
      <distanceField fieldType="msdf" distanceRange="4" />
      <chars count="95">
        <char id="87" index="58" char="W" width="43" height="34" xoffset="-2" yoffset="4" xadvance="40" chnl="15" x="0" y="0" page="0" />
        <char id="64" index="35" char="@" width="37" height="38" xoffset="0" yoffset="4" xadvance="38" chnl="15" x="44" y="0" page="0" />
        <char id="81" index="52" char="Q" width="32" height="42" xoffset="0" yoffset="4" xadvance="33" chnl="15" x="0" y="35" page="0" />
        <char id="37" index="8" char="%" width="37" height="35" xoffset="0" yoffset="4" xadvance="36" chnl="15" x="82" y="0" page="0" />
        <char id="77" index="48" char="M" width="35" height="34" xoffset="2" yoffset="4" xadvance="39" chnl="15" x="120" y="0" page="0" />
        <char id="38" index="9" char="&amp;" width="33" height="35" xoffset="0" yoffset="4" xadvance="31" chnl="15" x="156" y="0" page="0" />
        <char id="79" index="50" char="O" width="32" height="35" xoffset="0" yoffset="4" xadvance="33" chnl="15" x="120" y="35" page="0" />
        <char id="65" index="36" char="A" width="32" height="34" xoffset="-2" yoffset="4" xadvance="28" chnl="15" x="82" y="36" page="0" />
        <char id="88" index="59" char="X" width="30" height="34" xoffset="-2" yoffset="4" xadvance="26" chnl="15" x="33" y="39" page="0" />
        <char id="86" index="57" char="V" width="30" height="34" xoffset="-2" yoffset="4" xadvance="26" chnl="15" x="190" y="0" page="0" />
        <char id="71" index="42" char="G" width="29" height="35" xoffset="0" yoffset="4" xadvance="30" chnl="15" x="221" y="0" page="0" />
        <char id="109" index="80" char="m" width="37" height="27" xoffset="1" yoffset="11" xadvance="40" chnl="15" x="251" y="0" page="0" />
        <char id="121" index="92" char="y" width="27" height="37" xoffset="-2" yoffset="11" xadvance="23" chnl="15" x="190" y="35" page="0" />
        <char id="119" index="90" char="w" width="37" height="27" xoffset="-2" yoffset="11" xadvance="34" chnl="15" x="64" y="71" page="0" />
        <char id="35" index="6" char="#" width="29" height="34" xoffset="-1" yoffset="4" xadvance="27" chnl="15" x="33" y="74" page="0" />
        <char id="89" index="60" char="Y" width="29" height="34" xoffset="-2" yoffset="4" xadvance="25" chnl="15" x="0" y="78" page="0" />
        <char id="78" index="49" char="N" width="29" height="34" xoffset="2" yoffset="4" xadvance="33" chnl="15" x="153" y="36" page="0" />
        <char id="103" index="74" char="g" width="26" height="37" xoffset="-2" yoffset="11" xadvance="23" chnl="15" x="102" y="71" page="0" />
        <char id="68" index="39" char="D" width="28" height="34" xoffset="2" yoffset="4" xadvance="31" chnl="15" x="63" y="99" page="0" />
        <char id="85" index="56" char="U" width="28" height="34" xoffset="2" yoffset="4" xadvance="31" chnl="15" x="30" y="109" page="0" />
        <char id="72" index="43" char="H" width="28" height="34" xoffset="2" yoffset="4" xadvance="32" chnl="15" x="0" y="113" page="0" />
        <char id="67" index="38" char="C" width="27" height="35" xoffset="0" yoffset="4" xadvance="27" chnl="15" x="129" y="71" page="0" />
        <char id="112" index="83" char="p" width="25" height="37" xoffset="1" yoffset="11" xadvance="26" chnl="15" x="157" y="71" page="0" />
        <char id="113" index="84" char="q" width="25" height="37" xoffset="0" yoffset="11" xadvance="26" chnl="15" x="129" y="107" page="0" />
        <char id="84" index="55" char="T" width="27" height="34" xoffset="-1" yoffset="4" xadvance="24" chnl="15" x="92" y="109" page="0" />
        <char id="75" index="46" char="K" width="27" height="34" xoffset="2" yoffset="4" xadvance="27" chnl="15" x="59" y="134" page="0" />
        <char id="36" index="7" char="$" width="24" height="38" xoffset="0" yoffset="2" xadvance="24" chnl="15" x="29" y="144" page="0" />
        <char id="98" index="69" char="b" width="25" height="36" xoffset="1" yoffset="2" xadvance="26" chnl="15" x="0" y="148" page="0" />
        <char id="100" index="71" char="d" width="25" height="36" xoffset="0" yoffset="2" xadvance="26" chnl="15" x="87" y="144" page="0" />
        <char id="82" index="53" char="R" width="26" height="34" xoffset="2" yoffset="4" xadvance="27" chnl="15" x="54" y="169" page="0" />
        <char id="52" index="23" char="4" width="26" height="34" xoffset="-1" yoffset="4" xadvance="24" chnl="15" x="26" y="183" page="0" />
        <char id="104" index="75" char="h" width="24" height="36" xoffset="1" yoffset="2" xadvance="27" chnl="15" x="0" y="185" page="0" />
        <char id="107" index="78" char="k" width="24" height="36" xoffset="1" yoffset="2" xadvance="24" chnl="15" x="289" y="0" page="0" />
        <char id="66" index="37" char="B" width="25" height="34" xoffset="2" yoffset="4" xadvance="28" chnl="15" x="251" y="28" page="0" />
        <char id="90" index="61" char="Z" width="25" height="34" xoffset="-1" yoffset="4" xadvance="24" chnl="15" x="218" y="36" page="0" />
        <char id="55" index="26" char="7" width="25" height="34" xoffset="0" yoffset="4" xadvance="24" chnl="15" x="314" y="0" page="0" />
        <char id="51" index="22" char="3" width="24" height="35" xoffset="0" yoffset="4" xadvance="24" chnl="15" x="340" y="0" page="0" />
        <char id="57" index="28" char="9" width="24" height="35" xoffset="0" yoffset="4" xadvance="24" chnl="15" x="314" y="35" page="0" />
        <char id="56" index="27" char="8" width="24" height="35" xoffset="0" yoffset="4" xadvance="24" chnl="15" x="277" y="37" page="0" />
        <char id="54" index="25" char="6" width="24" height="35" xoffset="0" yoffset="4" xadvance="24" chnl="15" x="244" y="63" page="0" />
        <char id="48" index="19" char="0" width="24" height="35" xoffset="0" yoffset="4" xadvance="24" chnl="15" x="218" y="71" page="0" />
        <char id="80" index="51" char="P" width="24" height="34" xoffset="2" yoffset="4" xadvance="26" chnl="15" x="183" y="73" page="0" />
        <char id="50" index="21" char="2" width="24" height="34" xoffset="0" yoffset="4" xadvance="24" chnl="15" x="365" y="0" page="0" />
        <char id="83" index="54" char="S" width="23" height="35" xoffset="0" yoffset="4" xadvance="23" chnl="15" x="390" y="0" page="0" />
        <char id="53" index="24" char="5" width="23" height="34" xoffset="0" yoffset="4" xadvance="24" chnl="15" x="365" y="35" page="0" />
        <char id="63" index="34" char="?" width="22" height="35" xoffset="-2" yoffset="4" xadvance="19" chnl="15" x="339" y="36" page="0" />
        <char id="76" index="47" char="L" width="22" height="34" xoffset="2" yoffset="4" xadvance="23" chnl="15" x="302" y="71" page="0" />
        <char id="123" index="94" char="{" width="18" height="41" xoffset="-1" yoffset="4" xadvance="16" chnl="15" x="269" y="73" page="0" />
        <char id="125" index="96" char="}" width="18" height="41" xoffset="-1" yoffset="4" xadvance="16" chnl="15" x="243" y="99" page="0" />
        <char id="102" index="73" char="f" width="20" height="36" xoffset="-1" yoffset="2" xadvance="15" chnl="15" x="208" y="107" page="0" />
        <char id="69" index="40" char="E" width="21" height="34" xoffset="2" yoffset="4" xadvance="23" chnl="15" x="183" y="108" page="0" />
        <char id="70" index="41" char="F" width="21" height="34" xoffset="2" yoffset="4" xadvance="22" chnl="15" x="155" y="109" page="0" />
        <char id="118" index="89" char="v" width="26" height="27" xoffset="-2" yoffset="11" xadvance="22" chnl="15" x="177" y="143" page="0" />
        <char id="120" index="91" char="x" width="26" height="27" xoffset="-1" yoffset="11" xadvance="23" chnl="15" x="113" y="145" page="0" />
        <char id="111" index="82" char="o" width="25" height="28" xoffset="0" yoffset="11" xadvance="26" chnl="15" x="140" y="145" page="0" />
        <char id="92" index="63" char="\" width="20" height="34" xoffset="-2" yoffset="4" xadvance="16" chnl="15" x="113" y="173" page="0" />
        <char id="47" index="18" char="/" width="20" height="34" xoffset="-2" yoffset="4" xadvance="16" chnl="15" x="81" y="181" page="0" />
        <char id="106" index="77" char="j" width="15" height="45" xoffset="-4" yoffset="3" xadvance="12" chnl="15" x="53" y="204" page="0" />
        <char id="74" index="45" char="J" width="16" height="42" xoffset="-5" yoffset="4" xadvance="13" chnl="15" x="25" y="218" page="0" />
        <char id="101" index="72" char="e" width="24" height="28" xoffset="0" yoffset="11" xadvance="24" chnl="15" x="0" y="222" page="0" />
        <char id="110" index="81" char="n" width="24" height="27" xoffset="1" yoffset="11" xadvance="27" chnl="15" x="0" y="251" page="0" />
        <char id="117" index="88" char="u" width="24" height="27" xoffset="1" yoffset="11" xadvance="27" chnl="15" x="414" y="0" page="0" />
        <char id="97" index="68" char="a" width="23" height="28" xoffset="0" yoffset="11" xadvance="24" chnl="15" x="439" y="0" page="0" />
        <char id="116" index="87" char="t" width="19" height="32" xoffset="-1" yoffset="6" xadvance="17" chnl="15" x="414" y="28" page="0" />
        <char id="60" index="31" char="&lt;" width="24" height="25" xoffset="0" yoffset="8" xadvance="24" chnl="15" x="389" y="36" page="0" />
        <char id="62" index="33" char=">" width="24" height="25" xoffset="0" yoffset="8" xadvance="24" chnl="15" x="463" y="0" page="0" />
        <char id="115" index="86" char="s" width="21" height="28" xoffset="0" yoffset="11" xadvance="20" chnl="15" x="488" y="0" page="0" />
        <char id="99" index="70" char="c" width="21" height="28" xoffset="0" yoffset="11" xadvance="21" chnl="15" x="463" y="26" page="0" />
        <char id="49" index="20" char="1" width="17" height="34" xoffset="1" yoffset="4" xadvance="24" chnl="15" x="434" y="29" page="0" />
        <char id="43" index="14" char="+" width="24" height="24" xoffset="0" yoffset="9" xadvance="24" chnl="15" x="389" y="62" page="0" />
        <char id="94" index="65" char="^" width="25" height="23" xoffset="-1" yoffset="4" xadvance="23" chnl="15" x="362" y="70" page="0" />
        <char id="41" index="12" char=")" width="14" height="41" xoffset="-1" yoffset="4" xadvance="13" chnl="15" x="414" y="61" page="0" />
        <char id="40" index="11" char="(" width="14" height="41" xoffset="0" yoffset="4" xadvance="13" chnl="15" x="388" y="87" page="0" />
        <char id="93" index="64" char="]" width="14" height="41" xoffset="-1" yoffset="4" xadvance="14" chnl="15" x="325" y="72" page="0" />
        <char id="91" index="62" char="[" width="14" height="41" xoffset="1" yoffset="4" xadvance="14" chnl="15" x="340" y="72" page="0" />
        <char id="122" index="93" char="z" width="21" height="27" xoffset="-1" yoffset="11" xadvance="20" chnl="15" x="355" y="94" page="0" />
        <char id="42" index="13" char="*" width="24" height="23" xoffset="0" yoffset="2" xadvance="23" chnl="15" x="288" y="106" page="0" />
        <char id="114" index="85" char="r" width="18" height="27" xoffset="1" yoffset="11" xadvance="18" chnl="15" x="262" y="115" page="0" />
        <char id="61" index="32" char="=" width="24" height="16" xoffset="0" yoffset="13" xadvance="24" chnl="15" x="229" y="141" page="0" />
        <char id="124" index="95" char="|" width="8" height="46" xoffset="8" yoffset="2" xadvance="23" chnl="15" x="166" y="144" page="0" />
        <char id="59" index="30" char=";" width="11" height="33" xoffset="-1" yoffset="11" xadvance="12" chnl="15" x="302" y="37" page="0" />
        <char id="33" index="4" char="!" width="10" height="35" xoffset="1" yoffset="4" xadvance="12" chnl="15" x="102" y="181" page="0" />
        <char id="108" index="79" char="l" width="9" height="36" xoffset="1" yoffset="2" xadvance="12" chnl="15" x="42" y="218" page="0" />
        <char id="105" index="76" char="i" width="9" height="35" xoffset="1" yoffset="3" xadvance="12" chnl="15" x="452" y="29" page="0" />
        <char id="73" index="918" char="I" width="9" height="34" xoffset="2" yoffset="4" xadvance="13" chnl="15" x="403" y="87" page="0" />
        <char id="58" index="29" char=":" width="10" height="28" xoffset="1" yoffset="11" xadvance="12" chnl="15" x="377" y="94" page="0" />
        <char id="34" index="5" char="&quot;" width="17" height="15" xoffset="1" yoffset="4" xadvance="18" chnl="15" x="64" y="39" page="0" />
        <char id="126" index="97" char="~" width="24" height="10" xoffset="0" yoffset="16" xadvance="24" chnl="15" x="204" y="144" page="0" />
        <char id="96" index="67" char="`" width="14" height="11" xoffset="5" yoffset="2" xadvance="25" chnl="15" x="64" y="55" page="0" />
        <char id="95" index="66" char="_" width="22" height="7" xoffset="-2" yoffset="38" xadvance="18" chnl="15" x="429" y="64" page="0" />
        <char id="44" index="15" char="," width="11" height="14" xoffset="-1" yoffset="29" xadvance="11" chnl="15" x="69" y="204" page="0" />
        <char id="39" index="10" char="'" width="9" height="15" xoffset="1" yoffset="4" xadvance="10" chnl="15" x="208" y="73" page="0" />
        <char id="45" index="16" char="-" width="15" height="8" xoffset="-1" yoffset="21" xadvance="14" chnl="15" x="81" y="216" page="0" />
        <char id="46" index="17" char="." width="10" height="10" xoffset="1" yoffset="28" xadvance="12" chnl="15" x="313" y="106" page="0" />
        <char id="32" index="3" char=" " width="0" height="0" xoffset="-2" yoffset="34" xadvance="11" chnl="15" x="153" y="35" page="0" />
      </chars>
      <kernings count="0" />
    </font>
  </div>
</div>
              
            
!

CSS

              
                body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  background: hsl(240, 56%, 98%);
}

:root {
  --blue-color-1: #2c3e50;
}

.bg-blue-1 {
  background: var(--blue-color-1);
}

              
            
!

JS

              
                import * as THREE from "https://cdn.skypack.dev/three@0.124.0";
import ky from "https://cdn.skypack.dev/kyouka@1.2.2";
import { OrbitControls } from "https://cdn.skypack.dev/three@0.124.0/examples/jsm/controls/OrbitControls";
import { GLTFLoader } from "https://cdn.skypack.dev/three@0.124.0/examples/jsm/loaders/GLTFLoader";
import { FBXLoader } from "https://cdn.skypack.dev/three@0.124.0/examples/jsm/loaders/FBXLoader";
import { EffectComposer } from "https://cdn.skypack.dev/three@0.124.0/examples/jsm/postprocessing/EffectComposer";
import Stats from "https://cdn.skypack.dev/three@0.124.0/examples/jsm/libs/stats.module";
import * as dat from "https://cdn.skypack.dev/dat.gui@0.7.7";
import createGeometry from "https://cdn.skypack.dev/three-bmfont-text@3.0.1";
import MSDFShader from "https://cdn.skypack.dev/three-bmfont-text@3.0.1/shaders/msdf";
import parseBmfontXml from "https://cdn.skypack.dev/parse-bmfont-xml@1.1.4";

interface Params {
  meshName: string;
  velocity: number;
  shadow: number;
  color: string;
  frequency: number;
  text: string;
  cameraZ: number;
}

const calcAspect = (el: HTMLElement) => el.clientWidth / el.clientHeight;

const font = parseBmfontXml(document.querySelector(".font").innerHTML);
const fontAtlas = "https://i.loli.net/2021/02/20/DcEhuYNjxCgeU42.png";

const kineticTextTorusKnotVertexShader = `
varying vec2 vUv;
varying vec3 vPosition;

void main(){
    vec4 modelPosition=modelMatrix*vec4(position,1.);
    vec4 viewPosition=viewMatrix*modelPosition;
    vec4 projectedPosition=projectionMatrix*viewPosition;
    gl_Position=projectedPosition;
    
    vUv=uv;
    vPosition=position;
}
`;

const kineticTextTorusKnotFragmentShader = `
uniform sampler2D uTexture;
uniform float uTime;
uniform float uVelocity;
uniform float uShadow;

varying vec2 vUv;
varying vec3 vPosition;

vec3 invert(vec3 n){
    return 1.-n;
}

void main(){
    vec2 repeat=vec2(12.,3.);
    vec2 repeatedUv=vUv*repeat;
    vec2 displacement=vec2(uTime*uVelocity,0.);
    vec2 uv=fract(repeatedUv+displacement);
    vec3 texture=texture2D(uTexture,uv).rgb;
    // texture*=vec3(uv.x,uv.y,1.);
    float shadow=clamp(vPosition.z/uShadow,0.,1.);// farther darker (to 0).
    vec3 color=vec3(texture*shadow);
    gl_FragColor=vec4(color,1.);
}
`;

const kineticTextSphereVertexShader = `
varying vec2 vUv;
varying vec3 vPosition;

void main(){
    vec4 modelPosition=modelMatrix*vec4(position,1.);
    vec4 viewPosition=viewMatrix*modelPosition;
    vec4 projectedPosition=projectionMatrix*viewPosition;
    gl_Position=projectedPosition;
    
    vUv=uv;
    vPosition=position;
}
`;

const kineticTextSphereFragmentShader = `
uniform sampler2D uTexture;
uniform float uTime;
uniform float uVelocity;
uniform float uShadow;

varying vec2 vUv;
varying vec3 vPosition;

vec3 invert(vec3 n){
    return 1.-n;
}

void main(){
    vec2 repeat=vec2(12.,12.);
    vec2 repeatedUv=vUv*repeat;
    vec2 displacement=vec2(sin(vUv.y)*5.,uTime*uVelocity);
    vec2 uv=fract(repeatedUv+displacement);
    vec3 texture=texture2D(uTexture,uv).rgb;
    // texture*=vec3(uv.x,uv.y,1.);
    float shadow=vPosition.z/uShadow;
    vec3 color=vec3(texture*shadow);
    gl_FragColor=vec4(color,1.);
}
`;

const kineticTextPlaneVertexShader = `
uniform float uTime;
uniform float uVelocity;
uniform float uFrequency;

varying vec2 vUv;
varying vec3 vPosition;
varying float vWave;

void main(){
    vec3 pos=position;
    float displacement=sin((pos.x-pos.y)*uFrequency-uTime*uVelocity);
    pos.z+=displacement;
    vec4 modelPosition=modelMatrix*vec4(pos,1.);
    vec4 viewPosition=viewMatrix*modelPosition;
    vec4 projectedPosition=projectionMatrix*viewPosition;
    gl_Position=projectedPosition;
    
    vUv=uv;
    vPosition=pos;
    vWave=pos.z;
}
`;

const kineticTextPlaneFragmentShader = `
uniform sampler2D uTexture;
uniform float uTime;
uniform float uVelocity;
uniform float uShadow;

varying vec2 vUv;
varying vec3 vPosition;
varying float vWave;

vec3 invert(vec3 n){
    return 1.-n;
}

// map函数:将本身的范围转换为另一个范围
// https://gist.github.com/companje/29408948f1e8be54dd5733a74ca49bb9
// https://stackoverflow.com/questions/17134839/how-does-the-map-function-in-processing-work
float map(float value,float min1,float max1,float min2,float max2){
    return min2+(value-min1)*(max2-min2)/(max1-min1);
}

void main(){
    vec2 repeat=vec2(4.,16.);
    vec2 repeatedUv=vUv*repeat;
    vec2 uv=fract(repeatedUv);
    vec3 texture=texture2D(uTexture,uv).rgb;
    // texture*=vec3(uv.x,uv.y,1.);
    float wave=vWave;
    wave=map(wave,-1.,1.,0.,.1);
    float shadow=1.-wave;
    vec3 color=vec3(texture*shadow);
    gl_FragColor=vec4(color,1.);
}
`;

const kineticTextTorusVertexShader = `
varying vec2 vUv;
varying vec3 vPosition;

void main(){
    vec4 modelPosition=modelMatrix*vec4(position,1.);
    vec4 viewPosition=viewMatrix*modelPosition;
    vec4 projectedPosition=projectionMatrix*viewPosition;
    gl_Position=projectedPosition;
    
    vUv=uv;
    vPosition=position;
}
`;

const kineticTextTorusFragmentShader = `
uniform sampler2D uTexture;
uniform float uTime;
uniform float uVelocity;
uniform float uShadow;

varying vec2 vUv;
varying vec3 vPosition;

vec3 invert(vec3 n){
    return 1.-n;
}

void main(){
    vec2 repeat=vec2(6.,6.);
    vec2 repeatedUv=vUv*repeat;
    vec2 displacement=vec2(0.,uTime*uVelocity);
    vec2 uv=fract(repeatedUv+displacement);
    vec3 texture=texture2D(uTexture,uv).rgb;
    // texture*=vec3(uv.x,uv.y,1.);
    float shadow=vPosition.z/uShadow;
    vec3 color=vec3(texture*shadow);
    gl_FragColor=vec4(color,1.);
}
`;

const kineticTextCylinderVertexShader = `
varying vec2 vUv;
varying vec3 vPosition;

void main(){
    vec4 modelPosition=modelMatrix*vec4(position,1.);
    vec4 viewPosition=viewMatrix*modelPosition;
    vec4 projectedPosition=projectionMatrix*viewPosition;
    gl_Position=projectedPosition;
    
    vUv=uv;
    vPosition=position;
}
`;

const kineticTextCylinderFragmentShader = `
uniform sampler2D uTexture;
uniform float uTime;
uniform float uVelocity;
uniform float uShadow;

varying vec2 vUv;
varying vec3 vPosition;

vec3 invert(vec3 n){
    return 1.-n;
}

void main(){
    vec2 repeat=vec2(12.,12.);
    vec2 repeatedUv=vUv*repeat;
    vec2 displacement=vec2(uTime*uVelocity+vUv.y,0.);
    vec2 uv=fract(repeatedUv+displacement);
    vec3 texture=texture2D(uTexture,uv).rgb;
    // texture*=vec3(uv.x,uv.y,1.);
    float shadow=vPosition.z/uShadow;
    vec3 color=vec3(texture*shadow);
    gl_FragColor=vec4(color,1.);
}
`;

class Base {
  debug: boolean;
  container: HTMLElement | null;
  scene!: THREE.Scene;
  camera!: THREE.PerspectiveCamera | THREE.OrthographicCamera;
  rendererParams!: Record<string, any>;
  perspectiveCameraParams!: Record<string, any>;
  orthographicCameraParams!: Record<string, any>;
  cameraPosition!: THREE.Vector3;
  lookAtPosition!: THREE.Vector3;
  renderer!: THREE.WebGLRenderer;
  controls!: OrbitControls;
  mousePos!: THREE.Vector2;
  raycaster!: THREE.Raycaster;
  sound!: THREE.Audio;
  stats!: Stats;
  composer!: EffectComposer;
  constructor(sel: string, debug = false) {
    this.debug = debug;
    this.container = document.querySelector(sel);
    this.perspectiveCameraParams = {
      fov: 75,
      near: 0.1,
      far: 100
    };
    this.orthographicCameraParams = {
      zoom: 2,
      near: -100,
      far: 1000
    };
    this.cameraPosition = new THREE.Vector3(0, 3, 10);
    this.lookAtPosition = new THREE.Vector3(0, 0, 0);
    this.rendererParams = {
      outputEncoding: THREE.LinearEncoding,
      config: {
        alpha: true,
        antialias: true
      }
    };
    this.mousePos = new THREE.Vector2(0, 0);
  }
  // 初始化
  init() {
    this.createScene();
    this.createPerspectiveCamera();
    this.createRenderer();
    this.createMesh({});
    this.createLight();
    this.createOrbitControls();
    this.addListeners();
    this.setLoop();
  }
  // 创建场景
  createScene() {
    const scene = new THREE.Scene();
    if (this.debug) {
      scene.add(new THREE.AxesHelper());
      const stats = Stats();
      this.container!.appendChild(stats.dom);
      this.stats = stats;
    }
    this.scene = scene;
  }
  // 创建透视相机
  createPerspectiveCamera() {
    const { perspectiveCameraParams, cameraPosition, lookAtPosition } = this;
    const { fov, near, far } = perspectiveCameraParams;
    const aspect = calcAspect(this.container!);
    const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
    camera.position.copy(cameraPosition);
    camera.lookAt(lookAtPosition);
    this.camera = camera;
  }
  // 创建正交相机
  createOrthographicCamera() {
    const { orthographicCameraParams, cameraPosition, lookAtPosition } = this;
    const { left, right, top, bottom, near, far } = orthographicCameraParams;
    const camera = new THREE.OrthographicCamera(
      left,
      right,
      top,
      bottom,
      near,
      far
    );
    camera.position.copy(cameraPosition);
    camera.lookAt(lookAtPosition);
    this.camera = camera;
  }
  // 更新正交相机参数
  updateOrthographicCameraParams() {
    const { container } = this;
    const { zoom, near, far } = this.orthographicCameraParams;
    const aspect = calcAspect(container!);
    this.orthographicCameraParams = {
      left: -zoom * aspect,
      right: zoom * aspect,
      top: zoom,
      bottom: -zoom,
      near,
      far,
      zoom
    };
  }
  // 创建渲染
  createRenderer(useWebGL1 = false) {
    const { rendererParams } = this;
    const { outputEncoding, config } = rendererParams;
    const renderer = !useWebGL1
      ? new THREE.WebGLRenderer(config)
      : new THREE.WebGL1Renderer(config);
    renderer.setSize(this.container!.clientWidth, this.container!.clientHeight);
    renderer.outputEncoding = outputEncoding;
    this.resizeRendererToDisplaySize();
    this.container?.appendChild(renderer.domElement);
    this.renderer = renderer;
    this.renderer.setClearColor(0x000000, 0);
  }
  // 允许投影
  enableShadow() {
    this.renderer.shadowMap.enabled = true;
  }
  // 调整渲染器尺寸
  resizeRendererToDisplaySize() {
    const { renderer } = this;
    if (!renderer) {
      return;
    }
    const canvas = renderer.domElement;
    const pixelRatio = window.devicePixelRatio;
    const { clientWidth, clientHeight } = canvas;
    const width = (clientWidth * pixelRatio) | 0;
    const height = (clientHeight * pixelRatio) | 0;
    const isResizeNeeded = canvas.width !== width || canvas.height !== height;
    if (isResizeNeeded) {
      renderer.setSize(width, height, false);
    }
    return isResizeNeeded;
  }
  // 创建网格
  createMesh(
    meshObject: MeshObject,
    container: THREE.Scene | THREE.Mesh = this.scene
  ) {
    const {
      geometry = new THREE.BoxGeometry(1, 1, 1),
      material = new THREE.MeshStandardMaterial({
        color: new THREE.Color("#d9dfc8")
      }),
      position = new THREE.Vector3(0, 0, 0)
    } = meshObject;
    const mesh = new THREE.Mesh(geometry, material);
    mesh.position.copy(position);
    container.add(mesh);
    return mesh;
  }
  // 创建光源
  createLight() {
    const dirLight = new THREE.DirectionalLight(
      new THREE.Color("#ffffff"),
      0.5
    );
    dirLight.position.set(0, 50, 0);
    this.scene.add(dirLight);
    const ambiLight = new THREE.AmbientLight(new THREE.Color("#ffffff"), 0.4);
    this.scene.add(ambiLight);
  }
  // 创建轨道控制
  createOrbitControls() {
    const controls = new OrbitControls(this.camera, this.renderer.domElement);
    const { lookAtPosition } = this;
    controls.target.copy(lookAtPosition);
    controls.update();
    this.controls = controls;
  }
  // 监听事件
  addListeners() {
    this.onResize();
  }
  // 监听画面缩放
  onResize() {
    window.addEventListener("resize", (e) => {
      if (this.camera instanceof THREE.PerspectiveCamera) {
        const aspect = calcAspect(this.container!);
        const camera = this.camera as THREE.PerspectiveCamera;
        camera.aspect = aspect;
        camera.updateProjectionMatrix();
      } else if (this.camera instanceof THREE.OrthographicCamera) {
        this.updateOrthographicCameraParams();
        const camera = this.camera as THREE.OrthographicCamera;
        const {
          left,
          right,
          top,
          bottom,
          near,
          far
        } = this.orthographicCameraParams;
        camera.left = left;
        camera.right = right;
        camera.top = top;
        camera.bottom = bottom;
        camera.near = near;
        camera.far = far;
        camera.updateProjectionMatrix();
      }
      this.renderer.setSize(
        this.container!.clientWidth,
        this.container!.clientHeight
      );
    });
  }
  // 动画
  update() {
    console.log("animation");
  }
  // 渲染
  setLoop() {
    this.renderer.setAnimationLoop(() => {
      this.resizeRendererToDisplaySize();
      this.update();
      if (this.controls) {
        this.controls.update();
      }
      if (this.stats) {
        this.stats.update();
      }
      if (this.composer) {
        this.composer.render();
      } else {
        this.renderer.render(this.scene, this.camera);
      }
    });
  }
  // 创建文本
  createText(
    text = "",
    config: THREE.TextGeometryParameters,
    material: THREE.Material = new THREE.MeshStandardMaterial({
      color: "#ffffff"
    })
  ) {
    const geo = new THREE.TextGeometry(text, config);
    const mesh = new THREE.Mesh(geo, material);
    return mesh;
  }
  // 创建音效源
  createAudioSource() {
    const listener = new THREE.AudioListener();
    this.camera.add(listener);
    const sound = new THREE.Audio(listener);
    this.sound = sound;
  }
  // 加载音效
  loadAudio(url: string): Promise<AudioBuffer> {
    const loader = new THREE.AudioLoader();
    return new Promise((resolve) => {
      loader.load(url, (buffer) => {
        this.sound.setBuffer(buffer);
        resolve(buffer);
      });
    });
  }
  // 加载模型
  loadModel(url: string): Promise<THREE.Object3D> {
    const loader = new GLTFLoader();
    return new Promise((resolve, reject) => {
      loader.load(
        url,
        (gltf) => {
          const model = gltf.scene;
          resolve(model);
        },
        undefined,
        (err) => {
          console.log(err);
          reject();
        }
      );
    });
  }
  // 加载FBX模型
  loadFBXModel(url: string): Promise<THREE.Object3D> {
    const loader = new FBXLoader();
    return new Promise((resolve, reject) => {
      loader.load(
        url,
        (obj) => {
          resolve(obj);
        },
        undefined,
        (err) => {
          console.log(err);
          reject();
        }
      );
    });
  }
  // 加载字体
  loadFont(url: string): Promise<THREE.Font> {
    const loader = new THREE.FontLoader();
    return new Promise((resolve) => {
      loader.load(url, (font) => {
        resolve(font);
      });
    });
  }
  // 创建点选模型
  createRaycaster() {
    this.raycaster = new THREE.Raycaster();
    this.trackMousePos();
  }
  // 追踪鼠标位置
  trackMousePos() {
    window.addEventListener("mousemove", (e) => {
      this.setMousePos(e);
    });
    window.addEventListener("mouseout", () => {
      this.clearMousePos();
    });
    window.addEventListener("mouseleave", () => {
      this.clearMousePos();
    });
    window.addEventListener(
      "touchstart",
      (e: TouchEvent) => {
        this.setMousePos(e.touches[0]);
      },
      { passive: false }
    );
    window.addEventListener("touchmove", (e: TouchEvent) => {
      this.setMousePos(e.touches[0]);
    });
    window.addEventListener("touchend", () => {
      this.clearMousePos();
    });
  }
  // 设置鼠标位置
  setMousePos(e: MouseEvent | Touch) {
    const { x, y } = getNormalizedMousePos(e);
    this.mousePos.x = x;
    this.mousePos.y = y;
  }
  // 清空鼠标位置
  clearMousePos() {
    this.mousePos.x = -100000;
    this.mousePos.y = -100000;
  }
  // 获取点击物
  getInterSects(): THREE.Intersection[] {
    this.raycaster.setFromCamera(this.mousePos, this.camera);
    const intersects = this.raycaster.intersectObjects(
      this.scene.children,
      true
    );
    return intersects;
  }
  // 选中点击物时
  onChooseIntersect(target: THREE.Object3D) {
    const intersects = this.getInterSects();
    const intersect = intersects[0];
    if (!intersect || !intersect.face) {
      return null;
    }
    const { object } = intersect;
    return target === object ? intersect : null;
  }
}

class KineticText extends Base {
  textMesh!: THREE.Mesh;
  rt!: THREE.WebGLRenderTarget;
  rtScene!: THREE.Scene;
  rtCamera!: THREE.PerspectiveCamera;
  clock: THREE.Clock;
  material!: THREE.ShaderMaterial | null;
  params!: Params;
  meshConfig!: any;
  mesh!: THREE.Mesh | null;
  meshNames!: any;
  constructor(sel: string, debug: boolean) {
    super(sel, debug);
    this.cameraPosition = new THREE.Vector3(0, 0, 40);
    this.clock = new THREE.Clock();
    this.meshConfig = {
      torusKnot: {
        vertexShader: kineticTextTorusKnotVertexShader,
        fragmentShader: kineticTextTorusKnotFragmentShader,
        geometry: new THREE.TorusKnotGeometry(9, 3, 768, 3, 4, 3)
      },
      sphere: {
        vertexShader: kineticTextSphereVertexShader,
        fragmentShader: kineticTextSphereFragmentShader,
        geometry: new THREE.SphereGeometry(15, 64, 64)
      },
      plane: {
        vertexShader: kineticTextPlaneVertexShader,
        fragmentShader: kineticTextPlaneFragmentShader,
        geometry: new THREE.PlaneGeometry(30, 30, 64, 64)
      },
      torus: {
        vertexShader: kineticTextTorusVertexShader,
        fragmentShader: kineticTextTorusFragmentShader,
        geometry: new THREE.TorusGeometry(12, 4, 20, 45)
      },
      cylinder: {
        vertexShader: kineticTextCylinderVertexShader,
        fragmentShader: kineticTextCylinderFragmentShader,
        geometry: new THREE.CylinderGeometry(12, 12, 21, 64, 64)
      }
    };
    this.meshNames = Object.keys(this.meshConfig);
    this.params = {
      meshName: "torusKnot",
      velocity: 0.5,
      shadow: 5,
      color: "#000000",
      frequency: 0.5,
      text: "ALPHARDEX",
      cameraZ: 2.5
    };
  }
  // 初始化
  async init() {
    this.createScene();
    this.createPerspectiveCamera();
    this.createRenderer(true);
    await this.createKineticText(this.params.text);
    this.createLight();
    this.createOrbitControls();
    this.createDebugPanel();
    this.addListeners();
    this.setLoop();
  }
  // 创建动态文字
  async createKineticText(text: string) {
    await this.createFontText(text);
    this.createRenderTarget();
    this.createTextContainer();
  }
  // 加载字体文本
  loadFontText(text: string): any {
    return new Promise((resolve) => {
      const fontGeo = createGeometry({
        font,
        text
      });
      const loader = new THREE.TextureLoader();
      loader.load(fontAtlas, (texture) => {
        const fontMat = new THREE.RawShaderMaterial(
          MSDFShader({
            map: texture,
            side: THREE.DoubleSide,
            transparent: true,
            negate: false,
            color: 0xffffff
          })
        );
        resolve({ fontGeo, fontMat });
      });
    });
  }
  // 创建字体文本
  async createFontText(text: string) {
    const { fontGeo, fontMat } = await this.loadFontText(text);
    const textMesh = this.createMesh({
      geometry: fontGeo,
      material: fontMat
    });
    textMesh.position.set(-0.965, -0.525, 0);
    textMesh.rotation.set(ky.deg2rad(180), 0, 0);
    textMesh.scale.set(0.008, 0.025, 1);
    this.textMesh = textMesh;
  }
  // 创建渲染目标
  createRenderTarget() {
    const rt = new THREE.WebGLRenderTarget(
      window.innerWidth,
      window.innerHeight
    );
    this.rt = rt;
    const rtCamera = new THREE.PerspectiveCamera(45, 1, 0.1, 1000);
    rtCamera.position.z = this.params.cameraZ;
    this.rtCamera = rtCamera;
    const rtScene = new THREE.Scene();
    rtScene.add(this.textMesh);
    this.rtScene = rtScene;
  }
  // 创建字体的容器
  createTextContainer() {
    if (this.mesh) {
      this.scene.remove(this.mesh);
      this.mesh = null;
      this.material!.dispose();
      this.material = null;
    }
    this.rtScene.background = new THREE.Color(this.params.color);
    const meshConfig = this.meshConfig[this.params.meshName];
    const geometry = meshConfig.geometry;
    const material = new THREE.ShaderMaterial({
      vertexShader: meshConfig.vertexShader,
      fragmentShader: meshConfig.fragmentShader,
      uniforms: {
        uTime: {
          value: 0
        },
        uVelocity: {
          value: this.params.velocity
        },
        uTexture: {
          value: this.rt.texture
        },
        uShadow: {
          value: this.params.shadow
        },
        uFrequency: {
          value: this.params.frequency
        }
      }
    });
    this.material = material;
    const mesh = this.createMesh({
      geometry,
      material
    });
    this.mesh = mesh;
  }
  // 动画
  update() {
    if (this.rtScene) {
      this.renderer.setRenderTarget(this.rt);
      this.renderer.render(this.rtScene, this.rtCamera);
      this.renderer.setRenderTarget(null);
    }
    const elapsedTime = this.clock.getElapsedTime();
    if (this.material) {
      this.material.uniforms.uTime.value = elapsedTime;
    }
  }
  // 创建调试面板
  createDebugPanel() {
    const gui = new dat.GUI();
    gui
      .add(this.params, "velocity")
      .min(0)
      .max(5)
      .step(0.01)
      .onFinishChange(() => {
        this.createTextContainer();
      });
    gui
      .add(this.params, "cameraZ")
      .min(0)
      .max(5)
      .step(0.01)
      .onFinishChange(async () => {
        await this.createKineticText(this.params.text);
      });
    gui.add(this.params, "meshName", this.meshNames).onFinishChange(() => {
      this.createTextContainer();
    });
    gui.add(this.params, "text").onFinishChange(async () => {
      await this.createKineticText(this.params.text);
    });
    gui.addColor(this.params, "color").onFinishChange(() => {
      this.createTextContainer();
    });
  }
}

const start = () => {
  const kineticText = new KineticText(".kinetic-text", false);
  kineticText.init();
};

start();

              
            
!
999px

Console