Skip to content

Commit

Permalink
Cleaned up the textureLoader code
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwidlund committed May 9, 2018
1 parent 2c15bc9 commit 056ea1d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
10 changes: 7 additions & 3 deletions debug/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ scene = new Scene
width: Screen.width
height: Screen.height

new Light
parent: scene
type: 'point'
y: 200
z: 100

new Model
path: './models/flamingo/flamingo.json'
parent: scene
rotationY: -40
material: new MeshNormalMaterial
morphTargets: true
flatShading: true
map: './images/icon.png'
onLoad: (model) ->

model.states =
Expand Down
28 changes: 21 additions & 7 deletions form/Model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class exports.Model extends BaseClass
castShadow: true
receiveShadow: true
reposition: true

@textureLoader = new THREE.TextureLoader()

switch @getExtension properties.path
when 'obj'
Expand Down Expand Up @@ -67,11 +69,6 @@ class exports.Model extends BaseClass
if properties.material
@applyMaterial properties.material

if properties.map
new THREE.TextureLoader().load properties.map, (map) =>
@mesh.material.map = map
@mesh.material.needsUpdate = true

@setupShadowSettings properties

if properties.smoothShading
Expand Down Expand Up @@ -100,7 +97,15 @@ class exports.Model extends BaseClass
@_states = new States @

if properties.onLoad
properties.onLoad @
if properties.map
@textureLoader.load properties.map, (map) =>
@mesh.material.map = map
@mesh.material.needsUpdate = true
properties.onLoad @
else
properties.onLoad @
else if properties.map
@applyTextureMap properties.map

repositionMesh: () ->
@boundingBox = new THREE.Box3().setFromObject @mesh
Expand All @@ -115,6 +120,10 @@ class exports.Model extends BaseClass
if c instanceof THREE.Mesh
c.material = material

applyTextureMap: (path) ->
@textureLoader.load path, (map) =>
@mesh.material.map = map
@mesh.material.needsUpdate = true

setupShadowSettings: (properties) ->
@mesh.traverse (c) ->
Expand Down Expand Up @@ -349,4 +358,9 @@ class exports.Model extends BaseClass
if @mesh.animations[animation - 1]
@animationIndex = animation - 1
@action = @mesh.mixer.clipAction @mesh.animations[@animationIndex]
@action.play()
@action.play()

@define 'map',
get: -> @mesh.material.map,
set: (path) ->
@applyTextureMap path

0 comments on commit 056ea1d

Please sign in to comment.