分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

three.js - (入门四)

发布时间:2023-09-06 01:35责任编辑:沈小雨关键词:js

好久没更新three.js 了,前三章了解了部分基础知识。这一章我们来搞点事情,做点有意思的东西。

效果如下图:

因为截图是静止的,实际效果是地球在自转。

废话不多说,上代码:

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>erath - three.js</title> ???<script src="js/three.js"></script> ???<script src="js/jquery.min.js"></script></head><style> ???* { ???????padding: 0px; ???????margin: 0px; ???} ???????html,body,.main { ???????????width: 100%; ???????????height: 100%; ???????????overflow: hidden; ???????}</style><body> ???<div class="main"></div></body></html><script> ???function Three(className) { ???????this.width = $(className).width(); ???????this.height = $(className).height(); ???????this.renderer = ?new THREE.WebGLRenderer({ ???????????antialias : true ??//开启锯齿,默认是false ???????}); ???????this.renderer.setSize(this.width, this.height); // 给渲染区域设置宽高 ???????this.renderer.setClearColor("white"); // 设置背景色 ???????$(className).append(this.renderer.domElement); ????} ???Three.prototype = { ???????init:function() { ???????????this.scence = new THREE.Scene(); ?// 创建舞台 ???????????????????????// 设置视角及参数 ???????????this.camera = new THREE.PerspectiveCamera(45, this.width / this.height, 1, 10000); ???????????this.camera.position.set(0,0,10); ???????????this.camera.lookAt(new THREE.Vector3(0, 0, 0)); ???    // 设置灯光及参数 ???????????this.light = new THREE.AmbientLight(0xDDDDDD); ???????????this.light.position.set(100, 100, 200); ???????????this.scence.add(this.light); ???????????// 创建角色 ???????????var circle = ?new THREE.SphereGeometry(755,50,50); ???????????var texture = new THREE.TextureLoader(); ???????????????????????var material = new THREE.MeshBasicMaterial(); ???????????// 给circle添加背景图片 ???????????material.map = texture.load("images/earth_atmos_4096.jpg",function(){ ???????????????three.renderer.render(three.scence, three.camera); ???????????}); ???????????three.mesh = new THREE.Mesh(circle,material); ???????????three.mesh.position.set(0,0,-5000); ???????????three.scence.add(three.mesh); ???????}, ???????animate:function() { ???????????requestAnimationFrame(three.animate); ???????????three.mesh.rotation.y += 0.01; ???????????three.renderer.render(three.scence, three.camera); ???????}, ???????start:function() { ???????????this.init(); ???????????this.animate(); ???????} ???} ???var three = new Three(".main"); ???three.start();</script>

这里我把地球图片上传上来,不然没有图片也无法加载。

最后提醒大家一点,当有图片加载到three.js 时,需要在服务器端运行。不然也会报错。

今天的three.js 就讲到这里了,下次再会。

three.js - (入门四)

原文地址:https://www.cnblogs.com/lafitewu/p/8241987.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved