<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Infinity Code Forum — replace building mesh with a new one]]></title>
	<link rel="self" href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=423&amp;type=atom" />
	<updated>2017-02-19T22:01:36Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.infinity-code.com/viewtopic.php?id=423</id>
		<entry>
			<title type="html"><![CDATA[Re: replace building mesh with a new one]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=1853#p1853" />
			<content type="html"><![CDATA[<p>Hello.</p><p>You can get the size of the original building from the collider (GetComponent&lt;Collider&gt;().bounds), and calculate the scale. <br />But there is a small problem - when you change the zoom, you will lose your scale. So it is better to do in this way:<br /></p><div class="codebox"><pre><code>//in Start
buildingsControl.OnBuildingCreated += OnBuildingCreated;

private void OnBuildingCreated (OnlineMapsBuildingBase building)
{
        var gMesh = debris1.GetComponent&lt;MeshFilter&gt;();
        var mesh = building.GetComponent&lt;MeshFilter&gt;();

//detect building volume
        var size = VolumeOfMesh(mesh.mesh);
        if (size &lt; 200000)
        {
            Collider cl = building.GetComponent&lt;Collider&gt;();
            Vector3 scale = CalculateScale(cl.bounds);
            OnlineMapsUtils.DestroyImmediate(building.GetComponent&lt;MeshRenderer&gt;());
            OnlineMapsUtils.DestroyImmediate(mesh);
            OnlineMapsUtils.DestroyImmediate(cl);
            GameObject newBuilding = Instantiate(debris1);
            newBuilding.transform.parent = building.transform;
            newBuilding.transform.localPosition = Vector3.zero;
            newBuilding.transform.localRotation = Quaternion.Euler(Vector3.zero);
            newBuilding.transform.localScale = scale;
        }
        return true;
}</code></pre></div><p>P.S. This code is a theoretical and I haven&#039;t tested it.</p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2017-02-19T22:01:36Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=1853#p1853</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[replace building mesh with a new one]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=1850#p1850" />
			<content type="html"><![CDATA[<p>Hi,</p><p>I am working on the function to&nbsp; replace the building meshes with a kind of &#039;debris&#039; meshes (created in Blender).<br />I made function to detect small buildings:<br /></p><div class="codebox"><pre><code>//in Start     
   buildingsControl.OnShowBuilding = OnShowBuilding;


 private bool OnShowBuilding(OnlineMapsBuildingBase onlineMapsBuildingBase)
    {
        var gMesh = debris1.GetComponent&lt;MeshFilter&gt;();
        var mesh = onlineMapsBuildingBase.GetComponent&lt;MeshFilter&gt;();

//detect building volume
        var size = VolumeOfMesh(mesh.mesh);
        if (size &lt; 200000)
        {
//replace (bad way)
            mesh.mesh = gMesh.sharedMesh;
        }
        return true;
    }</code></pre></div><p>I get an expected result.&nbsp; But the new mesh is not of the previous building size and scale (apparently it is of its initial prefab size).</p><p>Could you please sugest a better way or idea to do this?</p>]]></content>
			<author>
				<name><![CDATA[DolceVita]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=376</uri>
			</author>
			<updated>2017-02-18T20:13:15Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=1850#p1850</id>
		</entry>
</feed>
