public void GenerateMeshTerrain() { _objects = new List <ObjectData>(); for (int i = 0; i <= MeshTerrainMeshSourceList.Count - 1; i++) { if (MeshTerrainMeshSourceList[i].MeshRenderer.GetComponent <MeshFilter>().sharedMesh == null) { continue; } ObjectData o = new ObjectData(MeshTerrainMeshSourceList[i].MeshRenderer, (int)MeshTerrainMeshSourceList[i].TerrainSourceID); if (o.IsValid) { _objects.Add(o); } } BVH.Build(ref _objects, out _nodes, out Tris, out _finalPrims); BVH.BuildLbvhData(_nodes, _finalPrims, out MeshTerrainData.lNodes, out MeshTerrainData.lPrims); MeshTerrainData.Bounds = CalculateTerrainBounds(); #if UNITY_EDITOR EditorUtility.SetDirty(MeshTerrainData); #endif //GenerateCellCoverage(); CreateNativeArrays(); VegetationStudioManager.RefreshTerrainArea(TerrainBounds); }
public void SetTrigles(TrigleFace[] trigles) { this.trigles = trigles; if (trigles != null && trigles.Length > 0) { Bounds3 bounds = trigles[0].BoundBox; foreach (var item in trigles) { bounds = Bounds3.Union(bounds, item.BoundBox); //item.material = _material; } this.BoundBox = bounds; } else { this.BoundBox = new Bounds3(); } CenterPoint = (BoundBox.pMin + BoundBox.pMax) * 0.5f; BVH = BVH.Build(trigles.AsSpan()); }
public void BuildPipeline() { if (IsInitialized) { return; } if (m_PathTracingShader == null) { return; } if (m_Camera == null) { return; } m_KernelIndex = m_PathTracingShader.FindKernel(m_KernelName); if (m_KernelIndex < 0) { return; } if (m_EnableNEE) { m_PathTracingShader.EnableKeyword("ENABLE_NEXT_EVENT_ESTIMATION"); } else { m_PathTracingShader.DisableKeyword("ENABLE_NEXT_EVENT_ESTIMATION"); } if (m_EnableThinLens) { m_PathTracingShader.EnableKeyword("ENABLE_THIN_LENS"); } else { m_PathTracingShader.DisableKeyword("ENABLE_THIN_LENS"); } if (m_EnableSampleTexture) { m_PathTracingShader.EnableKeyword("ENABLE_SAMPLE_TEXTURE"); } else { m_PathTracingShader.DisableKeyword("ENABLE_SAMPLE_TEXTURE"); } if (m_EnableTangentSpace) { m_PathTracingShader.EnableKeyword("ENABLE_TANGENT_SPACE"); } else { m_PathTracingShader.DisableKeyword("ENABLE_TANGENT_SPACE"); } m_RenderTexture = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBFloat); m_RenderTexture.enableRandomWrite = true; m_RenderTexture.Create(); m_DispatchThreadGroupsX = Mathf.CeilToInt((float)m_RenderTexture.width / kTileSize); m_DispatchThreadGroupsY = Mathf.CeilToInt((float)m_RenderTexture.height / kTileSize); List <PTMaterial> materials; List <Primitive> primitives; List <Texture2D> baseColorTextures = null; List <Texture2D> mroTextures = null; List <Texture2D> normalTextures = null; if (m_EnableSampleTexture) { baseColorTextures = new List <Texture2D>(); } if (m_EnableTangentSpace) { mroTextures = new List <Texture2D>(); normalTextures = new List <Texture2D>(); } CollectPrimitives(out materials, out primitives, ref baseColorTextures, ref mroTextures, ref normalTextures); BVH bvhTree = new BVH(); bvhTree.Build(primitives); List <Sphere> spheres; List <Quad> quads; List <Cube> cubes; List <Triangle> triangles; List <LBVHNode> bvh; int bvhRoot = BuildLBVH(bvhTree, out bvh, out spheres, out quads, out cubes, out triangles); if (m_EnableSampleTexture) { m_baseColorTextureArray = CreateTextureArray(baseColorTextures); m_PathTracingShader.SetTexture(m_KernelIndex, "_Textures", m_baseColorTextureArray); } if (m_EnableTangentSpace) { m_mroTextureArray = CreateTextureArray(mroTextures); m_normalTextureArray = CreateTextureArray(normalTextures); m_PathTracingShader.SetTexture(m_KernelIndex, "_MROTextures", m_mroTextureArray); m_PathTracingShader.SetTexture(m_KernelIndex, "_NormalTextures", m_normalTextureArray); } m_NodesBuffer = CreateComputeBuffer <LBVHNode>(bvh); m_SpheresBuffer = CreateComputeBuffer <Sphere>(spheres); m_QuadsBuffer = CreateComputeBuffer <Quad>(quads); m_CubeBuffer = CreateComputeBuffer <Cube>(cubes); m_TrianglesBuffer = CreateComputeBuffer <Triangle>(triangles); m_MaterialsBuffer = CreateComputeBuffer <PTMaterial>(materials); m_PathTracingShader.SetBuffer(m_KernelIndex, "_Spheres", m_SpheresBuffer); m_PathTracingShader.SetBuffer(m_KernelIndex, "_Quads", m_QuadsBuffer); m_PathTracingShader.SetBuffer(m_KernelIndex, "_Cubes", m_CubeBuffer); m_PathTracingShader.SetBuffer(m_KernelIndex, "_Triangles", m_TrianglesBuffer); m_PathTracingShader.SetBuffer(m_KernelIndex, "_BVHTree", m_NodesBuffer); m_PathTracingShader.SetBuffer(m_KernelIndex, "_Materials", m_MaterialsBuffer); m_PathTracingShader.SetInt("_BVHRootNodeIndex", bvhRoot); m_PathTracingShader.SetInt("_BVHNodeCount", bvh.Count); IsInitialized = true; m_PathTracingShader.SetTexture(m_KernelIndex, "_Result", m_RenderTexture); m_PathTracingShader.SetInt("_ScreenWidth", m_RenderTexture.width); m_PathTracingShader.SetInt("_ScreenHeight", m_RenderTexture.height); Debug.Log($"BVH Root:{bvhRoot}, BVH Childs:{bvh.Count}"); }