Unity-Technologies / com.unity.demoteam.hair

An integrated solution for authoring / importing / simulating / rendering strand-based hair in Unity.
Other
730 stars 99 forks source link

Cannot use abc file for Hair Asset #74

Open crawlchange opened 1 year ago

crawlchange commented 1 year ago

This has already been reported:

https://github.com/Unity-Technologies/com.unity.demoteam.hair/issues/34

But let me first walk through the absurdity of this project as a whole.

1- There is no linked documentation 2- There is no linked usage instructions 3- Google searches lead to this resource: https://learn.unity.com/project/getting-started-with-hair-simulation. This resource demands a specific unity version. So newer versions of unity have no support for hair at all? 4- The above resource provides a sample scene. That scene doesn't work for the URP. It works (hair is loaded) for HDRP. Is unity hair impossible in URP? 5- The only interchange file format accepted by this plugin is alembic. 6- Alembic is entirely undocumented. 7- Alembic has no C# importer or exporter, only C++. How can alembic files be read into memory and debugged if needed? 8- The unity alembic plugin has no support for scripting, only dragging and dropping. How can hair be imported/exported by script?

So this cumulative complete absurdity and developer recklessness composes the context behind this particular issue.

These are the steps to reproduce the issue:

a) Import the SampleHair.abc file provided by the above resource into blender. b) Export as alembic abc file c) Try to use that on the hair asset (instead of the original SampleHair.abc file). d) Error: "Discarding provided curve set due to degenerate curves with less than two vertices"

So, I pose the following questions:

1- How can one PROGRAMMATICALLY create abc hair files for use with this plugin, if it only supports alembic interchange format, alembic is entirely undocumented and there are no alembic reader/writers for C#? 2- How can one generate these files at all, if not even Blender is capable of outputting abc files in the format you want? 3- WHAT is the format you want, at all? "Alembic" is too broad. That abc file produced above IS alembic. What is the full specification for the HAIR abc file your plugin demands, in order for it to accept the file and properly work? 4- How could, say, your SampleHair.abc file be loaded into memory and analyzed at all (so that I could maybe figure out by myself what is the required format and how to programmatically produce it) if there is no alembic reader published for C# and Unity's alembic plugin only works with drag and drop? How could the hair importing be debugged, if the abc files can't be loaded into memory and analyzed? 5- How can abc files be PROGRAMMATICALLY imported and new hair instances created via script?

crawlchange commented 1 year ago

Update: I'm working out on making things work with HairAssetCustomData instead of alembic, which seems to be able to be programmatically generated. But that's not for sure yet, and this is also entirely undocumented terrain. I really hope this works as the hair system is not of very much use if it is not exposed to scripting.

qlee01 commented 1 year ago

Regarding alembic import it works pretty much the same as Unreal Engine. It’s not supposed to be a hair authoring tool, you need to rely on Blender or Maya to author hair and export as alembic into Unity. I had no issues so far with the workflow tbh. But much try and error, as this is not yet officially part of Unity editor, but an experimental package. In my opinion for what it is it works great.

crawlchange commented 1 year ago

@qlee01 Notice the "steps to reproduce" that I mentioned. If you merely import the SampleHair.abc file (from the Unity Learn resources!) into blender, and then export back into alembic, it already cannot be read by this plugin anymore. So what exactly would "the workflow" be?

It is not about authoring hair, it is about WHAT IS THE FILE FORMAT OF HAIR that the plugin expects. "Alembic" is not a hair file format, and this plugin does not accept every alembic file. There are 1 billion ways of authoring hair in Blender, in fact any mesh in blender can be a hair. Also blender particles can be hair. But I can't export particles into alembic, can I? There is not "a" workflow for authoring hair in Blender.

"The workflow" is undocumented. So when you refer to "the workflow" you are using, I have no idea what are you referring to, which is just another way of saying that this plugin is entirely undocumented and I have no idea how to use it (I mean, I know how to use it from the moment a suitable alembic file is provided, but there is no documentation on how to produce that alembic file, and of course you can't just export a generic scene from blender, which would almost be the definition of an alembic file, and import into this: there is some specific kind of "scene" or "object" that this project would consider a "hair", but that has not been documented).

qlee01 commented 1 year ago

Maybe just check how it’s done for Unreal. It’s really the same, and you can import all alembic hair which were created corny unreal easily. There should be dozens of tutorials for blender to meta human hair.

crawlchange commented 1 year ago

Thanks @qlee01 ! That is something definitely worth checking. The issue still is that I need scripting capabilities (so just producing an alembic hair by hand wouldn't be enough), so I'm trying to guess how to create a CurveSet object for the HairAssetCustomData (which I've been successful in creating so far). But that unreal workflow could definitely provide some indirect clues. I hope someone else could provide a different look on this.

crawlchange commented 1 year ago

Hahahah! I was able to programmatically generate one hair strand! I'll be sharing the code and step-by-step here, as this effectively provides a general api for converting any strand-based hair into this system :)

-Create a C# script called "CustomHairData" with this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.DemoTeam.Hair;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;

[CreateAssetMenu(fileName = "CustomHairData", menuName = "ScriptableObjects/CustomHairData", order = 1)]
public class CustomHairData : HairAssetCustomData
{
    public override bool AcquireCurves(out HairAssetProvisional.CurveSet curveSet, Allocator allocator) {
        curveSet = new HairAssetProvisional.CurveSet(allocator);
        curveSet.curveCount = 1;
        curveSet.curveVertexCount = new UnsafeList<int>(1,allocator) { 2 };
        curveSet.vertexDataPosition = new UnsafeList<Vector3>(2,allocator) { new Vector3(0.0f, 0.0f, 0.0f), new Vector3(1.0f, 0.0f, 0.0f) };
        curveSet.vertexDataTexCoord = new UnsafeList<Vector2>(2,allocator) { new Vector2(), new Vector2() };
        curveSet.vertexDataDiameter = new UnsafeList<float>(2,allocator) { 1.0f, 1.0f };
        return true;
    }
}

-Go to: Assets -> Create -> ScriptableObjects -> CustomHairData

That will add this CustomHairData scriptable object in the project.

-Then: Assets -> Create -> Hair -> Hair Asset

-In the Hair Asset, under "Settings Basic", select Type: Custom. Then, under "Settings Custom", drag and drop the CustomHairData scriptable object (not the script) into "Data Provider". Then, click "Build strand groups".

-Now create a hair instance and use that hair asset. The code above creates a single-strand, two vertices hair. You can modify that to programmatically generate or import any shape.

qlee01 commented 1 year ago

Great! I also figured out how to create hair with textures, albeit without reading the source code of hair package it’s hard to tell what each channel of textures is for. But with this you can create grooms, control length, distribution and direction.

ldoppertin commented 9 months ago

I am experiencing the same issues with the alembic import. Have you made any progress or found a solution?

Currently I am using your code to convert my data to a curve set (thanks a lot!). However, I'm stuck as I don't understand how to provide curve UVs...