Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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 src="https://cdn.bootcss.com/dat-gui/0.6.5/dat.gui.min.js"></script>

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

CSS

              
                * {
  margin: 0;
  padding: 0;
}

body {
  overflow: hidden;
}
              
            
!

JS

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

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

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

        renderer.setClearColor(0xffffff);
        renderer.setSize(window.innerWidth, window.innerHeight);
  
        var cubeGeometry = new THREE.CubeGeometry(30,30,30);
        var sphereGeometry = new THREE.SphereGeometry(20, 100, 100);
  
        const mixers = [];
        const clock = new THREE.Clock();
        // 线框
        var cubeWireMaterial = new THREE.MeshBasicMaterial({ wireframe: true, color: 0x666666 });
        var cubeMeshWire = new THREE.Mesh(cubeGeometry, cubeWireMaterial);
        cubeMeshWire.position.set(0, 0, 0);
        scene.add(cubeMeshWire);
  
        // 上下移动动画
        const mixer = new THREE.AnimationMixer(cubeMeshWire);
        const action = mixer.clipAction(getClip());
        action.timeScale = 1;
        action.loop = THREE.LoopPingPong;
        action.play();
        mixers.push(mixer);
  
  
        // 创建一个平行光并设置其位置
        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 getClip() {
          const times = [0, 1]; // 关键帧时间数组,离散的时间点序列
          const values = [0, 0, 0, 0, 10, 0]; // 与时间点对应的值组成的数组
          // 创建位置关键帧对象:0时刻对应位置0, 0, 0   10时刻对应位置150, 0, 0
          const posTrack = new THREE.VectorKeyframeTrack('stone.position', times, values);
          const duration = 1;
          return new THREE.AnimationClip('stonePosClip', duration, [posTrack]);
        }

        function render() {
            const delta = clock.getDelta();
            mixers.forEach((mixer) => mixer.update(delta))
            // render using requestAnimationFrame
            requestAnimationFrame(render);
            renderer.render(scene, camera);
        }
    }
    window.onload = init

              
            
!
999px

Console