/// API functions. /// public uint ImportMesh() { Mesh mesh = m_mesh_filter.sharedMesh; bool has_normals = (mesh.normals.Length == mesh.vertices.Length); bool has_tangents = (mesh.tangents.Length == mesh.vertices.Length); bool has_uvs = (mesh.uv.Length == mesh.vertices.Length); uint mesh_id = RoveInternal.ImportMesh((uint)mesh.vertices.Length, (uint)mesh.triangles.Length / 3, has_normals, has_tangents, has_uvs, mesh.vertices, mesh.triangles, mesh.normals, mesh.tangents, mesh.uv, (uint)mesh.subMeshCount); if (mesh_id == RoveSetup.ERROR) { Debug.LogError("Rove: Failed to create mesh; check 'Rove/Data/rove.log' " + "for details"); return(RoveSetup.ERROR); } uint current_triangle = 0; for (uint sub_mesh = 0; sub_mesh < mesh.subMeshCount; ++sub_mesh) { uint tri_count = (uint)mesh.GetTriangles((int)sub_mesh).Length / 3; RoveInternal.DefineSubMesh(mesh_id, sub_mesh, current_triangle, tri_count, m_material_indices[sub_mesh]); current_triangle += tri_count; } return(mesh_id); }
/// public void FreeMesh() { if (m_mesh_id != RoveSetup.ERROR) { RoveInternal.FreeMesh(m_mesh_id); } }
/// public void SetAtlasRect(uint atlas_type, Rect bounds) { m_textures[atlas_type].m_atlas_rect = bounds; float[] new_bounds = new float[4]; new_bounds[0] = bounds.min.x; new_bounds[1] = bounds.max.x; new_bounds[2] = bounds.min.y; new_bounds[3] = bounds.max.y; if (atlas_type == RoveAtlas.TYPE_ALBEDO) { RoveInternal.SetMaterialAlbedoBounds(m_id, new_bounds); } else if (atlas_type == RoveAtlas.TYPE_NORMAL) { RoveInternal.SetMaterialNormalBounds(m_id, new_bounds); } else if (atlas_type == RoveAtlas.TYPE_METALLIC) { RoveInternal.SetMaterialMetallicBounds(m_id, new_bounds); } else if (atlas_type == RoveAtlas.TYPE_EMISSION) { RoveInternal.SetMaterialEmissionBounds(m_id, new_bounds); } }
/// public void SetMeshTransform(float[] transform) { if (m_mesh_id != RoveSetup.ERROR) { RoveInternal.SetMeshTransform(m_mesh_id, transform); } }
/// void OnRenderImage(RenderTexture source, RenderTexture destination) { if (!m_rove_setup.m_setup_success) { RenderTexture temp_rt = RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.ARGB32); RenderTexture.active = temp_rt; GL.Clear(false, true, new Color(0.0f, 0.0f, 0.0f, 0.0f)); Graphics.Blit(temp_rt, destination); RenderTexture.active = null; RenderTexture.ReleaseTemporary(temp_rt); if (m_rove_setup.m_pending_resize_success) { if (RoveInternal.GetLock() == RoveSetup.UNLOCKED) { m_rove_setup.m_setup_success = true; m_rove_setup.m_pending_resize_success = false; } } } else { Graphics.Blit(source, destination, m_rove_setup.m_target_material); } }
/// void OnPreRender() { if (!m_rove_setup.m_setup_success) { return; } if ((Screen.width != m_rove_setup.m_last_screen_size.x) || (Screen.height != m_rove_setup.m_last_screen_size.y)) { m_rove_setup.ResizeTarget(Screen.width, Screen.height); } else if (!m_rove_setup.m_pending_resize_success) { if (!m_rove_materials.m_atlases[RoveAtlas.TYPE_ALBEDO].ProcessAtlas() || !m_rove_materials.m_atlases[RoveAtlas.TYPE_NORMAL].ProcessAtlas() || !m_rove_materials.m_atlases[RoveAtlas.TYPE_METALLIC].ProcessAtlas() || !m_rove_materials.m_atlases[RoveAtlas.TYPE_EMISSION].ProcessAtlas()) { Debug.LogError("Rove: Failed to build texture atlases."); m_rove_setup.m_setup_success = false; Destroy(m_rove_setup); return; } if (RoveInternal.StartUpdate() != RoveSetup.SUCCESS) { Debug.LogError("Rove: StartUpdate() failed; " + "check 'Rove/Data/rove.log' for details."); m_rove_setup.m_setup_success = false; Destroy(m_rove_setup); return; } GL.IssuePluginEvent(RoveInternal.FinishUpdate(), 0); } }
/// public void SetMapProperties(float intensity, float rotation) { m_map_intensity = intensity; m_map_rotation = rotation; RoveInternal.SetEnvironmentProperties(intensity, rotation); }
/// public void UploadAtlas() { byte[] atlas_bytes = m_texture.GetRawTextureData(); RoveStatus result = 0; uint width = (uint)m_width; uint height = (uint)m_height; if (m_type == RoveAtlas.TYPE_ALBEDO) { result = RoveInternal.SetAlbedoAtlas(atlas_bytes, width, height); } else if (m_type == RoveAtlas.TYPE_NORMAL) { result = RoveInternal.SetNormalAtlas(atlas_bytes, width, height); } else if (m_type == RoveAtlas.TYPE_METALLIC) { result = RoveInternal.SetMetallicAtlas(atlas_bytes, width, height); } else if (m_type == RoveAtlas.TYPE_EMISSION) { result = RoveInternal.SetEmissionAtlas(atlas_bytes, width, height); } if (result != RoveSetup.SUCCESS) { Debug.LogError("Rove: Failed to upload atlas: " + m_type + "."); } }
/// private IEnumerator SaveFrame(bool is_animation) { #if UNITY_EDITOR yield return(new WaitForEndOfFrame()); int width = Screen.width; int height = Screen.height; Texture2D rendered_image = new Texture2D(width, height, TextureFormat.RGB24, false); rendered_image.ReadPixels(new Rect(0, 0, width, height), 0, 0); rendered_image.Apply(); byte[] bytes = rendered_image.EncodeToPNG(); DestroyImmediate(rendered_image, true); uint i = 0; while (true) { string filename = m_rendering_directory + "/frame_" + i + ".png"; if (File.Exists(filename)) { ++i; continue; } try { File.WriteAllBytes(filename, bytes); } catch { Debug.LogError("Rove: Failed to write rendered image to: " + filename + "."); throw; } if (is_animation) { Debug.Log("Rove: Saved rendered frame: " + m_animation_current_frame + "/" + m_animation_framecount + ", out to: " + filename + "."); ++m_animation_current_frame; if (m_animation_current_frame >= m_animation_framecount) { Debug.Log("Rove: Animation capture completed. " + "Saved to: '" + m_rendering_directory + "/'."); m_animation_rendering = false; EditorApplication.isPlaying = false; Application.Quit(); AssetDatabase.Refresh(); } } else { Debug.Log("Rove: Saved rendered frame out to: " + filename + "."); m_frame_rendering = false; AssetDatabase.Refresh(); } break; } if (is_animation) { RoveInternal.ResetRenderAtStart(); } #endif yield return(null); }
/// public void SetAlbedo(Color albedo) { m_albedo = albedo; float[] new_albedo = new float[4]; new_albedo[0] = albedo.r; new_albedo[1] = albedo.g; new_albedo[2] = albedo.b; new_albedo[3] = albedo.a; RoveInternal.SetMaterialAlbedo(m_id, new_albedo); }
/// public void SetSunColor(Color color) { m_sun_color = color; float[] new_color = new float[4]; new_color[0] = color.r; new_color[1] = color.g; new_color[2] = color.b; new_color[3] = 1.0f; RoveInternal.SetSunColor(new_color); }
/// API functions. /// public void SetCameraTransform(Transform new_transform) { gameObject.transform.position = new_transform.position; gameObject.transform.rotation = new_transform.rotation; float[] camera_transform = new float[16]; Matrix4x4 camera_mat = new_transform.localToWorldMatrix; for (int e = 0; e < 16; ++e) { camera_transform[e] = camera_mat[e]; } RoveInternal.SetCameraTransform(camera_transform); }
/// public void ResizeTarget(int width, int height) { m_setup_success = false; m_pending_resize_success = true; m_last_screen_size.x = Screen.width; m_last_screen_size.y = Screen.height; m_target_width = width >> m_target_downsample; m_target_height = height >> m_target_downsample; m_target.Resize(m_target_width, m_target_height); m_target.Apply(); IntPtr native_tex = m_target.GetNativeTexturePtr(); GL.IssuePluginEvent(RoveInternal.Resize(native_tex), 0); }
/// public void SetImageProperties(bool tonemap, float gamma, float exposure, float contrast, float saturation, float brightness) { m_tonemap = tonemap; m_gamma = gamma; m_exposure = exposure; m_contrast = contrast; m_saturation = saturation; m_brightness = brightness; RoveInternal.SetImageProperties(tonemap, gamma, exposure, contrast, saturation, brightness); }
/// void Start() { QualitySettings.vSyncCount = 0; m_setup_success = false; m_pending_resize_success = false; if (IntPtr.Size != 8) { Debug.LogError("Rove: Rove3D currently requires 64-bit editor/build."); return; } if (!RoveInternal.LoadPlugin(m_force_opencl)) { Debug.LogError("Rove: Failed to load plugin."); return; } StartCoroutine(SetupRove()); }
/// API functions. /// public void CaptureNewAnimation(string sub_directory, int framecount) { #if UNITY_EDITOR RoveInternal.ResetRenderAtStart(); m_animation_framecount = framecount; m_animation_current_frame = 0; m_rendering_sub_directory = sub_directory; m_rendering_directory = Application.dataPath + "/RoveRenders"; if (m_rendering_sub_directory.Length > 0) { m_rendering_directory += "/" + m_rendering_sub_directory; } Directory.CreateDirectory(m_rendering_directory); m_frame_rendering = false; m_animation_rendering = true; #endif }
/// public void SetEnvironmentMap(Texture2D environment_map) { if (environment_map != null) { m_environment_map = environment_map; RenderTexture temp_rt = RenderTexture.GetTemporary(environment_map.width, environment_map.height, 0, RenderTextureFormat.ARGBHalf); RenderTexture.active = temp_rt; GL.PushMatrix(); GL.LoadPixelMatrix(0, environment_map.width, environment_map.height, 0); m_atlas_material.SetTexture("target", environment_map); Graphics.DrawTexture( new Rect(0, 0, environment_map.width, environment_map.height), environment_map, m_atlas_material); GL.PopMatrix(); Texture2D readable_map = new Texture2D(environment_map.width, environment_map.height, TextureFormat.RGBAHalf, false); readable_map.ReadPixels(new Rect(0, 0, environment_map.width, environment_map.height), 0, 0, false); readable_map.Apply(); RenderTexture.active = null; RenderTexture.ReleaseTemporary(temp_rt); RoveInternal.SetEnvironmentMap(readable_map.GetRawTextureData(), (uint)environment_map.width, (uint)environment_map.height); DestroyImmediate(readable_map, true); } else { Color[] empty_color = new Color[1]; empty_color[0].r = 1.0f; empty_color[0].g = 1.0f; empty_color[0].b = 1.0f; empty_color[0].a = 1.0f; Texture2D null_texture = new Texture2D(1, 1, TextureFormat.RGBAHalf, false); null_texture.SetPixels(empty_color); null_texture.Apply(); RoveInternal.SetEnvironmentMap(null_texture.GetRawTextureData(), 1, 1); DestroyImmediate(null_texture, true); } }
/// public void ChangeSubMeshMaterial(int sub_mesh_index, RoveMaterial material) { uint material_index; if (material != null) { material_index = m_rove_materials.AddMaterial(m_materials[sub_mesh_index]); if (material_index == RoveSetup.ERROR) { m_rove_setup.m_setup_success = false; Destroy(m_rove_setup); return; } } else { material_index = 0; } RoveInternal.ChangeSubMeshMaterial(m_mesh_id, (uint)sub_mesh_index, material_index); }
/// API functions. /// public void SetType(uint type) { m_type = type; RoveInternal.SetEnvironmentType(type); }
/// API functions. /// public void SetMapFlags(uint map_flags) { m_map_flags = map_flags; RoveInternal.SetMaterialMapFlags(m_id, map_flags); }
/// void OnDestroy() { m_setup_success = false; RoveInternal.Shutdown(); RoveInternal.UnloadPlugin(); }
/// public void SetSunIntensity(float intensity) { m_sun_intensity = intensity; RoveInternal.SetSunIntensity(intensity); }
/// public void SetSmoothness(float smoothness) { m_smoothness = smoothness; RoveInternal.SetMaterialSmoothness(m_id, smoothness); }
/// IEnumerator SetupRove() { yield return(new WaitForEndOfFrame()); m_last_screen_size = new Vector2(Screen.width, Screen.height); string data_path = Application.dataPath + "/Rove/Data"; string log_path = data_path + "/rove.log"; bool valid_log_file = false; try { File.WriteAllText(log_path, "Test write to log."); valid_log_file = true; } catch { valid_log_file = false; } if (!valid_log_file) { Debug.LogError("Rove: Unable to write to log file at: " + log_path + "."); Destroy(this); yield return(null); } m_target_width = Screen.width >> m_target_downsample; m_target_height = Screen.height >> m_target_downsample; #if (UNITY_EDITOR) if (PlayerSettings.colorSpace != ColorSpace.Gamma) { Debug.LogWarning("Rove: Gamma color space is recommended."); } #endif m_target = new Texture2D(m_target_width, m_target_height, TextureFormat.ARGB32, false, true); m_target.filterMode = FilterMode.Bilinear; IntPtr native_tex = m_target.GetNativeTexturePtr(); m_target_material = (Material)Resources.Load("RoveTargetMaterial"); m_target_material.SetTexture("target", m_target); #if ((!ROVE_FORCE_GL) && (UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN)) IntPtr d3d11_device = RoveInternal.RoveUnityGetD3D11Device(); if (d3d11_device == IntPtr.Zero) { Debug.LogError("Failed to get D3D11 device."); Destroy(this); yield return(null); } RoveInternal.SetD3D11Device(d3d11_device); #endif GL.IssuePluginEvent(RoveInternal.Setup(data_path, m_use_multi_device, m_use_integrated_device, m_optimization_level, native_tex), 0); yield return(new WaitForEndOfFrame()); if (RoveInternal.GetLock() == LOCKED) { Debug.LogError("Rove: Failed setup; check 'Rove/Data/rove.log' " + "for details."); Destroy(this); yield return(null); } else { // Additional resize fixes bug on some Linux drivers. m_target.Resize(m_target_width, m_target_height); m_target.Apply(); native_tex = m_target.GetNativeTexturePtr(); GL.IssuePluginEvent(RoveInternal.Resize(native_tex), 0); yield return(new WaitForEndOfFrame()); if (RoveInternal.GetLock() == LOCKED) { Debug.LogError("Rove: Failed to resize; check 'Rove/Data/rove.log' " + "for details."); Destroy(this); yield return(null); } Color[] black_pixels = new Color[m_target_width * m_target_height]; for (uint p = 0; p < m_target_width * m_target_height; ++p) { black_pixels[p] = Color.black; } m_target.SetPixels(black_pixels); m_target.Apply(); m_system_info.m_thread_count = RoveInternal.GetThreadCount(); m_system_info.m_compute_api = RoveInternal.GetComputeAPI(); m_system_info.m_device_count = RoveInternal.GetDeviceCount(); m_system_info.m_device_names = new string[m_system_info.m_device_count]; for (uint d = 0; d < m_system_info.m_device_count; ++d) { m_system_info.m_device_names[d] = Marshal.PtrToStringAnsi(RoveInternal.GetDeviceName(d)); } } m_rove_camera = GetAPIComponent <RoveCamera>(); if (!m_rove_camera) { Debug.LogError("Rove: Could not find an active RoveCamera component."); Destroy(this); yield return(null); } m_rove_environment = GetAPIComponent <RoveEnvironment>(); if (!m_rove_environment) { Debug.LogError("Rove: Could not find an active RoveEnvironment component."); Destroy(this); yield return(null); } SetMaxBounces(m_max_bounces); SetSamplesPerFrame(m_samples_per_frame); m_rove_camera.SetCameraApertureSize(m_rove_camera.m_aperture_size); m_rove_camera.SetCameraFocalDepth(m_rove_camera.m_focal_depth); m_rove_camera.SetImageProperties(m_rove_camera.m_tonemap, m_rove_camera.m_gamma, m_rove_camera.m_exposure, m_rove_camera.m_contrast, m_rove_camera.m_saturation, m_rove_camera.m_brightness); m_rove_environment.SetType(m_rove_environment.m_type); if (m_rove_environment.m_type == RoveEnvironment.TYPE_MAP) { m_rove_environment.SetEnvironmentMap(m_rove_environment.m_environment_map); m_rove_environment.SetMapProperties(m_rove_environment.m_map_intensity, m_rove_environment.m_map_rotation); } m_setup_success = true; }
/// public void SetMaxBounces(int max_bounces) { m_max_bounces = max_bounces; RoveInternal.SetMaxBounces((uint)m_max_bounces); }
/// public void SetSunDirection(float[] direction) { RoveInternal.SetSunDirection(direction); }
/// public void SetSkyIntensity(float intensity) { m_sky_intensity = intensity; RoveInternal.SetSkyIntensity(intensity); }
/// public void SetMetallic(float metallic) { m_metallic = metallic; RoveInternal.SetMaterialMetallic(m_id, metallic); }
/// public void SetSamplesPerFrame(int samples_per_frame) { m_samples_per_frame = samples_per_frame; RoveInternal.SetSamplesPerFrame((uint)m_samples_per_frame); }
/// public void SetDoubleSided(bool double_sided) { m_double_sided = double_sided; RoveInternal.SetMaterialDoubleSided(m_id, double_sided); }