Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  About the cloth generation script before execution
#1
Pregunta 
I'm trying to generate multiple fabrics using Obi.
I created it by referring to SackGenerator, RuntimeClothGenerator, etc., but there is a problem because initialization is not executed.

I want to execute initialization. what should I do?

I know how to generate it during execution, but how can I run it in an editor script before execution?
Reply
#2
(10-10-2019, 10:33 AM)tukuyo Wrote: I'm trying to generate multiple fabrics using Obi.
I created it by referring to SackGenerator, RuntimeClothGenerator, etc., but there is a problem because initialization is not executed.

I want to execute initialization. what should I do?

I know how to generate it during execution, but how can I run it in an editor script before execution?

Hi,

I'd need to see what your current code is to be able to tell what's wrong.
Reply
#3
(10-10-2019, 10:42 AM)josemendez Wrote: Hi,

I'd need to see what your current code is to be able to tell what's wrong.


Thank you for your quick reply.

The script is attached below.

The first is the Editor script.

The second is the part of the script that is run from the Editor script.

The third is a script attached to the generated Cloth object.

(In some places, comment out and Japanese is written, but don't worry.)

Best regards,



Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Master))]
public class GenerateCloth : Editor
{
   public override void OnInspectorGUI()
   {
       DrawDefaultInspector();

       Master script = (Master)target;
       if(!EditorApplication.isPlaying){
           if (GUILayout.Button("Genarete!!")){
               script.CreateCloth();
           }
       }
   }

}



Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Linq;
using Obi;

public class Master : MonoBehaviour {

   // 布オブジェクトを持つ親
   [SerializeField]
   private GameObject testParent;

   [Header("=====基本布生成=====")]
   // 生成する布の大きさm と分割数
   [SerializeField]
    private float _scale = 1;
    [SerializeField]
    private int _resolution = 10;
   public Material outsideMaterial;
   public Material insideMaterial;
   
   public ObiSolver solver;
   public ObiMeshTopology topology;

   public bool useObi = false;


   public void CreateCloth()
    {
       if(!useObi){
              // Processing when Obi is not used
       } else {
           int num = obisettings.Length;
           if(num <= 0) return;
           // テスト分オブジェクト生成
           for (int i = 0; i < num; i++)
           {
               Mesh mesh = createMesh(_scale, _resolution);

               GameObject sheet = new GameObject("Sheet" + i, typeof(MeshFilter), typeof(SkinnedMeshRenderer));
               sheet.GetComponent<SkinnedMeshRenderer>().materials = new Material[] { outsideMaterial, insideMaterial };
               sheet.transform.parent = testParent.transform;
               sheet.transform.localPosition = Vector3.forward;
               sheet.tag = "sheet";
               sheet.AddComponent<ObiCloth>();
               genCloth generator = sheet.AddComponent<genCloth>();
               generator.mesh = mesh;
               generator.resolution = _resolution;
               generator.topology = topology;
               generator.solver = solver;
               generator.setUpObi();
           }
       }
    }
}



Code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(MeshFilter))]
//[RequireComponent (typeof(MeshRenderer))]
[RequireComponent(typeof(SkinnedMeshRenderer))]

public class genCloth : MonoBehaviour
{

   public Mesh mesh;
   public SphereCollider virtualHand;
   public float Damping = 0;
   public float Stretching = 0;
   public float Bending = 0;
   public int resolution;
   public Vector3 Accelerration;
   private Cloth component;

   public ObiSolver solver;                /**< solver to add the sack to.*/
    public ObiMeshTopology topology;

   public void Awake(){
       if(this.gameObject.GetComponent<ObiCloth>()){
           ObiCloth cloth = GetComponent<ObiCloth>();
           CoroutineJob job = new CoroutineJob();
           job.Start(cloth.GeneratePhysicRepresentationForMesh());
       }
   }

public void setUpObi() {
       if(mesh == null || topology == null){
           Debug.LogError("Either the mesh or the topology are null. You must provide a mesh and an empty topology asset in order to generate cloth.");
       }

       ObiCloth cloth = GetComponent<ObiCloth>();

       var filter = GetComponent<MeshFilter>();
       filter.sharedMesh = mesh;
       var skin = GetComponent<SkinnedMeshRenderer>();
       skin.sharedMesh = mesh;

       topology.InputMesh = mesh;
       topology.Generate();

       cloth.Solver = solver;
       cloth.SharedTopology = topology;

       cloth.enabled = true;
       cloth.AddToSolver(null);

       CoroutineJob job = new CoroutineJob();
        job.Start(cloth.GeneratePhysicRepresentationForMesh());

   }

}
Reply
#4
(12-10-2019, 04:35 PM)tukuyo Wrote: Thank you for your quick reply.

The script is attached below.

The first is the Editor script.

The second is the part of the script that is run from the Editor script.

The third is a script attached to the generated Cloth object.

(In some places, comment out and Japanese is written, but don't worry.)

Best regards,



Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Master))]
public class GenerateCloth : Editor
{
   public override void OnInspectorGUI()
   {
       DrawDefaultInspector();

       Master script = (Master)target;
       if(!EditorApplication.isPlaying){
           if (GUILayout.Button("Genarete!!")){
               script.CreateCloth();
           }
       }
   }

}



Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Linq;
using Obi;

public class Master : MonoBehaviour {

   // 布オブジェクトを持つ親
   [SerializeField]
   private GameObject testParent;

   [Header("=====基本布生成=====")]
   // 生成する布の大きさm と分割数
   [SerializeField]
   private float _scale = 1;
   [SerializeField]
   private int _resolution = 10;
   public Material outsideMaterial;
   public Material insideMaterial;
   
   public ObiSolver solver;
   public ObiMeshTopology topology;

   public bool useObi = false;


   public void CreateCloth()
    {
       if(!useObi){
              // Processing when Obi is not used
       } else {
           int num = obisettings.Length;
           if(num <= 0) return;
           // テスト分オブジェクト生成
           for (int i = 0; i < num; i++)
           {
               Mesh mesh = createMesh(_scale, _resolution);

               GameObject sheet = new GameObject("Sheet" + i, typeof(MeshFilter), typeof(SkinnedMeshRenderer));
               sheet.GetComponent<SkinnedMeshRenderer>().materials = new Material[] { outsideMaterial, insideMaterial };
               sheet.transform.parent = testParent.transform;
               sheet.transform.localPosition = Vector3.forward;
               sheet.tag = "sheet";
               sheet.AddComponent<ObiCloth>();
               genCloth generator = sheet.AddComponent<genCloth>();
               generator.mesh = mesh;
               generator.resolution = _resolution;
               generator.topology = topology;
               generator.solver = solver;
               generator.setUpObi();
           }
       }
   }
}



Code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(MeshFilter))]
//[RequireComponent (typeof(MeshRenderer))]
[RequireComponent(typeof(SkinnedMeshRenderer))]

public class genCloth : MonoBehaviour
{

   public Mesh mesh;
   public SphereCollider virtualHand;
   public float Damping = 0;
   public float Stretching = 0;
   public float Bending = 0;
   public int resolution;
   public Vector3 Accelerration;
   private Cloth component;

   public ObiSolver solver;                /**< solver to add the sack to.*/
    public ObiMeshTopology topology;

   public void Awake(){
       if(this.gameObject.GetComponent<ObiCloth>()){
           ObiCloth cloth = GetComponent<ObiCloth>();
           CoroutineJob job = new CoroutineJob();
           job.Start(cloth.GeneratePhysicRepresentationForMesh());
       }
   }

public void setUpObi() {
       if(mesh == null || topology == null){
           Debug.LogError("Either the mesh or the topology are null. You must provide a mesh and an empty topology asset in order to generate cloth.");
       }

       ObiCloth cloth = GetComponent<ObiCloth>();

       var filter = GetComponent<MeshFilter>();
       filter.sharedMesh = mesh;
       var skin = GetComponent<SkinnedMeshRenderer>();
       skin.sharedMesh = mesh;

       topology.InputMesh = mesh;
       topology.Generate();

       cloth.Solver = solver;
       cloth.SharedTopology = topology;

       cloth.enabled = true;
       cloth.AddToSolver(null);

       CoroutineJob job = new CoroutineJob();
        job.Start(cloth.GeneratePhysicRepresentationForMesh());

   }

}
Hi.

cloth.GeneratePhysicRepresentationForMesh is a coroutine, and CoroutineJob() is meant to execute coroutines in the editor. Use StartCoroutine instead. See:
https://docs.unity3d.com/ScriptReference...utine.html
Reply
#5
(12-10-2019, 05:12 PM)josemendez Wrote: Hi.

cloth.GeneratePhysicRepresentationForMesh is a coroutine, and CoroutineJob() is meant  to execute coroutines in the editor. Use StartCoroutine instead. See:
https://docs.unity3d.com/ScriptReference...utine.html




Thank you for your polite explanation and quick reply.

I would like to use StartCoroutine.
Thank you very much.
Reply