Inheritance: GlTF_Writer
コード例 #1
0
    void OnWizardCreate() // Create (Export) button has been hit (NOT wizard has been created!)
    {
		writer = new GlTF_Writer();
		writer.Init ();
/*
		Object[] deps = EditorUtility.CollectDependencies  (trs);
		foreach (Object o in deps)
		{
			Debug.Log("obj "+o.name+"  "+o.GetType());
		}
*/		
		
		path = EditorUtility.SaveFilePanel("Save glTF file as", savedPath, savedFile, "gltf");
		if (path.Length != 0)
		{
			Debug.Log ("attempting to save to "+path);
			writer.OpenFiles (path);

			// FOR NOW!
			GlTF_Sampler sampler = new GlTF_Sampler("sampler1"); // make the default one for now
			GlTF_Writer.samplers.Add (sampler);
			// first, collect objects in the scene, add to lists
			Transform[] trs = Selection.GetTransforms (SelectionMode.Deep);
			foreach (Transform tr in trs)
			{
				if (tr.camera != null)
				{
					if (tr.camera.isOrthoGraphic)
					{
						GlTF_Orthographic cam;
						cam = new GlTF_Orthographic();
						cam.type = "orthographic";
						cam.zfar = tr.camera.farClipPlane;
						cam.znear = tr.camera.nearClipPlane;
						cam.name = tr.name;
						//cam.orthographic.xmag = tr.camera.
						GlTF_Writer.cameras.Add(cam);
					}
					else
					{
						GlTF_Perspective cam;
						cam = new GlTF_Perspective();
						cam.type = "perspective";
						cam.zfar = tr.camera.farClipPlane;
						cam.znear = tr.camera.nearClipPlane;
						cam.aspect_ratio = tr.camera.aspect;
						cam.yfov = tr.camera.fieldOfView;
						cam.name = tr.name;
						GlTF_Writer.cameras.Add(cam);
					}
				}
				
				if (tr.light != null)
				{
					switch (tr.light.type)
					{
					case LightType.Point:
						GlTF_PointLight pl = new GlTF_PointLight();
						pl.color = new GlTF_ColorRGB (tr.light.color);
						pl.name = tr.name;
						GlTF_Writer.lights.Add (pl);
						break;

					case LightType.Spot:
						GlTF_SpotLight sl = new GlTF_SpotLight();
						sl.color = new GlTF_ColorRGB (tr.light.color);
						sl.name = tr.name;
						GlTF_Writer.lights.Add (sl);
						break;
						
					case LightType.Directional:
						GlTF_DirectionalLight dl = new GlTF_DirectionalLight();
						dl.color = new GlTF_ColorRGB (tr.light.color);
						dl.name = tr.name;
						GlTF_Writer.lights.Add (dl);
						break;
						
					case LightType.Area:
						GlTF_AmbientLight al = new GlTF_AmbientLight();
						al.color = new GlTF_ColorRGB (tr.light.color);
						al.name = tr.name;
						GlTF_Writer.lights.Add (al);
						break;
					}
				}

				MeshRenderer mr = tr.GetComponent<MeshRenderer>();
				if (mr != null)
				{
					MeshFilter mf = tr.GetComponent<MeshFilter>();
					Mesh m = mf.sharedMesh;
					GlTF_Accessor normalAccessor = new GlTF_Accessor("normalAccessor-" + tr.name + "_FIXTHIS", "VEC3", "FLOAT");
					GlTF_Accessor positionAccessor = new GlTF_Accessor("positionAccessor-" + tr.name + "_FIXTHIS", "VEC3", "FLOAT");
					GlTF_Accessor texCoord0Accessor = new GlTF_Accessor("texCoord0Accessor-" + tr.name + "_FIXTHIS", "VEC2", "FLOAT");
					GlTF_Accessor indexAccessor = new GlTF_Accessor("indicesAccessor-" + tr.name + "_FIXTHIS", "SCALAR", "USHORT");
					indexAccessor.bufferView = GlTF_Writer.ushortBufferView;
					normalAccessor.bufferView = GlTF_Writer.vec3BufferView;
					positionAccessor.bufferView = GlTF_Writer.vec3BufferView;
					texCoord0Accessor.bufferView = GlTF_Writer.vec2BufferView;
					GlTF_Mesh mesh = new GlTF_Mesh();
					mesh.name = "mesh-" + tr.name;
					GlTF_Primitive primitive = new GlTF_Primitive();
					primitive.name = "primitive-"+tr.name+"_FIXTHIS";
					GlTF_Attributes attributes = new GlTF_Attributes();
					attributes.normalAccessor = normalAccessor;
					attributes.positionAccessor = positionAccessor;
					attributes.texCoord0Accessor = texCoord0Accessor;
					primitive.attributes = attributes;
					primitive.indices = indexAccessor;
					mesh.primitives.Add (primitive);
					mesh.Populate (m);
					GlTF_Writer.accessors.Add (normalAccessor);
					GlTF_Writer.accessors.Add (positionAccessor);
					GlTF_Writer.accessors.Add (texCoord0Accessor);
					GlTF_Writer.accessors.Add (indexAccessor);
					GlTF_Writer.meshes.Add (mesh);

					// next, add material(s) to dictionary (when unique)
					string matName = mr.sharedMaterial.name;
					if (matName == "")
						matName = "material-diffault-diffuse";
					else
						matName = "material-" + matName;
					primitive.materialName = matName;
					if (!GlTF_Writer.materials.ContainsKey (matName))
					{
						GlTF_Material material = new GlTF_Material();
						material.name = matName;
						if (mr.sharedMaterial.HasProperty ("shininess"))
							material.shininess = mr.sharedMaterial.GetFloat("shininess");
						material.diffuse = new GlTF_MaterialColor ("diffuse", mr.sharedMaterial.color);
						//material.ambient = new GlTF_Color ("ambient", mr.material.color);
						
						if (mr.sharedMaterial.HasProperty ("specular"))
						{
							Color sc = mr.sharedMaterial.GetColor ("specular");
							material.specular = new GlTF_MaterialColor ("specular", sc);
						}
						GlTF_Writer.materials.Add (material.name, material);

						// if there are textures, add them too
						if (mr.sharedMaterial.mainTexture != null)
						{
							if (!GlTF_Writer.textures.ContainsKey (mr.sharedMaterial.mainTexture.name))
							{
								GlTF_Texture texture = new GlTF_Texture ();
								texture.name = mr.sharedMaterial.mainTexture.name;
								texture.source = AssetDatabase.GetAssetPath(mr.sharedMaterial.mainTexture);
								texture.samplerName = sampler.name; // FIX! For now!
								GlTF_Writer.textures.Add (mr.sharedMaterial.mainTexture.name, texture);
								material.diffuse = new GlTF_MaterialTexture ("diffuse", texture);
							}
						}
					}
					
				}

				Animation a = tr.animation;
				
//				Animator a = tr.GetComponent<Animator>();				
				if (a != null)
				{
					AnimationClip[] clips = AnimationUtility.GetAnimationClips(tr.gameObject);
					int nClips = clips.Length;
//					int nClips = a.GetClipCount();
					for (int i = 0; i < nClips; i++)
					{
						GlTF_Animation anim = new GlTF_Animation(a.name);
						anim.Populate (clips[i]);
						GlTF_Writer.animations.Add (anim);
					}
				}

	
				// next, build hierarchy of nodes
				GlTF_Node node = new GlTF_Node();
				if (tr.parent != null)
					node.hasParent = true;
				if (tr.localPosition != Vector3.zero)
					node.translation = new GlTF_Translation (tr.localPosition);
				if (tr.localScale != Vector3.one)
					node.scale = new GlTF_Scale (tr.localScale);
				if (tr.localRotation != Quaternion.identity)
					node.rotation = new GlTF_Rotation (tr.localRotation);
				node.name = tr.name;
				if (tr.camera != null)
				{
					node.cameraName = tr.name;
				}
				else if (tr.light != null)
					node.lightName = tr.name;
				else if (mr != null)
				{
					node.meshNames.Add ("mesh-" + tr.name);
				}

				foreach (Transform t in tr.transform)
					node.childrenNames.Add ("node-" + t.name);
				
				GlTF_Writer.nodes.Add (node);
			}

			// third, add meshes etc to byte stream, keeping track of buffer offsets
			writer.Write ();
			writer.CloseFiles();
		}
	}