AddMaterials(SsPartRes part, SsColorBlendOperation colorBlendType) { SsImageFile img = part.imageFile; SsShaderType shaderType = SsShaderManager.EnumToType(colorBlendType, part.AlphaBlendType, SsMaterialColorBlendOperation.Non); Material material = img.GetMaterial(shaderType); if (material) { return; // already added } // create material #if _BUILD_UNIFIED_SHADERS img.useUnifiedShader = _anmRes.UseCgProgram; Shader shader = SsShaderManager.Get(shaderType, _anmRes.UseCgProgram); #else Shader shader = SsShaderManager.Get(shaderType, false); #endif // get material asset path string assetPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(img.texture)) + "/assets/"; string shaderName = shader.name; shaderName = shaderName.Replace("Ss/", ""); string assetName = assetPath + img.texture.name + "_Mat_" + shaderName + ".asset"; // try to load the exiting Material existedMat = (Material)AssetDatabase.LoadAssetAtPath(assetName, typeof(Material)); Material mat = (existedMat ?? new Material(shader)); mat.SetTexture("_MainTex", img.texture); if (existedMat == null) { // if none, create material as asset file newly // Debug.Log("create material: " + img.path + " shader: " + shaderType); if (!Directory.Exists(assetPath)) { string parentFolder = Path.GetDirectoryName(assetPath.TrimEnd('\\', '/')); AssetDatabase.CreateFolder(parentFolder, "assets"); } AssetDatabase.CreateAsset(mat, assetName); } else { // update the existing content mat.shader = shader; EditorUtility.SetDirty(mat); AssetDatabase.SaveAssets(); //same as EditorApplication.SaveAssets(); } // add to material list img.materials[SsShaderManager.ToSerial(shaderType)] = mat; }
SsPart( SsSprite manager, int index, ///< index of parts SsPartRes partRes, ///< part resource SsImageFile imageFile) ///< source image path, texture and material. { _mgr = manager; _index = index; _vIndex = (index - 1) * 4; _subMeshIndex = index - 1; _res = partRes; _mesh = _mgr._mesh; if (_res.HasParent) { _parent = _mgr.Sprite(_res.ParentId); if (_parent == null) { Debug.LogError("##### parent sprite must be created already!!"); //throw ArgumentNullException; Debug.Break(); return; } } // attach parent's transform to inherit its SRT. _pivotMatrix = Matrix4x4.identity; switch (_res.Type) { case SsPartType.Root: // root has only position, no vertices #if _APPLY_ROOT_POS_AS_PIVOT _rootPivot = new Vector3(-_res.PosX(0), +_res.PosY(0), 0f); #endif break; case SsPartType.Normal: case SsPartType.Bound: // each vertices are attached to pivot _vertPositions = new Vector3[4]; _orgVertices = new Vector3[4]; for (int i = 0; i < _vertPositions.Length; ++i) { _orgVertices[i] = _vertPositions[i] = _res.OrgVertices[i]; #if _MOVE_BOUND_PART_TO_THE_FRONT if (_res.Type == SsPartType.Bound) { _vertPositions[i].z = -0.1f; _orgVertices[i].z = -0.1f; } #endif } break; default: // other types don't require vertices. break; } // set startup value _visible = !_res.Hide(0); _flipH = false; _flipV = false; _pos = Vector3.zero; _quaternion = Quaternion.identity; _scale = Vector3.one; #if _MAKE_ROOT_TO_LOCAL_TRANSFORM _rotChanged = false; #endif // not any normal types don't require a material, colors, and vertices. if (_res.Type != SsPartType.Normal && _res.Type != SsPartType.Bound) { return; } // shortcut #if _USE_UNIFIED_SHADER _useUnifiedShader = _res.imageFile.useUnifiedShader; #endif _useCgShader = (SystemInfo.graphicsShaderLevel >= 20); if (_res.Type == SsPartType.Bound) { #if !_BOUND_PART_DRAW_AS_INVALID // set vertex color to transparent red. // this alpha value will be overwritten in AlphaValue property later. _vertexColor = new Color(1, 0, 0, 1); for (int i = 0; i < 4; ++i) { // set UVs to a point at left-top. _mgr._uvs[_vIndex + i] = Vector2.zero; // set vertex colors _mgr._colors[_vIndex + i] = _vertexColor; } // invisible is default. _visible = false; #else _visible = _mgr.DrawBoundingParts; #endif } else { // default vertex color _vertexColor = new Color(1, 1, 1, 1); for (int i = 0; i < 4; ++i) { // set UVs. use precalculated UVs, it is stored clockwise _mgr._uvs[_vIndex + i] = _res.UVs[i]; // set vertex colors _mgr._colors[_vIndex + i] = _vertexColor; } // set blend type and _shaderType ColorBlendType = SsColorBlendOperation.Non; } // set boolean about having transparency if (_res.Type == SsPartType.Bound) { #if _BOUND_PART_DRAW_AS_INVALID // become purple that mean invalid. _material = null; #else // needs any appropriate material _hasTransparency = true; AlphaValue = 0.5f; #endif } else { _hasTransparency = _res.HasTrancparency || (_parent != null && _res.Inherits(SsKeyAttr.Trans)); // always inherits parent's alpha whether the immediate parent has transparency or not. 2012.12.19 bug fixed // set alpha value AlphaValue = _res.Trans(0); // set appropriate material. _shaderType was set inside ColorBlendType property. _material = imageFile.GetMaterial(_shaderType); } //--------- calculates various info... Update(true); // set triangle indices. never changed so far. #if _USE_TRIANGLE_STRIP _triIndices = new int[] { _vIndex + 0, _vIndex + 1, _vIndex + 3, _vIndex + 2 }; // order is LT->RT->LB->RB. #else _triIndices = new int[] { _vIndex + 0, _vIndex + 1, _vIndex + 2, _vIndex + 2, _vIndex + 3, _vIndex + 0 }; // order is LT->RT->RB->RB->LB->LT #endif SetToSubmeshArray(_index - 1); }