public void Prepare(bool generateDefaultLight = true, bool generateDefaultCamera = true) { meshes = MeshesList.ToArray(); sounds = SoundsList.ToArray(); materials = MaterialsList.ToArray(); multiMaterials = MultiMaterialsList.ToArray(); shadowGenerators = ShadowGeneratorsList.ToArray(); skeletons = SkeletonsList.ToArray(); if (MorphTargetManagersList.Count > 0) { morphTargetManagers = MorphTargetManagersList.ToArray(); } if (CamerasList.Count == 0 && generateDefaultCamera) { var camera = new BabylonCamera { name = "Default camera", id = Guid.NewGuid().ToString() }; // Default camera init gives infinit values // Indeed, float.MaxValue - float.MinValue always leads to infinity var distanceVector = MaxVector - MinVector; var midPoint = MinVector + distanceVector / 2; camera.target = midPoint.ToArray(); camera.position = (midPoint + distanceVector).ToArray(); var distance = distanceVector.Length(); camera.speed = distance / 50.0f; camera.maxZ = distance * 4f; camera.minZ = distance < 100.0f ? 0.1f : 1.0f; CamerasList.Add(camera); } if (LightsList.Count == 0 && generateDefaultLight) { var light = new BabylonLight { name = "Default light", id = Guid.NewGuid().ToString(), type = 3, groundColor = new float[] { 0, 0, 0 }, direction = new[] { 0, 1.0f, 0 }, intensity = 1 }; LightsList.Add(light); } cameras = (CamerasList.Count > 0) ? CamerasList.ToArray() : null; lights = (LightsList.Count > 0) ? LightsList.ToArray() : null; if (activeCameraID == null && CamerasList.Count > 0) { activeCameraID = CamerasList[0].id; } }
public void Prepare() { scenes[0].Prepare(); // Do not export empty arrays if (NodesList.Count > 0) { nodes = NodesList.ToArray(); NodesList.ForEach(node => node.Prepare()); } if (CamerasList.Count > 0) { cameras = CamerasList.ToArray(); } if (BuffersList.Count > 0) { buffers = BuffersList.ToArray(); } if (BufferViewsList.Count > 0) { bufferViews = BufferViewsList.ToArray(); } if (AccessorsList.Count > 0) { accessors = AccessorsList.ToArray(); } if (MeshesList.Count > 0) { meshes = MeshesList.ToArray(); } if (MaterialsList.Count > 0) { materials = MaterialsList.ToArray(); } if (TexturesList.Count > 0) { textures = TexturesList.ToArray(); } if (ImagesList.Count > 0) { images = ImagesList.ToArray(); } if (SamplersList.Count > 0) { samplers = SamplersList.ToArray(); } if (AnimationsList.Count > 0) { animations = AnimationsList.ToArray(); AnimationsList.ForEach(animation => animation.Prepare()); } if (SkinsList.Count > 0) { skins = SkinsList.ToArray(); } }
public void Prepare(bool generateDefaultLight = true) { meshes = MeshesList.ToArray(); materials = MaterialsList.ToArray(); multiMaterials = MultiMaterialsList.ToArray(); shadowGenerators = ShadowGeneratorsList.ToArray(); skeletons = SkeletonsList.ToArray(); if (CamerasList.Count == 0) { var camera = new BabylonCamera { name = "Default camera", id = Guid.NewGuid().ToString() }; var distanceVector = MaxVector - MinVector; var midPoint = MinVector + distanceVector / 2; camera.target = midPoint.ToArray(); camera.position = (midPoint + distanceVector).ToArray(); var distance = distanceVector.Length(); camera.speed = distance / 50.0f; camera.maxZ = distance * 4f; camera.minZ = distance < 100.0f ? 0.1f : 1.0f; CamerasList.Add(camera); } if (LightsList.Count == 0 && generateDefaultLight) { var light = new BabylonLight { name = "Default light", id = Guid.NewGuid().ToString() }; var midPoint = MinVector + (MaxVector - MinVector) / 2; light.type = 0; light.position = (midPoint + (MaxVector - MinVector)).ToArray(); light.diffuse = new Vector3(1, 1, 1).ToArray(); light.specular = new Vector3(1, 1, 1).ToArray(); LightsList.Add(light); } cameras = CamerasList.ToArray(); lights = LightsList.ToArray(); if (activeCameraID == null) { activeCameraID = CamerasList[0].id; } }
public void Prepare() { scenes[0].Prepare(); // Do not export empty arrays if (NodesList.Count > 0) { nodes = NodesList.ToArray(); NodesList.ForEach(node => node.Prepare()); } if (CamerasList.Count > 0) { cameras = CamerasList.ToArray(); } if (BuffersList.Count > 0) { buffers = BuffersList.ToArray(); } if (BufferViewsList.Count > 0) { bufferViews = BufferViewsList.ToArray(); } if (AccessorsList.Count > 0) { accessors = AccessorsList.ToArray(); } if (MeshesList.Count > 0) { meshes = MeshesList.ToArray(); } if (MaterialsList.Count > 0) { materials = MaterialsList.ToArray(); } if (TexturesList.Count > 0) { textures = TexturesList.ToArray(); } if (ImagesList.Count > 0) { images = ImagesList.ToArray(); } if (SamplersList.Count > 0) { samplers = SamplersList.ToArray(); } if (AnimationsList.Count > 0) { var animationsList = new List <GLTFAnimation>(); AnimationsList.ForEach(animation => { animation.Prepare(); // Exclude empty animations if (animation.channels != null) { animationsList.Add(animation); } }); if (animationsList.Count > 0) { animations = animationsList.ToArray(); } } if (SkinsList.Count > 0) { skins = SkinsList.ToArray(); } if (extensionsUsed != null && extensionsUsed.Count == 0) { extensionsUsed = null; } if (extensionsRequired != null && extensionsRequired.Count == 0) { extensionsRequired = null; } }
//Made of shapes mode, generates multiple shapes alon path public void GenerateMadeOfShapes(List <Vector3> vertices, List <int> triangles, List <Vector3> normals) { if (path.Length <= 1 || pathLength <= arrowHead.getLength() + arrowTail.getLength()) { return; } if (customShapes == null || customShapes.meshesList == null || customShapes.meshesList.Count == 0) { customShapes = ScriptableObject.CreateInstance <MeshesList>(); customShapes.meshesList = new List <SolidMesh>(); customShapes.meshesList.Add(ScriptableObject.CreateInstance <SphereMesh>()); } //FIrst generate all shapes that will be used for (int i = 0; i < customShapes.meshesList.Count; i++) { customShapes.meshesList[i].generate(); } //Last point on path that was visited Vector3 currentShapeLocation = path[0]; int shapeIndex = 0; int currentPathIndex = 1; //Whenver current distance passes the distanceBetweenShapes threshold add another shape to the mesh if (distanceBetweenShapes == null || distanceBetweenShapes.Count == 0) { distanceBetweenShapes = new List <float>(); } while (distanceBetweenShapes.Count < customShapes.meshesList.Count) { distanceBetweenShapes.Add(1f); } if (atPathPoints) { distanceBetweenShapes = new List <float>(); for (int i = 1; i < path.Length; i++) { distanceBetweenShapes.Add((path[currentPathIndex] - path[currentPathIndex - 1]).magnitude); } } for (int i = 0; i < distanceBetweenShapes.Count; i++) { if (distanceBetweenShapes[i] < 0.05) { distanceBetweenShapes[i] = 1f; } } float currentDistance = distanceBetweenShapes[shapeIndex]; float percentDistnace = 0; //Current used length for rotation function madeOfShapesColors = new List <Color32>(); float currentLength = 0; shapeIndex++; shapeIndex = shapeIndex % customShapes.meshesList.Count; float magnitudeOfPathPart = (path[1] - path[0]).magnitude; //mesh.property is expensive save it somewher temporarly Vector3[][] tempVerts = new Vector3[customShapes.meshesList.Count][]; int[][] tempTris = new int[customShapes.meshesList.Count][]; Vector3[][] tempNormals = new Vector3[customShapes.meshesList.Count][]; Color32[][] tempShapeColors = new Color32[customShapes.meshesList.Count][]; for (int i = 0; i < customShapes.meshesList.Count; i++) { tempVerts[i] = customShapes.meshesList[i].verts; tempNormals[i] = customShapes.meshesList[i].normals; tempTris[i] = customShapes.meshesList[i].tris; tempShapeColors[i] = customShapes.meshesList[i].colors; } Vector3 pathDiff = (path[1] - path[0]); defaultRotateAngle = 0; arrowTail.defaultRotateAngle = 0; Quaternion lastDirectionX = calculateUpDownQuaternion(pathDiff); Quaternion lastDirectionY = calculateLeftRightQuaternion(pathDiff); //Calculate when the next mesh at percent of path part needs to be placed, for each path part while (currentPathIndex < path.Length) { while (currentDistance > magnitudeOfPathPart) { currentLength += magnitudeOfPathPart; currentShapeLocation = path[currentPathIndex]; currentDistance -= magnitudeOfPathPart; currentPathIndex++; if (currentPathIndex >= path.Length) { break; } magnitudeOfPathPart = (path[currentPathIndex] - path[currentPathIndex - 1]).magnitude; percentDistnace += magnitudeOfPathPart; pathDiff = (path[currentPathIndex] - path[currentPathIndex - 1]); defaultRotateAngle = 0; arrowTail.defaultRotateAngle = 0; lastDirectionX = calculateUpDownQuaternion(pathDiff); lastDirectionY = calculateLeftRightQuaternion(pathDiff); } if (currentPathIndex >= path.Length) { break; } while (currentDistance <= magnitudeOfPathPart) { int currentTriangleCount = vertices.Count; currentLength += currentDistance; percentDistnace += currentDistance; for (int i = 0; i < tempVerts[shapeIndex].Length; i++) { vertices.Add(Quaternion.AngleAxis(rotateFunction.Evaluate(percentDistnace / pathLength * rotationFunctionLength) * 360, lastDirectionY * lastDirectionX * Vector2.up) * lastDirectionY * lastDirectionX * tempVerts[shapeIndex][i] + currentShapeLocation + (path[currentPathIndex] - path[currentPathIndex - 1]).normalized * currentDistance); normals.Add(Quaternion.AngleAxis(rotateFunction.Evaluate(percentDistnace / pathLength * rotationFunctionLength) * 360, lastDirectionY * lastDirectionX * Vector2.up) * lastDirectionY * lastDirectionX * tempNormals[shapeIndex][i]); if (useShapeColors) { madeOfShapesColors.Add(tempShapeColors[shapeIndex][i]); } else { madeOfShapesColors.Add(colorFunctions[0].Evaluate(percentDistnace / pathLength)); } } for (int i = 0; i < tempTris[shapeIndex].Length; i++) { triangles.Add(tempTris[shapeIndex][i] + currentTriangleCount); } currentDistance += distanceBetweenShapes[shapeIndex]; shapeIndex++; shapeIndex = shapeIndex % customShapes.meshesList.Count; } currentShapeLocation = path[currentPathIndex]; } }
public override void generate() { if (colorFunction == null) { prepareGradient(ref colorFunction); } List <Vector3> path = pathArray.path; prepareOffset(); mesh = new Mesh(); List <Vector3> vertices = new List <Vector3>(); List <int> triangles = new List <int>(); if (customShapes == null || customShapes.meshesList == null || customShapes.meshesList.Count == 0) { customShapes = ScriptableObject.CreateInstance <MeshesList>(); customShapes.meshesList = new List <ProceduralMesh>(); customShapes.meshesList.Add(ScriptableObject.CreateInstance <SphereMesh>()); } for (int i = 0; i < customShapes.meshesList.Count; i++) { customShapes.meshesList[i].generate(); } //Whenver current distance passes the distanceBetweenShapes threshold add another shape to the mesh if (SpaceTakenByShape == null || SpaceTakenByShape.Count == 0) { SpaceTakenByShape = new List <float>(); } while (SpaceTakenByShape.Count < customShapes.meshesList.Count) { SpaceTakenByShape.Add(1f); } for (int i = 0; i < SpaceTakenByShape.Count; i++) { if (SpaceTakenByShape[i] < 0.05) { SpaceTakenByShape[i] = 1f; } } float shapesMaxDistance = 0; for (int i = 0; i < SpaceTakenByShape.Count; i++) { shapesMaxDistance += SpaceTakenByShape[i]; } float pathLength = 0; for (int i = 1; i < path.Count; i++) { pathLength += (path[i] - path[i - 1]).magnitude; } int numberOfShapeInstances = Mathf.FloorToInt((pathLength / shapesMaxDistance)); float maxBrakeDistance = ((pathLength / shapesMaxDistance) - numberOfShapeInstances) * shapesMaxDistance; float brakeDistance = maxBrakeDistance / (numberOfShapeInstances * customShapes.meshesList.Count); //Last point on path that was visited Vector3 currentShapeLocation = path[0]; int shapeIndex = 0; int lastShapeIndex = 0; int currentPathIndex = 1; float currentDistance = SpaceTakenByShape[shapeIndex] / 2; float percentDistnace = 0; madeOfShapesColors = new List <Color32>(); float currentLength = 0; float magnitudeOfPathPart = (path[1] - path[0]).magnitude; //mesh.property is expensive save it somewher temporarly Vector3[][] tempVerts = new Vector3[customShapes.meshesList.Count][]; int[][] tempTris = new int[customShapes.meshesList.Count][]; Color32[][] tempShapeColors = new Color32[customShapes.meshesList.Count][]; for (int i = 0; i < customShapes.meshesList.Count; i++) { tempVerts[i] = customShapes.meshesList[i].verts; tempTris[i] = customShapes.meshesList[i].tris; tempShapeColors[i] = customShapes.meshesList[i].colors; } //Calculate when the next mesh at percent of path part needs to be placed, for each path part while (currentPathIndex < path.Count) { while (currentDistance > magnitudeOfPathPart) { currentLength += magnitudeOfPathPart; currentShapeLocation = path[currentPathIndex]; currentDistance -= magnitudeOfPathPart; currentPathIndex++; if (currentPathIndex >= path.Count) { break; } magnitudeOfPathPart = (path[currentPathIndex] - path[currentPathIndex - 1]).magnitude; percentDistnace += magnitudeOfPathPart; } if (currentPathIndex >= path.Count) { break; } while (currentDistance <= magnitudeOfPathPart) { int currentTriangleCount = vertices.Count; currentLength += currentDistance; percentDistnace += currentDistance; for (int i = 0; i < tempVerts[shapeIndex].Length; i++) { vertices.Add(tempVerts[shapeIndex][i] + currentShapeLocation + (path[currentPathIndex] - path[currentPathIndex - 1]).normalized * currentDistance); if (useShapeColors) { madeOfShapesColors.Add(tempShapeColors[shapeIndex][i]); } else { madeOfShapesColors.Add(colorFunction.Evaluate(percentDistnace / pathLength)); } } for (int i = 0; i < tempTris[shapeIndex].Length; i++) { triangles.Add(tempTris[shapeIndex][i] + currentTriangleCount); } currentDistance += SpaceTakenByShape[lastShapeIndex] / 2 + SpaceTakenByShape[shapeIndex] / 2 + brakeDistance; lastShapeIndex = shapeIndex; shapeIndex++; shapeIndex = shapeIndex % customShapes.meshesList.Count; } currentShapeLocation = path[currentPathIndex]; } mesh.SetVertices(vertices); mesh.SetTriangles(triangles, 0); mesh.uv = uvs; mesh.SetColors(madeOfShapesColors); mesh.RecalculateNormals(); mesh.name = "MadeOfShapesSelectionPath"; }