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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
[div id = "box" /] [script type = "module"] let box = document.getElementById("box"); box.width = 700; box.height = 500; // FBXLoader用到。https://github.com/mrdoob/three.js/tree/master/examples/js/libs/fflate.min.js let fflate_script = document.createElement("script"); fflate_script.src = "https://github.com/mrdoob/three.js/tree/master/examples/js/libs/fflate.min.js"; let three_script = document.createElement("script"); three_script.src = "https://github.com/mrdoob/three.js/tree/master/build/three_r132_min.js"; let head = document.getElementsByTagName("head")[0]; head.appendChild(fflate_script); head.appendChild(three_script); three_script.onload = function () { create3d(); }; class FbxActor { constructor(scene) { this.scene = scene; this.position_x = 0; this.position_y = -100; this.position_z = 0; // 地面。光光的平面 let mesh = new THREE.Mesh( new THREE.PlaneGeometry(2000, 2000), new THREE.MeshPhongMaterial({color: 0x999999, depthWrite: false})); mesh.rotation.x = -Math.PI / 2; mesh.position.x = this.position_x; mesh.position.y = this.position_y; mesh.position.z = this.position_z; mesh.receiveShadow = true; this.scene.add(mesh); // 格子。位置附在地面,看起来像是地板砖 let grid = new THREE.GridHelper(2000, 20, 0x000000, 0x000000); grid.material.opacity = 0.2; grid.material.transparent = true; grid.position.x = this.position_x; grid.position.y = this.position_y; grid.position.z = this.position_z; this.scene.add(grid); } /** * @param callback 加载模型比较耗时,加载完毕后的回调函数 */ intoScence(callback) { let that = this; new THREE.FBXLoader() // https://github.com/mrdoob/three.js/tree/master/examples/models/fbx .load("https://github.com/mrdoob/three.js/tree/master/examples/models/fbx/Samba Dancing.fbx", function (fbx) { fbx.traverse(function (child) { if (child.isMesh) { child.castShadow = true; child.receiveShadow = true; } }); fbx.translateY(0); fbx.position.x = that.position_x; fbx.position.y = that.position_y; fbx.position.z = that.position_z; that.scene.add(fbx); let mixer = new THREE.AnimationMixer(fbx); let action = mixer.clipAction(fbx.animations[0]); action.play(); callback(mixer); }); } } class LightManager { constructor(scene) { this.scene = scene; } addAmbientLight(color, intensity = 1) { let ambient = new THREE.AmbientLight(color, intensity); this.scene.add(ambient); } addDirrectionalLight(x, y, z, color, intensity = 1) { let directionalLight = new THREE.DirectionalLight(color, intensity); directionalLight.position.set(x, y, z);// 设置光源的方向 this.scene.add(directionalLight); } } class CameraManager { constructor(scene, x = 0, y = 0, z = 0) { this.camera = new THREE.PerspectiveCamera(60, box.width / box.height); this.camera.position.set(x, y, z); // 相机位置 this.camera.lookAt(scene.position); // 相机方向(指向的场景对象) } setPosition(x, y, z) { this.camera.position.set(x, y, z); // 相机位置 } lookAt(x, y, z) { this.camera.lookAt(new Vector3(x, y, z)); // 相机方向 } } class Cinema { constructor(scene, camera, width = box.width, height = box.height, bkg_color = 0x7c8577) { this.scene = scene; this.camera = camera; this.renderer = new THREE.WebGLRenderer(); this.renderer.setSize(width, height); this.renderer.setClearColor(bkg_color); this.renderer.shadowMap.enabled = true; box.appendChild(this.renderer.domElement); this.mixer = null; } setMixer(mixer) { this.mixer = mixer; this.clock = new THREE.Clock(); } play() { this.renderer.render(this.scene, this.camera); requestAnimationFrame(this.play.bind(this)); if (null !== this.mixer) { this.mixer.update(this.clock.getDelta()); // 渲染模型的动画 } } } function create3d() { var scene = new THREE.Scene(); scene.background = new THREE.Color(0xafafa0); scene.fog = new THREE.Fog( // background和fog配合产生远处的雾效 0xafafa0, // color 10, // near 1000); // far let lightManager = new LightManager(scene); lightManager.addAmbientLight(0xffffff); lightManager.addDirrectionalLight(100, 50, 50, 0xffffff); let cameraManager = new CameraManager(scene); cameraManager.setPosition(0, 40, 260); let cinema = new Cinema(scene, cameraManager.camera); cinema.play(); let controls = new THREE.OrbitControls(cameraManager.camera, cinema.renderer.domElement); let fbxActor = new FbxActor(scene); fbxActor.intoScence(function (mixer) { cinema.setMixer(mixer); // fbx模型动画 }); } [/scrip] |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3070.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设