threeJS案例模板
2022-08-03 09:57:03
107
{{single.collect_count}}

需要电子档书籍或者源码可以Q群:828202939   希望可以和大家一起学习、一起进步!!

如有错别字或有理解不到位的地方,可以留言或者加微信15250969798,博主会及时修改!!!!!

涉及的知识点博主已经从three源码库里面摘要出来放在对应的注释里面!

今天一个朋友需要一个orb的three场景,为了更多对threeJS感兴趣的同学,特此写了一个小小的Demo模板。

本模板的基本功能包括:1.灯光  2.渲染器  3.鼠标控制 4.简单球形(如果是模型,需要添加加载对应模型格式的加载器,加载好模型也可以用本模板) 5.天空盒子  6.自动旋转

效果图:

代码:

<html><head><title>threeJS模板</title><style>body {background-color: #000;margin: 0px;overflow: hidden;}#WebGL {width: 100%;height: 100%;position: absolute;left: 0;top: 0;z-index: 999;}</style></head><body><script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script><script src="three.js"></script><script src="OrbitControls.js"></script><div id="WebGL"></div><script>'use strict';var container, camera, scene, renderer, geometry, material, controls; //常用变量var spotLight, mesh; //自定义对象变量var target = new THREE.Vector3(0, 30, 0);var webGLW = $('#WebGL').width();var webGLH = $('#WebGL').height();init();animate();function init() {container = document.getElementById('WebGL');rendererScene(); //场景渲染sceneBackground();camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 10000);camera.position.set(0, 50, -200);camera.lookAt(target);plane()地面lights(); //灯光:聚光灯loadModel(); //添加模型OrbitControls(camera, renderer); //OrbitControls控件模块,90版本鼠标右键上下是移动,96版本之后右键上下是缩放window.addEventListener('resize', onWindowResize, false); //监听屏幕变化}function sceneBackground() {scene = new THREE.Scene();// scene.background = new THREE.Color(0xcfcfcf);//可以用图片代替作为背景var path = './sky/';var format = '.jpg';new THREE.CubeTextureLoader().load([path + 'px' + format, path + 'nx' + format, //右左path + 'py' + format, path + 'ny' + format, //上下path + 'pz' + format, path + 'nz' + format //前后], function (res) {scene.background = res;});// // scene.fog = new THREE.Fog(0xa0a0a0, 200, 1000); //雾}function plane() {// 地面let grid_mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(200, 200), new THREE.MeshPhongMaterial({color: 0x999999,// depthWrite: false}));grid_mesh.rotation.x = -Math.PI / 2;grid_mesh.receiveShadow = true;scene.add(grid_mesh);var grid = new THREE.GridHelper(200, 20, 0x000000, 0x000000);grid.material.opacity = 0.2;grid.material.transparent = true;scene.add(grid);}function lights() { //光影自己改哦var ambient = new THREE.AmbientLight(0xffffff);scene.add(ambient);//聚光灯//SpotLight( color:颜色, intensity:强度, distance:发光距离, angle:角度, penumbra:边缘范围, decay:衰减 )spotLight = new THREE.SpotLight(0xffffff, 1);spotLight.position.set(0, 120, 0);spotLight.angle = Math.PI / 6;spotLight.penumbra = 0.05; //边缘范围,反比spotLight.decay = 2; //衰减系数,反比spotLight.distance = 400; //发光距离spotLight.castShadow = true; //阴影spotLight.shadow.mapSize.width = 1024;spotLight.shadow.mapSize.height = 1024;spotLight.shadow.camera.near = 10; //近截面spotLight.shadow.camera.far = 250;scene.add(spotLight);}function lightsHelper(lightsObject) {// 聚光灯显示助手SpotLightHelper( light:灯光, color:颜色 )var lightHelper = new THREE.SpotLightHelper(lightsObject, 0xdfdfdf);scene.add(lightHelper);var mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(100, 100), new THREE.MeshPhongMaterial({color: 0x9cfcf99,depthWrite: false}));mesh.rotation.x = -Math.PI / 2;mesh.position.set(0, -20, 0)mesh.receiveShadow = true;scene.add(mesh);}function loadModel() { //模型mesh = new THREE.Mesh(new THREE.SphereBufferGeometry(20, 16, 8),new THREE.MeshBasicMaterial({color: 0xffffff,wireframe: true}));mesh.position.set(0, 30, 0)scene.add(mesh);}function onWindowResize() {camera.aspect = window.innerWidth / window.innerHeight;camera.updateProjectionMatrix();renderer.setSize(window.innerWidth, window.innerHeight);}function rendererScene() {renderer = new THREE.WebGLRenderer({antialias: true});renderer.setPixelRatio(window.devicePixelRatio);renderer.setSize(window.innerWidth, window.innerHeight);renderer.shadowMap.enabled = true;renderer.shadowMap.type = THREE.PCFSoftShadowMap;renderer.gammaInput = true;renderer.gammaOutput = true;container.appendChild(renderer.domElement);}function OrbitControls(camera, renderer) {//OrbitControls控件操作模块controls = new THREE.OrbitControls(camera, renderer.domElement);controls.target = target; //控制的targetcontrols.autoRotate = true; //是否自动旋转controls.autoRotateSpeed = 0.5; //自动旋转速度,正比}function animate() {requestAnimationFrame(animate);if (controls) controls.update();renderer.render(scene, camera);};</script></body></html>

 

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}