最近公司需要用tree.js实现一个3D图的显示,就看了官方文档,正好有时间,就记录下来。
由于我们公司的前端框架用的是angular,所以我就把我的treejs封装在一个directives里面。后面放源码
首先我们要知道three.js的下载地址它的地址是: https://github.com/mrdoob/three.js。
其次,什么是three.js?
three.js的几个步骤:
1:引入three.js文件(打开调试窗口,并在Console下输入 THREE.REVISION命令,得到版本号,成功)
2:设置一个场景// var scene = new THREE. Scene();
3: var camera = new THREE. PerspectiveCamera( 75, window.innerWidth /window.innerHeight, 0.1, 1000);设置一个 透视相机
4: var renderer = new THREE. WebGLRenderer(); renderer. setSize(window.innerWidth, window.innerHeight); 设置一个渲染器
5:把一个物体添加到场景中
modelUrl是所添加文件例如:$scope. DView = cy3DView. newCanvas ; $scope. DView. config( 'canvas')
$scope.process3DUrl = data.result.data.engineering_stl_mcube; $scope.DView.plan($scope.process3DUrl) function plan(modelUrl) { stlLoader = new THREE.STLLoader(); group = new THREE.Object3D(); stlLoader.load(modelUrl, function (geometry) { //console.log(geometry); var mat = new THREE.MeshLambertMaterial({color: 0x7777ff}); group = new THREE.Mesh(geometry, mat); group.rotation.x = -0.5 * Math.PI; group.scale.set(0.6, 0.6, 0.6); scene.add(group); animation(); }); }
6:渲染
renderer.render(scene, camera);
ok 是不是很简单,个人认为是这样,没有看懂的小伙伴可以私信我哦
源码如下:
(function(window, document) { 'use strict'; var three = window.THREE; var angular = window.angular; angular.module('cy-3D-view', []).factory('cy3DView', cy3DView); cy3DView.$inject = ['$rootScope']; function cy3DView($rootScope) { return { newCanvas: new Object(newCanvas($rootScope)) }; } function newCanvas() { var scene, camera, renderer, controls, group, ambient, fov, near, far, stlLoader; var width, height, keyLight, fillLight, backLight, spotLight, lighting; function config() { //设置3D图的宽和高 width = document.getElementById('canvas').clientWidth; height = document.getElementById('canvas').clientHeight; renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(width, height); renderer.shadowMapEnabled = true; document.getElementById('canvas').appendChild(renderer.domElement); renderer.setClearColor(0xFFFFFF, 1.0); scene = new THREE.Scene(); lighting = false; //设置3D图的颜色 ambient = new THREE.AmbientLight(0xffffff, 1.0); scene.add(ambient); keyLight = new THREE.DirectionalLight(new THREE.Color('hsl(30, 100%, 75%)'), 1.0); keyLight.position.set( - 100, 0, 100); fillLight = new THREE.DirectionalLight(new THREE.Color('hsl(240, 100%, 75%)'), 0.75); fillLight.position.set(100, 0, 100); backLight = new THREE.DirectionalLight(0xffffff, 1.0); backLight.position.set(100, 0, -100).normalize(); spotLight = new THREE.SpotLight(0xffffff); spotLight.position.set(150, 150, 150); scene.add(spotLight); //照相机配置 fov = 40; near = 1; far = 1000; camera = new THREE.PerspectiveCamera(fov, width / height, near, far); camera.position.x = 150; camera.position.y = 150; camera.position.z = 150; camera.lookAt({ x: 0, y: 0, z: 0 }); camera.lookAt(new THREE.Vector3(0, 40, 0)); controls = new THREE.OrbitControls(camera, renderer.domElement); controls.enableDamping = true; controls.dampingFactor = 0.25; controls.enableZoom = false; window.addEventListener('resize', onWindowResize, false); window.addEventListener('keydown', onKeyboardEvent, false); window.addEventListener('mousewheel', mousewheel, false); } function mousewheel(e) { e.preventDefault(); if (e.wheelDelta) { //判断浏览器IE,谷歌滑轮事件 if (e.wheelDelta > 0) { //当滑轮向上滚动时 fov -= (near < fov ? 1 : 0); } if (e.wheelDelta < 0) { //当滑轮向下滚动时 fov += (fov < far ? 1 : 0); } } else if (e.detail) { //Firefox滑轮事件 if (e.detail > 0) { //当滑轮向上滚动时 fov -= 1; } if (e.detail < 0) { //当滑轮向下滚动时 fov += 1; } } camera.fov = fov; camera.updateProjectionMatrix(); renderer.render(scene, camera); } function onWindowResize() { camera.aspect = width / height; camera.updateProjectionMatrix(); renderer.setSize(width, height); } function onKeyboardEvent(e) { if (e.code === 'KeyL') { lighting = !lighting; if (lighting) { ambient.intensity = 0.25; scene.add(keyLight); scene.add(fillLight); scene.add(backLight); } else { ambient.intensity = 1.0; scene.remove(keyLight); scene.remove(fillLight); scene.remove(backLight); } } } function plan(modelUrl) { stlLoader = new THREE.STLLoader(); group = new THREE.Object3D(); stlLoader.load(modelUrl, function(geometry) { //console.log(geometry); var mat = new THREE.MeshLambertMaterial({ color: 0x7777ff }); group = new THREE.Mesh(geometry, mat); group.rotation.x = -0.5 * Math.PI; group.scale.set(0.6, 0.6, 0.6); scene.add(group); animation(); }); } function animation() { renderer.render(scene, camera); requestAnimationFrame(animation); } return { config: config, plan: plan, }; } })(window, document);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]