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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <!-- three.min.js r87 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/87/three.min.js"></script>

<!-- OBJLoader.js -->
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r87/examples/js/loaders/OBJLoader.js"></script>

<script src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js" type="text/javascript"></script>

            
          
!

CSS

            
              body { background-color: #DDDDDD; font: 30px sans-serif; }
.overlay {
    position:absolute; top:250px; left:50px; width:350px; height:150px;    
    padding: 5px;
    background: rgba(0, 0, 0, 0.8);
    color: rgb(255,255,255);
    border-radius: 5px;
    font: 20px bold 80%/150%  sans-serif;
}

.overlayButton {
    position:absolute; top:150px; left:310px;
    font: 20px bold 80%/150% sans-serif;
    font-family: 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif;
    background: rgba(0, 0, 0, 0.8);
    color: rgb(255,255,255);
    letter-spacing: 5px;
    padding: 5px 15px;
    border-radius: 25px;
    margin-bottom: 0.5em;

}


#gui { position: absolute; top: 10px; left: 200px; height: 140px }
            
          
!

JS

            
              // forked from siouxcitizen's "Three.js上にCSSで「はなす」ボタンを作成" http://jsdo.it/siouxcitizen/lvmQ
//
//勇者っぽい3Dモデル表示してみました
//マウスのドラッグで、3D用カメラが3Dモデルにフォーカスしながら左右にまわりこみながら移動します
//「はなす」ボタン押下でメッセージ表示します
//
//dat.guiのサイズ項目をドラッグで3Dモデルのサイズ(0.1~1)を変更できます(デフォルトでは最小値の0.1になってます)
//display項目で3Dモデルの表示・非表示を選択できます
//
//
//以下の自分のコードを参考にしました
//Three.js r70 で自前でカメラ操作機能作成(WebGL使用)
//http://jsdo.it/siouxcitizen/8bYv

var container;
var scene; 
var camera; 
var renderer;

//読み込みリソースURL
//フィールドマップ用テクスチャ画像URL
//var planeFieldPngURL = "http://jsrun.it/assets/d/c/u/e/dcue2.png";
//var planeDesertPngURL = "http://jsrun.it/assets/s/t/h/2/sth2z.png";
//var planeWaterPngURL = "http://jsrun.it/assets/5/z/t/g/5ztgk.png";
var planeFieldPngURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAdUlEQVR4nGMo/Cz9fyAxw6gD0AWsJzOAMbUtwmUu1hCgtiPwmYfXAdRwBCGzcKYBaocA0WmA3njUAaMOGHUA2Q5AztuUlBkUOYAaJSZZDoBZiGw5uY4YulFALTzqgFEHjDpgcDbJBrxROqDN8kHRMaEnHnUAADjp2hzJ3BYuAAAAAElFTkSuQmCC";
var planeDesertPngURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAUklEQVR4nGP4v1r3/0BihlEHjDpg1AH4JN/3M4AxuYYTo3/wO4DSIKbIAQOeBkYdMOqAUQeMOmDUAQNeFwz52hDmAHzmDO40MOqAUQeMOoAeGAArslGyN38s2wAAAABJRU5ErkJggg==";
var planeWaterPngURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACTklEQVR4nK2X3VHDMBCEDQRCgBkqYRJIQuCJMiiCRiiFFqgqSgUmZ1ixWt/JDuHhxpaseD+d7sdpbp53rdnHZ5sNc2a3L21nfO9ZM992dvfa5ivMxnj32/u3YdywOO4jgAgC4grBY7NOcD82AEA1vHOQMYBntmb2mHrCkbE3IA5PZA/YwO7ZPTUAs9PFtuoR3OtxAKIbq7iKeOKAnDykUUfCEL2jgitswBCeJ7xAqgHgzCOg7h4vMwFAeAAsrmsikRzp8/5RZQgIQ5zvIaYZgucXq5QFPBAO8BCABT0A9gR7SqM6EsbzKsBQ2gFEj2qyTDm/2QPqjcgDnUW7x5jPXJ+f3W9zWnm75iroeaoAsCuXXN49uxzzV5tdFtHiwuu1FKtnGt6dll4NTAaYrlOxS96hl/NcPwqveK5lD0QAUXXTXqAwgMjrvUqncxw8Nr5cJ1cgqnYMyfGR64BXfhmEAa6fdu3Jot50huaqANVmtBe3+u9Vt7EtWY+u0d1z8VGA82UqIrkGoJBhu0ZgQRxppQCTH3GOZAXA1esBHCcFFItzDuMls81vwVEP1ZqQlmA+CszlLyItsewF3pECRgBeu+bnBQDELbrtxdNVKtLGK638zGtCvQ/PoFl17/VSRHdTy2utbOwlrzExQO6GvLuoium8upo7JtZ43wp6nE2RkwISVTmvvHo5jljyIPPvOTC8/I4CyPu4iDYTQRZHMCQwtvJ5FdBrVCFAzcbsWtdFYHnuEADvBUMAuq73m7+K145s6M9JAfHfAIc+PxrgWMAvxUC/XyZ/924AAAAASUVORK5CYII=";

//マップ配列 0:草原 1:砂漠 2:水辺
//マップ配列はマップの縦横の長さが一定(10*10)になるように設定
var mapData = [[ 1, 2, 1, 1, 1, 1, 0, 0, 2, 2],
               [ 1, 2, 1, 1, 1, 1, 1, 1, 0, 2],
               [ 1, 2, 1, 1, 1, 1, 1, 1, 1, 1],
               [ 1, 2, 1, 1, 1, 0, 0, 0, 0, 0],
               [ 2, 2, 1, 1, 1, 0, 0, 1, 1, 1],
               [ 2, 1, 1, 1, 0, 0, 0, 1, 0, 1],
               [ 2, 1, 1, 1, 0, 0, 0, 0, 1, 1],
               [ 2, 1, 1, 1, 1, 1, 0, 1, 0, 1],
               [ 2, 1, 1, 0, 1, 1, 0, 1, 0, 0],
               [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]];


//3Dモデル保持用
var threeDModel;

var rot = 0; // 角度
// マウスを押した状態かどうかを判別するフラグ
var isMouseDown = false;
// 一時的なマウスの値を格納する変数
var oldX = 0;
var targetRot = 0;

// メッセージ表示タイル
var messageTile;
// 「はなす」ボタン
var talkButton;
// 「はなす」状態かどうか判定
var isTalk = false;

init();
render();

function init() {
    
    container = document.createElement( 'div' );
    document.body.appendChild( container );

    // 3Dシーン作成
    scene = new THREE.Scene(); 

    // カメラ設定
    camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); 
    camera.position.x = 4.5;
    camera.position.y = 3; 
    camera.position.z = 5.5; 

    // 3D描画用レンダラ設定
    renderer = new THREE.WebGLRenderer(); 
    renderer.setSize( window.innerWidth, window.innerHeight ); 
    document.body.appendChild( renderer.domElement ); 
    //背景を白にする
    renderer.setClearColor(0x00ffff, 1); 

    // 光源の設定
    var light = new THREE.DirectionalLight("#ffeedd", 1);
    var ambient = new THREE.AmbientLight("#221100");
    light.position.set( 0, -70, 100 ).normalize();
    scene.add(light);
    scene.add(ambient);

    // PLANEオブジェクト作成
    // 共通PLANEジオメトリ作成
    var geometry = new THREE.PlaneGeometry( 1, 1, 1 ); 

    // 草原テクスチャの作成
    var fieldTexture = THREE.ImageUtils.loadTexture( planeFieldPngURL );
    var fieldMaterial = new THREE.MeshBasicMaterial( { map: fieldTexture, side: THREE.DoubleSide } );

    // 砂漠テクスチャの作成
    var desertTexture = THREE.ImageUtils.loadTexture( planeDesertPngURL );
    var desertMaterial = new THREE.MeshBasicMaterial( { map: desertTexture, side: THREE.DoubleSide } );

    // 水辺テクスチャの作成
    var waterTexture = THREE.ImageUtils.loadTexture( planeWaterPngURL );
    var waterMaterial = new THREE.MeshBasicMaterial( { map: waterTexture, side: THREE.DoubleSide } );

    // マップ配列を読み取り、Planeにマップ用テクスチャ画像を貼り付けて表示
    for (var xCounter = 0; xCounter < mapData.length; xCounter++) {

        for (var zCounter = 0; zCounter < mapData[0].length; zCounter++) {

            if(mapData[xCounter][zCounter] == 0) {
	            
	            var fieldPlane = new THREE.Mesh( geometry, fieldMaterial ); 
	            fieldPlane.position.x = xCounter;
	            fieldPlane.position.z = -zCounter;
	            fieldPlane.rotation.x =  Math.PI / 360 * 180;
	            scene.add( fieldPlane ); 

            } else if (mapData[xCounter][zCounter] == 1) {

	            var desertPlane = new THREE.Mesh( geometry, desertMaterial ); 
	            desertPlane.position.x = xCounter;
	            desertPlane.position.z = -zCounter;
	            desertPlane.rotation.x = Math.PI / 360 * 180;
	            scene.add( desertPlane ); 

            } else if (mapData[xCounter][zCounter] == 2) {

	            var waterPlane = new THREE.Mesh( geometry, waterMaterial ); 
	            waterPlane.position.x = xCounter;
	            waterPlane.position.z = -zCounter;
	            waterPlane.rotation.x =  Math.PI / 360 * 180;
	            scene.add( waterPlane.clone() ); 

            }

        }
    }
    
    var texture = new THREE.Texture();
    var manager = new THREE.LoadingManager();
    manager.onProgress = function ( item, loaded, total ) {};
    var onProgress = function ( xhr ) {};
    var onError = function ( xhr ) {};

    
    // 3Dモデル用テクスチャ画像の読込
    var imageLoader = new THREE.ImageLoader( manager );
    //imageLoader.load( 'http://jsrun.it/assets/9/9/W/G/99WGE.png', function ( image ) {
    //imageLoader.load( '/assets/9/9/W/G/99WGE.png', function ( image ) {
    imageLoader.load( 'https://cdn.jsdelivr.net/gh/siouxcitizen/3DModel@master/yuusha.png', function ( image ) {

        texture.image = image;
        texture.needsUpdate = true;

    } );

    // 3Dモデル読込
    var objLoader = new THREE.OBJLoader( manager );
    //objLoader.load( 'http://jsrun.it/assets/g/U/i/X/gUiXN', function ( object ) {
    //objLoader.load( '/assets/g/U/i/X/gUiXN', function ( object ) {
    objLoader.load( 'https://cdn.jsdelivr.net/gh/siouxcitizen/3DModel@master/yuusha.obj', function ( object ) {

        object.traverse( function ( child ) {

            if ( child instanceof THREE.Mesh ) {

                child.material.map = texture;
                child.material.wireframe = false;
            }

        } );

        //object.scale.set( 2, 2, 2 );
        object.scale.set( 0.1, 0.1, 0.1 );
        threeDModel = object;
        scene.add( threeDModel );
        threeDModel.position.set( 5, 0, -1 );

    }, onProgress, onError );
    
    var text = {
        "message": "Hello, world!",
        "size": 0.1,
        "display": true
    };
    //gui = new dat.GUI();
    gui = new dat.GUI();
    gui.domElement.id = 'gui';
        
    
    
    gui.add(text, "message");
    var saizu = gui.add(text, "size", 0.1, 1);
    var hyouji = gui.add(text, "display");
    
    hyouji.onChange(function(value) {
        if (value) threeDModel.visible = true;
        
        if (!value) threeDModel.visible = false;
    });
    saizu.onChange(function(value) {
        threeDModel.scale.set( value, value, value );
    });

    //メッセージ表示用タイルを作成    
    messageTile = document.createElement('div');
    messageTile.classList.add( 'overlay' );
    messageTile.innerHTML = "はじめまして勇者です!";
    messageTile.style.display = 'none';
    container.appendChild(messageTile);
  
    //「はなす」ボタンを作成    
    talkButton = document.createElement('div');
    talkButton.classList.add( 'overlayButton' );
    talkButton.innerHTML = "はなす";
    talkButton.onclick = function() {
        
        if (isTalk) {
        
            messageTile.style.display = 'none';
            isTalk = false;
        
        } else {
            
            messageTile.style.display = '';
            isTalk = true;
            
        }
    };
    container.appendChild(talkButton);
    //container.removeChild(talkButton);
    
        // マウス、タッチ処理を呼び出すイベントリスナーをセット
    document.addEventListener("mousedown", onMouseDown);
    document.addEventListener("touchstart", onMouseDown);
    document.addEventListener("mouseup", onMouseUp);
    document.addEventListener("touchend", onMouseUp);
    document.addEventListener("mousemove", onMouseMove);
    document.addEventListener("touchmove", onMouseMove);
    
}

// 3Dモデル読込時コールバック関数
function createScene( geometry, materials, x, y, z) {

	threeDModel = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
	threeDModel.position.set( x, y, z );
	threeDModel.scale.set( 4, 4, 4 );
	scene.add( threeDModel );

}

function render() { 

    requestAnimationFrame( render ); 

    //rot += 0.5; // 毎フレーム角度を0.5度ずつ足していく
    // イージングの公式を用いて滑らかにする
    // 値 += (目標値 - 現在の値) * 減速値
    rot += (targetRot - rot) * 0.05;

    // 角度に応じてカメラの位置を設定
    camera.position.x = threeDModel.position.x + 5 * Math.sin(rot * Math.PI / 180);
    camera.position.z = threeDModel.position.z + 5 * Math.cos(rot * Math.PI / 180);
    // キャラクタ方向を見つめる
    camera.lookAt({x:threeDModel.position.x,  y:threeDModel.position.y,  z:threeDModel.position.z});

    renderer.render(scene, camera);
    
}

// マウスを押したとき
function onMouseDown(event) {
    isMouseDown = true;
    oldX = event.pageX;
}

// マウスを動かした時
function onMouseMove(event) {
    if (isMouseDown) {
        // 3DモデルをX軸とY軸方向に回転させます
        var dy = event.pageX - oldX;
        targetRot += dy * 0.25;
        oldX = event.pageX;
    }
}

// マウスを離したとき
function onMouseUp(event) {
    isMouseDown = false;
}
            
          
!
999px

Console