Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.IO;
- using System.Collections.Generic;
- using System;
- using System.Text;
- public struct StudioModelBone
- {
- public string name;
- public int parent;
- public int flags;
- public int[] boneController;
- public float[] value;
- public float[] scale;
- public string path;
- }
- public class StudioModel
- {
- private string m_name;
- private string[] m_meshPartNames; // [part]
- private Mesh[][] m_meshes; // [part, mesh]
- private Material[][][] m_meshMaterials; // [part, mesh, submesh]
- private Material[] m_materials; // [textureId]
- private Texture[] m_textures; // [textureId]
- private AnimationClip[] m_animations; // [animation]
- private AnimationClip[] m_boneControllers; // [controller]
- private StudioModelBone[] m_bones; // [bone]
- public string Name
- {
- get
- {
- return m_name;
- }
- }
- public string[] MeshPartNames
- {
- get
- {
- return m_meshPartNames;
- }
- }
- public int MeshPartCount
- {
- get
- {
- return m_meshes.Length;
- }
- }
- public Mesh[][] Meshes
- {
- get
- {
- return m_meshes;
- }
- }
- public Material[][][] Materials
- {
- get
- {
- return m_meshMaterials;
- }
- }
- public AnimationClip[] Animations
- {
- get
- {
- return m_animations;
- }
- }
- public StudioModelBone[] Bones
- {
- get
- {
- return m_bones;
- }
- }
- public int BoneCount
- {
- get
- {
- return m_bones.Length;
- }
- }
- public StudioModel(string filePath)
- {
- Read(filePath);
- }
- public void Read(string filePath)
- {
- FileStream stream;
- string directory, file;
- int textureIndex, groupCount;
- string path;
- byte[] modelBytes, textureBytes;
- byte[][] groupBytes;
- file = Path.GetFileNameWithoutExtension(filePath);
- directory = Path.GetDirectoryName(filePath);
- stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- modelBytes = new byte[stream.Length];
- stream.Read(modelBytes, 0, modelBytes.Length);
- stream.Close();
- stream.Dispose();
- m_name = ByteArrayUtils.ReadASCIIString(modelBytes, 8, 64);
- textureIndex = BitConverter.ToInt32(modelBytes, 184);
- if(textureIndex == 0)
- {
- path = new StringBuilder(directory).Append(Path.DirectorySeparatorChar).Append(file).Append("t.mdl").ToString();
- if(File.Exists(path))
- {
- stream = new FileStream(path, FileMode.Open, FileAccess.Read);
- textureBytes = new byte[stream.Length];
- stream.Read(textureBytes, 0, textureBytes.Length);
- stream.Close();
- stream.Dispose();
- }
- else
- {
- textureBytes = null;
- }
- }
- else
- {
- textureBytes = modelBytes;
- }
- groupBytes = null;
- groupCount = BitConverter.ToInt32(modelBytes, 172);
- if(groupCount != 0)
- {
- groupBytes = new byte[groupCount - 1][];
- for(int i = 0; i < groupCount - 1; i++)
- {
- path = new StringBuilder(directory).Append(Path.DirectorySeparatorChar).Append(file).Append((i + 1).ToString("00")).Append(".mdl").ToString();
- if(File.Exists(path))
- {
- stream = new FileStream(path, FileMode.Open, FileAccess.Read);
- groupBytes[i] = new byte[stream.Length];
- stream.Read(groupBytes[i], 0, groupBytes[i].Length);
- stream.Close();
- stream.Dispose();
- }
- else
- {
- groupBytes[i] = null;
- }
- }
- }
- ReadBones(modelBytes);
- ReadAnimations(modelBytes, groupBytes);
- ReadControllers(modelBytes);
- ReadTextures(textureBytes);
- ReadMeshes(modelBytes, textureBytes);
- }
- private void ReadBones(byte[] modelBytes)
- {
- int numBones, boneIndex, parentBone;
- string path;
- numBones = BitConverter.ToInt32(modelBytes, 140);
- boneIndex = BitConverter.ToInt32(modelBytes, 144);
- m_bones = new StudioModelBone[numBones];
- for(int b = 0; b < numBones; b++)
- {
- m_bones[b] = new StudioModelBone();
- m_bones[b].parent = BitConverter.ToInt32(modelBytes, boneIndex + 32 + b * 112);
- m_bones[b].flags = BitConverter.ToInt32(modelBytes, boneIndex + 36 + b * 112);
- m_bones[b].boneController = ByteArrayUtils. ReadIntArray(modelBytes, boneIndex + 40 + b * 112, 6);
- m_bones[b].value = ByteArrayUtils.ReadFloatArray(modelBytes, boneIndex + 64 + b * 112, 6);
- m_bones[b].scale = ByteArrayUtils.ReadFloatArray(modelBytes, boneIndex + 88 + b * 112, 6);
- m_bones[b].name = new StringBuilder("#Bone").Append(b).Append(" (")
- .Append(ByteArrayUtils.ReadASCIIString(modelBytes, boneIndex + b * 112, 32)).Append(")").ToString();
- parentBone = m_bones[b].parent;
- path = m_bones[b].name;
- while(parentBone != -1)
- {
- path = m_bones[parentBone].name + '/' + path;
- parentBone = m_bones[parentBone].parent;
- }
- m_bones[b].path = path;
- }
- }
- private void ReadAnimations(byte[] modelBytes, byte[][] groupBytes)
- {
- int sequenceGroupIndex, sequenceGroup, sequenceGroupData, animationIndex, sequenceIndex,
- animationOffsetIndex, boneAnimationIndex, animationValue, k,
- numSequences, numFrames, numBones;
- ushort[] animOffset;
- byte numValid, numTotal;
- byte[] bytes;
- float fps, frame;
- string path;
- Keyframe[] xPositionKeys, yPositionKeys, zPositionKeys, xRotationKeys, yRotationKeys, zRotationKeys, wRotationKeys;
- Vector3 eulerRotation, position;
- Quaternion rotation;
- numBones = BitConverter.ToInt32(modelBytes, 140);
- numSequences = BitConverter.ToInt32(modelBytes, 164);
- sequenceIndex = BitConverter.ToInt32(modelBytes, 168);
- sequenceGroupIndex = BitConverter.ToInt32(modelBytes, 176);
- m_animations = new AnimationClip[numSequences];
- for(int s = 0; s < numSequences; s++)
- {
- fps = BitConverter.ToSingle(modelBytes, sequenceIndex + 32 + s * 176);
- numFrames = BitConverter.ToInt32(modelBytes, sequenceIndex + 56 + s * 176);
- sequenceGroup = BitConverter.ToInt32(modelBytes, sequenceIndex + 156 + s * 176);
- sequenceGroupData = BitConverter.ToInt32(modelBytes, sequenceGroupIndex + 100 + sequenceGroup * 104);
- animationIndex = BitConverter.ToInt32(modelBytes, sequenceIndex + 124 + s * 176);
- m_animations[s] = new AnimationClip();
- m_animations[s].name = ByteArrayUtils.ReadASCIIString(modelBytes, sequenceIndex + s * 176, 32);
- if(sequenceGroup == 0)
- {
- animationOffsetIndex = sequenceGroupData + animationIndex;
- bytes = modelBytes;
- }
- else
- {
- animationOffsetIndex = animationIndex;
- bytes = groupBytes[sequenceGroup - 1];
- if(bytes == null) // файл не был найден
- {
- continue;
- }
- }
- for(int b = 0; b < numBones; b++, animationOffsetIndex += 12)
- {
- animOffset = ByteArrayUtils.ReadUshortArray(bytes, animationOffsetIndex, 6);
- xPositionKeys = new Keyframe[numFrames];
- yPositionKeys = new Keyframe[numFrames];
- zPositionKeys = new Keyframe[numFrames];
- xRotationKeys = new Keyframe[numFrames];
- yRotationKeys = new Keyframe[numFrames];
- zRotationKeys = new Keyframe[numFrames];
- wRotationKeys = new Keyframe[numFrames];
- position = Vector3.zero;
- eulerRotation = Vector3.zero;
- rotation = Quaternion.identity;
- for(int f = 0; f < numFrames; f++)
- {
- // Position
- for(int j = 0; j < 3; j++)
- {
- position[j] = m_bones[b].value[j];
- if(animOffset[j] != 0)
- {
- boneAnimationIndex = animationOffsetIndex + animOffset[j];
- numValid = bytes[boneAnimationIndex];
- numTotal = bytes[boneAnimationIndex + 1];
- k = f;
- while (numTotal <= k)
- {
- k -= numTotal;
- boneAnimationIndex += (numValid + 1) * 2;
- numValid = bytes[boneAnimationIndex];
- numTotal = bytes[boneAnimationIndex + 1];
- }
- if(numValid > k)
- {
- animationValue = BitConverter.ToInt16(bytes, boneAnimationIndex + (k + 1) * 2);
- }
- else
- {
- animationValue = BitConverter.ToInt16(bytes, boneAnimationIndex + numValid * 2);
- }
- position[j] += animationValue * m_bones[b].scale[j];
- }
- }
- // Rotation
- for(int j = 0; j < 3; j++)
- {
- if(animOffset[j + 3] == 0)
- {
- eulerRotation[j] = m_bones[b].value[j + 3];
- }
- else
- {
- boneAnimationIndex = animationOffsetIndex + animOffset[j + 3];
- numValid = bytes[boneAnimationIndex];
- numTotal = bytes[boneAnimationIndex + 1];
- k = f;
- while (numTotal <= k)
- {
- k -= numTotal;
- boneAnimationIndex += (numValid + 1) * 2;
- numValid = bytes[boneAnimationIndex];
- numTotal = bytes[boneAnimationIndex + 1];
- }
- if(numValid > k)
- {
- eulerRotation[j] = BitConverter.ToInt16(bytes, boneAnimationIndex + (k + 1) * 2);
- }
- else
- {
- eulerRotation[j] = BitConverter.ToInt16(bytes, boneAnimationIndex + numValid * 2);
- }
- }
- eulerRotation[j] = (m_bones[b].value[j + 3] + eulerRotation[j] * m_bones[b].scale[j + 3]);
- }
- rotation = AngleQuaternion(eulerRotation);
- frame = f / fps;
- xPositionKeys[f] = new Keyframe(frame, position.x);
- yPositionKeys[f] = new Keyframe(frame, position.y);
- zPositionKeys[f] = new Keyframe(frame, position.z);
- xRotationKeys[f] = new Keyframe(frame, rotation.x);
- yRotationKeys[f] = new Keyframe(frame, rotation.y);
- zRotationKeys[f] = new Keyframe(frame, rotation.z);
- wRotationKeys[f] = new Keyframe(frame, rotation.w);
- }
- path = m_bones[b].path;
- m_animations[s].SetCurve(path, typeof(Transform), "localPosition.x", new AnimationCurve(xPositionKeys));
- m_animations[s].SetCurve(path, typeof(Transform), "localPosition.y", new AnimationCurve(yPositionKeys));
- m_animations[s].SetCurve(path, typeof(Transform), "localPosition.z", new AnimationCurve(zPositionKeys));
- m_animations[s].SetCurve(path, typeof(Transform), "localRotation.x", new AnimationCurve(xRotationKeys));
- m_animations[s].SetCurve(path, typeof(Transform), "localRotation.y", new AnimationCurve(yRotationKeys));
- m_animations[s].SetCurve(path, typeof(Transform), "localRotation.z", new AnimationCurve(zRotationKeys));
- m_animations[s].SetCurve(path, typeof(Transform), "localRotation.w", new AnimationCurve(wRotationKeys));
- }
- m_animations[s].EnsureQuaternionContinuity();
- m_animations[s].wrapMode = WrapMode.Loop; // TEMP
- }
- }
- private void ReadControllers(byte[] modelBytes)
- {
- int controllerCount, controllerIndex, boneId, type, rest, controllerId;
- float startValue, endValue;
- AnimationCurve curve;
- controllerCount = BitConverter.ToInt32(modelBytes, 148);
- controllerIndex = BitConverter.ToInt32(modelBytes, 152);
- m_boneControllers = new AnimationClip[5];
- for(int i = 0; i < controllerCount; i++)
- {
- boneId = BitConverter.ToInt32(modelBytes, controllerIndex + i * 24);
- type = BitConverter.ToInt32(modelBytes, controllerIndex + 4 + i * 24);
- startValue = BitConverter.ToSingle(modelBytes, controllerIndex + 8 + i * 24);
- endValue = BitConverter.ToSingle(modelBytes, controllerIndex + 12 + i * 24);
- rest = BitConverter.ToInt32(modelBytes, controllerIndex + 16 + i * 24);
- controllerId = BitConverter.ToInt32(modelBytes, controllerIndex + 20 + i * 24);
- if(m_boneControllers[controllerId] == null)
- {
- m_boneControllers[controllerId] = new AnimationClip();
- }
- curve = new AnimationCurve();
- curve.AddKey(new Keyframe(0, startValue));
- curve.AddKey(new Keyframe(1, endValue));
- m_boneControllers[controllerId].SetCurve(m_bones[boneId].path, typeof(Transform), "", curve);
- }
- }
- private void ReadMeshes(byte[] modelBytes, byte[] textureBytes)
- {
- int numBodyParts, numModels, numMeshes, numVertices, numIterations, numBones, numNormals,
- bodyPartsIndex, modelIndex, meshIndex, vertexIndex, vertexInfoIndex, normalIndex,
- triangleIndex, skinIndex, textureIndex, bufferIndex,
- textureWidth, textureHeight;
- float uCoord, vCoord;
- bool isTriangleFan, invertTriangle;
- int[] triangleBuffer;
- string textureName;
- Vector3[] normalArray;
- List<Vector3> vertices;
- List<Vector3> normals;
- List<Vector2> uvCoords;
- List<BoneWeight> weights;
- List<int>[] triangles;
- List<bool> usedVertex;
- Vector3 vector3;
- Vector2 vector2;
- BoneWeight boneWeight;
- GameObject[] tempObjects;
- GameObject rootObject;
- Matrix4x4[] bindPoses;
- numBones = m_bones.Length;
- textureIndex = BitConverter.ToInt32(textureBytes, 184);
- numBodyParts = BitConverter.ToInt32(modelBytes, 204);
- bodyPartsIndex = BitConverter.ToInt32(modelBytes, 208);
- m_meshPartNames = new string[numBodyParts];
- m_meshes = new Mesh[numBodyParts][];
- m_meshMaterials = new Material[numBodyParts][][];
- vertices = new List<Vector3>();
- normals = new List<Vector3>();
- uvCoords = new List<Vector2>();
- weights = new List<BoneWeight>();
- usedVertex = new List<bool>();
- bindPoses = new Matrix4x4[numBones];
- tempObjects = new GameObject[numBones];
- rootObject = new GameObject();
- for(int i = 0; i < numBones; i++)
- {
- tempObjects[i] = new GameObject();
- if(m_bones[i].parent == -1)
- {
- tempObjects[i].transform.parent = rootObject.transform;
- }
- else
- {
- tempObjects[i].transform.parent = tempObjects[m_bones[i].parent].transform;
- }
- bindPoses[i] = tempObjects[i].transform.worldToLocalMatrix * rootObject.transform.localToWorldMatrix;
- }
- GameObject.DestroyImmediate(rootObject);
- for(int i = 0; i < tempObjects.Length; i++)
- {
- GameObject.DestroyImmediate(tempObjects[i]);
- }
- for(int p = 0; p < numBodyParts; p++)
- {
- m_meshPartNames[p] = ByteArrayUtils.ReadASCIIString(modelBytes, bodyPartsIndex + p * 76, 64);
- numModels = BitConverter.ToInt32(modelBytes, bodyPartsIndex + 64 + p * 76);
- modelIndex = BitConverter.ToInt32(modelBytes, bodyPartsIndex + 72 + p * 76);
- m_meshes[p] = new Mesh[numModels];
- m_meshMaterials[p] = new Material[numModels][];
- for(int m = 0; m < numModels; m++)
- {
- m_meshes[p][m] = new Mesh();
- m_meshes[p][m].name = ByteArrayUtils.ReadASCIIString(modelBytes, modelIndex + m * 112, 64);
- numMeshes = BitConverter.ToInt32(modelBytes, modelIndex + 72 + m * 112);
- meshIndex = BitConverter.ToInt32(modelBytes, modelIndex + 76 + m * 112);
- numVertices = BitConverter.ToInt32(modelBytes, modelIndex + 80 + m * 112);
- vertexInfoIndex = BitConverter.ToInt32(modelBytes, modelIndex + 84 + m * 112);
- vertexIndex = BitConverter.ToInt32(modelBytes, modelIndex + 88 + m * 112);
- numNormals = BitConverter.ToInt32(modelBytes, modelIndex + 92 + m * 112);
- normalIndex = BitConverter.ToInt32(modelBytes, modelIndex + 100 + m * 112);
- vertices.Clear();
- normals.Clear();
- uvCoords.Clear();
- weights.Clear();
- usedVertex.Clear();
- normalArray = new Vector3[numNormals];
- triangles = new List<int>[numMeshes];
- m_meshMaterials[p][m] = new Material[numMeshes];
- for(int i = 0; i < numVertices; i++)
- {
- vector3 = ByteArrayUtils.ReadVector3(modelBytes, vertexIndex + i * 12);
- boneWeight = new BoneWeight();
- boneWeight.boneIndex0 = modelBytes[vertexInfoIndex + i];
- boneWeight.weight0 = 1;
- vertices.Add(vector3);
- weights.Add(boneWeight);
- uvCoords.Add(Vector2.zero);
- normals.Add(Vector3.one);
- usedVertex.Add(false);
- }
- for(int i = 0; i < numNormals; i++)
- {
- normalArray[i] = ByteArrayUtils.ReadVector3(modelBytes, normalIndex + i * 12);
- }
- for(int i = 0; i < numMeshes; i++)
- {
- triangleIndex = BitConverter.ToInt32(modelBytes, meshIndex + 4 + i * 20);
- skinIndex = BitConverter.ToInt32(modelBytes, meshIndex + 8 + i * 20);
- textureName = ByteArrayUtils.ReadASCIIString(textureBytes, textureIndex + skinIndex * 80, 64);
- textureWidth = BitConverter.ToInt32(textureBytes, textureIndex + 68 + skinIndex * 80);
- textureHeight = BitConverter.ToInt32(textureBytes, textureIndex + 72 + skinIndex * 80);
- m_meshMaterials[p][m][i] = GetMaterial(textureName);
- triangles[i] = new List<int>();
- while((numIterations = BitConverter.ToInt16(modelBytes, triangleIndex)) != 0)
- {
- triangleIndex += 2;
- if(numIterations < 0)
- {
- isTriangleFan = true;
- numIterations = -numIterations;
- }
- else
- {
- isTriangleFan = false;
- }
- triangleBuffer = new int[numIterations];
- for(; numIterations > 0; numIterations--, triangleIndex += 8)
- {
- bufferIndex = triangleBuffer.Length - numIterations;
- triangleBuffer[bufferIndex] = BitConverter.ToInt16(modelBytes, triangleIndex);
- uCoord = BitConverter.ToInt16(modelBytes, triangleIndex + 4) / (float)textureWidth;
- vCoord = BitConverter.ToInt16(modelBytes, triangleIndex + 6) / (float)textureHeight;
- vector2 = new Vector2(uCoord, vCoord);
- vector3 = normalArray[BitConverter.ToInt16(modelBytes, triangleIndex + 2)];
- if(!usedVertex[triangleBuffer[bufferIndex]])
- {
- uvCoords[triangleBuffer[bufferIndex]] = vector2;
- normals[triangleBuffer[bufferIndex]] = vector3;
- usedVertex[triangleBuffer[bufferIndex]] = true;
- }
- else
- {
- // TODO: Оптимизировать этот участок
- if(uvCoords[triangleBuffer[bufferIndex]] != vector2 || normals[triangleBuffer[bufferIndex]] != vector3)
- {
- vertices.Add(vertices[triangleBuffer[bufferIndex]]);
- normals.Add(normals[triangleBuffer[bufferIndex]]);
- weights.Add(weights[triangleBuffer[bufferIndex]]);
- uvCoords.Add(vector2);
- usedVertex.Add(true);
- triangleBuffer[bufferIndex] = vertices.Count - 1;
- }
- }
- }
- if(isTriangleFan)
- {
- for(int j = 0; j < triangleBuffer.Length - 2; j++)
- {
- triangles[i].Add(triangleBuffer[j + 2]);
- triangles[i].Add(triangleBuffer[j + 1]);
- triangles[i].Add(triangleBuffer[0]);
- }
- }
- else
- {
- invertTriangle = false;
- for(int j = 0; j < triangleBuffer.Length - 2; j++)
- {
- triangles[i].Add(triangleBuffer[j + (invertTriangle?1:2)]);
- triangles[i].Add(triangleBuffer[j + (invertTriangle?2:1)]);
- triangles[i].Add(triangleBuffer[j]);
- invertTriangle = !invertTriangle;
- }
- }
- }
- }
- m_meshes[p][m].subMeshCount = numMeshes;
- m_meshes[p][m].vertices = vertices.ToArray();
- m_meshes[p][m].normals = normals.ToArray();
- m_meshes[p][m].uv = uvCoords.ToArray();
- m_meshes[p][m].boneWeights = weights.ToArray();
- m_meshes[p][m].bindposes = bindPoses;
- for(int i = 0; i < triangles.Length; i++)
- {
- m_meshes[p][m].SetTriangles(triangles[i].ToArray(), i);
- }
- m_meshes[p][m].RecalculateBounds();
- m_meshes[p][m].Optimize();
- }
- }
- }
- private void ReadTextures(byte[] textureBytes)
- {
- Texture2D texture;
- int pix1, pix2, pix3, pix4, outWidth, outHeight,palStartIndex, dataStartIndex,
- flags, textureWidth, textureHeight, textureIndex, numTextures;
- int[] row1, row2, col1, col2;
- float r, g, b;
- Color[] colors;
- string name;
- if(textureBytes != null)
- {
- numTextures = BitConverter.ToInt32(textureBytes, 180);
- textureIndex = BitConverter.ToInt32(textureBytes, 184);
- if(textureIndex != 0)
- {
- m_materials = new Material[numTextures];
- m_textures = new Texture[numTextures];
- for(int t = 0; t < numTextures; t++)
- {
- name = ByteArrayUtils.ReadASCIIString(textureBytes, textureIndex + t * 80, 64);
- flags = BitConverter.ToInt32(textureBytes, textureIndex + 64 + t * 80);
- textureWidth = BitConverter.ToInt32(textureBytes, textureIndex + 68 + t * 80);
- textureHeight = BitConverter.ToInt32(textureBytes, textureIndex + 72 + t * 80);
- dataStartIndex = BitConverter.ToInt32(textureBytes, textureIndex + 76 + t * 80);
- for(outWidth = 1; outWidth < textureWidth; outWidth <<= 1);
- if (outWidth > 1024) outWidth = 1024;
- for (outHeight = 1; outHeight < textureHeight; outHeight <<= 1);
- if (outHeight > 1024) outHeight = 1024;
- colors = new Color[outWidth * outHeight];
- palStartIndex = textureWidth * textureHeight + dataStartIndex;
- col1 = new int[outWidth];
- col2 = new int[outWidth];
- for (int i = 0; i < outWidth; i++)
- {
- col1[i] = (int)((i + 0.25f) * (textureWidth / (float)outWidth));
- col2[i] = (int)((i + 0.75f) * (textureWidth / (float)outWidth));
- }
- row1 = new int[outHeight];
- row2 = new int[outHeight];
- for (int i = 0; i < outHeight; i++)
- {
- row1[i] = (int)((i + 0.25f) * (textureHeight / (float)outHeight)) * textureWidth;
- row2[i] = (int)((i + 0.75f) * (textureHeight / (float)outHeight)) * textureWidth;
- }
- for (int i = 0 ; i < outHeight ; i++)
- {
- for (int j = 0 ; j < outWidth ; j++)
- {
- pix1 = palStartIndex + textureBytes[dataStartIndex + row1[i] + col1[j]] * 3;
- pix2 = palStartIndex + textureBytes[dataStartIndex + row1[i] + col2[j]] * 3;
- pix3 = palStartIndex + textureBytes[dataStartIndex + row2[i] + col1[j]] * 3;
- pix4 = palStartIndex + textureBytes[dataStartIndex + row2[i] + col2[j]] * 3;
- r = (float)((textureBytes[pix1 ] + textureBytes[pix2 ] + textureBytes[pix3 ] + textureBytes[pix4 ]) / 1020.0f);
- g = (float)((textureBytes[pix1 + 1] + textureBytes[pix2 + 1] + textureBytes[pix3 + 1] + textureBytes[pix4 + 1]) / 1020.0f);
- b = (float)((textureBytes[pix1 + 2] + textureBytes[pix2 + 2] + textureBytes[pix3 + 2] + textureBytes[pix4 + 2]) / 1020.0f);
- colors[i * outWidth + j] = new Color(r,g,b,1);
- }
- }
- texture = new Texture2D(outWidth, outHeight, TextureFormat.ARGB32, false);
- texture.SetPixels(colors);
- texture.Apply(false, true);
- m_materials[t] = new Material(Shader.Find("Diffuse"));
- m_materials[t].name = name;
- m_materials[t].mainTexture = texture;
- m_textures[t] = texture;
- }
- }
- }
- }
- private Material GetMaterial(string name)
- {
- if(m_materials != null)
- {
- for(int i = 0; i < m_materials.Length; i++)
- {
- if(m_materials[i].name == name)
- {
- return m_materials[i];
- }
- }
- }
- return null;
- }
- private Quaternion AngleQuaternion(Vector3 vector)
- {
- float angle;
- float sr, sp, sy, cr, cp, cy, crcp, srsp, srcp, crsp;
- angle = vector[2] * 0.5f;
- sy = Mathf.Sin(angle);
- cy = Mathf.Cos(angle);
- angle = vector[1] * 0.5f;
- sp = Mathf.Sin(angle);
- cp = Mathf.Cos(angle);
- angle = vector[0] * 0.5f;
- sr = Mathf.Sin(angle);
- cr = Mathf.Cos(angle);
- crcp = cr*cp;
- srsp = sr*sp;
- srcp = sr*cp;
- crsp = cr*sp;
- return new Quaternion(srcp*cy-crsp*sy, crsp*cy+srcp*sy, crcp*sy-srsp*cy, crcp*cy+srsp*sy);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment