/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// AddNewWayPoint /// # Add a new part to car /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// void AddNewWayPoint(bool useHit, RaycastHit hit) { //Debug.Log ("Add new wayPoint"); GameObject wayPointsContainer = BasicFunctions.CreateContainerIfNotExists("_DECAL_WAYPOINTS"); GameObject newWayPoint = GameObject.CreatePrimitive(PrimitiveType.Capsule); newWayPoint.transform.parent = wayPointsContainer.transform; newWayPoint.transform.localScale = 0.2f * Vector3.one; newWayPoint.GetComponent <Renderer> ().sharedMaterial = new Material(newWayPoint.GetComponent <Renderer> ().sharedMaterial); newWayPoint.GetComponent <Renderer> ().sharedMaterial.color = Color.yellow; newWayPoint.AddComponent <WayPoint> (); newWayPoint.GetComponent <WayPoint> ().index = GetWayPointLogicalIndex(); newWayPoint.name = "Waypoint_" + newWayPoint.GetComponent <WayPoint> ().index; if (useHit) { newWayPoint.transform.position = hit.point; newWayPoint.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); newWayPoint.GetComponent <WayPoint> ().hitNormal = hit.normal; } Selection.activeObject = newWayPoint; }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// CreateNewDecal /// # Create a new mesh decal in the scene /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// public GenericMeshDecal CreateNewMeshDecal(Material decalMaterial, Transform parent, Vector3 point, Vector3 normal, float scaleMultiplier, Vector2 rotationRange, bool attachDecalToCollisionObject) { //print ("CreateNewDecal"); GenericMeshDecal actualDecal = Instantiate(decalPrefab.gameObject).GetComponent <GenericMeshDecal> (); actualDecal.material = decalMaterial; actualDecal.transform.position = point + 0.001f * normal; actualDecal.transform.localScale = scaleMultiplier * actualDecal.transform.localScale; actualDecal.transform.rotation = Quaternion.FromToRotation(Vector3.up, normal); if (attachDecalToCollisionObject) { actualDecal.transform.parent = parent; } else { GameObject decalsContainer = BasicFunctions.CreateContainerIfNotExists(BasicDefines.MESH_DECAL_CONTAINER_NAME); actualDecal.transform.parent = decalsContainer.transform; } actualDecal.rotationRange = rotationRange; actualDecal.name = "RunTimeDecal"; actualDecal.UpdateDecallShape(true, false); return(actualDecal.GetComponent <GenericMeshDecal> ()); }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Update /// # Update the class /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// void Update() { if (timerBetweenDistributions <= 0) { GameObject baseContainer = BasicFunctions.CreateContainerIfNotExists("_MEAT_PARTS"); timerBetweenDistributions = Random.Range(1, 2); int index = Random.Range(0, meatPrefabs.Count); GameObject clone = Instantiate(meatPrefabs [index]); clone.transform.position = this.transform.position; //clone.GetComponent<Rigidbody> ().velocity = (-4 + 4 * Random.Range (0, 1)) * Vector3.one; Vector3 objectScale = clone.transform.localScale; objectScale.x *= Random.Range(0.5f, 2); if (index == 0) { objectScale.y *= Random.Range(0.5f, 2); objectScale.z *= Random.Range(0.5f, 2); } else { objectScale.y = objectScale.x; objectScale.z = objectScale.x; } clone.transform.localScale = objectScale; clone.transform.Rotate(Random.Range(0, 360) * Vector3.one); clone.transform.parent = baseContainer.transform; if (!clone.GetComponent <NotStainableObject> ()) { clone.AddComponent <NotStainableObject> (); } actualCloneList.Add(clone); if (actualCloneList.Count > 24) { GameObject removable = actualCloneList [0]; actualCloneList.Remove(removable); Destroy(removable); } } timerBetweenDistributions -= Time.deltaTime; }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// LateUpdate /// # Update the rain /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// void LateUpdate() { List <Particle> particleList = GetComponent <ParticleEmitter> ().particles.ToList(); int[] livingParticleList = new int[particleList.Count]; int particlesToKeep = 0; for (var i = 0; i < GetComponent <ParticleEmitter>().particleCount; i++) { Particle actualParticle = particleList [i]; if (actualParticle.energy > energyLimit) { actualParticle.color = Color.yellow; particleList [i] = actualParticle; if (splashPrefab) { GameObject rainContainer = BasicFunctions.CreateContainerIfNotExists("_RAIN"); Transform actualSplash = Instantiate(splashPrefab, particleList [i].position, Quaternion.identity) as Transform; if (actualSplash) { actualSplash.parent = rainContainer.transform; } } } else { livingParticleList [particlesToKeep++] = i; } } Particle[] keepParticles = new Particle[particlesToKeep]; for (var j = 0; j < particlesToKeep; j++) { keepParticles [j] = particleList [livingParticleList [j]]; } GetComponent <ParticleEmitter> ().particles = keepParticles; }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// HandleObjectsMode /// # To insert objects in scene using mouse /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// void HandleObjectsMode() { if ((GetEditorTimeDiff() > 0.1f) && EditorBasicFunctions.GetMouseButtonDown(0) && EditorBasicFunctions.GetInsertModeKeyPressed() && !GetDoingSomethingSpecial()) { previousEditorTime = EditorApplication.timeSinceStartup; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (lastPrefabHitPoint.ToString() == hit.point.ToString()) { //Debug.Log ("NOTE: same point duplicate -> lastPrefabHitPoint: " + lastPrefabHitPoint); } else { //Debug.Log ("lastPrefabHitPoint A: " + lastPrefabHitPoint); lastPrefabHitPoint = hit.point; //Debug.Log ("New Object"); //Debug.Log ("Hit position: " + hit.point); //Debug.Log ("Collider Name: " + hit.collider.name); GameObject actualObject = Instantiate(genericObject); switch (pivotMode) { case cPivotMode.useOriginalPivot: { actualObject.transform.position = hit.point + extraNormalOffset * hit.normal; } break; case cPivotMode.autoCalculate: { MeshFilter actualMeshFilter = BasicFunctions.GetMeshFilterInChilds(actualObject.transform); if (actualMeshFilter == null) { actualObject.transform.position = hit.point + extraNormalOffset * hit.normal; } else { float pivotOffset = extraNormalOffset + 0.5f * (Mathf.Abs(actualMeshFilter.sharedMesh.bounds.max.y) + Mathf.Abs(actualMeshFilter.sharedMesh.bounds.min.y)); //Debug.Log ("pivotOffset: " + pivotOffset); //Debug.Log ("max: " + actualMeshFilter.sharedMesh.bounds.max); //Debug.Log ("min: " + actualMeshFilter.sharedMesh.bounds.min); //Debug.Log ("Center: " + actualMeshFilter.sharedMesh.bounds.center); actualObject.transform.position = hit.point + pivotOffset * hit.normal; } } break; } actualObject.transform.localScale = Random.Range(objectScaleRange.x, objectScaleRange.y) * actualObject.transform.localScale; actualObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); actualObject.transform.Rotate(genericObject.transform.rotation.eulerAngles); actualObject.transform.Rotate(Random.Range(objectRotationRangeX.x, objectRotationRangeX.y), Random.Range(objectRotationRangeY.x, objectRotationRangeY.y), Random.Range(objectRotationRangeZ.x, objectRotationRangeZ.y)); if (attachObjectToCollisionObject) { actualObject.transform.parent = hit.collider.transform; } else { GameObject objectsContainer = BasicFunctions.CreateContainerIfNotExists(BasicDefines.OBJECT_CONTAINER_NAME); actualObject.transform.parent = objectsContainer.transform; } actualObject.AddComponent <GenericObject> (); actualObject.name = actualObject.GetComponent <GenericObject> ().Generate(BasicDefines.OBJECT_BASE_NAME, GetSeedForInstancies(), true, genericObject.name); actualObjectToForceSelect = actualObject.gameObject; } } } }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Start /// # Initialise all /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// void Start() { objectsContainer = BasicFunctions.CreateContainerIfNotExists("_OBJECTS"); }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// HandleProjectedDecalsMode /// # To insert projected decals in scene using mouse /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// void HandleProjectedDecalsMode() { if ((GetEditorTimeDiff() > 0.1f) && EditorBasicFunctions.GetMouseButtonDown(0) && EditorBasicFunctions.GetInsertModeKeyPressed() && !GetDoingSomethingSpecial()) { previousEditorTime = EditorApplication.timeSinceStartup; //Debug.Log ("Event.current.mousePosition: " + Event.current.mousePosition); Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (lastProjectedDecalsHitPoint.ToString() == hit.point.ToString()) { //Debug.Log ("NOTE: same point duplicate -> lastProjectedDecalsHitPoint: " + lastProjectedDecalsHitPoint); } else { //Debug.Log ("lastProjectedDecalsHitPoint A: " + lastProjectedDecalsHitPoint); lastProjectedDecalsHitPoint = hit.point; //Debug.Log ("New Decal"); //Debug.Log ("Hit position: " + hit.point); //Debug.Log ("Collider Name: " + hit.collider.name); GenericProjectorDecal actualProjectedDecal = Instantiate(projectedDecalPrefab.gameObject).GetComponent <GenericProjectorDecal> () as GenericProjectorDecal; actualProjectedDecal.SetOldParameters(projectedDecalPrefab.transform.localScale, projectedDecalPrefab.GetComponent <Projector> ().orthographicSize, projectedDecalPrefab.GetComponent <Projector> ().aspectRatio); //Debug.Log (actualProjectedDecal.material.mainTexture.name); actualProjectedDecal.transform.position = hit.point + 0.3f * hit.normal; Vector3 finalScale = Random.Range(actualProjectedDecal.scaleRange.x, actualProjectedDecal.scaleRange.y) * actualProjectedDecal.transform.localScale; float textureAspectRatio = (float)actualProjectedDecal.material.mainTexture.width / (float)actualProjectedDecal.material.mainTexture.height; finalScale.x = textureAspectRatio * finalScale.x; actualProjectedDecal.transform.localScale = finalScale; actualProjectedDecal.transform.LookAt(hit.point); actualProjectedDecal.transform.Rotate(new Vector3(0, 0, Random.Range(actualProjectedDecal.rotationRange.x, actualProjectedDecal.rotationRange.y))); actualProjectedDecal.name = actualProjectedDecal.Generate(BasicDefines.PROJECTED_DECAL_BASE_NAME, GetSeedForInstancies(), true, actualProjectedDecal.material.name); //Debug.Log ("actualProjectedDecal.attachToCollisionObject: " + actualProjectedDecal.attachToCollisionObject); if (actualProjectedDecal.attachToCollisionObject) { //Debug.Log ("Parent name: " + hit.collider.name); actualProjectedDecal.transform.parent = hit.collider.transform; } else { GameObject decalsContainer = BasicFunctions.CreateContainerIfNotExists(BasicDefines.PROJECTED_DECAL_CONTAINER_NAME); actualProjectedDecal.transform.parent = decalsContainer.transform; } actualProjectedDecal.UpdateShape(); actualObjectToForceSelect = actualProjectedDecal.gameObject; } } } }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// HandleMeshDecalsMode /// # To insert mesh decals in scene using mouse /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// void HandleMeshDecalsMode() { if ((GetEditorTimeDiff() > 0.1f) && EditorBasicFunctions.GetMouseButtonDown(0) && EditorBasicFunctions.GetInsertModeKeyPressed() && !GetDoingSomethingSpecial()) { previousEditorTime = EditorApplication.timeSinceStartup; //Debug.Log ("Event.current.mousePosition: " + Event.current.mousePosition); Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; //Debug.Log ("-----------------------------------------------------------"); if (Physics.Raycast(ray, out hit)) { if (lastMeshDecalsHitPoint.ToString() == hit.point.ToString()) { //Debug.Log ("NOTE: same point duplicate -> lastMeshDecalsHitPoint: " + lastMeshDecalsHitPoint); } else { //Debug.Log ("lastMeshDecalsHitPoint A: " + lastMeshDecalsHitPoint); lastMeshDecalsHitPoint = hit.point; //Debug.Log ("hit.point: " + hit.point); //Debug.Log ("lastMeshDecalsHitPoint B: " + lastMeshDecalsHitPoint); //Debug.Log ("New Decal"); //Debug.Log ("Hit position: " + hit.point); //Debug.Log ("Collider Name: " + hit.collider.name); bool setPlanarDecal = EditorBasicFunctions.planarMeshDecals; if (hit.collider.GetComponent <Terrain> ()) { //Debug.Log ("It's a terrain, set the decal as planar"); setPlanarDecal = true; } meshDecalPrefab.planarDecal = setPlanarDecal; meshDecalPrefab.comeFromEditor = setPlanarDecal; GenericMeshDecal actualDecal = Instantiate(meshDecalPrefab.gameObject).GetComponent <GenericMeshDecal> (); actualDecal.transform.position = hit.point; meshDecalPrefab.comeFromEditor = false; Vector3 finalScale = Random.Range(actualDecal.scaleRange.x, actualDecal.scaleRange.y) * actualDecal.transform.localScale; float textureAspectRatio = (float)actualDecal.material.mainTexture.width / (float)actualDecal.material.mainTexture.height; if (actualDecal.GetPlanar()) { finalScale.x = 0.5f * finalScale.x; finalScale.y = textureAspectRatio * finalScale.x; } else { finalScale.x = textureAspectRatio * finalScale.x; } //finalScale.y = finalScale.x; //finalScale.z = finalScale.x; actualDecal.transform.localScale = finalScale; actualDecal.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); actualDecal.name = actualDecal.Generate(BasicDefines.MESH_DECAL_BASE_NAME, GetSeedForInstancies(), true, actualDecal.material.name); actualDecal.UpdateDecallShape(true, false); //Debug.Log ("actualDecal.attachToCollisionObject: " + actualDecal.attachToCollisionObject); if (actualDecal.attachToCollisionObject) { //Debug.Log ("Parent name: " + hit.collider.name); actualDecal.transform.parent = hit.collider.transform; } else { GameObject decalsContainer = BasicFunctions.CreateContainerIfNotExists(BasicDefines.MESH_DECAL_CONTAINER_NAME); actualDecal.transform.parent = decalsContainer.transform; } if (actualDecal.addCollider) { actualDecal.gameObject.AddComponent <MeshCollider> (); //actualDecal.gameObject.GetComponent<MeshCollider> ().convex = true; } actualObjectToForceSelect = actualDecal.gameObject; } } } }