|
7 | 7 | <style> |
8 | 8 | body { |
9 | 9 | font-family: Monospace; |
10 | | - background-color: #f0f0f0; |
11 | 10 | margin: 0px; |
12 | 11 | overflow: hidden; |
13 | 12 | } |
| 13 | + a { |
| 14 | + color:#0078ff; |
| 15 | + } |
| 16 | + #info { |
| 17 | + position: absolute; |
| 18 | + top: 10px; width: 100%; |
| 19 | + color: #ffffff; |
| 20 | + padding: 5px; |
| 21 | + font-family: Monospace; |
| 22 | + font-size: 13px; |
| 23 | + text-align: center; |
| 24 | + z-index:100; |
| 25 | + } |
14 | 26 | </style> |
15 | 27 | </head> |
16 | 28 | <body> |
17 | 29 |
|
| 30 | + <div id="info"> |
| 31 | + <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - software renderer</br> |
| 32 | + drag to change the point of view |
| 33 | + </div> |
| 34 | + |
18 | 35 | <script src="../build/three.js"></script> |
19 | 36 | <script src="js/geometries/hilbert3D.js"></script> |
20 | 37 | <script src="js/controls/TrackballControls.js"></script> |
|
23 | 40 | <script src="js/libs/stats.min.js"></script> |
24 | 41 | <script> |
25 | 42 |
|
26 | | - var container, stats; |
27 | | - |
28 | | - var camera, controls, scene, renderer; |
| 43 | + var camera, controls, scene, renderer, stats; |
29 | 44 |
|
30 | 45 | var torus, cube, texCube; |
31 | 46 |
|
|
36 | 51 |
|
37 | 52 | function init() { |
38 | 53 |
|
39 | | - container = document.createElement( 'div' ); |
| 54 | + var container = document.createElement( 'div' ); |
40 | 55 | document.body.appendChild( container ); |
41 | 56 |
|
42 | | - var info = document.createElement( 'div' ); |
43 | | - info.style.position = 'absolute'; |
44 | | - info.style.top = '10px'; |
45 | | - info.style.width = '100%'; |
46 | | - info.style.textAlign = 'center'; |
47 | | - info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js<a/> - software renderer<br/>drag to change the point of view'; |
48 | | - container.appendChild( info ); |
49 | | - |
50 | 57 | camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 2000 ); |
51 | 58 | camera.position.z = 600; |
52 | 59 |
|
|
55 | 62 | scene = new THREE.Scene(); |
56 | 63 |
|
57 | 64 | // Torus |
58 | | - var geometry = new THREE.TorusKnotGeometry( 150 ); |
| 65 | + var torusGeometry = new THREE.TorusKnotBufferGeometry( 150, 10 ); |
| 66 | + torusGeometry = torusGeometry.toNonIndexed(); |
59 | 67 |
|
60 | | - for ( var i = 0, j = geometry.faces.length; i < j; i ++ ) { |
| 68 | + var colors = []; |
| 69 | + var color = new THREE.Color(); |
61 | 70 |
|
62 | | - geometry.faces[ i ].color.setHex( Math.random() * 0xffffff ); |
| 71 | + for ( var i = 0; i < torusGeometry.attributes.position.count; i ++ ) { |
| 72 | + |
| 73 | + color.setHex( Math.random() * 0xffffff ); |
| 74 | + colors.push( color.r, color.g, color.b ); |
63 | 75 |
|
64 | 76 | } |
65 | 77 |
|
66 | | - torus = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x0000ff, vertexColors: THREE.FaceColors } ) ); |
| 78 | + torusGeometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) ); |
| 79 | + |
| 80 | + torus = new THREE.Mesh( torusGeometry, new THREE.MeshBasicMaterial( { color: 0x0000ff, vertexColors: THREE.VertexColors } ) ); |
67 | 81 | scene.add( torus ); |
68 | 82 |
|
69 | 83 | // Cube |
70 | | - var geometry = new THREE.BoxGeometry( 200, 200, 200 ); |
| 84 | + var boxGeometry = new THREE.BoxBufferGeometry( 200, 200, 200 ); |
| 85 | + boxGeometry = boxGeometry.toNonIndexed(); |
71 | 86 |
|
72 | | - for ( var i = 0, j = geometry.faces.length; i < j; i ++ ) { |
| 87 | + colors = []; |
73 | 88 |
|
74 | | - geometry.faces[ i ].color.setHex( Math.random() * 0xffffff ); |
| 89 | + for ( var i = 0; i < boxGeometry.attributes.position.count; i ++ ) { |
| 90 | + |
| 91 | + color.setHex( Math.random() * 0xffffff ); |
| 92 | + colors.push( color.r, color.g, color.b ); |
75 | 93 |
|
76 | 94 | } |
77 | 95 |
|
78 | | - cube = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x00ff00, vertexColors: THREE.FaceColors } ) ); |
| 96 | + boxGeometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) ); |
| 97 | + |
| 98 | + cube = new THREE.Mesh( boxGeometry, new THREE.MeshBasicMaterial( { color: 0x00ff00, vertexColors: THREE.VertexColors } ) ); |
79 | 99 | scene.position.set( 100, 0, 0 ); |
80 | 100 | scene.add( cube ); |
81 | 101 |
|
82 | 102 | // Cube with texture |
83 | 103 | var loader = new THREE.TextureLoader(); |
84 | 104 | var map = loader.load( 'textures/brick_diffuse.jpg' ); |
85 | | - texCube = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: map } ) ); |
| 105 | + texCube = new THREE.Mesh( boxGeometry, new THREE.MeshLambertMaterial( { map: map } ) ); |
86 | 106 | texCube.position.set( - 300, 0, 0 ); |
87 | 107 | scene.add( texCube ); |
88 | 108 |
|
|
101 | 121 | scene.add( texSprite ); |
102 | 122 |
|
103 | 123 | // Line |
104 | | - var points = hilbert3D( new THREE.Vector3( 0, 0, 0 ), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 ); |
105 | | - var spline = new THREE.CatmullRomCurve3( points ); |
106 | | - var n_sub = 6, colors = []; |
| 124 | + var hilbertPoints = hilbert3D( new THREE.Vector3( 0, 0, 0 ), 200.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 ); |
| 125 | + |
| 126 | + var lineGeometry = new THREE.BufferGeometry(); |
| 127 | + var subdivisions = 6; |
| 128 | + |
| 129 | + var vertices = []; |
| 130 | + colors = []; |
107 | 131 |
|
108 | | - var lineGeometry = new THREE.Geometry(); |
109 | | - var vertices = lineGeometry.vertices; |
| 132 | + var point = new THREE.Vector3(); |
| 133 | + color.setHex( 0x88aaff ); |
110 | 134 |
|
111 | | - for ( var i = 0; i < points.length * n_sub; i ++ ) { |
| 135 | + var spline = new THREE.CatmullRomCurve3( hilbertPoints ); |
112 | 136 |
|
113 | | - var t = i / ( points.length * n_sub ); |
114 | | - vertices[ i ] = spline.getPoint( t ); |
115 | | - colors[ i ] = new THREE.Color( 0x88aaff ); |
| 137 | + for ( var i = 0; i < hilbertPoints.length * subdivisions; i ++ ) { |
| 138 | + |
| 139 | + var t = i / ( hilbertPoints.length * subdivisions ); |
| 140 | + spline.getPoint( t, point ); |
| 141 | + |
| 142 | + vertices.push( point.x, point.y, point.z ); |
| 143 | + colors.push( color.r, color.g, color.b ); |
116 | 144 |
|
117 | 145 | } |
118 | 146 |
|
119 | | - lineGeometry.colors = colors; |
| 147 | + lineGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) ); |
| 148 | + lineGeometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) ); |
120 | 149 |
|
121 | 150 | var material = new THREE.LineBasicMaterial( { opacity: 1, linewidth: 3, vertexColors: THREE.VertexColors } ); |
122 | 151 | var line = new THREE.Line( lineGeometry, material ); |
123 | 152 | line.scale.set( 0.5, 0.5, 0.5 ); |
124 | 153 | line.position.set( 0, - 200, 0 ); |
125 | 154 | scene.add( line ); |
126 | 155 |
|
| 156 | + // |
| 157 | + |
127 | 158 | renderer = new THREE.SoftwareRenderer(); |
128 | 159 | renderer.setSize( window.innerWidth, window.innerHeight ); |
129 | 160 |
|
|
0 commit comments