State findPlayer() { // Turn towards target Quaternion q = Quaternion.LookRotation(target.transform.position - this.transform.position); this.transform.rotation = Quaternion.Lerp(this.transform.rotation, q, .015f); this.transform.eulerAngles = new Vector3(0, this.transform.eulerAngles.y, this.transform.eulerAngles.z); // Move forward transform.Translate(Vector3.forward * this.speed * Time.deltaTime); // Lock Sandshark to desert terrain - TODO: account for other objects in desert RaycastHit hit = new RaycastHit(); Ray r = new Ray(new Vector3(this.transform.position.x, 100, this.transform.position.z), Vector3.down); terrain.Raycast(r, out hit, 100); this.transform.position = new Vector3(this.transform.position.x, hit.point.y, this.transform.position.z); // State transitions if (Vector3.Distance(this.transform.position, this.target.transform.position) < 15.0f) { return(State.waiting); } else { return(State.searching); } }
public void adjustHeights(SimpleTerrainOperator op) { var m = new FieldManipulator(min, max, op.fieldUnitR, op.fieldLength); var hs = m.getHeights(op.td); var tofs = new Vector3(op.terrainPosition.x, op.terrainPositionHeight, op.terrainPosition.y); for (var iy = 0; iy < m.len.y; iy++) { for (var ix = 0; ix < m.len.x; ix++) { var pos = m.getIterationPosition3d(ix, iy, op.fieldUnit) + tofs; var start = pos + Vector3.up * 512.0f; var end = pos + Vector3.down * 512.0f; var ray = new Ray(start, end - start); var res = new RaycastHit(); if (mc.Raycast(ray, out res, 1024.0f)) { hs[iy, ix] = (res.point.y - op.terrainPositionHeight) * op.fieldUnitHeightR; } } } m.setHeights(op.td, hs); }
private bool CheckMeshRaycast(Triangle t0, Triangle t1, MeshCollider collider, out Vector3 hit_pos) { Ray ray = default(Ray); ray.origin = t0.p0; ray.direction = t0.p1 - t0.p0; float magnitude = (t0.p1 - t0.p0).magnitude; RaycastHit raycastHit; if (magnitude > 0f && collider.Raycast(ray, out raycastHit, magnitude)) { hit_pos = raycastHit.point; return(true); } ray.origin = t0.p1; ray.direction = t0.p2 - t0.p1; magnitude = (t0.p2 - t0.p1).magnitude; if (magnitude > 0f && collider.Raycast(ray, out raycastHit, magnitude)) { hit_pos = raycastHit.point; return(true); } ray.origin = t0.p2; ray.direction = t0.p0 - t0.p2; magnitude = (t0.p0 - t0.p2).magnitude; if (magnitude > 0f && collider.Raycast(ray, out raycastHit, magnitude)) { hit_pos = raycastHit.point; return(true); } hit_pos = Vector3.zero; return(false); }
bool ConcaveHull(MeshCollider meshCollider, Vector3 position, Ray ray, RaycastHit hit) { tempRay.origin = position; tempRay.direction = -ray.direction; customDistance = distance - hit.distance; while (meshCollider.Raycast(tempRay, out tempHit, customDistance)) { if (tempHit.triangleIndex == hit.triangleIndex) { break; } ray.origin = -ray.direction * customDistance + position; if (!meshCollider.Raycast(ray, out hit, customDistance)) { return(true); } if (tempHit.triangleIndex == hit.triangleIndex) { break; } customDistance -= hit.distance; } return(false); }
void Update() { if (Input.GetButtonDown("Fire1")) { RaycastHit hitInfo; if (coll.Raycast(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f)), out hitInfo, 2)) { Modify(hitInfo.point, brushSize, false); } } else if (Input.GetButtonDown("Fire2")) { RaycastHit hitInfo; if (coll.Raycast(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f)), out hitInfo, 2)) { Modify(hitInfo.point, brushSize, true); } } brushSize = Mathf.Clamp01(brushSize + Input.GetAxisRaw("Mouse ScrollWheel") * 0.1f); if (meshModified) { RefreshMesh(); meshModified = false; } }
void leftpress() { if (leftcontrol == 0) { meshCollider.enabled = true; GameObject.Destroy(currentCollisionMesh); currentCollisionMesh = GameObject.Instantiate(cloth.clothMesh); meshCollider.sharedMesh = currentCollisionMesh; VRTK_StraightPointerRenderer tmp = GameObject.Find("LeftController").GetComponent <VRTK_StraightPointerRenderer>(); Ray ray = tmp.getray(); RaycastHit hitInfo; //Debug.Log("left_drag"); if (meshCollider.Raycast(ray, out hitInfo, 100)) { int[] tris = currentCollisionMesh.triangles; Vector3[] vertices = currentCollisionMesh.vertices; lefthit = hitInfo; // find closest vertex in the triangle we just hit: int closestVertex = -1; float minDistance = float.MaxValue; for (int i = 0; i < 3; ++i) { int vertex = tris[hitInfo.triangleIndex * 3 + i]; float distance = (vertices[vertex] - hitInfo.point).sqrMagnitude; if (distance < minDistance) { minDistance = distance; closestVertex = vertex; } } // get particle index: if (closestVertex >= 0 && closestVertex < cloth.topology.visualMap.Length) { pickedParticleIndexLeft = cloth.topology.visualMap[closestVertex]; pickedParticleDepthLeft = Mathf.Abs((cloth.transform.TransformPoint(vertices[closestVertex]) - Camera.main.transform.position).z); if (OnParticlePickedLeft != null) { //Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, pickedParticleDepthLeft)); Vector3 worldPosition = new Vector3(hitInfo.point.x, hitInfo.point.y, pickedParticleDepthLeft); OnParticlePickedLeft(this, new ParticlePickEventArgs(pickedParticleIndexLeft, worldPosition)); } } } meshCollider.enabled = false; leftcontrol++; } }
private void Update() { if (renderer.enabled) { return; } if (duration > 0) { duration -= Time.deltaTime; return; } RaycastHit hit; if (!collider.Raycast( new Ray(cameraTransform.position - cameraTransform.forward * 100, cameraTransform.forward), out hit, controller.Length + 100)) { renderer.enabled = true; for (int i = 0; i < renderer.gameObject.transform.childCount; i++) { renderer.gameObject.transform.GetChild(i).gameObject.SetActive(true); } for (int i = 0; i < DisappearedObjects.Count; i++) { DisappearedObjects[i].SetActive(true); } { var __list2 = CoveredObjects; var __listCount2 = __list2.Count; for (int __i2 = 0; __i2 < __listCount2; ++__i2) { var o = __list2[__i2]; { if (o == null) { continue; } var r = o.renderer; if (r == null) { continue; } r.enabled = false; } } } } }
/// <summary> /// Tests a "ray" against a collider; Really we are testing whether a point is located within or is intersecting with a collider /// </summary> /// <param name="collisionTester">A mesh collider located at the origin used to test the object in it's local space</param> /// <param name="obj">The object to test collision on</param> /// <param name="ray">A ray positioned at a vertex of the tester's collider</param> /// <returns>The result of whether the point/ray is intersection with or located within the object</returns> public static bool TestRay(MeshCollider collisionTester, Transform obj, Ray ray) { var mf = obj.GetComponent <MeshFilter>(); if (mf) { collisionTester.sharedMesh = mf.sharedMesh; } ray.origin = obj.InverseTransformPoint(ray.origin); ray.direction = obj.InverseTransformDirection(ray.direction); var boundsSize = collisionTester.bounds.size.magnitude; var maxDistance = boundsSize * 2f; // Shoot a ray from outside the object (due to face normals) in the direction of the ray to see if it is inside var forwardRay = new Ray(ray.origin, ray.direction); forwardRay.origin = forwardRay.GetPoint(-boundsSize); Vector3 forwardHit; RaycastHit hitInfo; if (collisionTester.Raycast(forwardRay, out hitInfo, maxDistance)) { forwardHit = hitInfo.point; } else { return(false); } // Shoot a ray in the other direction, too, from outside the object (due to face normals) Vector3 behindHit; var behindRay = new Ray(ray.origin, -ray.direction); behindRay.origin = behindRay.GetPoint(-boundsSize); if (collisionTester.Raycast(behindRay, out hitInfo, maxDistance)) { behindHit = hitInfo.point; } else { return(false); } // Check whether the point (i.e. ray origin) is contained within the object var collisionLine = forwardHit - behindHit; var projection = Vector3.Dot(collisionLine, ray.origin - behindHit); return(projection >= 0f && projection <= collisionLine.sqrMagnitude); }
bool ConcaveHull(Ray ray, RaycastHit hit) { tempRay.origin = transform.position; tempRay.direction = -ray.direction; float customDistance = distance - hit.distance; int lastPoint = hit.triangleIndex; while (meshCollider.Raycast(tempRay, out tempHit, customDistance)) { if (tempHit.triangleIndex == lastPoint) { break; } lastPoint = tempHit.triangleIndex; customDistance = tempHit.distance; ray.origin = -ray.direction * customDistance + transform.position; if (!meshCollider.Raycast(ray, out tempHit, customDistance)) { concaveHull = true; return(true); } if (tempHit.triangleIndex == lastPoint) { break; } lastPoint = tempHit.triangleIndex; customDistance -= tempHit.distance; } return(false); }
void SetTileCollision(int XID0, int YID0) { RaycastHit hitinfo; Ray PosRay = new Ray(); PosRay.origin = GetPosFromID(XID0, YID0); PosRay.origin = new Vector3(PosRay.origin.x, 100.0f, PosRay.origin.z); PosRay.direction = new Vector3(0, -1000.0f, 0); if (CollisionGround.Raycast(PosRay, out hitinfo, 200.0f) == true) { MapTile[YID0 * MapX + XID0].Attrib = TileAttrib.Stand; } }
/// <summary></summary> public static bool Collide(MeshCollider m, Line l) { if (Collide(m, l.origin)) { return(true); } RaycastHit hit; if (m.Raycast(l, out hit, 10000)) { return(true); } l.direction = -l.direction; return(m.Raycast(l, out hit, 10000)); }
void getindex1() { meshCollider.enabled = true; GameObject.Destroy(currentCollisionMesh); currentCollisionMesh = GameObject.Instantiate(cloth.clothMesh); meshCollider.sharedMesh = currentCollisionMesh; //change here Ray ray = Camera.main.ScreenPointToRay(new Vector3(coordinates[2 * (fournumberint - 1)], coordinates[2 * (fournumberint - 1) + 1])); // Debug.Log(new Vector3(coordinates[2 * (fournumberint - 1)], coordinates[2 * (fournumberint - 1) + 1])); RaycastHit hitInfo; if (meshCollider.Raycast(ray, out hitInfo, 100)) { int[] tris = currentCollisionMesh.triangles; Vector3[] vertices = currentCollisionMesh.vertices; // find closest vertex in the triangle we just hit: int closestVertex = -1; float minDistance = float.MaxValue; for (int i = 0; i < 3; ++i) { int vertex = tris[hitInfo.triangleIndex * 3 + i]; float distance = (vertices[vertex] - hitInfo.point).sqrMagnitude; if (distance < minDistance) { minDistance = distance; closestVertex = vertex; //update closest vertex } } // get particle index: if (closestVertex >= 0 && closestVertex < cloth.topology.visualMap.Length) { pickedParticleIndex = cloth.topology.visualMap[closestVertex]; pickedParticleDepth = Mathf.Abs((cloth.transform.TransformPoint(vertices[closestVertex]) - Camera.main.transform.position).z); if (OnParticlePickedaxis1 != null) { Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, pickedParticleDepth)); OnParticlePickedaxis1(this, new ParticleVectorArgs(pickedParticleIndex, worldPosition)); } } } meshCollider.enabled = false; }
public static bool Raycast(MeshCollider targetCollider, Ray pickRay, out int hitTriIndex, float rayLength) { bool usePhysX = false; if (usePhysX) { // Let Unity/PhysX perform the raycast (potentially faster, but buggy) RaycastHit hit; if (targetCollider.Raycast(pickRay, out hit, rayLength)) { hitTriIndex = hit.triangleIndex; return(true); } else { hitTriIndex = -1; return(false); } } else { // Manually raycast against the collider's triangles return(RaycastTriangles(targetCollider, pickRay, out hitTriIndex, rayLength)); } }
void Update() { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (meshCollider.Raycast(ray, out hit, 100.0f)) { clickedPoints.Add(hit.point); } } Vector3 h = Vector3.up; if (editMesh && mesh != null) { Debug.Log("Update"); Vector3[] vertices = mesh.vertices; vertices[0] = meanPoint + extrusionLevel * h; for (int i = 1; i <= clickedPoints.Count; i++) { vertices[i] = clickedPoints[i - 1] + extrusionLevel * h; } mesh.vertices = vertices; meshFilter.mesh = mesh; mesh.RecalculateBounds(); mesh.RecalculateNormals(); } }
public GameObjectRayHit RaycastMeshObject(Ray ray, GameObject gameObject) { Mesh objectMesh = gameObject.GetMesh(); RTMesh rtMesh = RTMeshDb.Get.GetRTMesh(objectMesh); if (rtMesh != null) { MeshRayHit meshRayHit = rtMesh.Raycast(ray, gameObject.transform.localToWorldMatrix); if (meshRayHit != null) { return(new GameObjectRayHit(ray, gameObject, meshRayHit)); } } else { // If no RTMesh instance is available, we will cast a ray against // the object's MeshCollider as a last resort. This is actually useful // when dealing with static mesh objects. These objects' meshes have // their 'isReadable' flag set to false and can not be used to create // an RTMesh instance. Thus a mesh collider is the next best choice. MeshCollider meshCollider = gameObject.GetComponent <MeshCollider>(); if (meshCollider != null) { RaycastHit rayHit; if (meshCollider.Raycast(ray, out rayHit, float.MaxValue)) { return(new GameObjectRayHit(ray, rayHit)); } } } return(null); }
static bool TestRay(MeshCollider mc, GameObject mcContainer, Texture2D tex, Color matCol, Mesh mesh, Vector3 point, Vector3 dir, out Vector3 hitpoint, out Color col) { RaycastHit hitInfo; Ray ray = new Ray(point, dir); if (mc.Raycast(ray, out hitInfo, Mathf.Infinity)) { if (hitInfo.collider.gameObject == mcContainer) { hitpoint = hitInfo.point; if (tex != null) { Vector3 uv = hitInfo.textureCoord; col = tex.GetPixel((int)(uv.x * tex.width), (int)(uv.y * tex.height)); } else { col = matCol; } return(true); } } hitpoint = Vector3.zero; col = Color.white; return(false); }
private bool IsHoveringUnder() { RaycastHit hitInfo; Ray ray = new Ray(HandPrototypeProxies.Instance.RightIndex.position, -transform.forward); return(BackwardsBackdrop.Raycast(ray, out hitInfo, float.PositiveInfinity)); }
private bool GetIsHovered(MeshCollider collider) { RaycastHit hitInfo; Ray ray = new Ray(HandPrototypeProxies.Instance.RightIndex.position - HoverZone.transform.forward, HoverZone.transform.forward); return(collider.Raycast(ray, out hitInfo, float.PositiveInfinity)); }
/// <summary> /// Checks if mesh generation on other thread or on the GPU is finished. If so, it is applied to the mesh. /// </summary> public void Update() { if (cookie != null && cookie.IsCompleted) { MeshData result = method.EndInvoke(cookie); ApplyToMesh(result); UpdateDistances(); cookie = null; method = null; } if (isComputingOnGPU && gpuReadbackReq.done) { isComputingOnGPU = false; if (gpuReadbackReq.hasError) { computeBuffer.Dispose(); computeBuffer = null; configurationOld = bool4.True; GetNeighbors(); } else { var a = gpuReadbackReq.GetData <Vector3>().ToArray(); MeshData md = new MeshData(a, planet.quadMesh.normals, planet.quadMesh.uv); //print(md.vertices.Length + ", [0]: " + md.vertices[0].ToString("F4") + ", [1089]: " + md.vertices[1089].ToString("F4")); method = SpherifyAndDisplace; cookie = method.BeginInvoke(md, null, null); computeBuffer.Dispose(); computeBuffer = null; } } //Foliage Stuff: if (planet.generateDetails && (planet.generateFoliageInEveryBiome || planet.foliageBiomes.Contains(uniformBiome))) //Generating details if enabled and right biome. { if (level >= planet.grassLevel && foliageRenderer == null && renderedQuad && collider && distance < planet.dtDisSqr && planet.detailObjectsGenerating < planet.detailObjectsGeneratingSimultaneously) { var down = planet.Vector3Down(renderedQuad.transform.position); Ray ray = new Ray(collider.bounds.center - (down * 500), down); RaycastHit hit; if (collider.Raycast(ray, out hit, 5000f)) //Only start foliage generation if the collider is working, it needs a few frames to initialize { foliageRenderer = renderedQuad.AddComponent <FoliageRenderer>(); foliageRenderer.planet = planet; foliageRenderer.quad = this; planet.detailObjectsGenerating++; } } if (foliageRenderer != null && distance > planet.dtDisSqr) { MonoBehaviour.Destroy(foliageRenderer); foliageRenderer = null; } } }
// Update is called once per frame public void WhileDown(Ray ray) { RaycastHit hit; if (person.Raycast(ray, out hit, 100.0f)) { down = true; hitPoint = hit.point; hitUV = hit.textureCoord; hitNormal = hit.normal; traceRepresent.transform.position = hitPoint; triIDs = new Vector3( person.sharedMesh.triangles[hit.triangleIndex * 3 + 0], person.sharedMesh.triangles[hit.triangleIndex * 3 + 1], person.sharedMesh.triangles[hit.triangleIndex * 3 + 2]); bary = hit.barycentricCoordinate; hitTangent = bary.x * HELP.ToV3(person.sharedMesh.tangents[(int)triIDs.x]); hitTangent += bary.y * HELP.ToV3(person.sharedMesh.tangents[(int)triIDs.y]); hitTangent += bary.z * HELP.ToV3(person.sharedMesh.tangents[(int)triIDs.z]); } else { down = false; } }
public void OnCollisionEnter(Collision collision) { bool dirty = false; for (int i = 0; i < collision.contacts.Length; i++) { ContactPoint cp = collision.contacts[i]; RaycastHit hitInfo; Ray ray = new Ray(cp.point - cp.normal, cp.normal); if (meshCollider.Raycast(ray, out hitInfo, 2.0f)) { if (OnCubeHit(triangleCubeMap[hitInfo.triangleIndex], cp)) { dirty = true; } } } if (dirty) { UpdateMesh(); } }
// Update is called once per frame void Update() { if (!tensorFlowIsRunning) { if (dragging) { if (Input.GetMouseButtonUp(0)) { endDragging(); } else { drag(); } } else { if (Input.GetMouseButtonDown(0)) { MeshCollider mc = GetComponent <MeshCollider>(); var ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (mc.Raycast(ray, out hit, 2000.0f)) { startDragging(hit); } } } } }
public static void PushOutFromMeshCollider(MeshCollider mesh, Collision collision, float segmentColliderRadius, ref Vector3 pos) { Vector3 collisionPoint = collision.contacts[0].point; Vector3 pushNormal = collision.contacts[0].normal; RaycastHit info; // Doing cheap mesh raycast from outside to hit surface if (mesh.Raycast(new Ray(pos + pushNormal * segmentColliderRadius * 2f, -pushNormal), out info, segmentColliderRadius * 5)) { pushNormal = info.point - pos; float pushMagn = pushNormal.sqrMagnitude; if (pushMagn > 0 && pushMagn < segmentColliderRadius * segmentColliderRadius) { pos = info.point - pushNormal * (segmentColliderRadius / Mathf.Sqrt(pushMagn)) * 0.9f; } } else { pushNormal = collisionPoint - pos; float pushMagn = pushNormal.sqrMagnitude; if (pushMagn > 0 && pushMagn < segmentColliderRadius * segmentColliderRadius) { pos = collisionPoint - pushNormal * (segmentColliderRadius / Mathf.Sqrt(pushMagn)) * 0.9f; } } }
public bool Raycast(Ray r, out Vector3 hitSpot) { MeshCollider mc = GetComponent <MeshCollider>(); hitSpot = Vector3.zero; if (mc == null) { return(false); } RaycastHit hit; if (mc.Raycast(r, out hit, 1000)) { if (Vector3.Dot(hit.normal, Vector3.up) > 0.3) { hitSpot = map.InverseTransformPointWorld(new Vector3( hit.point.x, hit.point.y, hit.point.z )); } return(true); } return(false); }
// Update is called once per frame void Awake() { meshCollider = GetComponent <MeshCollider>(); spawnTerrainXMin = meshCollider.transform.position.x; spawnTerrainXMax = spawnTerrainXMin + 25; spawnTerrainZMin = meshCollider.transform.position.z; spawnTerrainZMax = spawnTerrainZMin + 25; totalSpawns = 0; while (totalSpawns < totalSpawnMinimum) { float maxHeight = 20f; float positionX = Random.Range(spawnTerrainXMin, spawnTerrainXMax); float positionZ = Random.Range(spawnTerrainZMin, spawnTerrainZMax); Ray ray = new Ray(new Vector3(positionX, maxHeight, positionZ), Vector3.down); RaycastHit hit; if (meshCollider.Raycast(ray, out hit, maxHeight)) { Vector3 spawnPosition = new Vector3(positionX, hit.point.y, positionZ); Instantiate(spawnPrefab, spawnPosition, Quaternion.identity); } totalSpawns++; } }
private Vector2 GetPixelTextureCoords(Ray r) { RaycastHit hit; MeshCollider coll = videoSphere.GetComponent <MeshCollider>(); if (coll != null && coll.Raycast(r, out hit, Mathf.Infinity)) { //Texture2D tex = videoSphere.GetComponent<MeshRenderer>().material.mainTexture as Texture2D; //Texture2D tex = videoSphere.GetComponent<VideoPlayer>().targetTexture.material.mainTexture as Texture2D; float width = videoSphere.GetComponent <VideoPlayer>().targetTexture.width; float height = videoSphere.GetComponent <VideoPlayer>().targetTexture.height; Vector2 pixelUV = hit.textureCoord; float pixelX = pixelUV.x * width; float pixelY = pixelUV.y * height; //Debug.Log("Texture A size: " + width + ", " + height); //Debug.Log("Centre position in pano A: " + pixelX + ", " + pixelY); return(new Vector2(pixelX, pixelY)); } return(new Vector2(0f, 0f)); }
void Update() { var mouse = Input.mousePosition; var ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward); RaycastHit info; hit = col.Raycast(ray, out info, float.MaxValue); if (hit) { point = info.point; var t = info.triangleIndex * 3; var a = triangles[t]; var b = triangles[t + 1]; var c = triangles[t + 2]; var va = vertices[a]; var vb = vertices[b]; var vc = vertices[c]; normal = transform.TransformDirection(Vector3.Cross(vb - va, vc - va)); rotation = Quaternion.LookRotation(normal); } //if(Input.GetMouseButtonUp(0) && hit) { // Grow(); //} }
void Update() { if (Input.GetMouseButton(0)) //On touch { Ray _mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); //Ray with direction camera->screenpoint //Debug.DrawRay(_mouseRay.origin, _mouseRay.direction * 100, Color.yellow, 10); //check for ray collision if (m_oBody.Raycast(_mouseRay, out m_oRayHit, Mathf.Infinity)) { if (OnBodyHit != null) { OnBodyHit(true); } ColorBodyTexturePoint(m_oRayHit.textureCoord); } else { if (OnBodyHit != null) { OnBodyHit(false); } } } else { if (OnBodyHit != null) { OnBodyHit(false); } } }
void Update() { var mouse = Input.mousePosition; var ray = Camera.main.ScreenPointToRay(mouse); RaycastHit info; hit = col.Raycast(ray, out info, float.MaxValue); if (hit) { point = info.point; var t = info.triangleIndex * 3; var a = triangles[t]; var b = triangles[t + 1]; var c = triangles[t + 2]; var va = vertices[a]; var vb = vertices[b]; var vc = vertices[c]; normal = transform.TransformDirection(Vector3.Cross(vb - va, vc - va)); rotation = Quaternion.LookRotation(normal); } if (Input.GetMouseButtonUp(0) && hit) { var go = Instantiate(prefabs[Random.Range(0, prefabs.Count)]) as GameObject; go.transform.position = point; go.transform.localScale = Vector3.one * Random.Range(0.4f, 0.5f); go.transform.localRotation = Quaternion.LookRotation(Vector3.forward, normal); // * Quaternion.AngleAxis(Random.Range(0f, 360f), Vector3.up); } }
internal static int[] GetTrianglesInSquare(MeshCollider meshCollider, Vector2 centre, IBrush brush) { Ray ray = brush.GetRayFromPoint(centre); Debug.DrawRay(ray.origin, ray.direction * 10, Color.black, 1); RaycastHit hit; List <int> triangles = new List <int>(); if (meshCollider.Raycast(ray, out hit, float.MaxValue)) { triangles.Add(hit.triangleIndex); Stack <Rect> rects = new Stack <Rect>(); Dictionary <Vector2, RayResult> rayResults = new Dictionary <Vector2, RayResult>(); rects.Push(new Rect(centre + (Vector2.down + Vector2.left) * brush.Size * 0.5f, new Vector2(brush.Size, brush.Size))); int rectsChecked = 0; while (rects.Count > 0) { rectsChecked++; Rect rect = rects.Pop(); Vector2 BL = new Vector2(rect.xMin, rect.yMin); Vector2 BR = new Vector2(rect.xMax, rect.yMin); Vector2 TL = new Vector2(rect.xMin, rect.yMax); Vector2 TR = new Vector2(rect.xMax, rect.yMax); List <int> theseTriangles = new List <int>(); AddTriangleToListIfHit(brush, meshCollider, TR, theseTriangles, rayResults); AddTriangleToListIfHit(brush, meshCollider, TL, theseTriangles, rayResults); AddTriangleToListIfHit(brush, meshCollider, BR, theseTriangles, rayResults); AddTriangleToListIfHit(brush, meshCollider, BL, theseTriangles, rayResults); if (theseTriangles.Count > 1 && rect.width > MIN_TRIANGLE_WIDTH) { float size = rect.width * 0.5f; rects.Push(new Rect(rect.xMin, rect.yMin, size, size)); rects.Push(new Rect(rect.xMin, rect.yMin + size, size, size)); rects.Push(new Rect(rect.xMin + size, rect.yMin, size, size)); rects.Push(new Rect(rect.xMin + size, rect.yMin + size, size, size)); } // add the triangles we don't have already for (int t = 0; t < theseTriangles.Count; t++) { if (!triangles.Contains(theseTriangles[t])) { triangles.Add(theseTriangles[t]); } } } //Debug.Log("checked " + rectsChecked + " rects"); //Debug.Log("ray results dict size is " + rayResults.Count); } return(triangles.ToArray()); }