bool updateObjectPotition(GameObject obj, Vector3 ori, Vector2 localPos, bool isNew, int index = -1) { Vector3 normal = transform.forward; Vector3 surfacePoint = ToCircleSurface(localPos, normal, radius); Vector3 rayDir = surfacePoint - transform.position; if (Physics.Raycast(ori, rayDir, out RaycastHit hit, 1000)) { LightPath lightPath = null; if (isNew) { lightPath = new LightPath(hit.point); realPointPos.Add(lightPath); } else { lightPath = realPointPos[index]; lightPath.Clear(); lightPath.Add(hit.point, LightPath.Type.None); } Vector3 finalLightPos = hit.point - rayOffset * rayDir; if (hit.transform.tag == "ReflectionMaterial") { finalLightPos = updateObjectPotition_sup(rayDir, hit.normal, hit.point, LightPath.Type.Reflection, lightPath); } else if (hit.transform.tag == "RefractionMaterial") { finalLightPos = updateObjectPotition_sup(rayDir, hit.normal, hit.point, LightPath.Type.Refraction, lightPath); } obj.transform.position = finalLightPos; return(true); }
/** * @param normal previous hit normal * @param pos previous hit pos * @param type previous hit material (reflection or refraction) */ Vector3 updateObjectPotition_sup(Vector3 viewDir, Vector3 normal, Vector3 pos, LightPath.Type type, LightPath path) { Vector3 o = Vector3.zero; if (type == LightPath.Type.Reflection) { o = RotationFromNormal.VectorRotateAroundAxis(-viewDir, normal, 180); } else if (type == LightPath.Type.Refraction) { o = RotationFromNormal.RefractRayDirection(viewDir, normal, 0.6666f, RotationFromNormal.Space.AirTo); } if (Physics.Raycast(pos, o, out RaycastHit hit, 1000)) { path.Add(hit.point, type); LightPath.Type tp = LightPath.Type.None; if (hit.transform.tag == "ReflectionMaterial") { tp = LightPath.Type.Reflection; } else if (hit.transform.tag == "RefractionMaterial") { tp = LightPath.Type.Refraction; } if (tp != LightPath.Type.None) { return(updateObjectPotition_sup(o, hit.normal, hit.point, tp, path)); } return(hit.point - rayOffset * viewDir); } Debug.Log("Reflection/Refraction ray do not hit anything !"); return(pos - rayOffset * viewDir); }