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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!-- 来自 learning three.js -->
<script src="https://unpkg.com/three@0.111.0/build/three.js"></script>
<script src="https://unpkg.com/stats.js"></script></script>
<script src="https://cdn.bootcss.com/dat-gui/0.6.5/dat.gui.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/16.3.5/Tween.min.js"></script>

</div>
<!-- Div which will hold the Output -->
<div id="webgl">
</div>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
}

body {
  overflow: hidden;
}
              
            
!

JS

              
                function combineCube(geo) {
     const tweens = geo.tweens || [];
     var cubeGeometry = new THREE.CubeGeometry(40, 40, 40, 5, 5, 5);
     var positions = new Float32Array( 152 * 3 );
     // console.log(geo)
     for(let i = 0; i < 152; i++) {
        const newVertice = new THREE.Vector3(
          geo.attributes.position.array[i * 3],
          geo.attributes.position.array[i * 3 + 1], 
          geo.attributes.position.array[i * 3 + 2],
         );
       
        if (!tweens[i] && cubeGeometry.vertices[i]) {
            tweens.push(new TWEEN.Tween(newVertice).easing(TWEEN.Easing.Exponential.In));
        }
       
        function onUpdate() {
          positions[i * 3] = newVertice.x;
          positions[i * 3 + 1] = newVertice.y;
          positions[i * 3 + 2] = newVertice.z;
          geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
        }
       
        tweens[i].to({ x: cubeGeometry.vertices[i].x, y: cubeGeometry.vertices[i].y, z: cubeGeometry.vertices[i].z }, 5000).onUpdate(onUpdate);
       tweens[i].start();
     }
    // geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
}

function getRandom(geo) {
  const vertices = [];
  for ( var i = 0; i < 152; i ++ ) {
					var x = Math.random() * 200 - 100;
					var y = Math.random() * 200 - 100;
					var z = Math.random() * 200 - 100;
					vertices.push( x, y, z );
	}
  geo.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
}

function combineShape(geo, x = 0, y = 0) {
   var fishShape = new THREE.Shape()
					.moveTo( x, y )
					.quadraticCurveTo( x + 50, y - 80, x + 90, y - 10 )
					.quadraticCurveTo( x + 100, y - 10, x + 115, y - 40 )
					.quadraticCurveTo( x + 115, y, x + 115, y + 40 )
					.quadraticCurveTo( x + 100, y + 10, x + 90, y + 10 )
					.quadraticCurveTo( x + 50, y + 80, x, y );
   const points = fishShape.getSpacedPoints(151);
   const positions = new Float32Array( 152 * 3 );
   for(let i = 0; i < 152; i++) {
        positions[i * 3] = points[i].x;
        positions[i * 3 + 1] = points[i].y;
        positions[i * 3 + 2] = 0;
    }
    geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
}

function init() {
        // 创建一个场景
        var scene = new THREE.Scene();

        // 创建一个摄像机,它定义了我们的观察位置
        var camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.x = 100;
        camera.position.y = 100;
        camera.position.z = 300;

        // 创建一个渲染器并设置其大小
        var renderer = new THREE.WebGLRenderer();

        renderer.setClearColor(0x000000);
        renderer.setSize(window.innerWidth, window.innerHeight);
  
  
        const clock = new THREE.Clock();
        // const x = 0, y = 0;
        const WIDTH_SEGMENG = 5;
        const HEIGHT_SEGMENT= 5;
        const DEPTH_SEGMENT = 5;
        const amount = WIDTH_SEGMENG * HEIGHT_SEGMENT * DEPTH_SEGMENT;
  
        var cubeGeometry = new THREE.CubeGeometry(40, 40, 40, WIDTH_SEGMENG, HEIGHT_SEGMENT, DEPTH_SEGMENT);
        var geometry = new THREE.BufferGeometry();
        getRandom(geometry);
        // combineShape(geometry);
        setTimeout(() => {
          combineCube(geometry);
          // setTimeout(() => {
          //   getRandom(geometry)
          // }, 3000);
        }, 5000);
        var sizes = new Float32Array( amount );
        var positions = new Float32Array( amount );
        var colors = new Float32Array( amount );
        var vertices = [];
        const materials = [];
  
        const texture = new THREE.TextureLoader().load('https://p1.music.126.net/jgzbZtWZhDet2jWzED8BTw==/109951164579600342.png');
  
        materials[0] = new THREE.PointsMaterial({
          transparent: true,
          map: texture,
          size: 10,
          // 粒子的大小是否和其与摄像机的距离有光,默认值 true
          sizeAttenuation: true,
        });
        materials[0].alphaTest = 0.5;
        
        var particles = new THREE.Points( geometry, materials[0] );

        particles.position.set( 0, 0, 0 );
        // particles.rotation.set( rx, ry, rz );
        // particles.scale.set( s, s, s );
        scene.add( particles );
  
        // 创建一个平行光并设置其位置
        var directionalLight = new THREE.DirectionalLight(0xffffff, 0.7);
        directionalLight.position.set(-20, 40, 60);
        scene.add(directionalLight);

        // 增加一个环境光,提亮整个场景
        var ambientLight = new THREE.AmbientLight(0x292929);
        scene.add(ambientLight);

        // 将渲染器的输入(canvas)插入到特定 DOM 元素下
        document.getElementById("webgl").appendChild(renderer.domElement);

 
        // 确保摄像机第一时刻是看向场景中心(0,0,0)
        camera.lookAt(scene.position);
        render();
        function render() {
            TWEEN.update();
            const time = Date.now() * 0.00005;
            for ( var i = 0; i < scene.children.length; i ++ ) {
              var object = scene.children[ i ];
              if ( object instanceof THREE.Points ) {
                object.rotation.y = time * ( i < 4 ? i + 1 : - ( i + 1 ) );
              }
            }
            // render using requestAnimationFrame
            requestAnimationFrame(render);
            renderer.render(scene, camera);
        }
    }
    window.onload = init

              
            
!
999px

Console