|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
2 X2 u2 S G3 C% J m2 r9 ]
}1 O2 W0 @$ D9 O) A+ }4 J废话不多说,官网自己百度下, threejs.org ,入门很简单。8 s, Q4 e% J- G- v1 d
|, ]! f/ O( f$ M2 k5 ]! f+ \8 V7 f创建的javascript脚本如下
7 e! ?5 ^5 k8 k) J s [6 D- G% U* \5 x, X
2 a! R* |* ~+ G/ j- _$ e5 \* A
[mw_shl_code=javascript,true]
! D8 n$ @" S! B/ ^: ^0 Fvar scene = new THREE.Scene();% u5 _# j& k$ r# u9 Y/ V, J( s' p$ U* V
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
4 f, x" g' E0 o" h' q- A* lvar renderer = new THREE.WebGLRenderer();
. e0 e( H f; \renderer.setSize(window.innerWidth, window.innerHeight);
' o6 V; u) B- m ^/ |4 L) P7 q3 xdocument.body.appendChild(renderer.domElement);7 }" C7 E+ I7 V q
! a: i; u1 E+ c* x b# x" J" Y
// Refresh Issue $ l" v9 M' \3 m7 Q( a4 R
window.addEventListener('resize', function () {' Z, Q$ W, X/ N: U3 Y
var width = this.window.innerWidth;
5 W) r9 [1 z1 Y) w8 K' ]* C- m var height = this.window.innerHeight;5 r* o0 Q4 @8 s2 |
render.setSize(width, height);
: {' z5 Q$ Y2 V camera.aspect = width / height;
5 ]% w* m: h" c& e8 _ camera.updateProjectionMatrix();
' A j- h4 G! L, s! X0 Z; Y+ t1 }1 H})
9 Y: l Z' P3 F" B3 T! M9 B+ D. K. I
//orbit contorls7 f2 O" W/ O1 w3 ~
controls = new THREE.OrbiTControls(camera,renderer.domElement);9 N9 @; A" |! `% u
# o2 t# }! j; I) D0 B/ B, x
//create the shape
9 |' x( d: ?$ X& {$ a+ hvar geometry = new THREE.TorusGeometry(2, 1, 16, 100);
) O. L; p- Y6 `* {* m# M* [% ~//create a meterial
/ S5 K! N: p: M; Uvar material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true, });0 u# C3 x, u+ {2 A! t
//var myMaterial = [new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('image/plmhome.png'),side:THREE.DoubleSide}),];
7 B) { ~1 s& K//var material = new THREE.MeshFaceMaterial(myMaterial);; U6 F! K& [8 [
: {5 G6 @2 D* {5 L6 N2 Z: G
var cube = new THREE.Mesh(geometry, material);
) M0 \; c$ T! U- oscene.add(cube);5 {9 \4 s, q. b0 v- ~
camera.position.z = 5;
8 D3 b0 i( `0 X& N) O4 J8 F$ _/ D) C# L& x: v
+ s! v) T$ Z# F, i5 G
% a1 F5 q5 ? ?
- f9 p1 o& r1 o9 K: T
$ ?* g" @4 r7 Q
//rotate
% Y0 R. ~! S$ Hvar update = function () {
* { z" q- [5 R( B7 Q+ V cube.rotation.x += .01;% O$ i/ f+ O. p4 M/ T& l
cube.rotation.y += .02;
) {& m4 L1 E. X8 }& Q" p cube.rotation.z += .02;& R, ~# o5 m9 a; H1 d3 H4 p+ c V
}9 o) U2 g0 }3 H
F6 V+ [2 y+ I2 ^var render = function () { I7 ?% h* f: ]" x% ]
renderer.render(scene, camera);# o/ R) U" u" A( }
}! }, E, @2 Q5 Z2 `& o: B' Q" M
$ e. P: p: Z$ Q
5 H; k3 I, c' Z( p& |( z8 ^: @
+ i6 L2 ~! a3 X; K4 ~8 `5 Rvar Gameloop = function () {
' }# q1 k0 \+ x$ m requestAnimationFrame(Gameloop);
: a6 S" Y3 Z- f update();
j, s/ Z) S3 W9 g, ~* s render();+ B5 p& M* o+ S+ E: s) @8 I2 q
}
% K3 A+ a3 y" b: ~2 C( u( h% d6 ^4 d5 Z$ l
Gameloop();
* F _/ _5 L7 t: b% E[/mw_shl_code]/ C% T2 c, k' m
0 T3 [- R V' H* r" {1 D, z! I" n: U/ p
引入争取的html,
; j! J2 P3 ]+ |/ H9 v' F9 l- N3 F& u) [) U/ B* ^7 K
[mw_shl_code=html,true]<!DOCTYPE html>
! r/ N$ O9 a+ \( k5 L6 {4 V) ]% `<html>
' }, I4 q5 N8 c7 E4 k3 ?) z' N; s; u4 t$ C( h
<head>
+ {/ d7 | U% V+ R4 y <title>hello ThreeJS</title> S4 n9 v& |4 C/ \" N1 u
<style>1 O& k0 ]4 D) n/ I* [
body {
% o4 V# s* L4 `+ ` margin: 0;
/ `0 l5 V5 z- u( X) Q3 J2 @- M6 ~ }7 H$ n$ Y v* m6 g
1 x" c$ q, M( s7 Z$ Z X- l
canvas {
# H( T( f+ N' f$ X; f width: 100%;
9 v6 ~ i4 n6 U6 h height: 100%;
3 L* h" E8 @9 p6 w; @2 E }
3 e4 z7 o. ?+ ]- K9 z: `5 Z" \, l, Y+ C
h1 { c) Z- ], m7 _# R
color: aqua;
% M5 v+ j# C1 t% ]3 }0 B6 | }
. e: |8 N7 d- c </style>
8 q/ ]8 x1 D1 \6 Z1 S/ v3 u- W3 A</head>
% H( c. S% _6 W* k' W' a2 e5 {2 M0 c) k- x& A6 a5 o
<body>2 c. k0 O6 ^$ W* X
<script src="js/three.js"></script>4 e y3 g! F9 y3 E+ _% F
<script src="js/OrbitControls.js"></script>+ v# w0 A, U. s
<script src="myjs.js"></script>
! H# [- f( t+ q+ z4 K/ @7 E8 p
; k' ?6 x! J- G! r
' g& ~0 ]) O/ ~) e/ E8 ~5 y0 H" B
. F9 z2 B5 N$ {# \; g! o7 Y: q# k- a3 V. j- b
</body>
. w Z6 Z3 y) i* V {0 F
$ a. N u. k, `5 Z4 }$ @8 X</html>[/mw_shl_code]
8 o5 d' } ~+ h2 i
1 \* }4 a" S! D7 o: O/ A效果点击这里:
# V, h8 }+ d7 p6 y% C: m. W/ I1 Y, | [
http://plmhome.com/doteam/lesson1.html
9 d# I* c7 Y" M8 [' d' i2 ^4 _& q$ J X8 P9 P# K# E2 p+ ]* ]
% L5 s* X A- r: [ i2 f: v |
|