Browse Source

Merge branch 'dev-blender-lights' of https://github.com/fscz/three.js into dev

Mr.doob 11 years ago
parent
commit
e4f14ab41d

+ 55 - 44
src/loaders/SceneLoader.js

@@ -361,50 +361,61 @@ THREE.SceneLoader.prototype = {
 
 					// lights
 
-					} else if ( objJSON.type === "DirectionalLight" || objJSON.type === "PointLight" || objJSON.type === "AmbientLight" ) {
-
-						hex = ( objJSON.color !== undefined ) ? objJSON.color : 0xffffff;
-						intensity = ( objJSON.intensity !== undefined ) ? objJSON.intensity : 1;
-
-						if ( objJSON.type === "DirectionalLight" ) {
-
-							pos = objJSON.direction;
-
-							light = new THREE.DirectionalLight( hex, intensity );
-							light.position.fromArray( pos );
-
-							if ( objJSON.target ) {
-
-								target_array.push( { "object": light, "targetName" : objJSON.target } );
-
-								// kill existing default target
-								// otherwise it gets added to scene when parent gets added
-
-								light.target = null;
-
-							}
-
-						} else if ( objJSON.type === "PointLight" ) {
-
-							pos = objJSON.position;
-							dst = objJSON.distance;
-
-							light = new THREE.PointLight( hex, intensity, dst );
-							light.position.fromArray( pos );
-
-						} else if ( objJSON.type === "AmbientLight" ) {
-
-							light = new THREE.AmbientLight( hex );
-
-						}
-
-						parent.add( light );
-
-						light.name = objID;
-						result.lights[ objID ] = light;
-						result.objects[ objID ] = light;
-
-					// cameras
+		            // lights
+		          } else if (objJSON.type === "PointLight" || objJSON.type === "AmbientLight" || objJSON.type === "SpotLight" || objJSON.type === "HemisphereLight" || objJSON.type === "AreaLight") {
+		            function rotate(x, y, z, vector3) {
+		              var target = new THREE.Matrix4(
+		                vector3[0], 0, 0, 0
+		                , vector3[1], 0, 0, 0
+		                , vector3[2], 0, 0, 0
+		                , 0         , 0, 0, 0
+		              );
+		              var rotateX = new THREE.Matrix4(),
+		                  rotateY = new THREE.Matrix4(),
+		                  rotateZ = new THREE.Matrix4();
+		              rotateX.makeRotationX(x);
+		              rotateY.makeRotationY(y);
+		              rotateZ.makeRotationZ(z);
+		              var rotateM = new THREE.Matrix4();
+		              rotateM.multiplyMatrices(rotateX, rotateY, rotateZ);
+		              target.multiplyMatrices(rotateM, target);
+		              return target;
+		            }
+
+		            var intensity = objJSON.intensity;
+		            var color = objJSON.color;
+		            var distance = objJSON.distance;
+		            var position = objJSON.position;
+		            var rotation = objJSON.rotation;
+		            if (objJSON.type === "PointLight") {
+		              light = new THREE.PointLight(color, intensity, distance);
+		              light.position.fromArray(position);
+		            } else if (objJSON.type === "SpotLight") {
+		              light = new THREE.SpotLight(color, intensity, distance, 1);
+		              light.angle = objJSON.angle;
+		              light.position.fromArray(position);
+		              var target = rotate(rotation[0], rotation[1], rotation[2],
+		                                  [position[0], position[1] - distance, position[2]]);
+		              light.target.position = new THREE.Vector3(target.elements[0], target.elements[1], target.elements[2]);
+		            } else if (objJSON.type === "AmbientLight") {
+		              light = new THREE.AmbientLight(color);
+		            } else if (objJSON.type === "HemisphereLight") {
+		              light = new THREE.DirectionalLight(color, intensity, distance);
+		              var target = rotate(rotation[0], rotation[1], rotation[2],
+		                                  [position[0], position[1] - distance, position[2]]);
+		              light.target.position = new THREE.Vector3(target.elements[0], target.elements[1], target.elements[2]);
+		            } else if (objJSON.type === "AreaLight") {
+		              light = new THREE.AreaLight(color, intensity);
+		              light.position.set(position[0], position[1], position[2]);
+		              light.width = objJSON.size;
+		              light.height = objJSON.size_y;
+		            }
+		            parent.add( light );
+		            light.name = objID;
+		            result.lights[ objID ] = light;
+		            result.objects[ objID ] = light;
+
+		            // cameras
 
 					} else if ( objJSON.type === "PerspectiveCamera" || objJSON.type === "OrthographicCamera" ) {
 

+ 119 - 35
utils/exporters/blender/2.66/scripts/addons/io_mesh_threejs/export_threejs.py

@@ -196,21 +196,62 @@ TEMPLATE_CAMERA_ORTHO = """\
 		"target"  : %(target)s
 	}"""
 
-TEMPLATE_LIGHT_DIRECTIONAL = """\
-	%(light_id)s : {
-		"type"       : "DirectionalLight",
-		"direction"  : %(direction)s,
-		"color"      : %(color)d,
-		"intensity"  : %(intensity).2f
-	}"""
-
 TEMPLATE_LIGHT_POINT = """\
-	%(light_id)s : {
-		"type"       : "PointLight",
-		"position"   : %(position)s,
-		"color"      : %(color)d,
-		"intensity"  : %(intensity).3f
-	}"""
+        %(light_id)s : {
+                "type"       : "PointLight",
+                "position"   : %(position)s,
+                "rotation"   : %(rotation)s,
+                "color"      : %(color)d,
+                "distance"   : %(distance).3f,
+                "intensity"     : %(intensity).3f
+        }"""
+
+TEMPLATE_LIGHT_SUN = """\
+        %(light_id)s : {
+                "type"       : "AmbientLight",
+                "position"   : %(position)s,
+                "rotation"   : %(rotation)s,
+                "color"      : %(color)d,
+                "distance"   : %(distance).3f,
+                "intensity"     : %(intensity).3f
+        }"""
+
+TEMPLATE_LIGHT_SPOT = """\
+        %(light_id)s : {
+                "type"       : "SpotLight",
+                "position"   : %(position)s,
+                "rotation"   : %(rotation)s,
+                "color"      : %(color)d,
+                "distance"   : %(distance).3f,
+                "intensity"     : %(intensity).3f,
+                "use_shadow" : %(use_shadow)d,
+                "angle"      : %(angle).3f
+        }"""
+
+TEMPLATE_LIGHT_HEMI = """\
+        %(light_id)s : {
+                "type"       : "HemisphereLight",
+                "position"  : %(position)s,
+                "rotation"   : %(rotation)s,
+                "color"      : %(color)d,
+                "distance"   : %(distance).3f,
+                "intensity"     : %(intensity).3f
+        }"""
+
+TEMPLATE_LIGHT_AREA = """\
+        %(light_id)s : {
+                "type"       : "AreaLight",
+                "position"   : %(position)s,
+                "rotation"   : %(rotation)s,
+                "color"      : %(color)d,
+                "distance"   : %(distance).3f,
+                "intensity"     : %(intensity).3f,
+                "gamma"      : %(gamma).3f,
+                "shape"      : "%(shape)s",
+                "size"       : %(size).3f,
+                "size_y"     : %(size_y).3f
+        }"""
+
 
 TEMPLATE_VEC4 = '[ %g, %g, %g, %g ]'
 TEMPLATE_VEC3 = '[ %g, %g, %g ]'
@@ -1241,7 +1282,7 @@ def generate_materials_string(mesh, scene, option_colors, draw_type, option_copy
 
 def handle_texture(id, textures, material, filepath, option_copy_textures):
 
-    if textures[id]:
+    if textures[id] and textures[id]['texture'].users > 0 and len(textures[id]['texture'].users_material) > 0:
         texName     = 'map%s'       % id.capitalize()
         repeatName  = 'map%sRepeat' % id.capitalize()
         wrapName    = 'map%sWrap'   % id.capitalize()
@@ -1613,7 +1654,9 @@ def generate_quat(quat):
 def generate_vec4(vec):
     return TEMPLATE_VEC4 % (vec[0], vec[1], vec[2], vec[3])
 
-def generate_vec3(vec):
+def generate_vec3(vec, flipyz = False):
+    if flipyz:
+        return TEMPLATE_VEC3 % (vec[0], vec[2], vec[1])
     return TEMPLATE_VEC3 % (vec[0], vec[1], vec[2])
 
 def generate_vec2(vec):
@@ -1810,7 +1853,7 @@ def generate_textures_scene(data):
 
     for texture in bpy.data.textures:
 
-        if texture.type == 'IMAGE' and texture.image:
+        if texture.type == 'IMAGE' and texture.image and texture.users > 0 and len(texture.users_material) > 0:
 
             img = texture.image
 
@@ -1998,6 +2041,7 @@ def generate_material_string(material):
     material_type = type_map.get(shading, "MeshBasicMaterial")
 
     parameters = '"color": %d' % rgb2int(material["colorDiffuse"])
+    parameters += ', "ambient": %d' % rgb2int(material["colorDiffuse"])
     parameters += ', "opacity": %.2g' % material["transparency"]
 
     if shading == "Phong":
@@ -2147,33 +2191,73 @@ def generate_lights(data):
     chunks = []
 
     if data["use_lights"]:
+        lamps = data["objects"]
+        lamps = [ob for ob in lamps if (ob.type == 'LAMP')]
 
-        lights = data.get("lights", [])
-        if not lights:
-            lights.append(DEFAULTS["light"])
-
-        for light in lights:
-
-            if light["type"] == "DirectionalLight":
-                light_string = TEMPLATE_LIGHT_DIRECTIONAL % {
-                "light_id"      : generate_string(light["name"]),
-                "direction"     : generate_vec3(light["direction"]),
-                "color"         : rgb2int(light["color"]),
-                "intensity"     : light["intensity"]
-                }
+        for lamp in lamps:
+            light_string = ""
+            concrete_lamp = lamp.data
 
-            elif light["type"] == "PointLight":
+            if concrete_lamp.type == "POINT":
                 light_string = TEMPLATE_LIGHT_POINT % {
-                "light_id"      : generate_string(light["name"]),
-                "position"      : generate_vec3(light["position"]),
-                "color"         : rgb2int(light["color"]),
-                "intensity"     : light["intensity"]
+                    "light_id"      : generate_string(concrete_lamp.name),
+                    "position"      : generate_vec3(lamp.location, data["flipyz"]),
+                    "rotation"      : generate_vec3(lamp.rotation_euler, data["flipyz"]),
+                    "color"         : rgb2int(concrete_lamp.color),
+                    "distance"      : concrete_lamp.distance,
+                    "intensity"        : concrete_lamp.energy
+                }
+            elif concrete_lamp.type == "SUN":
+                light_string = TEMPLATE_LIGHT_SUN % {
+                    "light_id"      : generate_string(concrete_lamp.name),
+                    "position"      : generate_vec3(lamp.location, data["flipyz"]),
+                    "rotation"      : generate_vec3(lamp.rotation_euler, data["flipyz"]),
+                    "color"         : rgb2int(concrete_lamp.color),
+                    "distance"      : concrete_lamp.distance,
+                    "intensity"        : concrete_lamp.energy
+                }
+            elif concrete_lamp.type == "SPOT":
+                light_string = TEMPLATE_LIGHT_SPOT % {
+                    "light_id"      : generate_string(concrete_lamp.name),
+                    "position"      : generate_vec3(lamp.location, data["flipyz"]),
+                    "rotation"      : generate_vec3(lamp.rotation_euler, data["flipyz"]),
+                    "color"         : rgb2int(concrete_lamp.color),
+                    "distance"      : concrete_lamp.distance,
+                    "intensity"        : concrete_lamp.energy,
+                    "use_shadow"    : concrete_lamp.use_shadow,
+                    "angle"         : concrete_lamp.spot_size
+                }
+            elif concrete_lamp.type == "HEMI":
+                light_string = TEMPLATE_LIGHT_HEMI % {
+                    "light_id"      : generate_string(concrete_lamp.name),
+                    "position"      : generate_vec3(lamp.location, data["flipyz"]),
+                    "rotation"      : generate_vec3(lamp.rotation_euler, data["flipyz"]),
+                    "color"         : rgb2int(concrete_lamp.color),
+                    "distance"      : concrete_lamp.distance,
+                    "intensity"        : concrete_lamp.energy
+                }
+            elif concrete_lamp.type == "AREA":
+                light_string = TEMPLATE_LIGHT_AREA % {
+                    "light_id"      : generate_string(concrete_lamp.name),
+                    "position"      : generate_vec3(lamp.location, data["flipyz"]),
+                    "rotation"      : generate_vec3(lamp.rotation_euler, data["flipyz"]),
+                    "color"         : rgb2int(concrete_lamp.color),
+                    "distance"      : concrete_lamp.distance,
+                    "intensity"        : concrete_lamp.energy,
+                    "gamma"         : concrete_lamp.gamma,
+                    "shape"         : concrete_lamp.shape,
+                    "size"          : concrete_lamp.size,
+                    "size_y"        : concrete_lamp.size_y
                 }
 
             chunks.append(light_string)
 
+        if not lamps:
+            lamps.append(DEFAULTS["light"])
+
     return ",\n\n".join(chunks), len(chunks)
 
+
 # #####################################################
 # Scene exporter - embedded meshes
 # #####################################################