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 150
| import * as THREE from "three"; import * as YUKA from "yuka";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"; import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js"; import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); camera.position.set(0, 10, 20); camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.shadowMap.enabled = true; renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
const planeGeometry = new THREE.PlaneGeometry(50, 50); const planeMaterial = new THREE.MeshStandardMaterial({ color: 0x999999 }); const plane = new THREE.Mesh(planeGeometry, planeMaterial); plane.receiveShadow = true; plane.rotation.x = -Math.PI / 2; plane.position.y = -0.5; scene.add(plane);
const light = new THREE.SpotLight(0xffffff, 3, 100, Math.PI / 6, 0.5); light.position.set(10, 40, 10); light.castShadow = true; scene.add(light);
const controls = new OrbitControls(camera, renderer.domElement); controls.enableDamping = true;
const time = new YUKA.Time(); requestAnimationFrame(function animate() { const delta = time.update().getDelta(); controls.update(); entityManager.update(delta); renderer.render(scene, camera); requestAnimationFrame(animate); });
const loader = new GLTFLoader(); const dracoLoader = new DRACOLoader(); dracoLoader.setDecoderPath("./draco/"); loader.setDRACOLoader(dracoLoader); let c1, c2; c1 = loader.loadAsync("./model/car.gltf"); c2 = loader.loadAsync("./model/truck.gltf"); Promise.all([c1, c2]).then((res) => { const truck = res[1].scene; truck.children[0].rotation.y = Math.PI / 2; truck.children[0].scale.set(0.2, 0.2, 0.2); const vehicle2 = new YUKA.Vehicle(); vehicle2.maxSpeed = 4; vehicle2.position.set(Math.random() * 20 - 10, 0, Math.random() * 20 - 10); vehicle2.setRenderComponent(truck, callback); entityManager.add(vehicle2);
scene.add(truck);
const offsets = [ new YUKA.Vector3(0, 0, 1), new YUKA.Vector3(-1, 0, 1), new YUKA.Vector3(1, 0, 1), new YUKA.Vector3(3, 0, -3), new YUKA.Vector3(-3, 0, -3), ];
for (let i = 0; i < offsets.length; i++) { const car = res[0].scene.clone();
car.children[0].rotation.y = Math.PI / 2; car.children[0].scale.set(0.2, 0.2, 0.2); const vehicle = new YUKA.Vehicle(); vehicle.maxSpeed = 6; vehicle.position.set(Math.random() * 20 - 10, 0, Math.random() * 20 - 10); vehicle.setRenderComponent(car, callback); entityManager.add(vehicle); scene.add(car); const offsetPursuitBehavior = new YUKA.OffsetPursuitBehavior( vehicle2, offsets[i] ); vehicle.steering.add(offsetPursuitBehavior); }
const arriveBehavior = new YUKA.ArriveBehavior(target.position); vehicle2.steering.add(arriveBehavior);
setInterval(() => { target.position.set(Math.random() * 50 - 25, 0, Math.random() * 50 - 25); }, 3000); }); function callback(entity, renderComponent) { renderComponent.position.copy(entity.position); renderComponent.quaternion.copy(entity.rotation); }
const sphereGeometry = new THREE.SphereGeometry(0.1, 32, 32); const sphereMaterial = new THREE.MeshStandardMaterial({ color: 0xff00ff }); const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); sphere.receiveShadow = true; sphere.castShadow = true; scene.add(sphere);
const target = new YUKA.GameEntity(); target.setRenderComponent(sphere, callback);
target.position.set(Math.random() * 20 - 10, 0, Math.random() * 20 - 10);
const entityManager = new YUKA.EntityManager(); entityManager.add(target);
|