PLM之家PLMHome-工业软件践行者

[前端框架] Threejs 基于webGL的3D开发神器

[复制链接]

2019-9-1 18:45:09 2799 0

admin 发表于 2019-9-1 18:45:09 |阅读模式

admin 楼主

2019-9-1 18:45:09

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

x

5 ?" x  @8 O" j9 _
4 h: E' \) v( Q" g/ m, l8 f废话不多说,官网自己百度下, threejs.org ,入门很简单。
4 t6 v, y% F8 @! \5 E9 E7 Q
+ n% E! t( o' y9 K9 C' @8 i$ B创建的javascript脚本如下: u/ J8 A/ p- R5 ?2 P/ R
6 @5 Q; ^  h* @
GIF.gif
4 ^0 K. ^) W5 m# F2 g$ ~[mw_shl_code=javascript,true]
7 f1 o0 C2 R- R8 y/ h0 C4 wvar scene = new THREE.Scene();, ^9 h/ x5 }3 i6 @) G* n- a3 X$ h
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);' C$ S. j! M' j1 ^! \
var renderer = new THREE.WebGLRenderer();7 q3 g9 q/ h# v
renderer.setSize(window.innerWidth, window.innerHeight);) }. y* a) z6 s$ v9 P8 I
document.body.appendChild(renderer.domElement);
9 `5 }; L9 g1 \7 a( w7 U. e0 y/ e/ |" S8 ^! z
// Refresh Issue
1 s" S& @3 L. m! a$ e8 J( p  N2 v- Nwindow.addEventListener('resize', function () {- `/ }4 n& Y$ @" @2 F9 c2 Z4 O
    var width = this.window.innerWidth;1 k, n+ d4 l# S1 V4 \( {% _
    var height = this.window.innerHeight;+ r. p" z  S% y( \3 n
    render.setSize(width, height);
1 {% w4 I. T, f( L. W1 f% E" b    camera.aspect = width / height;7 Q5 v9 k( X0 N- I3 N3 ^
    camera.updateProjectionMatrix();- x: b& _1 v. O2 L0 i
})
' p7 k) S# V" x( ?% H3 f
$ R2 ~8 V: Z& R7 i2 \//orbit contorls
/ R9 p2 ]0 c8 u% M$ wcontrols = new THREE.OrbiTControls(camera,renderer.domElement);; Q! D6 q9 {2 F

. V9 _) H6 N0 _5 `& o, [% q# S//create the shape$ r, s$ P+ d5 A% x) f. O
var geometry = new THREE.TorusGeometry(2, 1, 16, 100);
9 U! F( q7 W; z& I- @% v9 n1 {//create  a meterial
" q8 r3 M; @& Z# g/ [, Yvar material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true, });+ o) H/ D0 H3 g0 K. E
//var myMaterial = [new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('image/plmhome.png'),side:THREE.DoubleSide}),];: f4 Q! ?# @" d+ c, R2 v4 }) V* z' Y
//var material = new THREE.MeshFaceMaterial(myMaterial);
; H' O+ m' C2 L1 X0 @
& H( j$ ~* D: o# e/ F+ Z" d, Uvar cube = new THREE.Mesh(geometry, material);) E+ E% Z) @0 d! p2 l; G
scene.add(cube);
" `; l4 p& A) @( H+ c+ ocamera.position.z = 5;
# I5 p) _+ ^; t- V9 P$ N
5 O9 m- Z# N4 E
+ s( T, a3 z+ R% V
6 j) S6 e) R# U7 Y% G7 h/ S% S
& o9 i( q2 d4 q# F! L
# F0 {8 g4 u: J1 K! F//rotate
6 c5 o( r& S' a7 e( k( Xvar update = function () {, }5 B7 y# V1 w7 w
    cube.rotation.x += .01;- I6 B: Q( B" L
    cube.rotation.y += .02;
% M9 G& L0 E) x" r    cube.rotation.z += .02;
8 b5 ^8 y; J, t9 ~}
: u4 `( u6 i0 R7 J' E
' C) C5 n% i1 T- l$ Vvar render = function () {
" L( Q& A9 V+ j    renderer.render(scene, camera);
& l" i% l( U/ g/ {; C  z) m}+ r$ M, l0 ?+ I3 a
; \2 f# S" Y9 l6 {5 T

7 p3 S6 N1 g- C  n+ p/ L2 l* V0 r7 V# ?8 a2 C; w0 d1 ^
var Gameloop = function () {& l7 X0 e7 g( d# s7 M# ?
    requestAnimationFrame(Gameloop);
3 j) m! k! e. k6 ]1 D    update();* P4 j8 q6 P  M
    render();
# L% ^  m7 M7 i) |( u: W/ J: y}2 N, t) q6 z1 ~: L

0 X& j' U1 E7 WGameloop();  i7 r/ {: ~  Q. }7 a6 e" v4 E% a5 K
[/mw_shl_code]. D5 g4 Q6 W* a' M  R" L, G/ k

% i  P& |, q2 G8 w% _
4 z  }% x) u! [, n* ?7 {6 c) U引入争取的html,
( g) `0 g" t; A' T  D
- i8 p/ C' K+ t# y/ _6 l[mw_shl_code=html,true]<!DOCTYPE html>1 d# C# S9 e2 e+ `
<html>3 {1 d# ~- [) _

8 U/ q. M# O$ L1 b3 {6 h% }4 k<head>+ S: F0 Q) z9 P6 H
    <title>hello ThreeJS</title>
7 W7 h/ S4 Z: Q/ v/ X* R' w8 S    <style>, @. N  R$ m! U- h/ A
        body {
: l: E! T% r! C0 W2 `            margin: 0;' y2 c1 v  q% z2 X
        }: j( S6 [: I) _2 o; T6 f3 b

7 l$ `: b8 s6 ^+ T! N        canvas {. V  N5 R% x+ h2 L
            width: 100%;9 A+ m3 J; m/ t5 x; \; z  M4 C5 o8 k0 N
            height: 100%;* T( n0 g* |$ j3 t- h  |- K
        }* E: l5 k% M4 i+ F
1 D/ Y& h2 \" p8 x7 I
        h1 {
+ ?6 H$ ?. q- c. V            color: aqua;4 z# _& r6 J; @! w2 Z
        }
5 _9 }' g. x% O2 P9 c    </style>
; b- R9 ^& f% Q5 g. w</head>* O+ F! {7 @# Y! S, }  r

$ h) I0 E. L8 ]+ x/ V<body>
% r) s' x$ A  E <script src="js/three.js"></script>
* P2 v1 u9 f/ c) m  \    <script src="js/OrbitControls.js"></script>) k/ c* u+ G1 o
    <script src="myjs.js"></script>1 y0 w4 d+ P% W
  
! Q7 i: `' j1 H: ^) q+ o0 _! s$ q  V7 T( L

$ p/ |8 o2 ^% n6 k6 @) `$ W
% H  [3 [3 c" ^& A, D</body>- H& o/ ^7 a% ^) ~- A2 s
$ e( x- W% B+ B, \/ H, F' s9 i6 O
</html>[/mw_shl_code]
8 |! c0 m5 K/ _( E2 g  Y1 h( a3 T; ?0 o) Y, \
效果点击这里:$ h* |9 s! K/ Y, g' e

" i$ B, V+ W9 A6 a% [http://plmhome.com/doteam/lesson1.html
) o1 ^1 d" I( l8 W2 ~4 L7 A
  ^: t0 R0 v0 h( O8 C; B, j) [1 Y" i% |
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 doTeam.tech
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了