From 056ea1d2b6af8b6f26732fe81f62c101f4ba8deb Mon Sep 17 00:00:00 2001 From: Emil Widlund Date: Wed, 9 May 2018 10:39:25 +0200 Subject: [PATCH] Cleaned up the textureLoader code --- debug/app.coffee | 10 +++++++--- form/Model.coffee | 28 +++++++++++++++++++++------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/debug/app.coffee b/debug/app.coffee index 2167fb2..2dd9e21 100644 --- a/debug/app.coffee +++ b/debug/app.coffee @@ -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 = diff --git a/form/Model.coffee b/form/Model.coffee index 488981b..ae04108 100644 --- a/form/Model.coffee +++ b/form/Model.coffee @@ -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' @@ -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 @@ -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 @@ -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) -> @@ -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() \ No newline at end of file + @action.play() + + @define 'map', + get: -> @mesh.material.map, + set: (path) -> + @applyTextureMap path \ No newline at end of file