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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> html, body { height: 100%; } .bg-canvas { width: 100%; height: 100%; } </style> </head> <body> <div class="bg-canvas"></div> <script src="https://unpkg.com/three@0.131.3/build/three.min.js"></script> <script src="https://unpkg.com/three@0.131.3/examples/js/loaders/GLTFLoader.js"></script> <script> (function () { var scene, renderer, camera, container; var mixer, clock, actions, warningTexture;
function init() { container = document.querySelector(".bg-canvas"); var width = container.getBoundingClientRect().width; var height = container.getBoundingClientRect().height;
camera = new THREE.PerspectiveCamera(45, width / height, 1, 2000); camera.position.set(0, 2, 16);
clock = new THREE.Clock();
scene = new THREE.Scene();
var hemiLight = new THREE.HemisphereLight(0xf2f2f2, 0xf2f2f2); hemiLight.position.set(0, 50, 0); scene.add(hemiLight);
var dirLight = new THREE.DirectionalLight(0xffffff); dirLight.position.set(0, 30, 0); dirLight.castShadow = true; dirLight.shadow.camera.near = 0.1; dirLight.shadow.camera.far = 60; scene.add(dirLight);
warningTexture = new THREE.TextureLoader().load("./warning_zone.png"); warningTexture.wrapS = THREE.RepeatWrapping; warningTexture.repeat.set(4, 1);
var warningBar = new THREE.Mesh( new THREE.PlaneGeometry((0.8 * (424 * 4)) / 60, 0.8), new THREE.MeshBasicMaterial({ map: warningTexture }) ); warningBar.position.set(0, 1.5, 5.4); warningBar.rotation.set(0, -0.3, -0.1); scene.add(warningBar);
stage = new THREE.Object3D(); scene.add(stage);
var groundTexture = new THREE.TextureLoader().load("./textures/grass.png"); groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping; groundTexture.repeat.set(4, 4); groundTexture.anisotropy = 16; groundTexture.encoding = THREE.sRGBEncoding;
var bottomBase = new THREE.Mesh(new THREE.CylinderBufferGeometry(5, 5, 0.1, 32), new THREE.MeshLambertMaterial({ map: groundTexture })); bottomBase.receiveShadow = true; stage.add(bottomBase);
var loader = new THREE.GLTFLoader();
loader.load("./pumpkin.gltf", function (gltf) { var model = gltf.scene; model.traverse(function (object) { if (object.isMesh) { object.frustumCulled = false; object.castShadow = true; } }); model.rotation.set(0, 1, 0); model.position.set(1, 0.1, -2.4); let scale = 0.33; model.scale.set(scale, scale, scale); stage.add(model); }); loader.load("./zombie.gltf", function (gltf) { var model = gltf.scene; model.traverse(function (object) { if (object.isMesh) { object.frustumCulled = false; object.castShadow = true; } }); model.position.set(0.6, 0, 2.5); model.scale.set(1.5, 1.5, 1.5); stage.add(model);
mixer = new THREE.AnimationMixer(model); actions = { push_up: mixer.clipAction(gltf.animations[0]), idle: mixer.clipAction(gltf.animations[1]), walk: mixer.clipAction(gltf.animations[2]), }; actions.walk.play(); animate(); });
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true }); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(width, height); renderer.outputEncoding = THREE.sRGBEncoding; renderer.shadowMap.enabled = true;
renderer.setSize(width, height); container.appendChild(renderer.domElement); }
function animate() { requestAnimationFrame(animate); var delta = clock.getDelta(); warningTexture.offset.x -= 0.007; mixer.update(delta); render(); }
function render() { renderer.render(scene, camera); }
init(); })(); </script> </body> </html>
|