-
Notifications
You must be signed in to change notification settings - Fork 332
/
script.js
138 lines (111 loc) · 4.5 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { Rhino3dmLoader } from 'three/examples/jsm/loaders/3DMLoader'
import { GUI } from 'dat.gui'
const model = 'hello_mesh.3dm'
let scene, camera, renderer, material, light
init()
initGui()
load()
function load() {
material = new THREE.MeshPhysicalMaterial()
loadPBRMaterial(material, 'chipped-paint-metal')
material.metalness = 0.75
material.roughness = 0.15
material.normalScale.x = 1.0
material.normalScale.y = 1.0
material.envMap = scene.background
const loader = new Rhino3dmLoader()
loader.setLibraryPath( 'https://unpkg.com/[email protected]/' )
// load 3dm file into three.js scene
loader.load( model, function ( object ) {
object.traverse( function (child) {
// rotate each child object to match three.js default coordinate system (y = up)
// normally we'd update the camera but this messes with the cubemaps
child.rotateX( - Math.PI / 4 )
// apply initial pbr material
child.material = material
})
scene.add( object )
console.log( object )
// hide spinner
document.getElementById('loader').style.display = 'none'
} )
}
function loadPBRMaterial(material, name) {
let tl = new THREE.TextureLoader()
tl.setPath('materials/PBR/' + name + '/')
material.map = tl.load(name + '_base.png')
material.aoMmap = tl.load(name + '_ao.png')
material.normalMap = tl.load(name + '_normal.png')
material.metalnessMap = tl.load(name + '_metallic.png')
}
function loadCubeMap(name, prefix, format) {
const ctl = new THREE.CubeTextureLoader()
ctl.setPath('textures/cube/' + name + '/')
return ctl.load([prefix + 'px.' + format, prefix + 'nx.' + format, prefix + 'py.' + format,
prefix + 'ny.' + format, prefix + 'pz.' + format, prefix + 'nz.' + format])
}
function init() {
scene = new THREE.Scene()
scene.background = loadCubeMap('Park2', '', 'jpg')
camera = new THREE.PerspectiveCamera( 35, window.innerWidth/window.innerHeight, 1, 10000 )
camera.position.z = 50
light = new THREE.DirectionalLight(0xffffff, 1.150)
light.position.set(-10, 10, 0)
camera.add( light )
scene.add(camera)
renderer = new THREE.WebGLRenderer({antialias: true})
renderer.setPixelRatio( window.devicePixelRatio )
renderer.setSize( window.innerWidth, window.innerHeight )
renderer.shadowMap.enabled = true
document.body.appendChild( renderer.domElement )
const controls = new OrbitControls( camera, renderer.domElement )
window.addEventListener('resize', onWindowResize, false)
animate()
}
function initGui() {
var UserInterfaceControls = function() {
this.lightColor = [light.color.r*255, light.color.g*255, light.color.b*255]
this.cubemap = 'Park2'
this.metalness = 0.75
this.roughness = 0.15
this.textures = 'chipped-paint-metal'
}
var ui_callbacks = new UserInterfaceControls()
var gui = new GUI()
gui.addColor(ui_callbacks, 'lightColor').onChange(function(value) {
light.color.r = value[0]/255.0
light.color.g = value[1]/255.0
light.color.b = value[2]/255.0
})
gui.add(ui_callbacks, 'cubemap',[ 'Bridge2', 'MilkyWay', 'Park2', 'Park3Med', 'pisa', 'skyboxsun25deg', 'SwedishRoyalCastle' ]).onChange(function(value){
const format = value === 'pisa' ? 'png' : 'jpg'
scene.background = loadCubeMap(value, '', format)
material.envMap = scene.background
})
let matFolder = gui.addFolder('Material')
matFolder.add(ui_callbacks, 'metalness', 0.0, 1.0).onChange(function(value) {
material.metalness = value
})
matFolder.add(ui_callbacks, 'roughness', 0.0, 1.0).onChange(function(value) {
material.roughness = value
})
matFolder.add(ui_callbacks, 'textures',
[ 'carbon-fiber', 'chipped-paint-metal', 'copper-rock1', 'grimy-metal',
'metalgrid2', 'ornate-brass2', 'rust-coated', 'rust-panel', 'scuffed-plastic',
'streaked-metal1' ]).onChange(function(value){
loadPBRMaterial(material, value)
})
matFolder.open()
}
function animate() {
requestAnimationFrame( animate )
renderer.render( scene, camera )
}
function onWindowResize(){
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize( window.innerWidth, window.innerHeight )
animate()
}